Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put error spans deep on nested object literals #25140

Merged
merged 22 commits into from
Jul 4, 2018
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
91d292d
Add ncie deep elaborations
weswigham May 31, 2018
4303c00
Merge branch 'master' into nice-elaborations
weswigham Jun 15, 2018
c463bd5
Nice stuff
weswigham Jun 15, 2018
5733e3f
Merge branch 'master' into nice-elaborations
weswigham Jun 15, 2018
167ec23
Merge branch 'master' into nice-elaborations
weswigham Jun 21, 2018
f832eae
Modify tuple error to use length error mroe often
weswigham Jun 21, 2018
36206c8
Accept good baselines
weswigham Jun 21, 2018
c8fc8db
Accept meh baselines
weswigham Jun 21, 2018
f52c9b9
Fix literal types
weswigham Jun 22, 2018
ea3300a
Calculate elaborations like it was the very first time again~
weswigham Jun 22, 2018
23d1e17
Use tristate for enum relationship to ensure elaborations are printed…
weswigham Jun 22, 2018
4a757fa
Merge branch 'master' into nice-elaborations
weswigham Jun 25, 2018
10b1292
Merge branch 'master' into nice-elaborations
weswigham Jun 28, 2018
e663505
Update message text, nits
weswigham Jun 29, 2018
7b83484
Merge branch 'master' into nice-elaborations
weswigham Jul 2, 2018
b819165
move some functions back to where they were
weswigham Jul 2, 2018
1bcf91e
Add test of deep JSX elaboration
weswigham Jul 2, 2018
9eb759b
Add elaboration test with parenthesized expressions, comma expression…
weswigham Jul 2, 2018
62ba57c
Move check to allow elaborations on more anonymous types
weswigham Jul 2, 2018
7a344f5
Fix nits
weswigham Jul 3, 2018
dcbf33a
Add specialized error to elaborations of nonliteral computed named-me…
weswigham Jul 3, 2018
ae46816
Update error message
weswigham Jul 4, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
212 changes: 163 additions & 49 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3730,6 +3730,11 @@
"code": 6371
},

"The expected type comes from property '{0}' which is declared here on type '{1}'": {
"category": "Message",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was "category": "Error"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed duing our sync earlier, right now the category is unused for related information - we can put off deciding why they aughta be until we have a story for using them in some principled way (for, eg, elaborations vs explanations)

"code": 6500
},

"Variable '{0}' implicitly has an '{1}' type.": {
"category": "Error",
"code": 7005
Expand Down
19 changes: 19 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6674,6 +6674,25 @@ namespace ts {
return node.kind === SyntaxKind.NamedImports || node.kind === SyntaxKind.NamedExports;
}

/* @internal */
/**
* Strip off existed single quotes or double quotes from a given string
*
* @return non-quoted string
*/
export function stripQuotes(name: string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we should just be able to call symbolToString and get the right thing in certain cases, and just funnel along a string.

const length = name.length;
if (length >= 2 && name.charCodeAt(0) === name.charCodeAt(length - 1) && startsWithQuote(name)) {
return name.substring(1, length - 1);
}
return name;
}

/* @internal */
export function startsWithQuote(name: string): boolean {
return isSingleOrDoubleQuote(name.charCodeAt(0));
}

export interface ObjectAllocator {
getNodeConstructor(): new (kind: SyntaxKind, pos?: number, end?: number) => Node;
getTokenConstructor(): new <TKind extends SyntaxKind>(kind: TKind, pos?: number, end?: number) => Token<TKind>;
Expand Down
17 changes: 0 additions & 17 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1581,23 +1581,6 @@ namespace ts {
(<ImportOrExportSpecifier>location.parent).propertyName === location;
}

/**
* Strip off existed single quotes or double quotes from a given string
*
* @return non-quoted string
*/
export function stripQuotes(name: string) {
const length = name.length;
if (length >= 2 && name.charCodeAt(0) === name.charCodeAt(length - 1) && startsWithQuote(name)) {
return name.substring(1, length - 1);
}
return name;
}

export function startsWithQuote(name: string): boolean {
return isSingleOrDoubleQuote(name.charCodeAt(0));
}

export function scriptKindIs(fileName: string, host: LanguageServiceHost, ...scriptKinds: ScriptKind[]): boolean {
const scriptKind = getScriptKind(fileName, host);
return some(scriptKinds, k => k === scriptKind);
Expand Down
2 changes: 1 addition & 1 deletion src/tsconfig-base.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"pretty": true,
"lib": ["es5"],
"lib": ["es2015"],
"target": "es5",

"declaration": true,
Expand Down
15 changes: 8 additions & 7 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5767,6 +5767,7 @@ declare namespace ts {
Option_build_must_be_the_first_command_line_argument: DiagnosticMessage;
Options_0_and_1_cannot_be_combined: DiagnosticMessage;
Skipping_clean_because_not_all_projects_could_be_located: DiagnosticMessage;
The_expected_type_comes_from_property_0_which_is_declared_here_on_type_1: DiagnosticMessage;
Variable_0_implicitly_has_an_1_type: DiagnosticMessage;
Parameter_0_implicitly_has_an_1_type: DiagnosticMessage;
Member_0_implicitly_has_an_1_type: DiagnosticMessage;
Expand Down Expand Up @@ -7091,6 +7092,13 @@ declare namespace ts {
declare namespace ts {
/** @internal */
function isNamedImportsOrExports(node: Node): node is NamedImportsOrExports;
/**
* Strip off existed single quotes or double quotes from a given string
*
* @return non-quoted string
*/
function stripQuotes(name: string): string;
function startsWithQuote(name: string): boolean;
interface ObjectAllocator {
getNodeConstructor(): new (kind: SyntaxKind, pos?: number, end?: number) => Node;
getTokenConstructor(): new <TKind extends SyntaxKind>(kind: TKind, pos?: number, end?: number) => Token<TKind>;
Expand Down Expand Up @@ -10873,13 +10881,6 @@ declare namespace ts {
function symbolToDisplayParts(typeChecker: TypeChecker, symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags, flags?: SymbolFormatFlags): SymbolDisplayPart[];
function signatureToDisplayParts(typechecker: TypeChecker, signature: Signature, enclosingDeclaration?: Node, flags?: TypeFormatFlags): SymbolDisplayPart[];
function isImportOrExportSpecifierName(location: Node): location is Identifier;
/**
* Strip off existed single quotes or double quotes from a given string
*
* @return non-quoted string
*/
function stripQuotes(name: string): string;
function startsWithQuote(name: string): boolean;
function scriptKindIs(fileName: string, host: LanguageServiceHost, ...scriptKinds: ScriptKind[]): boolean;
function getScriptKind(fileName: string, host?: LanguageServiceHost): ScriptKind;
function getUniqueSymbolId(symbol: Symbol, checker: TypeChecker): number;
Expand Down
16 changes: 10 additions & 6 deletions tests/baselines/reference/arrayLiterals3.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(10,5): error TS2322: Type '[]' is not assignable to type '[any, any, any]'.
Property '0' is missing in type '[]'.
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(11,5): error TS2322: Type '[string, number, boolean]' is not assignable to type '[boolean, string, number]'.
Type 'string' is not assignable to type 'boolean'.
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(11,38): error TS2322: Type 'string' is not assignable to type 'boolean'.
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(11,48): error TS2322: Type 'number' is not assignable to type 'string'.
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(11,51): error TS2322: Type 'true' is not assignable to type 'number'.
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(17,5): error TS2322: Type '[number, number, string, boolean]' is not assignable to type '[number, number]'.
Types of property 'length' are incompatible.
Type '4' is not assignable to type '2'.
Expand All @@ -18,7 +19,7 @@ tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(34,5): error
'number' is a primitive, but 'Number' is a wrapper object. Prefer using 'number' when possible.


==== tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts (6 errors) ====
==== tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts (8 errors) ====
// Each element expression in a non-empty array literal is processed as follows:
// - If the array literal contains no spread elements, and if the array literal is contextually typed (section 4.19)
// by a type T and T has a property with the numeric name N, where N is the index of the element expression in the array literal,
Expand All @@ -33,9 +34,12 @@ tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(34,5): error
!!! error TS2322: Type '[]' is not assignable to type '[any, any, any]'.
!!! error TS2322: Property '0' is missing in type '[]'.
var a1: [boolean, string, number] = ["string", 1, true]; // Error
~~
!!! error TS2322: Type '[string, number, boolean]' is not assignable to type '[boolean, string, number]'.
!!! error TS2322: Type 'string' is not assignable to type 'boolean'.
~~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'boolean'.
~
!!! error TS2322: Type 'number' is not assignable to type 'string'.
~~~~
!!! error TS2322: Type 'true' is not assignable to type 'number'.

// The resulting type an array literal expression is determined as follows:
// - If the array literal contains no spread elements and is an array assignment pattern in a destructuring assignment (section 4.17.1),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
tests/cases/compiler/assignmentToObjectAndFunction.ts(1,5): error TS2322: Type '{ toString: number; }' is not assignable to type 'Object'.
Types of property 'toString' are incompatible.
Type 'number' is not assignable to type '() => string'.
tests/cases/compiler/assignmentToObjectAndFunction.ts(1,24): error TS2322: Type 'number' is not assignable to type '() => string'.
tests/cases/compiler/assignmentToObjectAndFunction.ts(8,5): error TS2322: Type '{}' is not assignable to type 'Function'.
Property 'apply' is missing in type '{}'.
tests/cases/compiler/assignmentToObjectAndFunction.ts(29,5): error TS2322: Type 'typeof bad' is not assignable to type 'Function'.
Expand All @@ -10,10 +8,8 @@ tests/cases/compiler/assignmentToObjectAndFunction.ts(29,5): error TS2322: Type

==== tests/cases/compiler/assignmentToObjectAndFunction.ts (3 errors) ====
var errObj: Object = { toString: 0 }; // Error, incompatible toString
~~~~~~
!!! error TS2322: Type '{ toString: number; }' is not assignable to type 'Object'.
!!! error TS2322: Types of property 'toString' are incompatible.
!!! error TS2322: Type 'number' is not assignable to type '() => string'.
~~~~~~~~
!!! error TS2322: Type 'number' is not assignable to type '() => string'.
var goodObj: Object = {
toString(x?) {
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@ tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesize
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(17,1): error TS2539: Cannot assign to 'M' because it is not a variable.
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(18,2): error TS2539: Cannot assign to 'M' because it is not a variable.
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(25,5): error TS2539: Cannot assign to 'M3' because it is not a variable.
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(31,1): error TS2322: Type '{ x: string; }' is not assignable to type 'typeof M3'.
Types of property 'x' are incompatible.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(32,1): error TS2322: Type '{ x: string; }' is not assignable to type 'typeof M3'.
Types of property 'x' are incompatible.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(33,1): error TS2322: Type '{ x: string; }' is not assignable to type 'typeof M3'.
Types of property 'x' are incompatible.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(31,11): error TS2322: Type 'string' is not assignable to type 'number'.
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(32,13): error TS2322: Type 'string' is not assignable to type 'number'.
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(33,13): error TS2322: Type 'string' is not assignable to type 'number'.
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(37,1): error TS2539: Cannot assign to 'fn' because it is not a variable.
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(38,2): error TS2539: Cannot assign to 'fn' because it is not a variable.
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(43,5): error TS2322: Type '""' is not assignable to type 'number'.
Expand Down Expand Up @@ -78,20 +72,14 @@ tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesize
(M2.M3) = { x: 3 }; // OK

M2.M3 = { x: '' }; // Error
~~~~~
!!! error TS2322: Type '{ x: string; }' is not assignable to type 'typeof M3'.
!!! error TS2322: Types of property 'x' are incompatible.
!!! error TS2322: Type 'string' is not assignable to type 'number'.
~
!!! error TS2322: Type 'string' is not assignable to type 'number'.
(M2).M3 = { x: '' }; // Error
~~~~~~~
!!! error TS2322: Type '{ x: string; }' is not assignable to type 'typeof M3'.
!!! error TS2322: Types of property 'x' are incompatible.
!!! error TS2322: Type 'string' is not assignable to type 'number'.
~
!!! error TS2322: Type 'string' is not assignable to type 'number'.
(M2.M3) = { x: '' }; // Error
~~~~~~~
!!! error TS2322: Type '{ x: string; }' is not assignable to type 'typeof M3'.
!!! error TS2322: Types of property 'x' are incompatible.
!!! error TS2322: Type 'string' is not assignable to type 'number'.
~
!!! error TS2322: Type 'string' is not assignable to type 'number'.


function fn() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ tests/cases/compiler/baseClassImprovedMismatchErrors.ts(8,5): error TS2416: Prop
Types of property 'n' are incompatible.
Type 'string | Derived' is not assignable to type 'string | Base'.
Type 'Derived' is not assignable to type 'string | Base'.
Type 'Derived' is not assignable to type 'Base'.
tests/cases/compiler/baseClassImprovedMismatchErrors.ts(9,5): error TS2416: Property 'fn' in type 'Derived' is not assignable to the same property in base type 'Base'.
Type '() => string | number' is not assignable to type '() => number'.
Type 'string | number' is not assignable to type 'number'.
Expand All @@ -17,7 +16,6 @@ tests/cases/compiler/baseClassImprovedMismatchErrors.ts(14,5): error TS2416: Pro
Types of property 'n' are incompatible.
Type 'string | DerivedInterface' is not assignable to type 'string | Base'.
Type 'DerivedInterface' is not assignable to type 'string | Base'.
Type 'DerivedInterface' is not assignable to type 'Base'.
tests/cases/compiler/baseClassImprovedMismatchErrors.ts(15,5): error TS2416: Property 'fn' in type 'DerivedInterface' is not assignable to the same property in base type 'Base'.
Type '() => string | number' is not assignable to type '() => number'.
Type 'string | number' is not assignable to type 'number'.
Expand All @@ -41,7 +39,6 @@ tests/cases/compiler/baseClassImprovedMismatchErrors.ts(15,5): error TS2416: Pro
!!! error TS2416: Types of property 'n' are incompatible.
!!! error TS2416: Type 'string | Derived' is not assignable to type 'string | Base'.
!!! error TS2416: Type 'Derived' is not assignable to type 'string | Base'.
!!! error TS2416: Type 'Derived' is not assignable to type 'Base'.
fn() {
~~
!!! error TS2416: Property 'fn' in type 'Derived' is not assignable to the same property in base type 'Base'.
Expand All @@ -61,7 +58,6 @@ tests/cases/compiler/baseClassImprovedMismatchErrors.ts(15,5): error TS2416: Pro
!!! error TS2416: Types of property 'n' are incompatible.
!!! error TS2416: Type 'string | DerivedInterface' is not assignable to type 'string | Base'.
!!! error TS2416: Type 'DerivedInterface' is not assignable to type 'string | Base'.
!!! error TS2416: Type 'DerivedInterface' is not assignable to type 'Base'.
fn() {
~~
!!! error TS2416: Property 'fn' in type 'DerivedInterface' is not assignable to the same property in base type 'Base'.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
tests/cases/conformance/jsx/file.tsx(13,54): error TS2326: Types of property 'nextValues' are incompatible.
Type '(a: { x: string; }) => string' is not assignable to type '(cur: { x: string; }) => { x: string; }'.
Type 'string' is not assignable to type '{ x: string; }'.
tests/cases/conformance/jsx/file.tsx(13,54): error TS2322: Type '(a: { x: string; }) => string' is not assignable to type '(cur: { x: string; }) => { x: string; }'.
Type 'string' is not assignable to type '{ x: string; }'.


==== tests/cases/conformance/jsx/file.tsx (1 errors) ====
Expand All @@ -17,7 +16,6 @@ tests/cases/conformance/jsx/file.tsx(13,54): error TS2326: Types of property 'ne
let b = <GenericComponent initialValues={12} nextValues={a => a} />; // No error - Values should be reinstantiated with `number` (since `object` is a default, not a constraint)
let c = <GenericComponent initialValues={{ x: "y" }} nextValues={a => ({ x: a.x })} />; // No Error
let d = <GenericComponent initialValues={{ x: "y" }} nextValues={a => a.x} />; // Error - `string` is not assignable to `{x: string}`
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2326: Types of property 'nextValues' are incompatible.
!!! error TS2326: Type '(a: { x: string; }) => string' is not assignable to type '(cur: { x: string; }) => { x: string; }'.
!!! error TS2326: Type 'string' is not assignable to type '{ x: string; }'.
~~~~~~~~~~
!!! error TS2322: Type '(a: { x: string; }) => string' is not assignable to type '(cur: { x: string; }) => { x: string; }'.
!!! error TS2322: Type 'string' is not assignable to type '{ x: string; }'.
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts(6,5): error TS2322: Type '{ [x: string]: string | number; }' is not assignable to type 'I'.
Index signatures are incompatible.
Type 'string | number' is not assignable to type 'boolean'.
Type 'string' is not assignable to type 'boolean'.
tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts(7,5): error TS2322: Type 'string' is not assignable to type 'boolean'.
tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts(8,5): error TS2322: Type 'number' is not assignable to type 'boolean'.


==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts (1 errors) ====
==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts (2 errors) ====
interface I {
[s: string]: boolean;
[s: number]: boolean;
}

var o: I = {
~
!!! error TS2322: Type '{ [x: string]: string | number; }' is not assignable to type 'I'.
!!! error TS2322: Index signatures are incompatible.
!!! error TS2322: Type 'string | number' is not assignable to type 'boolean'.
!!! error TS2322: Type 'string' is not assignable to type 'boolean'.
[""+"foo"]: "",
~~~~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'boolean'.
[""+"bar"]: 0
~~~~~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'boolean'.
}
Loading