From 595cb11f22d4bb3fe4bbf3e4d0366c6b5903c57a Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Thu, 8 Jun 2017 11:01:32 -0700 Subject: [PATCH 01/54] Excess property checks for discriminated unions This uses the same code as #14006, which improves error messages for discriminated unions. --- src/compiler/checker.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 266fbe573be..ddfebd84ad6 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8964,6 +8964,13 @@ namespace ts { (isTypeSubsetOf(globalObjectType, target) || (!isComparingJsxAttributes && isEmptyObjectType(target)))) { return false; } + if (target.flags & TypeFlags.Union) { + const discriminantType = findMatchingDiscriminantType(source, target as UnionType); + if (discriminantType) { + // check excess properties against discriminant type only, not the entire union + return hasExcessProperties(source, discriminantType, reportErrors); + } + } for (const prop of getPropertiesOfObjectType(source)) { if (!isKnownProperty(target, prop.name, isComparingJsxAttributes)) { if (reportErrors) { From 8302ebcd8b2f493539abf6fd1d9f9f3718af252c Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Thu, 8 Jun 2017 11:03:52 -0700 Subject: [PATCH 02/54] Test:excess property checks--discriminated unions --- .../discriminatedUnionErrorMessage.errors.txt | 12 +++---- .../excessPropertyCheckWithUnions.errors.txt | 33 +++++++++++++++++++ .../excessPropertyCheckWithUnions.js | 19 +++++++++++ .../compiler/excessPropertyCheckWithUnions.ts | 12 +++++++ 4 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 tests/baselines/reference/excessPropertyCheckWithUnions.errors.txt create mode 100644 tests/baselines/reference/excessPropertyCheckWithUnions.js create mode 100644 tests/cases/compiler/excessPropertyCheckWithUnions.ts diff --git a/tests/baselines/reference/discriminatedUnionErrorMessage.errors.txt b/tests/baselines/reference/discriminatedUnionErrorMessage.errors.txt index 6f1eb511d37..e54727befd0 100644 --- a/tests/baselines/reference/discriminatedUnionErrorMessage.errors.txt +++ b/tests/baselines/reference/discriminatedUnionErrorMessage.errors.txt @@ -1,6 +1,5 @@ -tests/cases/compiler/discriminatedUnionErrorMessage.ts(8,5): error TS2322: Type '{ kind: "sq"; x: number; y: number; }' is not assignable to type 'Shape'. - Type '{ kind: "sq"; x: number; y: number; }' is not assignable to type 'Square'. - Property 'size' is missing in type '{ kind: "sq"; x: number; y: number; }'. +tests/cases/compiler/discriminatedUnionErrorMessage.ts(10,5): error TS2322: Type '{ kind: "sq"; x: number; y: number; }' is not assignable to type 'Shape'. + Object literal may only specify known properties, and 'x' does not exist in type 'Square'. ==== tests/cases/compiler/discriminatedUnionErrorMessage.ts (1 errors) ==== @@ -12,12 +11,11 @@ tests/cases/compiler/discriminatedUnionErrorMessage.ts(8,5): error TS2322: Type | Rectangle | Circle; let shape: Shape = { - ~~~~~ -!!! error TS2322: Type '{ kind: "sq"; x: number; y: number; }' is not assignable to type 'Shape'. -!!! error TS2322: Type '{ kind: "sq"; x: number; y: number; }' is not assignable to type 'Square'. -!!! error TS2322: Property 'size' is missing in type '{ kind: "sq"; x: number; y: number; }'. kind: "sq", x: 12, + ~~~~~ +!!! error TS2322: Type '{ kind: "sq"; x: number; y: number; }' is not assignable to type 'Shape'. +!!! error TS2322: Object literal may only specify known properties, and 'x' does not exist in type 'Square'. y: 13, } \ No newline at end of file diff --git a/tests/baselines/reference/excessPropertyCheckWithUnions.errors.txt b/tests/baselines/reference/excessPropertyCheckWithUnions.errors.txt new file mode 100644 index 00000000000..1a01f8cb5b8 --- /dev/null +++ b/tests/baselines/reference/excessPropertyCheckWithUnions.errors.txt @@ -0,0 +1,33 @@ +tests/cases/compiler/excessPropertyCheckWithUnions.ts(10,30): error TS2322: Type '{ tag: "T"; a1: string; }' is not assignable to type 'ADT'. + Object literal may only specify known properties, and 'a1' does not exist in type '{ tag: "T"; }'. +tests/cases/compiler/excessPropertyCheckWithUnions.ts(11,21): error TS2322: Type '{ tag: "A"; d20: 12; }' is not assignable to type 'ADT'. + Object literal may only specify known properties, and 'd20' does not exist in type '{ tag: "A"; a1: string; }'. +tests/cases/compiler/excessPropertyCheckWithUnions.ts(12,1): error TS2322: Type '{ tag: "D"; }' is not assignable to type 'ADT'. + Type '{ tag: "D"; }' is not assignable to type '{ tag: "D"; d20: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20; }'. + Property 'd20' is missing in type '{ tag: "D"; }'. + + +==== tests/cases/compiler/excessPropertyCheckWithUnions.ts (3 errors) ==== + type ADT = { + tag: "A", + a1: string + } | { + tag: "D", + d20: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 + } | { + tag: "T", + } + let wrong: ADT = { tag: "T", a1: "extra" } + ~~~~~~~~~~~ +!!! error TS2322: Type '{ tag: "T"; a1: string; }' is not assignable to type 'ADT'. +!!! error TS2322: Object literal may only specify known properties, and 'a1' does not exist in type '{ tag: "T"; }'. + wrong = { tag: "A", d20: 12 } + ~~~~~~~ +!!! error TS2322: Type '{ tag: "A"; d20: 12; }' is not assignable to type 'ADT'. +!!! error TS2322: Object literal may only specify known properties, and 'd20' does not exist in type '{ tag: "A"; a1: string; }'. + wrong = { tag: "D" } + ~~~~~ +!!! error TS2322: Type '{ tag: "D"; }' is not assignable to type 'ADT'. +!!! error TS2322: Type '{ tag: "D"; }' is not assignable to type '{ tag: "D"; d20: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20; }'. +!!! error TS2322: Property 'd20' is missing in type '{ tag: "D"; }'. + \ No newline at end of file diff --git a/tests/baselines/reference/excessPropertyCheckWithUnions.js b/tests/baselines/reference/excessPropertyCheckWithUnions.js new file mode 100644 index 00000000000..79c30bce9c7 --- /dev/null +++ b/tests/baselines/reference/excessPropertyCheckWithUnions.js @@ -0,0 +1,19 @@ +//// [excessPropertyCheckWithUnions.ts] +type ADT = { + tag: "A", + a1: string +} | { + tag: "D", + d20: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 +} | { + tag: "T", +} +let wrong: ADT = { tag: "T", a1: "extra" } +wrong = { tag: "A", d20: 12 } +wrong = { tag: "D" } + + +//// [excessPropertyCheckWithUnions.js] +var wrong = { tag: "T", a1: "extra" }; +wrong = { tag: "A", d20: 12 }; +wrong = { tag: "D" }; diff --git a/tests/cases/compiler/excessPropertyCheckWithUnions.ts b/tests/cases/compiler/excessPropertyCheckWithUnions.ts new file mode 100644 index 00000000000..8b1abf1b764 --- /dev/null +++ b/tests/cases/compiler/excessPropertyCheckWithUnions.ts @@ -0,0 +1,12 @@ +type ADT = { + tag: "A", + a1: string +} | { + tag: "D", + d20: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 +} | { + tag: "T", +} +let wrong: ADT = { tag: "T", a1: "extra" } +wrong = { tag: "A", d20: 12 } +wrong = { tag: "D" } From c8d856a5d4489860114d809c010a52a56a5f91a7 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Fri, 9 Jun 2017 09:51:07 -0700 Subject: [PATCH 03/54] Correct excess property error on ambiguous discriminated unions --- src/compiler/checker.ts | 7 ++- .../excessPropertyCheckWithUnions.errors.txt | 58 ++++++++++++++++++- .../excessPropertyCheckWithUnions.js | 41 +++++++++++++ .../compiler/excessPropertyCheckWithUnions.ts | 28 +++++++++ 4 files changed, 132 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ddfebd84ad6..559d6ae71ba 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -9029,6 +9029,7 @@ namespace ts { function findMatchingDiscriminantType(source: Type, target: UnionOrIntersectionType) { const sourceProperties = getPropertiesOfObjectType(source); + let match: Type; if (sourceProperties) { for (const sourceProperty of sourceProperties) { if (isDiscriminantProperty(target, sourceProperty.name)) { @@ -9036,12 +9037,16 @@ namespace ts { for (const type of target.types) { const targetType = getTypeOfPropertyOfType(type, sourceProperty.name); if (targetType && isRelatedTo(sourceType, targetType)) { - return type; + if (match) { + return undefined; + } + match = type; } } } } } + return match; } function typeRelatedToEachType(source: Type, target: IntersectionType, reportErrors: boolean): Ternary { diff --git a/tests/baselines/reference/excessPropertyCheckWithUnions.errors.txt b/tests/baselines/reference/excessPropertyCheckWithUnions.errors.txt index 1a01f8cb5b8..9714c90ba61 100644 --- a/tests/baselines/reference/excessPropertyCheckWithUnions.errors.txt +++ b/tests/baselines/reference/excessPropertyCheckWithUnions.errors.txt @@ -5,9 +5,21 @@ tests/cases/compiler/excessPropertyCheckWithUnions.ts(11,21): error TS2322: Type tests/cases/compiler/excessPropertyCheckWithUnions.ts(12,1): error TS2322: Type '{ tag: "D"; }' is not assignable to type 'ADT'. Type '{ tag: "D"; }' is not assignable to type '{ tag: "D"; d20: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20; }'. Property 'd20' is missing in type '{ tag: "D"; }'. +tests/cases/compiler/excessPropertyCheckWithUnions.ts(33,28): error TS2322: Type '{ tag: "A"; x: string; extra: number; }' is not assignable to type 'Ambiguous'. + Object literal may only specify known properties, and 'extra' does not exist in type 'Ambiguous'. +tests/cases/compiler/excessPropertyCheckWithUnions.ts(34,26): error TS2322: Type '{ tag: "A"; y: number; extra: number; }' is not assignable to type 'Ambiguous'. + Object literal may only specify known properties, and 'extra' does not exist in type 'Ambiguous'. +tests/cases/compiler/excessPropertyCheckWithUnions.ts(39,1): error TS2322: Type '{ tag: "A"; }' is not assignable to type 'Ambiguous'. + Type '{ tag: "A"; }' is not assignable to type '{ tag: "C"; }'. + Types of property 'tag' are incompatible. + Type '"A"' is not assignable to type '"C"'. +tests/cases/compiler/excessPropertyCheckWithUnions.ts(40,1): error TS2322: Type '{ tag: "A"; z: true; }' is not assignable to type 'Ambiguous'. + Type '{ tag: "A"; z: true; }' is not assignable to type '{ tag: "C"; }'. + Types of property 'tag' are incompatible. + Type '"A"' is not assignable to type '"C"'. -==== tests/cases/compiler/excessPropertyCheckWithUnions.ts (3 errors) ==== +==== tests/cases/compiler/excessPropertyCheckWithUnions.ts (7 errors) ==== type ADT = { tag: "A", a1: string @@ -30,4 +42,48 @@ tests/cases/compiler/excessPropertyCheckWithUnions.ts(12,1): error TS2322: Type !!! error TS2322: Type '{ tag: "D"; }' is not assignable to type 'ADT'. !!! error TS2322: Type '{ tag: "D"; }' is not assignable to type '{ tag: "D"; d20: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20; }'. !!! error TS2322: Property 'd20' is missing in type '{ tag: "D"; }'. + + type Ambiguous = { + tag: "A", + x: string + } | { + tag: "A", + y: number + } | { + tag: "B", + z: boolean + } | { + tag: "C" + } + let amb: Ambiguous + // no error for ambiguous tag, even when it could satisfy both constituents at once + amb = { tag: "A", x: "hi" } + amb = { tag: "A", y: 12 } + amb = { tag: "A", x: "hi", y: 12 } + + // correctly error on excess property 'extra', even when ambiguous + amb = { tag: "A", x: "hi", extra: 12 } + ~~~~~~~~~ +!!! error TS2322: Type '{ tag: "A"; x: string; extra: number; }' is not assignable to type 'Ambiguous'. +!!! error TS2322: Object literal may only specify known properties, and 'extra' does not exist in type 'Ambiguous'. + amb = { tag: "A", y: 12, extra: 12 } + ~~~~~~~~~ +!!! error TS2322: Type '{ tag: "A"; y: number; extra: number; }' is not assignable to type 'Ambiguous'. +!!! error TS2322: Object literal may only specify known properties, and 'extra' does not exist in type 'Ambiguous'. + + // assignability errors still work. + // But note that the error for `z: true` is the fallback one of reporting on + // the last constituent since assignability error reporting can't find a single best discriminant either. + amb = { tag: "A" } + ~~~ +!!! error TS2322: Type '{ tag: "A"; }' is not assignable to type 'Ambiguous'. +!!! error TS2322: Type '{ tag: "A"; }' is not assignable to type '{ tag: "C"; }'. +!!! error TS2322: Types of property 'tag' are incompatible. +!!! error TS2322: Type '"A"' is not assignable to type '"C"'. + amb = { tag: "A", z: true } + ~~~ +!!! error TS2322: Type '{ tag: "A"; z: true; }' is not assignable to type 'Ambiguous'. +!!! error TS2322: Type '{ tag: "A"; z: true; }' is not assignable to type '{ tag: "C"; }'. +!!! error TS2322: Types of property 'tag' are incompatible. +!!! error TS2322: Type '"A"' is not assignable to type '"C"'. \ No newline at end of file diff --git a/tests/baselines/reference/excessPropertyCheckWithUnions.js b/tests/baselines/reference/excessPropertyCheckWithUnions.js index 79c30bce9c7..fb0a5a6f819 100644 --- a/tests/baselines/reference/excessPropertyCheckWithUnions.js +++ b/tests/baselines/reference/excessPropertyCheckWithUnions.js @@ -11,9 +11,50 @@ type ADT = { let wrong: ADT = { tag: "T", a1: "extra" } wrong = { tag: "A", d20: 12 } wrong = { tag: "D" } + +type Ambiguous = { + tag: "A", + x: string +} | { + tag: "A", + y: number +} | { + tag: "B", + z: boolean +} | { + tag: "C" +} +let amb: Ambiguous +// no error for ambiguous tag, even when it could satisfy both constituents at once +amb = { tag: "A", x: "hi" } +amb = { tag: "A", y: 12 } +amb = { tag: "A", x: "hi", y: 12 } + +// correctly error on excess property 'extra', even when ambiguous +amb = { tag: "A", x: "hi", extra: 12 } +amb = { tag: "A", y: 12, extra: 12 } + +// assignability errors still work. +// But note that the error for `z: true` is the fallback one of reporting on +// the last constituent since assignability error reporting can't find a single best discriminant either. +amb = { tag: "A" } +amb = { tag: "A", z: true } //// [excessPropertyCheckWithUnions.js] var wrong = { tag: "T", a1: "extra" }; wrong = { tag: "A", d20: 12 }; wrong = { tag: "D" }; +var amb; +// no error for ambiguous tag, even when it could satisfy both constituents at once +amb = { tag: "A", x: "hi" }; +amb = { tag: "A", y: 12 }; +amb = { tag: "A", x: "hi", y: 12 }; +// correctly error on excess property 'extra', even when ambiguous +amb = { tag: "A", x: "hi", extra: 12 }; +amb = { tag: "A", y: 12, extra: 12 }; +// assignability errors still work. +// But note that the error for `z: true` is the fallback one of reporting on +// the last constituent since assignability error reporting can't find a single best discriminant either. +amb = { tag: "A" }; +amb = { tag: "A", z: true }; diff --git a/tests/cases/compiler/excessPropertyCheckWithUnions.ts b/tests/cases/compiler/excessPropertyCheckWithUnions.ts index 8b1abf1b764..51f36d137fd 100644 --- a/tests/cases/compiler/excessPropertyCheckWithUnions.ts +++ b/tests/cases/compiler/excessPropertyCheckWithUnions.ts @@ -10,3 +10,31 @@ type ADT = { let wrong: ADT = { tag: "T", a1: "extra" } wrong = { tag: "A", d20: 12 } wrong = { tag: "D" } + +type Ambiguous = { + tag: "A", + x: string +} | { + tag: "A", + y: number +} | { + tag: "B", + z: boolean +} | { + tag: "C" +} +let amb: Ambiguous +// no error for ambiguous tag, even when it could satisfy both constituents at once +amb = { tag: "A", x: "hi" } +amb = { tag: "A", y: 12 } +amb = { tag: "A", x: "hi", y: 12 } + +// correctly error on excess property 'extra', even when ambiguous +amb = { tag: "A", x: "hi", extra: 12 } +amb = { tag: "A", y: 12, extra: 12 } + +// assignability errors still work. +// But note that the error for `z: true` is the fallback one of reporting on +// the last constituent since assignability error reporting can't find a single best discriminant either. +amb = { tag: "A" } +amb = { tag: "A", z: true } From d04f4a93a7fff465eaf2da93931f1949f6e49a22 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Fri, 9 Jun 2017 15:42:14 -0700 Subject: [PATCH 04/54] Do not check excess properties for multi-discriminant unions --- src/compiler/checker.ts | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 559d6ae71ba..f79e74746d4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -9028,20 +9028,19 @@ namespace ts { } function findMatchingDiscriminantType(source: Type, target: UnionOrIntersectionType) { - const sourceProperties = getPropertiesOfObjectType(source); let match: Type; + const sourceProperties = getPropertiesOfObjectType(source); if (sourceProperties) { - for (const sourceProperty of sourceProperties) { - if (isDiscriminantProperty(target, sourceProperty.name)) { - const sourceType = getTypeOfSymbol(sourceProperty); - for (const type of target.types) { - const targetType = getTypeOfPropertyOfType(type, sourceProperty.name); - if (targetType && isRelatedTo(sourceType, targetType)) { - if (match) { - return undefined; - } - match = type; + const sourceProperty = findSingleDiscriminantProperty(sourceProperties, target); + if (sourceProperty) { + const sourceType = getTypeOfSymbol(sourceProperty); + for (const type of target.types) { + const targetType = getTypeOfPropertyOfType(type, sourceProperty.name); + if (targetType && isRelatedTo(sourceType, targetType)) { + if (match) { + return undefined; } + match = type; } } } @@ -10839,6 +10838,19 @@ namespace ts { return false; } + function findSingleDiscriminantProperty(sourceProperties: Symbol[], target: Type): Symbol | undefined { + let result: Symbol; + for (const sourceProperty of sourceProperties) { + if (isDiscriminantProperty(target, sourceProperty.name)) { + if (result) { + return undefined; + } + result = sourceProperty; + } + } + return result; + } + function isOrContainsMatchingReference(source: Node, target: Node) { return isMatchingReference(source, target) || containsMatchingReference(source, target); } From 8a7186d1901197e9d050b1a200470c1cd1a065c5 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Fri, 9 Jun 2017 15:47:57 -0700 Subject: [PATCH 05/54] Add more excess property check tests for unions --- .../excessPropertyCheckWithUnions.errors.txt | 10 ++++++++++ .../reference/excessPropertyCheckWithUnions.js | 14 ++++++++++++++ .../compiler/excessPropertyCheckWithUnions.ts | 10 ++++++++++ 3 files changed, 34 insertions(+) diff --git a/tests/baselines/reference/excessPropertyCheckWithUnions.errors.txt b/tests/baselines/reference/excessPropertyCheckWithUnions.errors.txt index 9714c90ba61..3b7e5a787d2 100644 --- a/tests/baselines/reference/excessPropertyCheckWithUnions.errors.txt +++ b/tests/baselines/reference/excessPropertyCheckWithUnions.errors.txt @@ -86,4 +86,14 @@ tests/cases/compiler/excessPropertyCheckWithUnions.ts(40,1): error TS2322: Type !!! error TS2322: Type '{ tag: "A"; z: true; }' is not assignable to type '{ tag: "C"; }'. !!! error TS2322: Types of property 'tag' are incompatible. !!! error TS2322: Type '"A"' is not assignable to type '"C"'. + + type Overlapping = + | { a: 1, b: 1, first: string } + | { a: 2, second: string } + | { b: 3, third: string } + let over: Overlapping + + // these two are not reported because there are two discriminant properties + over = { a: 1, b: 1, first: "ok", second: "error" } + over = { a: 1, b: 1, first: "ok", third: "error" } \ No newline at end of file diff --git a/tests/baselines/reference/excessPropertyCheckWithUnions.js b/tests/baselines/reference/excessPropertyCheckWithUnions.js index fb0a5a6f819..c6da660b52d 100644 --- a/tests/baselines/reference/excessPropertyCheckWithUnions.js +++ b/tests/baselines/reference/excessPropertyCheckWithUnions.js @@ -39,6 +39,16 @@ amb = { tag: "A", y: 12, extra: 12 } // the last constituent since assignability error reporting can't find a single best discriminant either. amb = { tag: "A" } amb = { tag: "A", z: true } + +type Overlapping = + | { a: 1, b: 1, first: string } + | { a: 2, second: string } + | { b: 3, third: string } +let over: Overlapping + +// these two are not reported because there are two discriminant properties +over = { a: 1, b: 1, first: "ok", second: "error" } +over = { a: 1, b: 1, first: "ok", third: "error" } //// [excessPropertyCheckWithUnions.js] @@ -58,3 +68,7 @@ amb = { tag: "A", y: 12, extra: 12 }; // the last constituent since assignability error reporting can't find a single best discriminant either. amb = { tag: "A" }; amb = { tag: "A", z: true }; +var over; +// these two are not reported because there are two discriminant properties +over = { a: 1, b: 1, first: "ok", second: "error" }; +over = { a: 1, b: 1, first: "ok", third: "error" }; diff --git a/tests/cases/compiler/excessPropertyCheckWithUnions.ts b/tests/cases/compiler/excessPropertyCheckWithUnions.ts index 51f36d137fd..9a7968fe511 100644 --- a/tests/cases/compiler/excessPropertyCheckWithUnions.ts +++ b/tests/cases/compiler/excessPropertyCheckWithUnions.ts @@ -38,3 +38,13 @@ amb = { tag: "A", y: 12, extra: 12 } // the last constituent since assignability error reporting can't find a single best discriminant either. amb = { tag: "A" } amb = { tag: "A", z: true } + +type Overlapping = + | { a: 1, b: 1, first: string } + | { a: 2, second: string } + | { b: 3, third: string } +let over: Overlapping + +// these two are not reported because there are two discriminant properties +over = { a: 1, b: 1, first: "ok", second: "error" } +over = { a: 1, b: 1, first: "ok", third: "error" } From 4de96abd8fc5d678d6b494a953d3127dd2acf871 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Fri, 16 Jun 2017 14:37:39 -0700 Subject: [PATCH 06/54] Use the same logic of getting current directory as the one used when emitting files through project It means we would use currentDirectory as project Root or script info's directory as the current directory Fixes issue reported in https://developercommunity.visualstudio.com/content/problem/57099/typescript-generated-source-maps-have-invalid-path.html --- src/compiler/program.ts | 12 +++---- src/compiler/types.ts | 6 +++- src/harness/unittests/compileOnSave.ts | 45 +++++++++++++++++++++++++- src/server/builder.ts | 4 +-- src/server/project.ts | 11 ++++++- src/services/services.ts | 4 +-- src/services/transpile.ts | 2 +- src/services/types.ts | 2 +- 8 files changed, 71 insertions(+), 15 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index bbc0fa09780..30c61f50dff 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -874,12 +874,12 @@ namespace ts { return oldProgram.structureIsReused = StructureIsReused.Completely; } - function getEmitHost(writeFileCallback?: WriteFileCallback): EmitHost { + function getEmitHost(writeFileCallback?: WriteFileCallback, getCurrentDirectoryCallback?: GetCurrentDirectoryCallback): EmitHost { return { getCanonicalFileName, getCommonSourceDirectory: program.getCommonSourceDirectory, getCompilerOptions: program.getCompilerOptions, - getCurrentDirectory: () => currentDirectory, + getCurrentDirectory: getCurrentDirectoryCallback || (() => currentDirectory), getNewLine: () => host.getNewLine(), getSourceFile: program.getSourceFile, getSourceFileByPath: program.getSourceFileByPath, @@ -907,15 +907,15 @@ namespace ts { return noDiagnosticsTypeChecker || (noDiagnosticsTypeChecker = createTypeChecker(program, /*produceDiagnostics:*/ false)); } - function emit(sourceFile?: SourceFile, writeFileCallback?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, transformers?: CustomTransformers): EmitResult { - return runWithCancellationToken(() => emitWorker(program, sourceFile, writeFileCallback, cancellationToken, emitOnlyDtsFiles, transformers)); + function emit(sourceFile?: SourceFile, writeFileCallback?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, transformers?: CustomTransformers, getCurrentDirectoryCallback?: GetCurrentDirectoryCallback): EmitResult { + return runWithCancellationToken(() => emitWorker(program, sourceFile, writeFileCallback, cancellationToken, emitOnlyDtsFiles, transformers, getCurrentDirectoryCallback)); } function isEmitBlocked(emitFileName: string): boolean { return hasEmitBlockingDiagnostics.contains(toPath(emitFileName, currentDirectory, getCanonicalFileName)); } - function emitWorker(program: Program, sourceFile: SourceFile, writeFileCallback: WriteFileCallback, cancellationToken: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult { + function emitWorker(program: Program, sourceFile: SourceFile, writeFileCallback: WriteFileCallback, cancellationToken: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers, getCurrentDirectoryCallback?: GetCurrentDirectoryCallback): EmitResult { let declarationDiagnostics: Diagnostic[] = []; if (options.noEmit) { @@ -960,7 +960,7 @@ namespace ts { const transformers = emitOnlyDtsFiles ? [] : getTransformers(options, customTransformers); const emitResult = emitFiles( emitResolver, - getEmitHost(writeFileCallback), + getEmitHost(writeFileCallback, getCurrentDirectoryCallback), sourceFile, emitOnlyDtsFiles, transformers); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index ac64624e1a6..ac5de153226 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2405,6 +2405,10 @@ namespace ts { (fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void, sourceFiles?: SourceFile[]): void; } + export interface GetCurrentDirectoryCallback { + (): string; + } + export class OperationCanceledException { } export interface CancellationToken { @@ -2436,7 +2440,7 @@ namespace ts { * used for writing the JavaScript and declaration files. Otherwise, the writeFile parameter * will be invoked when writing the JavaScript and declaration files. */ - emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult; + emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers, getCurrentDirectoryCallback?: GetCurrentDirectoryCallback): EmitResult; getOptionsDiagnostics(cancellationToken?: CancellationToken): Diagnostic[]; getGlobalDiagnostics(cancellationToken?: CancellationToken): Diagnostic[]; diff --git a/src/harness/unittests/compileOnSave.ts b/src/harness/unittests/compileOnSave.ts index 7e262a1b257..3183ffd71b0 100644 --- a/src/harness/unittests/compileOnSave.ts +++ b/src/harness/unittests/compileOnSave.ts @@ -600,5 +600,48 @@ namespace ts.projectSystem { assert.isTrue(outFileContent.indexOf(file2.content) === -1); assert.isTrue(outFileContent.indexOf(file3.content) === -1); }); + + it("should use project root as current directory so that compile on save results in correct file mapping", () => { + const inputFileName = "Foo.ts"; + const file1 = { + path: `/root/TypeScriptProject3/TypeScriptProject3/${inputFileName}`, + content: "consonle.log('file1');" + }; + const externalProjectName = "/root/TypeScriptProject3/TypeScriptProject3/TypeScriptProject3.csproj"; + const host = createServerHost([file1, libFile]); + const session = createSession(host); + const projectService = session.getProjectService(); + + const outFileName = "bar.js"; + projectService.openExternalProject({ + rootFiles: toExternalFiles([file1.path]), + options: { + outFile: outFileName, + sourceMap: true, + compileOnSave: true + }, + projectFileName: externalProjectName + }); + + const emitRequest = makeSessionRequest(CommandNames.CompileOnSaveEmitFile, { file: file1.path }); + session.executeCommand(emitRequest); + + // Verify js file + const expectedOutFileName = "/root/TypeScriptProject3/TypeScriptProject3/" + outFileName; + assert.isTrue(host.fileExists(expectedOutFileName)); + const outFileContent = host.readFile(expectedOutFileName); + verifyContentHasString(outFileContent, file1.content); + verifyContentHasString(outFileContent, `//# sourceMappingURL=${outFileName}.map`); + + // Verify map file + const expectedMapFileName = expectedOutFileName + ".map"; + assert.isTrue(host.fileExists(expectedMapFileName)); + const mapFileContent = host.readFile(expectedMapFileName); + verifyContentHasString(mapFileContent, `"sources":["${inputFileName}"]`); + + function verifyContentHasString(content: string, string: string) { + assert.isTrue(content.indexOf(string) !== -1, `Expected "${content}" to have "${string}"`); + } + }); }); -} \ No newline at end of file +} diff --git a/src/server/builder.ts b/src/server/builder.ts index 895732ebece..711045d0ae6 100644 --- a/src/server/builder.ts +++ b/src/server/builder.ts @@ -148,9 +148,9 @@ namespace ts.server { const { emitSkipped, outputFiles } = this.project.getFileEmitOutput(fileInfo.scriptInfo, /*emitOnlyDtsFiles*/ false); if (!emitSkipped) { - const projectRootPath = this.project.getProjectRootPath(); + const currentDirectoryForEmit = this.project.getCurrentDirectoryForScriptInfoEmit(scriptInfo); for (const outputFile of outputFiles) { - const outputFileAbsoluteFileName = getNormalizedAbsolutePath(outputFile.name, projectRootPath ? projectRootPath : getDirectoryPath(scriptInfo.fileName)); + const outputFileAbsoluteFileName = getNormalizedAbsolutePath(outputFile.name, currentDirectoryForEmit); writeFile(outputFileAbsoluteFileName, outputFile.text, outputFile.writeByteOrderMark); } } diff --git a/src/server/project.ts b/src/server/project.ts index ac040a77ace..a7d8605315b 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -367,7 +367,16 @@ namespace ts.server { if (!this.languageServiceEnabled) { return undefined; } - return this.getLanguageService().getEmitOutput(info.fileName, emitOnlyDtsFiles); + + const getCurrentDirectoryCallback = memoize( + () => this.getCurrentDirectoryForScriptInfoEmit(info) + ); + return this.getLanguageService().getEmitOutput(info.fileName, emitOnlyDtsFiles, getCurrentDirectoryCallback); + } + + getCurrentDirectoryForScriptInfoEmit(info: ScriptInfo) { + const projectRootPath = this.getProjectRootPath(); + return projectRootPath || getDirectoryPath(info.fileName); } getFileNames(excludeFilesFromExternalLibraries?: boolean, excludeConfigFiles?: boolean) { diff --git a/src/services/services.ts b/src/services/services.ts index b508285b182..11061181ee6 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1528,7 +1528,7 @@ namespace ts { return ts.NavigateTo.getNavigateToItems(sourceFiles, program.getTypeChecker(), cancellationToken, searchValue, maxResultCount, excludeDtsFiles); } - function getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean): EmitOutput { + function getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean, getCurrentDirectoryCallback?: GetCurrentDirectoryCallback): EmitOutput { synchronizeHostData(); const sourceFile = getValidSourceFile(fileName); @@ -1543,7 +1543,7 @@ namespace ts { } const customTransformers = host.getCustomTransformers && host.getCustomTransformers(); - const emitOutput = program.emit(sourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers); + const emitOutput = program.emit(sourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers, getCurrentDirectoryCallback); return { outputFiles, diff --git a/src/services/transpile.ts b/src/services/transpile.ts index 561c188c6cd..5ba393a90c9 100644 --- a/src/services/transpile.ts +++ b/src/services/transpile.ts @@ -104,7 +104,7 @@ namespace ts { addRange(/*to*/ diagnostics, /*from*/ program.getOptionsDiagnostics()); } // Emit - program.emit(/*targetSourceFile*/ undefined, /*writeFile*/ undefined, /*cancellationToken*/ undefined, /*emitOnlyDtsFiles*/ undefined, transpileOptions.transformers); + program.emit(/*targetSourceFile*/ undefined, /*writeFile*/ undefined, /*cancellationToken*/ undefined, /*emitOnlyDtsFiles*/ undefined, transpileOptions.transformers, /*getCurrentDirectoryCallback*/ undefined); Debug.assert(outputText !== undefined, "Output generation failed"); diff --git a/src/services/types.ts b/src/services/types.ts index 2d47da2fd1d..07aaaeeb4b4 100644 --- a/src/services/types.ts +++ b/src/services/types.ts @@ -269,7 +269,7 @@ namespace ts { getApplicableRefactors(fileName: string, positionOrRaneg: number | TextRange): ApplicableRefactorInfo[]; getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string): RefactorEditInfo | undefined; - getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean): EmitOutput; + getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean, getCurrentDirectoryCallBack?: GetCurrentDirectoryCallback): EmitOutput; getProgram(): Program; From db78d5a5875becd3859aa414263c9ca6f91aabc2 Mon Sep 17 00:00:00 2001 From: Arthur Ozga Date: Thu, 21 Sep 2017 16:53:38 -0700 Subject: [PATCH 07/54] add error message test --- src/harness/unittests/session.ts | 55 ++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/harness/unittests/session.ts b/src/harness/unittests/session.ts index 18109cfa9db..58d8c1b66d8 100644 --- a/src/harness/unittests/session.ts +++ b/src/harness/unittests/session.ts @@ -386,6 +386,61 @@ namespace ts.server { }); }); + describe("exceptions", () => { + const command = "testhandler"; + class TestSession extends Session { + lastSent: protocol.Message; + private exceptionRaisingHandler(_request: protocol.Request): { response?: any, responseRequired: boolean } { + f1(); + return; + function f1() { + throw new Error("myMessage"); + } + } + + constructor() { + super({ + host: mockHost, + cancellationToken: nullCancellationToken, + useSingleInferredProject: false, + useInferredProjectPerProjectRoot: false, + typingsInstaller: undefined, + byteLength: Utils.byteLength, + hrtime: process.hrtime, + logger: projectSystem.nullLogger, + canUseEvents: true + }); + this.addProtocolHandler(command, this.exceptionRaisingHandler); + } + send(msg: protocol.Message) { + this.lastSent = msg; + } + } + + it("raised in a protocol handler generate an event", () => { + + const session = new TestSession(); + + const request = { + command, + seq: 0, + type: "request" + }; + + session.onMessage(JSON.stringify(request)); + const lastSent = session.lastSent as protocol.Response; + + expect(lastSent).to.contain({ + seq: 0, + type: "response", + command, + success: false + }); + + expect(lastSent.message).has.string("myMessage").and.has.string("f1"); + }); + }); + describe("how Session is extendable via subclassing", () => { class TestSession extends Session { lastSent: protocol.Message; From b21c46b9b56d1d8cf7a6ebe71dd26f43015fc552 Mon Sep 17 00:00:00 2001 From: Arthur Ozga Date: Fri, 22 Sep 2017 16:21:31 -0700 Subject: [PATCH 08/54] support @extends in jsdoc --- src/compiler/checker.ts | 4 +- src/compiler/parser.ts | 12 +- src/compiler/types.ts | 11 +- src/compiler/utilities.ts | 8 +- src/services/completions.ts | 4 +- tests/baselines/reference/APISample_jsdoc.js | 4 +- tests/cases/compiler/APISample_jsdoc.ts | 232 +++++++++---------- tests/cases/fourslash/jsDocAugments.ts | 3 +- tests/cases/fourslash/jsDocExtends.ts | 22 ++ 9 files changed, 164 insertions(+), 136 deletions(-) create mode 100644 tests/cases/fourslash/jsDocExtends.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5c835582da0..2dbfa6bd409 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4986,10 +4986,10 @@ namespace ts { baseType = getReturnTypeOfSignature(constructors[0]); } - // In a JS file, you can use the @augments jsdoc tag to specify a base type with type parameters + // In a JS file, you can use the @augments and @extends jsdoc tags to specify a base type with type parameters const valueDecl = type.symbol.valueDeclaration; if (valueDecl && isInJavaScriptFile(valueDecl)) { - const augTag = getJSDocAugmentsTag(type.symbol.valueDeclaration); + const augTag = getJSDocAugmentsOrExtendsTag(type.symbol.valueDeclaration); if (augTag) { baseType = getTypeFromTypeNode(augTag.typeExpression.type); } diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 3809a071421..7e9ac8c5804 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -423,8 +423,9 @@ namespace ts { return visitNode(cbNode, (node).typeExpression); case SyntaxKind.JSDocTypeTag: return visitNode(cbNode, (node).typeExpression); - case SyntaxKind.JSDocAugmentsTag: - return visitNode(cbNode, (node).typeExpression); + case SyntaxKind.JSDocAugmentsOrExtendsTag: + case SyntaxKind.JSDocExtendsTag: + return visitNode(cbNode, (node).typeExpression); case SyntaxKind.JSDocTemplateTag: return visitNodes(cbNode, cbNodes, (node).typeParameters); case SyntaxKind.JSDocTypedefTag: @@ -6366,7 +6367,8 @@ namespace ts { if (tagName) { switch (tagName.escapedText) { case "augments": - tag = parseAugmentsTag(atToken, tagName); + case "extends": + tag = parseAugmentsOrExtendsTag(atToken, tagName); break; case "class": case "constructor": @@ -6603,10 +6605,10 @@ namespace ts { return finishNode(result); } - function parseAugmentsTag(atToken: AtToken, tagName: Identifier): JSDocAugmentsTag { + function parseAugmentsOrExtendsTag(atToken: AtToken, tagName: Identifier): JSDocAugmentsOrExtendsTag { const typeExpression = tryParseTypeExpression(); - const result = createNode(SyntaxKind.JSDocAugmentsTag, atToken.pos); + const result = createNode(SyntaxKind.JSDocAugmentsOrExtendsTag, atToken.pos); result.atToken = atToken; result.tagName = tagName; result.typeExpression = typeExpression; diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 4e5ca9f07e7..e8f54236798 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -363,7 +363,8 @@ namespace ts { JSDocVariadicType, JSDocComment, JSDocTag, - JSDocAugmentsTag, + JSDocAugmentsOrExtendsTag, + JSDocExtendsTag, JSDocClassTag, JSDocParameterTag, JSDocReturnTag, @@ -2159,8 +2160,12 @@ namespace ts { kind: SyntaxKind.JSDocTag; } - export interface JSDocAugmentsTag extends JSDocTag { - kind: SyntaxKind.JSDocAugmentsTag; + /** + * Note that `@extends` is a synonym of `@augments`. + * Both are covered by this interface. + */ + export interface JSDocAugmentsOrExtendsTag extends JSDocTag { + kind: SyntaxKind.JSDocAugmentsOrExtendsTag; typeExpression: JSDocTypeExpression; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 674215b0583..f9a0c4c38ab 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -4072,8 +4072,8 @@ namespace ts { } /** Gets the JSDoc augments tag for the node if present */ - export function getJSDocAugmentsTag(node: Node): JSDocAugmentsTag | undefined { - return getFirstJSDocTag(node, SyntaxKind.JSDocAugmentsTag) as JSDocAugmentsTag; + export function getJSDocAugmentsOrExtendsTag(node: Node): JSDocAugmentsOrExtendsTag | undefined { + return getFirstJSDocTag(node, SyntaxKind.JSDocAugmentsOrExtendsTag) as JSDocAugmentsOrExtendsTag; } /** Gets the JSDoc class tag for the node if present */ @@ -4765,8 +4765,8 @@ namespace ts { return node.kind === SyntaxKind.JSDocComment; } - export function isJSDocAugmentsTag(node: Node): node is JSDocAugmentsTag { - return node.kind === SyntaxKind.JSDocAugmentsTag; + export function isJSDocAugmentsOrExtendsTag(node: Node): node is JSDocAugmentsOrExtendsTag { + return node.kind === SyntaxKind.JSDocAugmentsOrExtendsTag; } export function isJSDocParameterTag(node: Node): node is JSDocParameterTag { diff --git a/src/services/completions.ts b/src/services/completions.ts index e271ef12104..a954f687cb5 100644 --- a/src/services/completions.ts +++ b/src/services/completions.ts @@ -581,11 +581,11 @@ namespace ts.Completions { return { symbols, isGlobalCompletion, isMemberCompletion, allowStringLiteral, isNewIdentifierLocation, location, isRightOfDot: (isRightOfDot || isRightOfOpenTag), request, keywordFilters }; - type JSDocTagWithTypeExpression = JSDocAugmentsTag | JSDocParameterTag | JSDocPropertyTag | JSDocReturnTag | JSDocTypeTag | JSDocTypedefTag; + type JSDocTagWithTypeExpression = JSDocAugmentsOrExtendsTag | JSDocParameterTag | JSDocPropertyTag | JSDocReturnTag | JSDocTypeTag | JSDocTypedefTag; function isTagWithTypeExpression(tag: JSDocTag): tag is JSDocTagWithTypeExpression { switch (tag.kind) { - case SyntaxKind.JSDocAugmentsTag: + case SyntaxKind.JSDocAugmentsOrExtendsTag: case SyntaxKind.JSDocParameterTag: case SyntaxKind.JSDocPropertyTag: case SyntaxKind.JSDocReturnTag: diff --git a/tests/baselines/reference/APISample_jsdoc.js b/tests/baselines/reference/APISample_jsdoc.js index c74e188f38b..33857d06a6a 100644 --- a/tests/baselines/reference/APISample_jsdoc.js +++ b/tests/baselines/reference/APISample_jsdoc.js @@ -101,7 +101,7 @@ function getAllTags(node: ts.Node) { function getSomeOtherTags(node: ts.Node) { const tags: (ts.JSDocTag | undefined)[] = []; - tags.push(ts.getJSDocAugmentsTag(node)); + tags.push(ts.getJSDocAugmentsOrExtendsTag(node)); tags.push(ts.getJSDocClassTag(node)); tags.push(ts.getJSDocReturnTag(node)); const type = ts.getJSDocTypeTag(node); @@ -200,7 +200,7 @@ function getAllTags(node) { } function getSomeOtherTags(node) { var tags = []; - tags.push(ts.getJSDocAugmentsTag(node)); + tags.push(ts.getJSDocAugmentsOrExtendsTag(node)); tags.push(ts.getJSDocClassTag(node)); tags.push(ts.getJSDocReturnTag(node)); var type = ts.getJSDocTypeTag(node); diff --git a/tests/cases/compiler/APISample_jsdoc.ts b/tests/cases/compiler/APISample_jsdoc.ts index 70b814ffff4..491ff2b7a20 100644 --- a/tests/cases/compiler/APISample_jsdoc.ts +++ b/tests/cases/compiler/APISample_jsdoc.ts @@ -1,116 +1,116 @@ -// @module: commonjs -// @includebuiltfile: typescript_standalone.d.ts -// @strict:true - -/* - * Note: This test is a public API sample. The original sources can be found - * at: https://github.com/YousefED/typescript-json-schema - * https://github.com/vega/ts-json-schema-generator - * Please log a "breaking change" issue for any API breaking change affecting this issue - */ - -declare var console: any; - -import * as ts from "typescript"; - -// excerpted from https://github.com/YousefED/typescript-json-schema -// (converted from a method and modified; for example, `this: any` to compensate, among other changes) -function parseCommentsIntoDefinition(this: any, - symbol: ts.Symbol, - definition: {description?: string, [s: string]: string | undefined}, - otherAnnotations: { [s: string]: true}): void { - if (!symbol) { - return; - } - - // the comments for a symbol - let comments = symbol.getDocumentationComment(); - - if (comments.length) { - definition.description = comments.map(comment => comment.kind === "lineBreak" ? comment.text : comment.text.trim().replace(/\r\n/g, "\n")).join(""); - } - - // jsdocs are separate from comments - const jsdocs = symbol.getJsDocTags(); - jsdocs.forEach(doc => { - // if we have @TJS-... annotations, we have to parse them - const { name, text } = doc; - if (this.userValidationKeywords[name]) { - definition[name] = this.parseValue(text); - } else { - // special annotations - otherAnnotations[doc.name] = true; - } - }); -} - - -// excerpted from https://github.com/vega/ts-json-schema-generator -export interface Annotations { - [name: string]: any; -} -function getAnnotations(this: any, node: ts.Node): Annotations | undefined { - const symbol: ts.Symbol = (node as any).symbol; - if (!symbol) { - return undefined; - } - - const jsDocTags: ts.JSDocTagInfo[] = symbol.getJsDocTags(); - if (!jsDocTags || !jsDocTags.length) { - return undefined; - } - - const annotations: Annotations = jsDocTags.reduce((result: Annotations, jsDocTag: ts.JSDocTagInfo) => { - const value = this.parseJsDocTag(jsDocTag); - if (value !== undefined) { - result[jsDocTag.name] = value; - } - - return result; - }, {}); - return Object.keys(annotations).length ? annotations : undefined; -} - -// these examples are artificial and mostly nonsensical -function parseSpecificTags(node: ts.Node) { - if (node.kind === ts.SyntaxKind.Parameter) { - return ts.getJSDocParameterTags(node as ts.ParameterDeclaration); - } - if (node.kind === ts.SyntaxKind.FunctionDeclaration) { - const func = node as ts.FunctionDeclaration; - if (ts.hasJSDocParameterTags(func)) { - const flat: ts.JSDocTag[] = []; - for (const tags of func.parameters.map(ts.getJSDocParameterTags)) { - if (tags) flat.push(...tags); - } - return flat; - } - } -} - -function getReturnTypeFromJSDoc(node: ts.Node) { - if (node.kind === ts.SyntaxKind.FunctionDeclaration) { - return ts.getJSDocReturnType(node); - } - let type = ts.getJSDocType(node); - if (type && type.kind === ts.SyntaxKind.FunctionType) { - return (type as ts.FunctionTypeNode).type; - } -} - -function getAllTags(node: ts.Node) { - ts.getJSDocTags(node); -} - -function getSomeOtherTags(node: ts.Node) { - const tags: (ts.JSDocTag | undefined)[] = []; - tags.push(ts.getJSDocAugmentsTag(node)); - tags.push(ts.getJSDocClassTag(node)); - tags.push(ts.getJSDocReturnTag(node)); - const type = ts.getJSDocTypeTag(node); - if (type) { - tags.push(type); - } - tags.push(ts.getJSDocTemplateTag(node)); - return tags; -} +// @module: commonjs +// @includebuiltfile: typescript_standalone.d.ts +// @strict:true + +/* + * Note: This test is a public API sample. The original sources can be found + * at: https://github.com/YousefED/typescript-json-schema + * https://github.com/vega/ts-json-schema-generator + * Please log a "breaking change" issue for any API breaking change affecting this issue + */ + +declare var console: any; + +import * as ts from "typescript"; + +// excerpted from https://github.com/YousefED/typescript-json-schema +// (converted from a method and modified; for example, `this: any` to compensate, among other changes) +function parseCommentsIntoDefinition(this: any, + symbol: ts.Symbol, + definition: {description?: string, [s: string]: string | undefined}, + otherAnnotations: { [s: string]: true}): void { + if (!symbol) { + return; + } + + // the comments for a symbol + let comments = symbol.getDocumentationComment(); + + if (comments.length) { + definition.description = comments.map(comment => comment.kind === "lineBreak" ? comment.text : comment.text.trim().replace(/\r\n/g, "\n")).join(""); + } + + // jsdocs are separate from comments + const jsdocs = symbol.getJsDocTags(); + jsdocs.forEach(doc => { + // if we have @TJS-... annotations, we have to parse them + const { name, text } = doc; + if (this.userValidationKeywords[name]) { + definition[name] = this.parseValue(text); + } else { + // special annotations + otherAnnotations[doc.name] = true; + } + }); +} + + +// excerpted from https://github.com/vega/ts-json-schema-generator +export interface Annotations { + [name: string]: any; +} +function getAnnotations(this: any, node: ts.Node): Annotations | undefined { + const symbol: ts.Symbol = (node as any).symbol; + if (!symbol) { + return undefined; + } + + const jsDocTags: ts.JSDocTagInfo[] = symbol.getJsDocTags(); + if (!jsDocTags || !jsDocTags.length) { + return undefined; + } + + const annotations: Annotations = jsDocTags.reduce((result: Annotations, jsDocTag: ts.JSDocTagInfo) => { + const value = this.parseJsDocTag(jsDocTag); + if (value !== undefined) { + result[jsDocTag.name] = value; + } + + return result; + }, {}); + return Object.keys(annotations).length ? annotations : undefined; +} + +// these examples are artificial and mostly nonsensical +function parseSpecificTags(node: ts.Node) { + if (node.kind === ts.SyntaxKind.Parameter) { + return ts.getJSDocParameterTags(node as ts.ParameterDeclaration); + } + if (node.kind === ts.SyntaxKind.FunctionDeclaration) { + const func = node as ts.FunctionDeclaration; + if (ts.hasJSDocParameterTags(func)) { + const flat: ts.JSDocTag[] = []; + for (const tags of func.parameters.map(ts.getJSDocParameterTags)) { + if (tags) flat.push(...tags); + } + return flat; + } + } +} + +function getReturnTypeFromJSDoc(node: ts.Node) { + if (node.kind === ts.SyntaxKind.FunctionDeclaration) { + return ts.getJSDocReturnType(node); + } + let type = ts.getJSDocType(node); + if (type && type.kind === ts.SyntaxKind.FunctionType) { + return (type as ts.FunctionTypeNode).type; + } +} + +function getAllTags(node: ts.Node) { + ts.getJSDocTags(node); +} + +function getSomeOtherTags(node: ts.Node) { + const tags: (ts.JSDocTag | undefined)[] = []; + tags.push(ts.getJSDocAugmentsOrExtendsTag(node)); + tags.push(ts.getJSDocClassTag(node)); + tags.push(ts.getJSDocReturnTag(node)); + const type = ts.getJSDocTypeTag(node); + if (type) { + tags.push(type); + } + tags.push(ts.getJSDocTemplateTag(node)); + return tags; +} diff --git a/tests/cases/fourslash/jsDocAugments.ts b/tests/cases/fourslash/jsDocAugments.ts index 24458c529fb..cd2190e5486 100644 --- a/tests/cases/fourslash/jsDocAugments.ts +++ b/tests/cases/fourslash/jsDocAugments.ts @@ -15,9 +15,8 @@ // @Filename: declarations.d.ts //// declare class Thing { -//// mine: T; +//// mine: T; //// } goTo.marker(); verify.quickInfoIs("(local var) x: string"); - diff --git a/tests/cases/fourslash/jsDocExtends.ts b/tests/cases/fourslash/jsDocExtends.ts new file mode 100644 index 00000000000..6bce5569533 --- /dev/null +++ b/tests/cases/fourslash/jsDocExtends.ts @@ -0,0 +1,22 @@ +/// + +// @allowJs: true +// @Filename: dummy.js + +//// /** +//// * @extends {Thing} +//// */ +//// class MyStringThing extends Thing { +//// constructor() { +//// var x = this.mine; +//// x/**/; +//// } +//// } + +// @Filename: declarations.d.ts +//// declare class Thing { +//// mine: T; +//// } + +goTo.marker(); +verify.quickInfoIs("(local var) x: string"); From 6ba62d2d8dbfcfcbf542ab3213949ed98beaf459 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 26 Sep 2017 16:29:25 -0700 Subject: [PATCH 09/54] Revert all the changes except test case --- src/compiler/program.ts | 12 ++++++------ src/compiler/types.ts | 6 +----- src/server/builder.ts | 4 ++-- src/server/project.ts | 11 +---------- src/services/services.ts | 4 ++-- src/services/transpile.ts | 2 +- src/services/types.ts | 2 +- 7 files changed, 14 insertions(+), 27 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 30c61f50dff..bbc0fa09780 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -874,12 +874,12 @@ namespace ts { return oldProgram.structureIsReused = StructureIsReused.Completely; } - function getEmitHost(writeFileCallback?: WriteFileCallback, getCurrentDirectoryCallback?: GetCurrentDirectoryCallback): EmitHost { + function getEmitHost(writeFileCallback?: WriteFileCallback): EmitHost { return { getCanonicalFileName, getCommonSourceDirectory: program.getCommonSourceDirectory, getCompilerOptions: program.getCompilerOptions, - getCurrentDirectory: getCurrentDirectoryCallback || (() => currentDirectory), + getCurrentDirectory: () => currentDirectory, getNewLine: () => host.getNewLine(), getSourceFile: program.getSourceFile, getSourceFileByPath: program.getSourceFileByPath, @@ -907,15 +907,15 @@ namespace ts { return noDiagnosticsTypeChecker || (noDiagnosticsTypeChecker = createTypeChecker(program, /*produceDiagnostics:*/ false)); } - function emit(sourceFile?: SourceFile, writeFileCallback?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, transformers?: CustomTransformers, getCurrentDirectoryCallback?: GetCurrentDirectoryCallback): EmitResult { - return runWithCancellationToken(() => emitWorker(program, sourceFile, writeFileCallback, cancellationToken, emitOnlyDtsFiles, transformers, getCurrentDirectoryCallback)); + function emit(sourceFile?: SourceFile, writeFileCallback?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, transformers?: CustomTransformers): EmitResult { + return runWithCancellationToken(() => emitWorker(program, sourceFile, writeFileCallback, cancellationToken, emitOnlyDtsFiles, transformers)); } function isEmitBlocked(emitFileName: string): boolean { return hasEmitBlockingDiagnostics.contains(toPath(emitFileName, currentDirectory, getCanonicalFileName)); } - function emitWorker(program: Program, sourceFile: SourceFile, writeFileCallback: WriteFileCallback, cancellationToken: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers, getCurrentDirectoryCallback?: GetCurrentDirectoryCallback): EmitResult { + function emitWorker(program: Program, sourceFile: SourceFile, writeFileCallback: WriteFileCallback, cancellationToken: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult { let declarationDiagnostics: Diagnostic[] = []; if (options.noEmit) { @@ -960,7 +960,7 @@ namespace ts { const transformers = emitOnlyDtsFiles ? [] : getTransformers(options, customTransformers); const emitResult = emitFiles( emitResolver, - getEmitHost(writeFileCallback, getCurrentDirectoryCallback), + getEmitHost(writeFileCallback), sourceFile, emitOnlyDtsFiles, transformers); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index ac5de153226..ac64624e1a6 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2405,10 +2405,6 @@ namespace ts { (fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void, sourceFiles?: SourceFile[]): void; } - export interface GetCurrentDirectoryCallback { - (): string; - } - export class OperationCanceledException { } export interface CancellationToken { @@ -2440,7 +2436,7 @@ namespace ts { * used for writing the JavaScript and declaration files. Otherwise, the writeFile parameter * will be invoked when writing the JavaScript and declaration files. */ - emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers, getCurrentDirectoryCallback?: GetCurrentDirectoryCallback): EmitResult; + emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult; getOptionsDiagnostics(cancellationToken?: CancellationToken): Diagnostic[]; getGlobalDiagnostics(cancellationToken?: CancellationToken): Diagnostic[]; diff --git a/src/server/builder.ts b/src/server/builder.ts index 711045d0ae6..895732ebece 100644 --- a/src/server/builder.ts +++ b/src/server/builder.ts @@ -148,9 +148,9 @@ namespace ts.server { const { emitSkipped, outputFiles } = this.project.getFileEmitOutput(fileInfo.scriptInfo, /*emitOnlyDtsFiles*/ false); if (!emitSkipped) { - const currentDirectoryForEmit = this.project.getCurrentDirectoryForScriptInfoEmit(scriptInfo); + const projectRootPath = this.project.getProjectRootPath(); for (const outputFile of outputFiles) { - const outputFileAbsoluteFileName = getNormalizedAbsolutePath(outputFile.name, currentDirectoryForEmit); + const outputFileAbsoluteFileName = getNormalizedAbsolutePath(outputFile.name, projectRootPath ? projectRootPath : getDirectoryPath(scriptInfo.fileName)); writeFile(outputFileAbsoluteFileName, outputFile.text, outputFile.writeByteOrderMark); } } diff --git a/src/server/project.ts b/src/server/project.ts index a7d8605315b..ac040a77ace 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -367,16 +367,7 @@ namespace ts.server { if (!this.languageServiceEnabled) { return undefined; } - - const getCurrentDirectoryCallback = memoize( - () => this.getCurrentDirectoryForScriptInfoEmit(info) - ); - return this.getLanguageService().getEmitOutput(info.fileName, emitOnlyDtsFiles, getCurrentDirectoryCallback); - } - - getCurrentDirectoryForScriptInfoEmit(info: ScriptInfo) { - const projectRootPath = this.getProjectRootPath(); - return projectRootPath || getDirectoryPath(info.fileName); + return this.getLanguageService().getEmitOutput(info.fileName, emitOnlyDtsFiles); } getFileNames(excludeFilesFromExternalLibraries?: boolean, excludeConfigFiles?: boolean) { diff --git a/src/services/services.ts b/src/services/services.ts index 11061181ee6..b508285b182 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1528,7 +1528,7 @@ namespace ts { return ts.NavigateTo.getNavigateToItems(sourceFiles, program.getTypeChecker(), cancellationToken, searchValue, maxResultCount, excludeDtsFiles); } - function getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean, getCurrentDirectoryCallback?: GetCurrentDirectoryCallback): EmitOutput { + function getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean): EmitOutput { synchronizeHostData(); const sourceFile = getValidSourceFile(fileName); @@ -1543,7 +1543,7 @@ namespace ts { } const customTransformers = host.getCustomTransformers && host.getCustomTransformers(); - const emitOutput = program.emit(sourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers, getCurrentDirectoryCallback); + const emitOutput = program.emit(sourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers); return { outputFiles, diff --git a/src/services/transpile.ts b/src/services/transpile.ts index 5ba393a90c9..561c188c6cd 100644 --- a/src/services/transpile.ts +++ b/src/services/transpile.ts @@ -104,7 +104,7 @@ namespace ts { addRange(/*to*/ diagnostics, /*from*/ program.getOptionsDiagnostics()); } // Emit - program.emit(/*targetSourceFile*/ undefined, /*writeFile*/ undefined, /*cancellationToken*/ undefined, /*emitOnlyDtsFiles*/ undefined, transpileOptions.transformers, /*getCurrentDirectoryCallback*/ undefined); + program.emit(/*targetSourceFile*/ undefined, /*writeFile*/ undefined, /*cancellationToken*/ undefined, /*emitOnlyDtsFiles*/ undefined, transpileOptions.transformers); Debug.assert(outputText !== undefined, "Output generation failed"); diff --git a/src/services/types.ts b/src/services/types.ts index 07aaaeeb4b4..2d47da2fd1d 100644 --- a/src/services/types.ts +++ b/src/services/types.ts @@ -269,7 +269,7 @@ namespace ts { getApplicableRefactors(fileName: string, positionOrRaneg: number | TextRange): ApplicableRefactorInfo[]; getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string): RefactorEditInfo | undefined; - getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean, getCurrentDirectoryCallBack?: GetCurrentDirectoryCallback): EmitOutput; + getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean): EmitOutput; getProgram(): Program; From fad71d3dc69bc5381ba8bb17605a9660f5a05339 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 26 Sep 2017 17:29:53 -0700 Subject: [PATCH 10/54] Use project root as the current directory whenever possible to create the project --- src/compiler/program.ts | 13 +++- src/harness/unittests/compileOnSave.ts | 2 +- src/harness/unittests/session.ts | 4 +- src/server/builder.ts | 3 +- src/server/editorServices.ts | 33 ++++++++-- src/server/lsHost.ts | 4 +- src/server/project.ts | 66 +++++++++---------- tests/cases/fourslash/server/projectInfo01.ts | 8 +-- tests/cases/fourslash/server/projectInfo02.ts | 2 +- .../server/projectWithNonExistentFiles.ts | 2 +- 10 files changed, 78 insertions(+), 59 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index c8ff2496725..1397d46af1c 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -984,11 +984,18 @@ namespace ts { return true; } - if (defaultLibraryPath && defaultLibraryPath.length !== 0) { - return containsPath(defaultLibraryPath, file.path, currentDirectory, /*ignoreCase*/ !host.useCaseSensitiveFileNames()); + if (!options.noLib) { + return false; } - return compareStrings(file.fileName, getDefaultLibraryFileName(), /*ignoreCase*/ !host.useCaseSensitiveFileNames()) === Comparison.EqualTo; + // If '--lib' is not specified, include default library file according to '--target' + // otherwise, using options specified in '--lib' instead of '--target' default library file + if (!options.lib) { + return compareStrings(file.fileName, getDefaultLibraryFileName(), /*ignoreCase*/ !host.useCaseSensitiveFileNames()) === Comparison.EqualTo; + } + else { + return forEach(options.lib, libFileName => compareStrings(file.fileName, combinePaths(defaultLibraryPath, libFileName), /*ignoreCase*/ !host.useCaseSensitiveFileNames()) === Comparison.EqualTo); + } } function getDiagnosticsProducingTypeChecker() { diff --git a/src/harness/unittests/compileOnSave.ts b/src/harness/unittests/compileOnSave.ts index 89b980f23f8..dddcd64cd39 100644 --- a/src/harness/unittests/compileOnSave.ts +++ b/src/harness/unittests/compileOnSave.ts @@ -567,7 +567,7 @@ namespace ts.projectSystem { path: "/a/b/file3.js", content: "console.log('file3');" }; - const externalProjectName = "externalproject"; + const externalProjectName = "/a/b/externalproject"; const host = createServerHost([file1, file2, file3, libFile]); const session = createSession(host); const projectService = session.getProjectService(); diff --git a/src/harness/unittests/session.ts b/src/harness/unittests/session.ts index f37c9ea2392..417591cdb6f 100644 --- a/src/harness/unittests/session.ts +++ b/src/harness/unittests/session.ts @@ -16,8 +16,8 @@ namespace ts.server { directoryExists: () => false, getDirectories: () => [], createDirectory: noop, - getExecutingFilePath(): string { return void 0; }, - getCurrentDirectory(): string { return void 0; }, + getExecutingFilePath(): string { return ""; }, + getCurrentDirectory(): string { return ""; }, getEnvironmentVariable(): string { return ""; }, readDirectory() { return []; }, exit: noop, diff --git a/src/server/builder.ts b/src/server/builder.ts index 5cf65611fb3..0279f08bfab 100644 --- a/src/server/builder.ts +++ b/src/server/builder.ts @@ -148,9 +148,8 @@ namespace ts.server { const { emitSkipped, outputFiles } = this.project.getFileEmitOutput(fileInfo.scriptInfo, /*emitOnlyDtsFiles*/ false); if (!emitSkipped) { - const projectRootPath = this.project.getProjectRootPath(); for (const outputFile of outputFiles) { - const outputFileAbsoluteFileName = getNormalizedAbsolutePath(outputFile.name, projectRootPath ? projectRootPath : getDirectoryPath(scriptInfo.fileName)); + const outputFileAbsoluteFileName = getNormalizedAbsolutePath(outputFile.name, this.project.currentDirectory); writeFile(outputFileAbsoluteFileName, outputFile.text, outputFile.writeByteOrderMark); } } diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index d86f45a9ad9..9a12c4cab7c 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -417,7 +417,7 @@ namespace ts.server { this.globalPlugins = opts.globalPlugins || emptyArray; this.pluginProbeLocations = opts.pluginProbeLocations || emptyArray; this.allowLocalPluginLoads = !!opts.allowLocalPluginLoads; - this.typesMapLocation = (opts.typesMapLocation === undefined) ? combinePaths(this.host.getExecutingFilePath(), "../typesMap.json") : opts.typesMapLocation; + this.typesMapLocation = (opts.typesMapLocation === undefined) ? combinePaths(this.getExecutingFilePath(), "../typesMap.json") : opts.typesMapLocation; Debug.assert(!!this.host.createHash, "'ServerHost.createHash' is required for ProjectService"); @@ -442,6 +442,16 @@ namespace ts.server { this.documentRegistry = createDocumentRegistry(this.host.useCaseSensitiveFileNames, this.host.getCurrentDirectory()); } + /*@internal*/ + getExecutingFilePath() { + return this.getNormalizedAbsolutePath(this.host.getExecutingFilePath()); + } + + /*@internal*/ + getNormalizedAbsolutePath(fileName: string) { + return getNormalizedAbsolutePath(fileName, this.host.getCurrentDirectory()); + } + /* @internal */ getChangedFiles_TestOnly() { return this.changedFiles; @@ -924,6 +934,14 @@ namespace ts.server { }); } + /*@internal*/ getScriptInfoPaths() { + const result: Path[] = []; + this.filenameToScriptInfo.forEach(info => { + result.push(info.path); + }); + return result; + } + /** * This function tries to search for a tsconfig.json for the given file. If we found it, * we first detect if there is already a configured project created for it: if so, we re-read @@ -1365,7 +1383,7 @@ namespace ts.server { return project; } } - return this.createInferredProject(/*isSingleInferredProject*/ false, projectRootPath); + return this.createInferredProject(projectRootPath, /*isSingleInferredProject*/ false, projectRootPath); } // we don't have an explicit root path, so we should try to find an inferred project @@ -1402,12 +1420,13 @@ namespace ts.server { return this.inferredProjects[0]; } - return this.createInferredProject(/*isSingleInferredProject*/ true); + // Single inferred project does not have a project root. + return this.createInferredProject(/*currentDirectory*/ undefined, /*isSingleInferredProject*/ true); } - private createInferredProject(isSingleInferredProject?: boolean, projectRootPath?: string): InferredProject { + private createInferredProject(currentDirectory: string | undefined, isSingleInferredProject?: boolean, projectRootPath?: string): InferredProject { const compilerOptions = projectRootPath && this.compilerOptionsForInferredProjectsPerProjectRoot.get(projectRootPath) || this.compilerOptionsForInferredProjects; - const project = new InferredProject(this, this.documentRegistry, compilerOptions, projectRootPath); + const project = new InferredProject(this, this.documentRegistry, compilerOptions, currentDirectory, projectRootPath); if (isSingleInferredProject) { this.inferredProjects.unshift(project); } @@ -1419,8 +1438,8 @@ namespace ts.server { createInferredProjectWithRootFileIfNecessary(root: ScriptInfo, projectRootPath?: string) { const project = this.getOrCreateInferredProjectForProjectRootPathIfEnabled(root, projectRootPath) || - this.getOrCreateSingleInferredProjectIfEnabled() || - this.createInferredProject(); + this.getOrCreateSingleInferredProjectIfEnabled() || + this.createInferredProject(getDirectoryPath(root.path)); project.addRoot(root); diff --git a/src/server/lsHost.ts b/src/server/lsHost.ts index 13b9505a658..08dd5000cba 100644 --- a/src/server/lsHost.ts +++ b/src/server/lsHost.ts @@ -173,7 +173,7 @@ namespace ts.server { } getDefaultLibFileName() { - const nodeModuleBinDir = getDirectoryPath(normalizePath(this.host.getExecutingFilePath())); + const nodeModuleBinDir = getDirectoryPath(this.project.projectService.getExecutingFilePath()); return combinePaths(nodeModuleBinDir, getDefaultLibFileName(this.compilationSettings)); } @@ -203,7 +203,7 @@ namespace ts.server { } getCurrentDirectory(): string { - return this.host.getCurrentDirectory(); + return this.project.currentDirectory; } resolvePath(path: string): string { diff --git a/src/server/project.ts b/src/server/project.ts index 9ef79530e51..084193a97f5 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -177,6 +177,9 @@ namespace ts.server { return result.module; } + /*@internal*/ + readonly currentDirectory: string; + constructor( private readonly projectName: string, readonly projectKind: ProjectKind, @@ -185,8 +188,9 @@ namespace ts.server { hasExplicitListOfFiles: boolean, languageServiceEnabled: boolean, private compilerOptions: CompilerOptions, - public compileOnSaveEnabled: boolean) { - + public compileOnSaveEnabled: boolean, + currentDirectory: string | undefined) { + this.currentDirectory = this.projectService.getNormalizedAbsolutePath(currentDirectory || ""); if (!this.compilerOptions) { this.compilerOptions = getDefaultCompilerOptions(); this.compilerOptions.allowNonTsExtensions = true; @@ -268,7 +272,6 @@ namespace ts.server { getProjectName() { return this.projectName; } - abstract getProjectRootPath(): string | undefined; abstract getTypeAcquisition(): TypeAcquisition; getExternalFiles(): SortedReadonlyArray { @@ -363,7 +366,7 @@ namespace ts.server { return map(this.program.getSourceFiles(), sourceFile => { const scriptInfo = this.projectService.getScriptInfoForPath(sourceFile.path); if (!scriptInfo) { - Debug.fail(`scriptInfo for a file '${sourceFile.fileName}' is missing.`); + Debug.fail(`scriptInfo for a file '${sourceFile.fileName}' Path: '${sourceFile.path}' is missing.\nProgram currentDirectory: '${this.program.getCurrentDirectory()}'\nCurrentScriptInfos: ${this.projectService.getScriptInfoPaths()}\ncurrentDirectory: ${this.projectService.host.getCurrentDirectory()}`); } return scriptInfo; }); @@ -842,8 +845,6 @@ namespace ts.server { * the file and its imports/references are put into an InferredProject. */ export class InferredProject extends Project { - public readonly projectRootPath: string | undefined; - private static readonly newName = (() => { let nextId = 1; return () => { @@ -882,7 +883,7 @@ namespace ts.server { // Used to keep track of what directories are watched for this project directoriesWatchedForTsconfig: string[] = []; - constructor(projectService: ProjectService, documentRegistry: DocumentRegistry, compilerOptions: CompilerOptions, projectRootPath?: string) { + constructor(projectService: ProjectService, documentRegistry: DocumentRegistry, compilerOptions: CompilerOptions, currentDirectory: string | undefined, readonly projectRootPath: string | undefined) { super(InferredProject.newName(), ProjectKind.Inferred, projectService, @@ -890,7 +891,8 @@ namespace ts.server { /*files*/ undefined, /*languageServiceEnabled*/ true, compilerOptions, - /*compileOnSaveEnabled*/ false); + /*compileOnSaveEnabled*/ false, + currentDirectory); this.projectRootPath = projectRootPath; } @@ -910,15 +912,6 @@ namespace ts.server { super.removeRoot(info); } - getProjectRootPath() { - // Single inferred project does not have a project root. - if (this.projectService.useSingleInferredProject) { - return undefined; - } - const rootFiles = this.getRootFiles(); - return getDirectoryPath(rootFiles[0]); - } - close() { super.close(); @@ -962,7 +955,15 @@ namespace ts.server { private wildcardDirectories: Map, languageServiceEnabled: boolean, public compileOnSaveEnabled: boolean) { - super(configFileName, ProjectKind.Configured, projectService, documentRegistry, hasExplicitListOfFiles, languageServiceEnabled, compilerOptions, compileOnSaveEnabled); + super(configFileName, + ProjectKind.Configured, + projectService, + documentRegistry, + hasExplicitListOfFiles, + languageServiceEnabled, + compilerOptions, + compileOnSaveEnabled, + getDirectoryPath(configFileName)); this.canonicalConfigFilePath = asNormalizedPath(projectService.toCanonicalFileName(configFileName)); this.enablePlugins(); } @@ -982,7 +983,7 @@ namespace ts.server { // Search our peer node_modules, then any globally-specified probe paths // ../../.. to walk from X/node_modules/typescript/lib/tsserver.js to X/node_modules/ - const searchPaths = [combinePaths(host.getExecutingFilePath(), "../../.."), ...this.projectService.pluginProbeLocations]; + const searchPaths = [combinePaths(this.projectService.getExecutingFilePath(), "../../.."), ...this.projectService.pluginProbeLocations]; if (this.projectService.allowLocalPluginLoads) { const local = getDirectoryPath(this.canonicalConfigFilePath); @@ -1062,10 +1063,6 @@ namespace ts.server { } } - getProjectRootPath() { - return getDirectoryPath(this.getConfigFilePath()); - } - setProjectErrors(projectErrors: ReadonlyArray) { this.projectErrors = projectErrors; } @@ -1196,25 +1193,22 @@ namespace ts.server { compilerOptions: CompilerOptions, languageServiceEnabled: boolean, public compileOnSaveEnabled: boolean, - private readonly projectFilePath?: string) { - super(externalProjectName, ProjectKind.External, projectService, documentRegistry, /*hasExplicitListOfFiles*/ true, languageServiceEnabled, compilerOptions, compileOnSaveEnabled); - + projectFilePath?: string) { + super(externalProjectName, + ProjectKind.External, + projectService, + documentRegistry, + /*hasExplicitListOfFiles*/ true, + languageServiceEnabled, + compilerOptions, + compileOnSaveEnabled, + getDirectoryPath(projectFilePath || normalizeSlashes(externalProjectName))); } getExcludedFiles() { return this.excludedFiles; } - getProjectRootPath() { - if (this.projectFilePath) { - return getDirectoryPath(this.projectFilePath); - } - // if the projectFilePath is not given, we make the assumption that the project name - // is the path of the project file. AS the project name is provided by VS, we need to - // normalize slashes before using it as a file name. - return getDirectoryPath(normalizeSlashes(this.getProjectName())); - } - getTypeAcquisition() { return this.typeAcquisition; } diff --git a/tests/cases/fourslash/server/projectInfo01.ts b/tests/cases/fourslash/server/projectInfo01.ts index 0d8707bf8a1..036aa5f0d4d 100644 --- a/tests/cases/fourslash/server/projectInfo01.ts +++ b/tests/cases/fourslash/server/projectInfo01.ts @@ -14,11 +14,11 @@ ////console.log("nothing"); goTo.file("a.ts") -verify.ProjectInfo(["lib.d.ts", "a.ts"]) +verify.ProjectInfo(["/lib.d.ts", "a.ts"]) goTo.file("b.ts") -verify.ProjectInfo(["lib.d.ts", "a.ts", "b.ts"]) +verify.ProjectInfo(["/lib.d.ts", "a.ts", "b.ts"]) goTo.file("c.ts") -verify.ProjectInfo(["lib.d.ts", "a.ts", "b.ts", "c.ts"]) +verify.ProjectInfo(["/lib.d.ts", "a.ts", "b.ts", "c.ts"]) goTo.file("d.ts") -verify.ProjectInfo(["lib.d.ts", "d.ts"]) +verify.ProjectInfo(["/lib.d.ts", "d.ts"]) diff --git a/tests/cases/fourslash/server/projectInfo02.ts b/tests/cases/fourslash/server/projectInfo02.ts index 3077deb453c..fb7c9cf8257 100644 --- a/tests/cases/fourslash/server/projectInfo02.ts +++ b/tests/cases/fourslash/server/projectInfo02.ts @@ -10,4 +10,4 @@ ////{ "files": ["a.ts", "b.ts"] } goTo.file("a.ts") -verify.ProjectInfo(["lib.d.ts", "a.ts", "b.ts", "tsconfig.json"]) +verify.ProjectInfo(["/lib.d.ts", "a.ts", "b.ts", "tsconfig.json"]) diff --git a/tests/cases/fourslash/server/projectWithNonExistentFiles.ts b/tests/cases/fourslash/server/projectWithNonExistentFiles.ts index 0e263d9aca6..a52c5f8918f 100644 --- a/tests/cases/fourslash/server/projectWithNonExistentFiles.ts +++ b/tests/cases/fourslash/server/projectWithNonExistentFiles.ts @@ -10,4 +10,4 @@ ////{ "files": ["a.ts", "c.ts", "b.ts"] } goTo.file("a.ts"); -verify.ProjectInfo(["lib.d.ts", "a.ts", "b.ts", "tsconfig.json"]) +verify.ProjectInfo(["/lib.d.ts", "a.ts", "b.ts", "tsconfig.json"]) From b029857528ba37ccec4d09afca69ab24cf8c8fe5 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 27 Sep 2017 20:30:10 -0700 Subject: [PATCH 11/54] Give a more helpful error message when users try decorating using expressions that take no arguments. --- src/compiler/checker.ts | 5 +++++ src/compiler/diagnosticMessages.json | 4 ++++ 2 files changed, 9 insertions(+) mode change 100644 => 100755 src/compiler/checker.ts mode change 100644 => 100755 src/compiler/diagnosticMessages.json diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts old mode 100644 new mode 100755 index d89247b70f9..b342003cb59 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16381,6 +16381,11 @@ namespace ts { return resolveUntypedCall(node); } + if (callSignatures.length === 1 && callSignatures[0].parameters.length === 0) { + error(node, Diagnostics.A_decorator_function_must_accept_some_number_of_arguments_but_this_expression_takes_none_Did_you_mean_to_call_it_first); + return resolveErrorCall(node); + } + const headMessage = getDiagnosticHeadMessageForDecoratorResolution(node); if (!callSignatures.length) { let errorInfo: DiagnosticMessageChain; diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json old mode 100644 new mode 100755 index bf8fcdc4f84..193c6879b13 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -907,6 +907,10 @@ "category": "Error", "code": 1328 }, + "A decorator function must accept some number of arguments, but this expression takes none. Did you mean to call it first?": { + "category": "Error", + "code": 1329 + }, "Duplicate identifier '{0}'.": { "category": "Error", From fbbf3d22e398ad894be3a79c85f2769175c41826 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 27 Sep 2017 20:56:59 -0700 Subject: [PATCH 12/54] Accepted baselines. --- tests/baselines/reference/decoratorOnClassMethod6.errors.txt | 4 ++-- .../baselines/reference/decoratorOnClassProperty11.errors.txt | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/baselines/reference/decoratorOnClassMethod6.errors.txt b/tests/baselines/reference/decoratorOnClassMethod6.errors.txt index 530e86117ee..ab09a01f19b 100644 --- a/tests/baselines/reference/decoratorOnClassMethod6.errors.txt +++ b/tests/baselines/reference/decoratorOnClassMethod6.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/decorators/class/method/decoratorOnClassMethod6.ts(4,5): error TS1241: Unable to resolve signature of method decorator when called as an expression. +tests/cases/conformance/decorators/class/method/decoratorOnClassMethod6.ts(4,5): error TS1329: A decorator function must accept some number of arguments, but this expression takes none. Did you mean to call it first? ==== tests/cases/conformance/decorators/class/method/decoratorOnClassMethod6.ts (1 errors) ==== @@ -7,5 +7,5 @@ tests/cases/conformance/decorators/class/method/decoratorOnClassMethod6.ts(4,5): class C { @dec ["method"]() {} ~~~~ -!!! error TS1241: Unable to resolve signature of method decorator when called as an expression. +!!! error TS1329: A decorator function must accept some number of arguments, but this expression takes none. Did you mean to call it first? } \ No newline at end of file diff --git a/tests/baselines/reference/decoratorOnClassProperty11.errors.txt b/tests/baselines/reference/decoratorOnClassProperty11.errors.txt index 2a72fefa53e..8e778300aa6 100644 --- a/tests/baselines/reference/decoratorOnClassProperty11.errors.txt +++ b/tests/baselines/reference/decoratorOnClassProperty11.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/decorators/class/property/decoratorOnClassProperty11.ts(4,5): error TS1240: Unable to resolve signature of property decorator when called as an expression. +tests/cases/conformance/decorators/class/property/decoratorOnClassProperty11.ts(4,5): error TS1329: A decorator function must accept some number of arguments, but this expression takes none. Did you mean to call it first? ==== tests/cases/conformance/decorators/class/property/decoratorOnClassProperty11.ts (1 errors) ==== @@ -7,5 +7,5 @@ tests/cases/conformance/decorators/class/property/decoratorOnClassProperty11.ts( class C { @dec prop; ~~~~ -!!! error TS1240: Unable to resolve signature of property decorator when called as an expression. +!!! error TS1329: A decorator function must accept some number of arguments, but this expression takes none. Did you mean to call it first? } \ No newline at end of file From 966f370712a5c16a527ce098acad0ff175262926 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 29 Sep 2017 16:42:30 -0700 Subject: [PATCH 13/54] Use a better check. --- src/compiler/checker.ts | 15 +++++++++++++-- src/compiler/diagnosticMessages.json | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) mode change 100755 => 100644 src/compiler/checker.ts mode change 100755 => 100644 src/compiler/diagnosticMessages.json diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts old mode 100755 new mode 100644 index b342003cb59..3760dd6a19f --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16381,8 +16381,8 @@ namespace ts { return resolveUntypedCall(node); } - if (callSignatures.length === 1 && callSignatures[0].parameters.length === 0) { - error(node, Diagnostics.A_decorator_function_must_accept_some_number_of_arguments_but_this_expression_takes_none_Did_you_mean_to_call_it_first); + if (isPotentiallyUncalledDecorator(node, callSignatures)) { + error(node, Diagnostics.This_function_cannot_be_used_as_a_decorator_Did_you_mean_to_call_it_first); return resolveErrorCall(node); } @@ -16398,6 +16398,17 @@ namespace ts { return resolveCall(node, callSignatures, candidatesOutArray, headMessage); } + /** + * Sometimes, we have a decorator that could accept zero arguments, + * but is receiving too many arguments as part of the decorator invocation. + * In those cases, a user may have meant to *call* the expression before using it as a decorator. + */ + function isPotentiallyUncalledDecorator(decorator: Decorator, signatures: Signature[]) { + return signatures.length && every(signatures, signature => + signature.minArgumentCount === 0 && + signature.parameters.length < getEffectiveArgumentCount(decorator, /*args*/ undefined, signature)) + } + /** * This function is similar to getResolvedSignature but is exclusively for trying to resolve JSX stateless-function component. * The main reason we have to use this function instead of getResolvedSignature because, the caller of this function will already check the type of openingLikeElement's tagName diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json old mode 100755 new mode 100644 index 193c6879b13..722985aac78 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -907,7 +907,7 @@ "category": "Error", "code": 1328 }, - "A decorator function must accept some number of arguments, but this expression takes none. Did you mean to call it first?": { + "This function cannot be used as a decorator. Did you mean to call it first?": { "category": "Error", "code": 1329 }, From 08ef6e4bea636cdabe05fd65cd07bc40f4924b9d Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 29 Sep 2017 16:42:46 -0700 Subject: [PATCH 14/54] Accepted baselines. --- src/compiler/checker.ts | 2 +- tests/baselines/reference/decoratorOnClassMethod6.errors.txt | 4 ++-- .../baselines/reference/decoratorOnClassProperty11.errors.txt | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3760dd6a19f..a272ac15e72 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16406,7 +16406,7 @@ namespace ts { function isPotentiallyUncalledDecorator(decorator: Decorator, signatures: Signature[]) { return signatures.length && every(signatures, signature => signature.minArgumentCount === 0 && - signature.parameters.length < getEffectiveArgumentCount(decorator, /*args*/ undefined, signature)) + signature.parameters.length < getEffectiveArgumentCount(decorator, /*args*/ undefined, signature)); } /** diff --git a/tests/baselines/reference/decoratorOnClassMethod6.errors.txt b/tests/baselines/reference/decoratorOnClassMethod6.errors.txt index ab09a01f19b..befe582025c 100644 --- a/tests/baselines/reference/decoratorOnClassMethod6.errors.txt +++ b/tests/baselines/reference/decoratorOnClassMethod6.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/decorators/class/method/decoratorOnClassMethod6.ts(4,5): error TS1329: A decorator function must accept some number of arguments, but this expression takes none. Did you mean to call it first? +tests/cases/conformance/decorators/class/method/decoratorOnClassMethod6.ts(4,5): error TS1329: This function cannot be used as a decorator. Did you mean to call it first? ==== tests/cases/conformance/decorators/class/method/decoratorOnClassMethod6.ts (1 errors) ==== @@ -7,5 +7,5 @@ tests/cases/conformance/decorators/class/method/decoratorOnClassMethod6.ts(4,5): class C { @dec ["method"]() {} ~~~~ -!!! error TS1329: A decorator function must accept some number of arguments, but this expression takes none. Did you mean to call it first? +!!! error TS1329: This function cannot be used as a decorator. Did you mean to call it first? } \ No newline at end of file diff --git a/tests/baselines/reference/decoratorOnClassProperty11.errors.txt b/tests/baselines/reference/decoratorOnClassProperty11.errors.txt index 8e778300aa6..771ca46b572 100644 --- a/tests/baselines/reference/decoratorOnClassProperty11.errors.txt +++ b/tests/baselines/reference/decoratorOnClassProperty11.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/decorators/class/property/decoratorOnClassProperty11.ts(4,5): error TS1329: A decorator function must accept some number of arguments, but this expression takes none. Did you mean to call it first? +tests/cases/conformance/decorators/class/property/decoratorOnClassProperty11.ts(4,5): error TS1329: This function cannot be used as a decorator. Did you mean to call it first? ==== tests/cases/conformance/decorators/class/property/decoratorOnClassProperty11.ts (1 errors) ==== @@ -7,5 +7,5 @@ tests/cases/conformance/decorators/class/property/decoratorOnClassProperty11.ts( class C { @dec prop; ~~~~ -!!! error TS1329: A decorator function must accept some number of arguments, but this expression takes none. Did you mean to call it first? +!!! error TS1329: This function cannot be used as a decorator. Did you mean to call it first? } \ No newline at end of file From 86315ed4119db22cc900b58d053f471d4d354119 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 29 Sep 2017 22:01:00 -0700 Subject: [PATCH 15/54] Added test and adjusted reporting logic. --- src/compiler/checker.ts | 3 +- src/compiler/diagnosticMessages.json | 2 +- src/compiler/emitter.ts | 0 .../compiler/potentiallyUncalledDecorators.ts | 74 +++++++++++++++++++ 4 files changed, 77 insertions(+), 2 deletions(-) mode change 100755 => 100644 src/compiler/emitter.ts create mode 100644 tests/cases/compiler/potentiallyUncalledDecorators.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a272ac15e72..23da94437ae 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16382,7 +16382,7 @@ namespace ts { } if (isPotentiallyUncalledDecorator(node, callSignatures)) { - error(node, Diagnostics.This_function_cannot_be_used_as_a_decorator_Did_you_mean_to_call_it_first); + error(node, Diagnostics.This_value_has_type_0_which_accepts_too_few_arguments_to_be_used_as_a_decorator_here_Did_you_mean_to_call_it_first, typeToString(funcType)); return resolveErrorCall(node); } @@ -16406,6 +16406,7 @@ namespace ts { function isPotentiallyUncalledDecorator(decorator: Decorator, signatures: Signature[]) { return signatures.length && every(signatures, signature => signature.minArgumentCount === 0 && + !signature.hasRestParameter && signature.parameters.length < getEffectiveArgumentCount(decorator, /*args*/ undefined, signature)); } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 722985aac78..57dc9f28834 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -907,7 +907,7 @@ "category": "Error", "code": 1328 }, - "This function cannot be used as a decorator. Did you mean to call it first?": { + "This value has type '{0}' which accepts too few arguments to be used as a decorator here. Did you mean to call it first?": { "category": "Error", "code": 1329 }, diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts old mode 100755 new mode 100644 diff --git a/tests/cases/compiler/potentiallyUncalledDecorators.ts b/tests/cases/compiler/potentiallyUncalledDecorators.ts new file mode 100644 index 00000000000..3c92a21eff0 --- /dev/null +++ b/tests/cases/compiler/potentiallyUncalledDecorators.ts @@ -0,0 +1,74 @@ +// @target: esnext +// @module: esnext +// @experimentalDecorators: true + +// Angular-style Input/Output API: +declare function Input(bindingPropertyName?: string): any; +class FooComponent { + @Input foo: string; +} + +// Glimmer-style tracked API: +declare const tracked: PropertyDecorator & { (...watchedProperties: string[]): any; } + +class Person { + @tracked person; any; +} + +class MultiplyByTwo { + args: any; + @tracked('args') + get multiplied() { + return this.args.number * 2; + } +} + +// Other fun stuff. + +interface OmniDecorator extends MethodDecorator, ClassDecorator, PropertyDecorator { +} + +declare function noArgs(): OmniDecorator; +declare function allRest(...args: any[]): OmniDecorator; +declare function oneOptional(x?: any): OmniDecorator; +declare function twoOptional(x?: any, y?: any): OmniDecorator; +declare function threeOptional(x?: any, y?: any, z?: any): OmniDecorator; +declare function oneOptionalWithRest(x?: any, ...args: any[]): OmniDecorator; + +@noArgs +class A { + @noArgs foo: any; + @noArgs bar() { } +} + +@allRest +class B { + @allRest foo: any; + @allRest bar() { } +} + +@oneOptional +class C { + @oneOptional foo: any; + @oneOptional bar() { } +} + +@twoOptional +class D { + @twoOptional foo: any; + @twoOptional bar() { } +} + +@threeOptional +class E { + @threeOptional foo: any; + @threeOptional bar() { } +} + +@oneOptionalWithRest +class F { + @oneOptionalWithRest foo: any; + @oneOptionalWithRest bar() { } +} + +export { }; From 803b5660fcf6b8d50b0751d76629de18c79540a3 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 29 Sep 2017 22:02:35 -0700 Subject: [PATCH 16/54] Accepted baselines. --- .../decoratorOnClassMethod6.errors.txt | 4 +- .../decoratorOnClassProperty11.errors.txt | 4 +- .../potentiallyUncalledDecorators.errors.txt | 167 ++++++++++++++++ .../potentiallyUncalledDecorators.js | 170 ++++++++++++++++ .../potentiallyUncalledDecorators.symbols | 182 +++++++++++++++++ .../potentiallyUncalledDecorators.types | 188 ++++++++++++++++++ 6 files changed, 711 insertions(+), 4 deletions(-) create mode 100644 tests/baselines/reference/potentiallyUncalledDecorators.errors.txt create mode 100644 tests/baselines/reference/potentiallyUncalledDecorators.js create mode 100644 tests/baselines/reference/potentiallyUncalledDecorators.symbols create mode 100644 tests/baselines/reference/potentiallyUncalledDecorators.types diff --git a/tests/baselines/reference/decoratorOnClassMethod6.errors.txt b/tests/baselines/reference/decoratorOnClassMethod6.errors.txt index befe582025c..7a9e5fea355 100644 --- a/tests/baselines/reference/decoratorOnClassMethod6.errors.txt +++ b/tests/baselines/reference/decoratorOnClassMethod6.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/decorators/class/method/decoratorOnClassMethod6.ts(4,5): error TS1329: This function cannot be used as a decorator. Did you mean to call it first? +tests/cases/conformance/decorators/class/method/decoratorOnClassMethod6.ts(4,5): error TS1329: This value has type '() => (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor) => TypedPropertyDescriptor' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? ==== tests/cases/conformance/decorators/class/method/decoratorOnClassMethod6.ts (1 errors) ==== @@ -7,5 +7,5 @@ tests/cases/conformance/decorators/class/method/decoratorOnClassMethod6.ts(4,5): class C { @dec ["method"]() {} ~~~~ -!!! error TS1329: This function cannot be used as a decorator. Did you mean to call it first? +!!! error TS1329: This value has type '() => (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor) => TypedPropertyDescriptor' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? } \ No newline at end of file diff --git a/tests/baselines/reference/decoratorOnClassProperty11.errors.txt b/tests/baselines/reference/decoratorOnClassProperty11.errors.txt index 771ca46b572..9d16d64df41 100644 --- a/tests/baselines/reference/decoratorOnClassProperty11.errors.txt +++ b/tests/baselines/reference/decoratorOnClassProperty11.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/decorators/class/property/decoratorOnClassProperty11.ts(4,5): error TS1329: This function cannot be used as a decorator. Did you mean to call it first? +tests/cases/conformance/decorators/class/property/decoratorOnClassProperty11.ts(4,5): error TS1329: This value has type '() => (target: any, propertyKey: string) => void' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? ==== tests/cases/conformance/decorators/class/property/decoratorOnClassProperty11.ts (1 errors) ==== @@ -7,5 +7,5 @@ tests/cases/conformance/decorators/class/property/decoratorOnClassProperty11.ts( class C { @dec prop; ~~~~ -!!! error TS1329: This function cannot be used as a decorator. Did you mean to call it first? +!!! error TS1329: This value has type '() => (target: any, propertyKey: string) => void' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? } \ No newline at end of file diff --git a/tests/baselines/reference/potentiallyUncalledDecorators.errors.txt b/tests/baselines/reference/potentiallyUncalledDecorators.errors.txt new file mode 100644 index 00000000000..e2858160ba9 --- /dev/null +++ b/tests/baselines/reference/potentiallyUncalledDecorators.errors.txt @@ -0,0 +1,167 @@ +tests/cases/compiler/potentiallyUncalledDecorators.ts(4,5): error TS1329: This value has type '(bindingPropertyName?: string) => any' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? +tests/cases/compiler/potentiallyUncalledDecorators.ts(34,1): error TS1329: This value has type '() => OmniDecorator' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? +tests/cases/compiler/potentiallyUncalledDecorators.ts(36,5): error TS1329: This value has type '() => OmniDecorator' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? +tests/cases/compiler/potentiallyUncalledDecorators.ts(37,5): error TS1329: This value has type '() => OmniDecorator' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? +tests/cases/compiler/potentiallyUncalledDecorators.ts(40,1): error TS1238: Unable to resolve signature of class decorator when called as an expression. + Type 'OmniDecorator' is not assignable to type 'typeof B'. + Type 'OmniDecorator' provides no match for the signature 'new (): B'. +tests/cases/compiler/potentiallyUncalledDecorators.ts(42,5): error TS1236: The return type of a property decorator function must be either 'void' or 'any'. + Unable to resolve signature of property decorator when called as an expression. +tests/cases/compiler/potentiallyUncalledDecorators.ts(43,5): error TS1241: Unable to resolve signature of method decorator when called as an expression. + Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'. +tests/cases/compiler/potentiallyUncalledDecorators.ts(46,1): error TS1238: Unable to resolve signature of class decorator when called as an expression. + Type 'OmniDecorator' is not assignable to type 'typeof C'. + Type 'OmniDecorator' provides no match for the signature 'new (): C'. +tests/cases/compiler/potentiallyUncalledDecorators.ts(48,5): error TS1329: This value has type '(x?: any) => OmniDecorator' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? +tests/cases/compiler/potentiallyUncalledDecorators.ts(49,5): error TS1329: This value has type '(x?: any) => OmniDecorator' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? +tests/cases/compiler/potentiallyUncalledDecorators.ts(52,1): error TS1238: Unable to resolve signature of class decorator when called as an expression. + Type 'OmniDecorator' is not assignable to type 'typeof D'. + Type 'OmniDecorator' provides no match for the signature 'new (): D'. +tests/cases/compiler/potentiallyUncalledDecorators.ts(54,5): error TS1236: The return type of a property decorator function must be either 'void' or 'any'. + Unable to resolve signature of property decorator when called as an expression. +tests/cases/compiler/potentiallyUncalledDecorators.ts(55,5): error TS1241: Unable to resolve signature of method decorator when called as an expression. + Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'. +tests/cases/compiler/potentiallyUncalledDecorators.ts(58,1): error TS1238: Unable to resolve signature of class decorator when called as an expression. + Type 'OmniDecorator' is not assignable to type 'typeof E'. + Type 'OmniDecorator' provides no match for the signature 'new (): E'. +tests/cases/compiler/potentiallyUncalledDecorators.ts(60,5): error TS1236: The return type of a property decorator function must be either 'void' or 'any'. + Unable to resolve signature of property decorator when called as an expression. +tests/cases/compiler/potentiallyUncalledDecorators.ts(61,5): error TS1241: Unable to resolve signature of method decorator when called as an expression. + Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'. +tests/cases/compiler/potentiallyUncalledDecorators.ts(64,1): error TS1238: Unable to resolve signature of class decorator when called as an expression. + Type 'OmniDecorator' is not assignable to type 'typeof F'. + Type 'OmniDecorator' provides no match for the signature 'new (): F'. +tests/cases/compiler/potentiallyUncalledDecorators.ts(66,5): error TS1236: The return type of a property decorator function must be either 'void' or 'any'. + Unable to resolve signature of property decorator when called as an expression. +tests/cases/compiler/potentiallyUncalledDecorators.ts(67,5): error TS1241: Unable to resolve signature of method decorator when called as an expression. + Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'. + + +==== tests/cases/compiler/potentiallyUncalledDecorators.ts (19 errors) ==== + // Angular-style Input/Output API: + declare function Input(bindingPropertyName?: string): any; + class FooComponent { + @Input foo: string; + ~~~~~~ +!!! error TS1329: This value has type '(bindingPropertyName?: string) => any' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? + } + + // Glimmer-style tracked API: + declare const tracked: PropertyDecorator & { (...watchedProperties: string[]): any; } + + class Person { + @tracked person; any; + } + + class MultiplyByTwo { + args: any; + @tracked('args') + get multiplied() { + return this.args.number * 2; + } + } + + // Other fun stuff. + + interface OmniDecorator extends MethodDecorator, ClassDecorator, PropertyDecorator { + } + + declare function noArgs(): OmniDecorator; + declare function allRest(...args: any[]): OmniDecorator; + declare function oneOptional(x?: any): OmniDecorator; + declare function twoOptional(x?: any, y?: any): OmniDecorator; + declare function threeOptional(x?: any, y?: any, z?: any): OmniDecorator; + declare function oneOptionalWithRest(x?: any, ...args: any[]): OmniDecorator; + + @noArgs + ~~~~~~~ +!!! error TS1329: This value has type '() => OmniDecorator' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? + class A { + @noArgs foo: any; + ~~~~~~~ +!!! error TS1329: This value has type '() => OmniDecorator' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? + @noArgs bar() { } + ~~~~~~~ +!!! error TS1329: This value has type '() => OmniDecorator' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? + } + + @allRest + ~~~~~~~~ +!!! error TS1238: Unable to resolve signature of class decorator when called as an expression. +!!! error TS1238: Type 'OmniDecorator' is not assignable to type 'typeof B'. +!!! error TS1238: Type 'OmniDecorator' provides no match for the signature 'new (): B'. + class B { + @allRest foo: any; + ~~~~~~~~ +!!! error TS1236: The return type of a property decorator function must be either 'void' or 'any'. +!!! error TS1236: Unable to resolve signature of property decorator when called as an expression. + @allRest bar() { } + ~~~~~~~~ +!!! error TS1241: Unable to resolve signature of method decorator when called as an expression. +!!! error TS1241: Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'. + } + + @oneOptional + ~~~~~~~~~~~~ +!!! error TS1238: Unable to resolve signature of class decorator when called as an expression. +!!! error TS1238: Type 'OmniDecorator' is not assignable to type 'typeof C'. +!!! error TS1238: Type 'OmniDecorator' provides no match for the signature 'new (): C'. + class C { + @oneOptional foo: any; + ~~~~~~~~~~~~ +!!! error TS1329: This value has type '(x?: any) => OmniDecorator' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? + @oneOptional bar() { } + ~~~~~~~~~~~~ +!!! error TS1329: This value has type '(x?: any) => OmniDecorator' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? + } + + @twoOptional + ~~~~~~~~~~~~ +!!! error TS1238: Unable to resolve signature of class decorator when called as an expression. +!!! error TS1238: Type 'OmniDecorator' is not assignable to type 'typeof D'. +!!! error TS1238: Type 'OmniDecorator' provides no match for the signature 'new (): D'. + class D { + @twoOptional foo: any; + ~~~~~~~~~~~~ +!!! error TS1236: The return type of a property decorator function must be either 'void' or 'any'. +!!! error TS1236: Unable to resolve signature of property decorator when called as an expression. + @twoOptional bar() { } + ~~~~~~~~~~~~ +!!! error TS1241: Unable to resolve signature of method decorator when called as an expression. +!!! error TS1241: Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'. + } + + @threeOptional + ~~~~~~~~~~~~~~ +!!! error TS1238: Unable to resolve signature of class decorator when called as an expression. +!!! error TS1238: Type 'OmniDecorator' is not assignable to type 'typeof E'. +!!! error TS1238: Type 'OmniDecorator' provides no match for the signature 'new (): E'. + class E { + @threeOptional foo: any; + ~~~~~~~~~~~~~~ +!!! error TS1236: The return type of a property decorator function must be either 'void' or 'any'. +!!! error TS1236: Unable to resolve signature of property decorator when called as an expression. + @threeOptional bar() { } + ~~~~~~~~~~~~~~ +!!! error TS1241: Unable to resolve signature of method decorator when called as an expression. +!!! error TS1241: Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'. + } + + @oneOptionalWithRest + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS1238: Unable to resolve signature of class decorator when called as an expression. +!!! error TS1238: Type 'OmniDecorator' is not assignable to type 'typeof F'. +!!! error TS1238: Type 'OmniDecorator' provides no match for the signature 'new (): F'. + class F { + @oneOptionalWithRest foo: any; + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS1236: The return type of a property decorator function must be either 'void' or 'any'. +!!! error TS1236: Unable to resolve signature of property decorator when called as an expression. + @oneOptionalWithRest bar() { } + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS1241: Unable to resolve signature of method decorator when called as an expression. +!!! error TS1241: Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'. + } + + export { }; + \ No newline at end of file diff --git a/tests/baselines/reference/potentiallyUncalledDecorators.js b/tests/baselines/reference/potentiallyUncalledDecorators.js new file mode 100644 index 00000000000..a0dcc80ea0e --- /dev/null +++ b/tests/baselines/reference/potentiallyUncalledDecorators.js @@ -0,0 +1,170 @@ +//// [potentiallyUncalledDecorators.ts] +// Angular-style Input/Output API: +declare function Input(bindingPropertyName?: string): any; +class FooComponent { + @Input foo: string; +} + +// Glimmer-style tracked API: +declare const tracked: PropertyDecorator & { (...watchedProperties: string[]): any; } + +class Person { + @tracked person; any; +} + +class MultiplyByTwo { + args: any; + @tracked('args') + get multiplied() { + return this.args.number * 2; + } +} + +// Other fun stuff. + +interface OmniDecorator extends MethodDecorator, ClassDecorator, PropertyDecorator { +} + +declare function noArgs(): OmniDecorator; +declare function allRest(...args: any[]): OmniDecorator; +declare function oneOptional(x?: any): OmniDecorator; +declare function twoOptional(x?: any, y?: any): OmniDecorator; +declare function threeOptional(x?: any, y?: any, z?: any): OmniDecorator; +declare function oneOptionalWithRest(x?: any, ...args: any[]): OmniDecorator; + +@noArgs +class A { + @noArgs foo: any; + @noArgs bar() { } +} + +@allRest +class B { + @allRest foo: any; + @allRest bar() { } +} + +@oneOptional +class C { + @oneOptional foo: any; + @oneOptional bar() { } +} + +@twoOptional +class D { + @twoOptional foo: any; + @twoOptional bar() { } +} + +@threeOptional +class E { + @threeOptional foo: any; + @threeOptional bar() { } +} + +@oneOptionalWithRest +class F { + @oneOptionalWithRest foo: any; + @oneOptionalWithRest bar() { } +} + +export { }; + + +//// [potentiallyUncalledDecorators.js] +var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; +}; +class FooComponent { +} +__decorate([ + Input +], FooComponent.prototype, "foo", void 0); +class Person { +} +__decorate([ + tracked +], Person.prototype, "person", void 0); +class MultiplyByTwo { + get multiplied() { + return this.args.number * 2; + } +} +__decorate([ + tracked('args') +], MultiplyByTwo.prototype, "multiplied", null); +let A = class A { + bar() { } +}; +__decorate([ + noArgs +], A.prototype, "foo", void 0); +__decorate([ + noArgs +], A.prototype, "bar", null); +A = __decorate([ + noArgs +], A); +let B = class B { + bar() { } +}; +__decorate([ + allRest +], B.prototype, "foo", void 0); +__decorate([ + allRest +], B.prototype, "bar", null); +B = __decorate([ + allRest +], B); +let C = class C { + bar() { } +}; +__decorate([ + oneOptional +], C.prototype, "foo", void 0); +__decorate([ + oneOptional +], C.prototype, "bar", null); +C = __decorate([ + oneOptional +], C); +let D = class D { + bar() { } +}; +__decorate([ + twoOptional +], D.prototype, "foo", void 0); +__decorate([ + twoOptional +], D.prototype, "bar", null); +D = __decorate([ + twoOptional +], D); +let E = class E { + bar() { } +}; +__decorate([ + threeOptional +], E.prototype, "foo", void 0); +__decorate([ + threeOptional +], E.prototype, "bar", null); +E = __decorate([ + threeOptional +], E); +let F = class F { + bar() { } +}; +__decorate([ + oneOptionalWithRest +], F.prototype, "foo", void 0); +__decorate([ + oneOptionalWithRest +], F.prototype, "bar", null); +F = __decorate([ + oneOptionalWithRest +], F); diff --git a/tests/baselines/reference/potentiallyUncalledDecorators.symbols b/tests/baselines/reference/potentiallyUncalledDecorators.symbols new file mode 100644 index 00000000000..ff7cc253a27 --- /dev/null +++ b/tests/baselines/reference/potentiallyUncalledDecorators.symbols @@ -0,0 +1,182 @@ +=== tests/cases/compiler/potentiallyUncalledDecorators.ts === +// Angular-style Input/Output API: +declare function Input(bindingPropertyName?: string): any; +>Input : Symbol(Input, Decl(potentiallyUncalledDecorators.ts, 0, 0)) +>bindingPropertyName : Symbol(bindingPropertyName, Decl(potentiallyUncalledDecorators.ts, 1, 23)) + +class FooComponent { +>FooComponent : Symbol(FooComponent, Decl(potentiallyUncalledDecorators.ts, 1, 58)) + + @Input foo: string; +>Input : Symbol(Input, Decl(potentiallyUncalledDecorators.ts, 0, 0)) +>foo : Symbol(FooComponent.foo, Decl(potentiallyUncalledDecorators.ts, 2, 20)) +} + +// Glimmer-style tracked API: +declare const tracked: PropertyDecorator & { (...watchedProperties: string[]): any; } +>tracked : Symbol(tracked, Decl(potentiallyUncalledDecorators.ts, 7, 13)) +>PropertyDecorator : Symbol(PropertyDecorator, Decl(lib.es5.d.ts, --, --)) +>watchedProperties : Symbol(watchedProperties, Decl(potentiallyUncalledDecorators.ts, 7, 46)) + +class Person { +>Person : Symbol(Person, Decl(potentiallyUncalledDecorators.ts, 7, 85)) + + @tracked person; any; +>tracked : Symbol(tracked, Decl(potentiallyUncalledDecorators.ts, 7, 13)) +>person : Symbol(Person.person, Decl(potentiallyUncalledDecorators.ts, 9, 14)) +>any : Symbol(Person.any, Decl(potentiallyUncalledDecorators.ts, 10, 20)) +} + +class MultiplyByTwo { +>MultiplyByTwo : Symbol(MultiplyByTwo, Decl(potentiallyUncalledDecorators.ts, 11, 1)) + + args: any; +>args : Symbol(MultiplyByTwo.args, Decl(potentiallyUncalledDecorators.ts, 13, 21)) + + @tracked('args') +>tracked : Symbol(tracked, Decl(potentiallyUncalledDecorators.ts, 7, 13)) + + get multiplied() { +>multiplied : Symbol(MultiplyByTwo.multiplied, Decl(potentiallyUncalledDecorators.ts, 14, 14)) + + return this.args.number * 2; +>this.args : Symbol(MultiplyByTwo.args, Decl(potentiallyUncalledDecorators.ts, 13, 21)) +>this : Symbol(MultiplyByTwo, Decl(potentiallyUncalledDecorators.ts, 11, 1)) +>args : Symbol(MultiplyByTwo.args, Decl(potentiallyUncalledDecorators.ts, 13, 21)) + } +} + +// Other fun stuff. + +interface OmniDecorator extends MethodDecorator, ClassDecorator, PropertyDecorator { +>OmniDecorator : Symbol(OmniDecorator, Decl(potentiallyUncalledDecorators.ts, 19, 1)) +>MethodDecorator : Symbol(MethodDecorator, Decl(lib.es5.d.ts, --, --)) +>ClassDecorator : Symbol(ClassDecorator, Decl(lib.es5.d.ts, --, --)) +>PropertyDecorator : Symbol(PropertyDecorator, Decl(lib.es5.d.ts, --, --)) +} + +declare function noArgs(): OmniDecorator; +>noArgs : Symbol(noArgs, Decl(potentiallyUncalledDecorators.ts, 24, 1)) +>OmniDecorator : Symbol(OmniDecorator, Decl(potentiallyUncalledDecorators.ts, 19, 1)) + +declare function allRest(...args: any[]): OmniDecorator; +>allRest : Symbol(allRest, Decl(potentiallyUncalledDecorators.ts, 26, 41)) +>args : Symbol(args, Decl(potentiallyUncalledDecorators.ts, 27, 25)) +>OmniDecorator : Symbol(OmniDecorator, Decl(potentiallyUncalledDecorators.ts, 19, 1)) + +declare function oneOptional(x?: any): OmniDecorator; +>oneOptional : Symbol(oneOptional, Decl(potentiallyUncalledDecorators.ts, 27, 56)) +>x : Symbol(x, Decl(potentiallyUncalledDecorators.ts, 28, 29)) +>OmniDecorator : Symbol(OmniDecorator, Decl(potentiallyUncalledDecorators.ts, 19, 1)) + +declare function twoOptional(x?: any, y?: any): OmniDecorator; +>twoOptional : Symbol(twoOptional, Decl(potentiallyUncalledDecorators.ts, 28, 53)) +>x : Symbol(x, Decl(potentiallyUncalledDecorators.ts, 29, 29)) +>y : Symbol(y, Decl(potentiallyUncalledDecorators.ts, 29, 37)) +>OmniDecorator : Symbol(OmniDecorator, Decl(potentiallyUncalledDecorators.ts, 19, 1)) + +declare function threeOptional(x?: any, y?: any, z?: any): OmniDecorator; +>threeOptional : Symbol(threeOptional, Decl(potentiallyUncalledDecorators.ts, 29, 62)) +>x : Symbol(x, Decl(potentiallyUncalledDecorators.ts, 30, 31)) +>y : Symbol(y, Decl(potentiallyUncalledDecorators.ts, 30, 39)) +>z : Symbol(z, Decl(potentiallyUncalledDecorators.ts, 30, 48)) +>OmniDecorator : Symbol(OmniDecorator, Decl(potentiallyUncalledDecorators.ts, 19, 1)) + +declare function oneOptionalWithRest(x?: any, ...args: any[]): OmniDecorator; +>oneOptionalWithRest : Symbol(oneOptionalWithRest, Decl(potentiallyUncalledDecorators.ts, 30, 73)) +>x : Symbol(x, Decl(potentiallyUncalledDecorators.ts, 31, 37)) +>args : Symbol(args, Decl(potentiallyUncalledDecorators.ts, 31, 45)) +>OmniDecorator : Symbol(OmniDecorator, Decl(potentiallyUncalledDecorators.ts, 19, 1)) + +@noArgs +>noArgs : Symbol(noArgs, Decl(potentiallyUncalledDecorators.ts, 24, 1)) + +class A { +>A : Symbol(A, Decl(potentiallyUncalledDecorators.ts, 31, 77)) + + @noArgs foo: any; +>noArgs : Symbol(noArgs, Decl(potentiallyUncalledDecorators.ts, 24, 1)) +>foo : Symbol(A.foo, Decl(potentiallyUncalledDecorators.ts, 34, 9)) + + @noArgs bar() { } +>noArgs : Symbol(noArgs, Decl(potentiallyUncalledDecorators.ts, 24, 1)) +>bar : Symbol(A.bar, Decl(potentiallyUncalledDecorators.ts, 35, 21)) +} + +@allRest +>allRest : Symbol(allRest, Decl(potentiallyUncalledDecorators.ts, 26, 41)) + +class B { +>B : Symbol(B, Decl(potentiallyUncalledDecorators.ts, 37, 1)) + + @allRest foo: any; +>allRest : Symbol(allRest, Decl(potentiallyUncalledDecorators.ts, 26, 41)) +>foo : Symbol(B.foo, Decl(potentiallyUncalledDecorators.ts, 40, 9)) + + @allRest bar() { } +>allRest : Symbol(allRest, Decl(potentiallyUncalledDecorators.ts, 26, 41)) +>bar : Symbol(B.bar, Decl(potentiallyUncalledDecorators.ts, 41, 22)) +} + +@oneOptional +>oneOptional : Symbol(oneOptional, Decl(potentiallyUncalledDecorators.ts, 27, 56)) + +class C { +>C : Symbol(C, Decl(potentiallyUncalledDecorators.ts, 43, 1)) + + @oneOptional foo: any; +>oneOptional : Symbol(oneOptional, Decl(potentiallyUncalledDecorators.ts, 27, 56)) +>foo : Symbol(C.foo, Decl(potentiallyUncalledDecorators.ts, 46, 9)) + + @oneOptional bar() { } +>oneOptional : Symbol(oneOptional, Decl(potentiallyUncalledDecorators.ts, 27, 56)) +>bar : Symbol(C.bar, Decl(potentiallyUncalledDecorators.ts, 47, 26)) +} + +@twoOptional +>twoOptional : Symbol(twoOptional, Decl(potentiallyUncalledDecorators.ts, 28, 53)) + +class D { +>D : Symbol(D, Decl(potentiallyUncalledDecorators.ts, 49, 1)) + + @twoOptional foo: any; +>twoOptional : Symbol(twoOptional, Decl(potentiallyUncalledDecorators.ts, 28, 53)) +>foo : Symbol(D.foo, Decl(potentiallyUncalledDecorators.ts, 52, 9)) + + @twoOptional bar() { } +>twoOptional : Symbol(twoOptional, Decl(potentiallyUncalledDecorators.ts, 28, 53)) +>bar : Symbol(D.bar, Decl(potentiallyUncalledDecorators.ts, 53, 26)) +} + +@threeOptional +>threeOptional : Symbol(threeOptional, Decl(potentiallyUncalledDecorators.ts, 29, 62)) + +class E { +>E : Symbol(E, Decl(potentiallyUncalledDecorators.ts, 55, 1)) + + @threeOptional foo: any; +>threeOptional : Symbol(threeOptional, Decl(potentiallyUncalledDecorators.ts, 29, 62)) +>foo : Symbol(E.foo, Decl(potentiallyUncalledDecorators.ts, 58, 9)) + + @threeOptional bar() { } +>threeOptional : Symbol(threeOptional, Decl(potentiallyUncalledDecorators.ts, 29, 62)) +>bar : Symbol(E.bar, Decl(potentiallyUncalledDecorators.ts, 59, 28)) +} + +@oneOptionalWithRest +>oneOptionalWithRest : Symbol(oneOptionalWithRest, Decl(potentiallyUncalledDecorators.ts, 30, 73)) + +class F { +>F : Symbol(F, Decl(potentiallyUncalledDecorators.ts, 61, 1)) + + @oneOptionalWithRest foo: any; +>oneOptionalWithRest : Symbol(oneOptionalWithRest, Decl(potentiallyUncalledDecorators.ts, 30, 73)) +>foo : Symbol(F.foo, Decl(potentiallyUncalledDecorators.ts, 64, 9)) + + @oneOptionalWithRest bar() { } +>oneOptionalWithRest : Symbol(oneOptionalWithRest, Decl(potentiallyUncalledDecorators.ts, 30, 73)) +>bar : Symbol(F.bar, Decl(potentiallyUncalledDecorators.ts, 65, 34)) +} + +export { }; + diff --git a/tests/baselines/reference/potentiallyUncalledDecorators.types b/tests/baselines/reference/potentiallyUncalledDecorators.types new file mode 100644 index 00000000000..d79972659d3 --- /dev/null +++ b/tests/baselines/reference/potentiallyUncalledDecorators.types @@ -0,0 +1,188 @@ +=== tests/cases/compiler/potentiallyUncalledDecorators.ts === +// Angular-style Input/Output API: +declare function Input(bindingPropertyName?: string): any; +>Input : (bindingPropertyName?: string) => any +>bindingPropertyName : string + +class FooComponent { +>FooComponent : FooComponent + + @Input foo: string; +>Input : (bindingPropertyName?: string) => any +>foo : string +} + +// Glimmer-style tracked API: +declare const tracked: PropertyDecorator & { (...watchedProperties: string[]): any; } +>tracked : PropertyDecorator & ((...watchedProperties: string[]) => any) +>PropertyDecorator : PropertyDecorator +>watchedProperties : string[] + +class Person { +>Person : Person + + @tracked person; any; +>tracked : PropertyDecorator & ((...watchedProperties: string[]) => any) +>person : any +>any : any +} + +class MultiplyByTwo { +>MultiplyByTwo : MultiplyByTwo + + args: any; +>args : any + + @tracked('args') +>tracked('args') : any +>tracked : PropertyDecorator & ((...watchedProperties: string[]) => any) +>'args' : "args" + + get multiplied() { +>multiplied : number + + return this.args.number * 2; +>this.args.number * 2 : number +>this.args.number : any +>this.args : any +>this : this +>args : any +>number : any +>2 : 2 + } +} + +// Other fun stuff. + +interface OmniDecorator extends MethodDecorator, ClassDecorator, PropertyDecorator { +>OmniDecorator : OmniDecorator +>MethodDecorator : MethodDecorator +>ClassDecorator : ClassDecorator +>PropertyDecorator : PropertyDecorator +} + +declare function noArgs(): OmniDecorator; +>noArgs : () => OmniDecorator +>OmniDecorator : OmniDecorator + +declare function allRest(...args: any[]): OmniDecorator; +>allRest : (...args: any[]) => OmniDecorator +>args : any[] +>OmniDecorator : OmniDecorator + +declare function oneOptional(x?: any): OmniDecorator; +>oneOptional : (x?: any) => OmniDecorator +>x : any +>OmniDecorator : OmniDecorator + +declare function twoOptional(x?: any, y?: any): OmniDecorator; +>twoOptional : (x?: any, y?: any) => OmniDecorator +>x : any +>y : any +>OmniDecorator : OmniDecorator + +declare function threeOptional(x?: any, y?: any, z?: any): OmniDecorator; +>threeOptional : (x?: any, y?: any, z?: any) => OmniDecorator +>x : any +>y : any +>z : any +>OmniDecorator : OmniDecorator + +declare function oneOptionalWithRest(x?: any, ...args: any[]): OmniDecorator; +>oneOptionalWithRest : (x?: any, ...args: any[]) => OmniDecorator +>x : any +>args : any[] +>OmniDecorator : OmniDecorator + +@noArgs +>noArgs : () => OmniDecorator + +class A { +>A : A + + @noArgs foo: any; +>noArgs : () => OmniDecorator +>foo : any + + @noArgs bar() { } +>noArgs : () => OmniDecorator +>bar : () => void +} + +@allRest +>allRest : (...args: any[]) => OmniDecorator + +class B { +>B : B + + @allRest foo: any; +>allRest : (...args: any[]) => OmniDecorator +>foo : any + + @allRest bar() { } +>allRest : (...args: any[]) => OmniDecorator +>bar : () => void +} + +@oneOptional +>oneOptional : (x?: any) => OmniDecorator + +class C { +>C : C + + @oneOptional foo: any; +>oneOptional : (x?: any) => OmniDecorator +>foo : any + + @oneOptional bar() { } +>oneOptional : (x?: any) => OmniDecorator +>bar : () => void +} + +@twoOptional +>twoOptional : (x?: any, y?: any) => OmniDecorator + +class D { +>D : D + + @twoOptional foo: any; +>twoOptional : (x?: any, y?: any) => OmniDecorator +>foo : any + + @twoOptional bar() { } +>twoOptional : (x?: any, y?: any) => OmniDecorator +>bar : () => void +} + +@threeOptional +>threeOptional : (x?: any, y?: any, z?: any) => OmniDecorator + +class E { +>E : E + + @threeOptional foo: any; +>threeOptional : (x?: any, y?: any, z?: any) => OmniDecorator +>foo : any + + @threeOptional bar() { } +>threeOptional : (x?: any, y?: any, z?: any) => OmniDecorator +>bar : () => void +} + +@oneOptionalWithRest +>oneOptionalWithRest : (x?: any, ...args: any[]) => OmniDecorator + +class F { +>F : F + + @oneOptionalWithRest foo: any; +>oneOptionalWithRest : (x?: any, ...args: any[]) => OmniDecorator +>foo : any + + @oneOptionalWithRest bar() { } +>oneOptionalWithRest : (x?: any, ...args: any[]) => OmniDecorator +>bar : () => void +} + +export { }; + From 96bb796730cc281a4850bfa43cffcc7929d98c6b Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Mon, 2 Oct 2017 17:56:10 -0700 Subject: [PATCH 17/54] Improve error message for uncalled decorators. --- src/compiler/checker.ts | 4 +++- src/compiler/diagnosticMessages.json | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 23da94437ae..c9d6c6949a9 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16382,7 +16382,9 @@ namespace ts { } if (isPotentiallyUncalledDecorator(node, callSignatures)) { - error(node, Diagnostics.This_value_has_type_0_which_accepts_too_few_arguments_to_be_used_as_a_decorator_here_Did_you_mean_to_call_it_first, typeToString(funcType)); + const printer = createPrinter({ removeComments: true }); + const nodeStr = printer.printNode(EmitHint.Expression, node.expression, getSourceFileOfNode(node)); + error(node, Diagnostics._0_accepts_too_few_arguments_to_be_used_as_a_decorator_here_Did_you_mean_to_call_it_first_and_write_0, nodeStr); return resolveErrorCall(node); } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 57dc9f28834..eb508857e6f 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -907,7 +907,7 @@ "category": "Error", "code": 1328 }, - "This value has type '{0}' which accepts too few arguments to be used as a decorator here. Did you mean to call it first?": { + "'{0}' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@{0}()'?": { "category": "Error", "code": 1329 }, From 392cd6117bb5c8941141e18919f051f66bf0dd5c Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Mon, 2 Oct 2017 18:00:00 -0700 Subject: [PATCH 18/54] Added a test for an 'any'-type decorator. --- tests/cases/compiler/potentiallyUncalledDecorators.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/cases/compiler/potentiallyUncalledDecorators.ts b/tests/cases/compiler/potentiallyUncalledDecorators.ts index 3c92a21eff0..6e537686d0f 100644 --- a/tests/cases/compiler/potentiallyUncalledDecorators.ts +++ b/tests/cases/compiler/potentiallyUncalledDecorators.ts @@ -34,6 +34,7 @@ declare function oneOptional(x?: any): OmniDecorator; declare function twoOptional(x?: any, y?: any): OmniDecorator; declare function threeOptional(x?: any, y?: any, z?: any): OmniDecorator; declare function oneOptionalWithRest(x?: any, ...args: any[]): OmniDecorator; +declare const anyDec: any; @noArgs class A { @@ -71,4 +72,10 @@ class F { @oneOptionalWithRest bar() { } } +@anyDec +class G { + @anyDec foo: any; + @anyDec bar() { } +} + export { }; From 35cfcff0d4d0861ec27049eb32fc56075ca0642c Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Mon, 2 Oct 2017 18:09:28 -0700 Subject: [PATCH 19/54] Use 'getTextOfNode'. --- src/compiler/checker.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c9d6c6949a9..8d8595409e8 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16382,8 +16382,7 @@ namespace ts { } if (isPotentiallyUncalledDecorator(node, callSignatures)) { - const printer = createPrinter({ removeComments: true }); - const nodeStr = printer.printNode(EmitHint.Expression, node.expression, getSourceFileOfNode(node)); + const nodeStr = getTextOfNode(node.expression, /*includeTrivia*/ false); error(node, Diagnostics._0_accepts_too_few_arguments_to_be_used_as_a_decorator_here_Did_you_mean_to_call_it_first_and_write_0, nodeStr); return resolveErrorCall(node); } From 7750a88957fec5c41fb286b8d59dfb4487a1b63c Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Mon, 2 Oct 2017 18:50:05 -0700 Subject: [PATCH 20/54] Accepted baselines. --- .../decoratorOnClassMethod6.errors.txt | 4 +- .../decoratorOnClassProperty11.errors.txt | 4 +- .../potentiallyUncalledDecorators.errors.txt | 57 +++++++++++-------- .../potentiallyUncalledDecorators.js | 19 +++++++ .../potentiallyUncalledDecorators.symbols | 54 ++++++++++++------ .../potentiallyUncalledDecorators.types | 18 ++++++ 6 files changed, 109 insertions(+), 47 deletions(-) diff --git a/tests/baselines/reference/decoratorOnClassMethod6.errors.txt b/tests/baselines/reference/decoratorOnClassMethod6.errors.txt index 7a9e5fea355..b863a9db72c 100644 --- a/tests/baselines/reference/decoratorOnClassMethod6.errors.txt +++ b/tests/baselines/reference/decoratorOnClassMethod6.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/decorators/class/method/decoratorOnClassMethod6.ts(4,5): error TS1329: This value has type '() => (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor) => TypedPropertyDescriptor' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? +tests/cases/conformance/decorators/class/method/decoratorOnClassMethod6.ts(4,5): error TS1329: 'dec' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@dec()'? ==== tests/cases/conformance/decorators/class/method/decoratorOnClassMethod6.ts (1 errors) ==== @@ -7,5 +7,5 @@ tests/cases/conformance/decorators/class/method/decoratorOnClassMethod6.ts(4,5): class C { @dec ["method"]() {} ~~~~ -!!! error TS1329: This value has type '() => (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor) => TypedPropertyDescriptor' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? +!!! error TS1329: 'dec' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@dec()'? } \ No newline at end of file diff --git a/tests/baselines/reference/decoratorOnClassProperty11.errors.txt b/tests/baselines/reference/decoratorOnClassProperty11.errors.txt index 9d16d64df41..537daaf44ea 100644 --- a/tests/baselines/reference/decoratorOnClassProperty11.errors.txt +++ b/tests/baselines/reference/decoratorOnClassProperty11.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/decorators/class/property/decoratorOnClassProperty11.ts(4,5): error TS1329: This value has type '() => (target: any, propertyKey: string) => void' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? +tests/cases/conformance/decorators/class/property/decoratorOnClassProperty11.ts(4,5): error TS1329: 'dec' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@dec()'? ==== tests/cases/conformance/decorators/class/property/decoratorOnClassProperty11.ts (1 errors) ==== @@ -7,5 +7,5 @@ tests/cases/conformance/decorators/class/property/decoratorOnClassProperty11.ts( class C { @dec prop; ~~~~ -!!! error TS1329: This value has type '() => (target: any, propertyKey: string) => void' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? +!!! error TS1329: 'dec' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@dec()'? } \ No newline at end of file diff --git a/tests/baselines/reference/potentiallyUncalledDecorators.errors.txt b/tests/baselines/reference/potentiallyUncalledDecorators.errors.txt index e2858160ba9..9028ebe0377 100644 --- a/tests/baselines/reference/potentiallyUncalledDecorators.errors.txt +++ b/tests/baselines/reference/potentiallyUncalledDecorators.errors.txt @@ -1,39 +1,39 @@ -tests/cases/compiler/potentiallyUncalledDecorators.ts(4,5): error TS1329: This value has type '(bindingPropertyName?: string) => any' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? -tests/cases/compiler/potentiallyUncalledDecorators.ts(34,1): error TS1329: This value has type '() => OmniDecorator' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? -tests/cases/compiler/potentiallyUncalledDecorators.ts(36,5): error TS1329: This value has type '() => OmniDecorator' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? -tests/cases/compiler/potentiallyUncalledDecorators.ts(37,5): error TS1329: This value has type '() => OmniDecorator' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? -tests/cases/compiler/potentiallyUncalledDecorators.ts(40,1): error TS1238: Unable to resolve signature of class decorator when called as an expression. +tests/cases/compiler/potentiallyUncalledDecorators.ts(4,5): error TS1329: 'Input' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@Input()'? +tests/cases/compiler/potentiallyUncalledDecorators.ts(35,1): error TS1329: 'noArgs' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@noArgs()'? +tests/cases/compiler/potentiallyUncalledDecorators.ts(37,5): error TS1329: 'noArgs' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@noArgs()'? +tests/cases/compiler/potentiallyUncalledDecorators.ts(38,5): error TS1329: 'noArgs' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@noArgs()'? +tests/cases/compiler/potentiallyUncalledDecorators.ts(41,1): error TS1238: Unable to resolve signature of class decorator when called as an expression. Type 'OmniDecorator' is not assignable to type 'typeof B'. Type 'OmniDecorator' provides no match for the signature 'new (): B'. -tests/cases/compiler/potentiallyUncalledDecorators.ts(42,5): error TS1236: The return type of a property decorator function must be either 'void' or 'any'. +tests/cases/compiler/potentiallyUncalledDecorators.ts(43,5): error TS1236: The return type of a property decorator function must be either 'void' or 'any'. Unable to resolve signature of property decorator when called as an expression. -tests/cases/compiler/potentiallyUncalledDecorators.ts(43,5): error TS1241: Unable to resolve signature of method decorator when called as an expression. +tests/cases/compiler/potentiallyUncalledDecorators.ts(44,5): error TS1241: Unable to resolve signature of method decorator when called as an expression. Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'. -tests/cases/compiler/potentiallyUncalledDecorators.ts(46,1): error TS1238: Unable to resolve signature of class decorator when called as an expression. +tests/cases/compiler/potentiallyUncalledDecorators.ts(47,1): error TS1238: Unable to resolve signature of class decorator when called as an expression. Type 'OmniDecorator' is not assignable to type 'typeof C'. Type 'OmniDecorator' provides no match for the signature 'new (): C'. -tests/cases/compiler/potentiallyUncalledDecorators.ts(48,5): error TS1329: This value has type '(x?: any) => OmniDecorator' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? -tests/cases/compiler/potentiallyUncalledDecorators.ts(49,5): error TS1329: This value has type '(x?: any) => OmniDecorator' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? -tests/cases/compiler/potentiallyUncalledDecorators.ts(52,1): error TS1238: Unable to resolve signature of class decorator when called as an expression. +tests/cases/compiler/potentiallyUncalledDecorators.ts(49,5): error TS1329: 'oneOptional' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@oneOptional()'? +tests/cases/compiler/potentiallyUncalledDecorators.ts(50,5): error TS1329: 'oneOptional' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@oneOptional()'? +tests/cases/compiler/potentiallyUncalledDecorators.ts(53,1): error TS1238: Unable to resolve signature of class decorator when called as an expression. Type 'OmniDecorator' is not assignable to type 'typeof D'. Type 'OmniDecorator' provides no match for the signature 'new (): D'. -tests/cases/compiler/potentiallyUncalledDecorators.ts(54,5): error TS1236: The return type of a property decorator function must be either 'void' or 'any'. +tests/cases/compiler/potentiallyUncalledDecorators.ts(55,5): error TS1236: The return type of a property decorator function must be either 'void' or 'any'. Unable to resolve signature of property decorator when called as an expression. -tests/cases/compiler/potentiallyUncalledDecorators.ts(55,5): error TS1241: Unable to resolve signature of method decorator when called as an expression. +tests/cases/compiler/potentiallyUncalledDecorators.ts(56,5): error TS1241: Unable to resolve signature of method decorator when called as an expression. Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'. -tests/cases/compiler/potentiallyUncalledDecorators.ts(58,1): error TS1238: Unable to resolve signature of class decorator when called as an expression. +tests/cases/compiler/potentiallyUncalledDecorators.ts(59,1): error TS1238: Unable to resolve signature of class decorator when called as an expression. Type 'OmniDecorator' is not assignable to type 'typeof E'. Type 'OmniDecorator' provides no match for the signature 'new (): E'. -tests/cases/compiler/potentiallyUncalledDecorators.ts(60,5): error TS1236: The return type of a property decorator function must be either 'void' or 'any'. +tests/cases/compiler/potentiallyUncalledDecorators.ts(61,5): error TS1236: The return type of a property decorator function must be either 'void' or 'any'. Unable to resolve signature of property decorator when called as an expression. -tests/cases/compiler/potentiallyUncalledDecorators.ts(61,5): error TS1241: Unable to resolve signature of method decorator when called as an expression. +tests/cases/compiler/potentiallyUncalledDecorators.ts(62,5): error TS1241: Unable to resolve signature of method decorator when called as an expression. Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'. -tests/cases/compiler/potentiallyUncalledDecorators.ts(64,1): error TS1238: Unable to resolve signature of class decorator when called as an expression. +tests/cases/compiler/potentiallyUncalledDecorators.ts(65,1): error TS1238: Unable to resolve signature of class decorator when called as an expression. Type 'OmniDecorator' is not assignable to type 'typeof F'. Type 'OmniDecorator' provides no match for the signature 'new (): F'. -tests/cases/compiler/potentiallyUncalledDecorators.ts(66,5): error TS1236: The return type of a property decorator function must be either 'void' or 'any'. +tests/cases/compiler/potentiallyUncalledDecorators.ts(67,5): error TS1236: The return type of a property decorator function must be either 'void' or 'any'. Unable to resolve signature of property decorator when called as an expression. -tests/cases/compiler/potentiallyUncalledDecorators.ts(67,5): error TS1241: Unable to resolve signature of method decorator when called as an expression. +tests/cases/compiler/potentiallyUncalledDecorators.ts(68,5): error TS1241: Unable to resolve signature of method decorator when called as an expression. Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'. @@ -43,7 +43,7 @@ tests/cases/compiler/potentiallyUncalledDecorators.ts(67,5): error TS1241: Unabl class FooComponent { @Input foo: string; ~~~~~~ -!!! error TS1329: This value has type '(bindingPropertyName?: string) => any' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? +!!! error TS1329: 'Input' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@Input()'? } // Glimmer-style tracked API: @@ -72,17 +72,18 @@ tests/cases/compiler/potentiallyUncalledDecorators.ts(67,5): error TS1241: Unabl declare function twoOptional(x?: any, y?: any): OmniDecorator; declare function threeOptional(x?: any, y?: any, z?: any): OmniDecorator; declare function oneOptionalWithRest(x?: any, ...args: any[]): OmniDecorator; + declare const anyDec: any; @noArgs ~~~~~~~ -!!! error TS1329: This value has type '() => OmniDecorator' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? +!!! error TS1329: 'noArgs' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@noArgs()'? class A { @noArgs foo: any; ~~~~~~~ -!!! error TS1329: This value has type '() => OmniDecorator' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? +!!! error TS1329: 'noArgs' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@noArgs()'? @noArgs bar() { } ~~~~~~~ -!!! error TS1329: This value has type '() => OmniDecorator' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? +!!! error TS1329: 'noArgs' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@noArgs()'? } @allRest @@ -109,10 +110,10 @@ tests/cases/compiler/potentiallyUncalledDecorators.ts(67,5): error TS1241: Unabl class C { @oneOptional foo: any; ~~~~~~~~~~~~ -!!! error TS1329: This value has type '(x?: any) => OmniDecorator' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? +!!! error TS1329: 'oneOptional' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@oneOptional()'? @oneOptional bar() { } ~~~~~~~~~~~~ -!!! error TS1329: This value has type '(x?: any) => OmniDecorator' which accepts too few arguments to be used as a decorator here. Did you mean to call it first? +!!! error TS1329: 'oneOptional' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@oneOptional()'? } @twoOptional @@ -163,5 +164,11 @@ tests/cases/compiler/potentiallyUncalledDecorators.ts(67,5): error TS1241: Unabl !!! error TS1241: Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'. } + @anyDec + class G { + @anyDec foo: any; + @anyDec bar() { } + } + export { }; \ No newline at end of file diff --git a/tests/baselines/reference/potentiallyUncalledDecorators.js b/tests/baselines/reference/potentiallyUncalledDecorators.js index a0dcc80ea0e..b69f848a8c9 100644 --- a/tests/baselines/reference/potentiallyUncalledDecorators.js +++ b/tests/baselines/reference/potentiallyUncalledDecorators.js @@ -31,6 +31,7 @@ declare function oneOptional(x?: any): OmniDecorator; declare function twoOptional(x?: any, y?: any): OmniDecorator; declare function threeOptional(x?: any, y?: any, z?: any): OmniDecorator; declare function oneOptionalWithRest(x?: any, ...args: any[]): OmniDecorator; +declare const anyDec: any; @noArgs class A { @@ -68,6 +69,12 @@ class F { @oneOptionalWithRest bar() { } } +@anyDec +class G { + @anyDec foo: any; + @anyDec bar() { } +} + export { }; @@ -168,3 +175,15 @@ __decorate([ F = __decorate([ oneOptionalWithRest ], F); +let G = class G { + bar() { } +}; +__decorate([ + anyDec +], G.prototype, "foo", void 0); +__decorate([ + anyDec +], G.prototype, "bar", null); +G = __decorate([ + anyDec +], G); diff --git a/tests/baselines/reference/potentiallyUncalledDecorators.symbols b/tests/baselines/reference/potentiallyUncalledDecorators.symbols index ff7cc253a27..38fe3f3dc52 100644 --- a/tests/baselines/reference/potentiallyUncalledDecorators.symbols +++ b/tests/baselines/reference/potentiallyUncalledDecorators.symbols @@ -88,94 +88,112 @@ declare function oneOptionalWithRest(x?: any, ...args: any[]): OmniDecorator; >args : Symbol(args, Decl(potentiallyUncalledDecorators.ts, 31, 45)) >OmniDecorator : Symbol(OmniDecorator, Decl(potentiallyUncalledDecorators.ts, 19, 1)) +declare const anyDec: any; +>anyDec : Symbol(anyDec, Decl(potentiallyUncalledDecorators.ts, 32, 13)) + @noArgs >noArgs : Symbol(noArgs, Decl(potentiallyUncalledDecorators.ts, 24, 1)) class A { ->A : Symbol(A, Decl(potentiallyUncalledDecorators.ts, 31, 77)) +>A : Symbol(A, Decl(potentiallyUncalledDecorators.ts, 32, 26)) @noArgs foo: any; >noArgs : Symbol(noArgs, Decl(potentiallyUncalledDecorators.ts, 24, 1)) ->foo : Symbol(A.foo, Decl(potentiallyUncalledDecorators.ts, 34, 9)) +>foo : Symbol(A.foo, Decl(potentiallyUncalledDecorators.ts, 35, 9)) @noArgs bar() { } >noArgs : Symbol(noArgs, Decl(potentiallyUncalledDecorators.ts, 24, 1)) ->bar : Symbol(A.bar, Decl(potentiallyUncalledDecorators.ts, 35, 21)) +>bar : Symbol(A.bar, Decl(potentiallyUncalledDecorators.ts, 36, 21)) } @allRest >allRest : Symbol(allRest, Decl(potentiallyUncalledDecorators.ts, 26, 41)) class B { ->B : Symbol(B, Decl(potentiallyUncalledDecorators.ts, 37, 1)) +>B : Symbol(B, Decl(potentiallyUncalledDecorators.ts, 38, 1)) @allRest foo: any; >allRest : Symbol(allRest, Decl(potentiallyUncalledDecorators.ts, 26, 41)) ->foo : Symbol(B.foo, Decl(potentiallyUncalledDecorators.ts, 40, 9)) +>foo : Symbol(B.foo, Decl(potentiallyUncalledDecorators.ts, 41, 9)) @allRest bar() { } >allRest : Symbol(allRest, Decl(potentiallyUncalledDecorators.ts, 26, 41)) ->bar : Symbol(B.bar, Decl(potentiallyUncalledDecorators.ts, 41, 22)) +>bar : Symbol(B.bar, Decl(potentiallyUncalledDecorators.ts, 42, 22)) } @oneOptional >oneOptional : Symbol(oneOptional, Decl(potentiallyUncalledDecorators.ts, 27, 56)) class C { ->C : Symbol(C, Decl(potentiallyUncalledDecorators.ts, 43, 1)) +>C : Symbol(C, Decl(potentiallyUncalledDecorators.ts, 44, 1)) @oneOptional foo: any; >oneOptional : Symbol(oneOptional, Decl(potentiallyUncalledDecorators.ts, 27, 56)) ->foo : Symbol(C.foo, Decl(potentiallyUncalledDecorators.ts, 46, 9)) +>foo : Symbol(C.foo, Decl(potentiallyUncalledDecorators.ts, 47, 9)) @oneOptional bar() { } >oneOptional : Symbol(oneOptional, Decl(potentiallyUncalledDecorators.ts, 27, 56)) ->bar : Symbol(C.bar, Decl(potentiallyUncalledDecorators.ts, 47, 26)) +>bar : Symbol(C.bar, Decl(potentiallyUncalledDecorators.ts, 48, 26)) } @twoOptional >twoOptional : Symbol(twoOptional, Decl(potentiallyUncalledDecorators.ts, 28, 53)) class D { ->D : Symbol(D, Decl(potentiallyUncalledDecorators.ts, 49, 1)) +>D : Symbol(D, Decl(potentiallyUncalledDecorators.ts, 50, 1)) @twoOptional foo: any; >twoOptional : Symbol(twoOptional, Decl(potentiallyUncalledDecorators.ts, 28, 53)) ->foo : Symbol(D.foo, Decl(potentiallyUncalledDecorators.ts, 52, 9)) +>foo : Symbol(D.foo, Decl(potentiallyUncalledDecorators.ts, 53, 9)) @twoOptional bar() { } >twoOptional : Symbol(twoOptional, Decl(potentiallyUncalledDecorators.ts, 28, 53)) ->bar : Symbol(D.bar, Decl(potentiallyUncalledDecorators.ts, 53, 26)) +>bar : Symbol(D.bar, Decl(potentiallyUncalledDecorators.ts, 54, 26)) } @threeOptional >threeOptional : Symbol(threeOptional, Decl(potentiallyUncalledDecorators.ts, 29, 62)) class E { ->E : Symbol(E, Decl(potentiallyUncalledDecorators.ts, 55, 1)) +>E : Symbol(E, Decl(potentiallyUncalledDecorators.ts, 56, 1)) @threeOptional foo: any; >threeOptional : Symbol(threeOptional, Decl(potentiallyUncalledDecorators.ts, 29, 62)) ->foo : Symbol(E.foo, Decl(potentiallyUncalledDecorators.ts, 58, 9)) +>foo : Symbol(E.foo, Decl(potentiallyUncalledDecorators.ts, 59, 9)) @threeOptional bar() { } >threeOptional : Symbol(threeOptional, Decl(potentiallyUncalledDecorators.ts, 29, 62)) ->bar : Symbol(E.bar, Decl(potentiallyUncalledDecorators.ts, 59, 28)) +>bar : Symbol(E.bar, Decl(potentiallyUncalledDecorators.ts, 60, 28)) } @oneOptionalWithRest >oneOptionalWithRest : Symbol(oneOptionalWithRest, Decl(potentiallyUncalledDecorators.ts, 30, 73)) class F { ->F : Symbol(F, Decl(potentiallyUncalledDecorators.ts, 61, 1)) +>F : Symbol(F, Decl(potentiallyUncalledDecorators.ts, 62, 1)) @oneOptionalWithRest foo: any; >oneOptionalWithRest : Symbol(oneOptionalWithRest, Decl(potentiallyUncalledDecorators.ts, 30, 73)) ->foo : Symbol(F.foo, Decl(potentiallyUncalledDecorators.ts, 64, 9)) +>foo : Symbol(F.foo, Decl(potentiallyUncalledDecorators.ts, 65, 9)) @oneOptionalWithRest bar() { } >oneOptionalWithRest : Symbol(oneOptionalWithRest, Decl(potentiallyUncalledDecorators.ts, 30, 73)) ->bar : Symbol(F.bar, Decl(potentiallyUncalledDecorators.ts, 65, 34)) +>bar : Symbol(F.bar, Decl(potentiallyUncalledDecorators.ts, 66, 34)) +} + +@anyDec +>anyDec : Symbol(anyDec, Decl(potentiallyUncalledDecorators.ts, 32, 13)) + +class G { +>G : Symbol(G, Decl(potentiallyUncalledDecorators.ts, 68, 1)) + + @anyDec foo: any; +>anyDec : Symbol(anyDec, Decl(potentiallyUncalledDecorators.ts, 32, 13)) +>foo : Symbol(G.foo, Decl(potentiallyUncalledDecorators.ts, 71, 9)) + + @anyDec bar() { } +>anyDec : Symbol(anyDec, Decl(potentiallyUncalledDecorators.ts, 32, 13)) +>bar : Symbol(G.bar, Decl(potentiallyUncalledDecorators.ts, 72, 21)) } export { }; diff --git a/tests/baselines/reference/potentiallyUncalledDecorators.types b/tests/baselines/reference/potentiallyUncalledDecorators.types index d79972659d3..cad861fc7b4 100644 --- a/tests/baselines/reference/potentiallyUncalledDecorators.types +++ b/tests/baselines/reference/potentiallyUncalledDecorators.types @@ -94,6 +94,9 @@ declare function oneOptionalWithRest(x?: any, ...args: any[]): OmniDecorator; >args : any[] >OmniDecorator : OmniDecorator +declare const anyDec: any; +>anyDec : any + @noArgs >noArgs : () => OmniDecorator @@ -184,5 +187,20 @@ class F { >bar : () => void } +@anyDec +>anyDec : any + +class G { +>G : G + + @anyDec foo: any; +>anyDec : any +>foo : any + + @anyDec bar() { } +>anyDec : any +>bar : () => void +} + export { }; From ee05d0eb1cfadee576defbae31e8c474b9efcd9e Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 4 Oct 2017 16:03:16 -0700 Subject: [PATCH 21/54] Compile public api so that all the updates are ensured to be correct --- src/harness/unittests/publicApi.ts | 34 ++++++++++++++++--- src/server/project.ts | 2 +- .../reference/api/tsserverlibrary.d.ts | 2 +- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/harness/unittests/publicApi.ts b/src/harness/unittests/publicApi.ts index bb37dbe9d8d..35acfec57f5 100644 --- a/src/harness/unittests/publicApi.ts +++ b/src/harness/unittests/publicApi.ts @@ -1,10 +1,34 @@ /// describe("Public APIs", () => { - it("for the language service and compiler should be acknowledged when they change", () => { - Harness.Baseline.runBaseline("api/typescript.d.ts", () => Harness.IO.readFile("built/local/typescript.d.ts")); + function verifyApi(fileName: string) { + const builtFile = `built/local/${fileName}`; + const api = `api/${fileName}`; + let fileContent: string; + before(() => { + fileContent = Harness.IO.readFile(builtFile); + }); + + it("should be acknowledged when they change", () => { + Harness.Baseline.runBaseline(api, () => fileContent); + }); + + it("should compile", () => { + const testFile: Harness.Compiler.TestFile = { + unitName: builtFile, + content: fileContent + }; + const inputFiles = [testFile]; + const output = Harness.Compiler.compileFiles(inputFiles, [], /*harnessSettings*/ undefined, /*options*/ {}, /*currentDirectory*/ undefined); + assert(!output.result.errors || !output.result.errors.length, Harness.Compiler.minimalDiagnosticsToString(output.result.errors, /*pretty*/ true)); + }); + } + + describe("for the language service and compiler", () => { + verifyApi("typescript.d.ts"); }); - it("for the language server should be acknowledged when they change", () => { - Harness.Baseline.runBaseline("api/tsserverlibrary.d.ts", () => Harness.IO.readFile("built/local/tsserverlibrary.d.ts")); + + describe("for the language server", () => { + verifyApi("tsserverlibrary.d.ts"); }); -}); \ No newline at end of file +}); diff --git a/src/server/project.ts b/src/server/project.ts index 655f3ed0cfc..ac18738027b 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -296,7 +296,7 @@ namespace ts.server { } } - getCancellationToken() { + getCancellationToken(): HostCancellationToken { return this.cancellationToken; } diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 1feb6b7d073..3bb2ed11674 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -7104,7 +7104,7 @@ declare namespace ts.server { getScriptKind(fileName: string): ScriptKind; getScriptVersion(filename: string): string; getScriptSnapshot(filename: string): IScriptSnapshot; - getCancellationToken(): ThrottledCancellationToken; + getCancellationToken(): HostCancellationToken; getCurrentDirectory(): string; getDefaultLibFileName(): string; useCaseSensitiveFileNames(): boolean; From 97ee9516d671eb52fe96fa6694f6a2bbe12fcf29 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Thu, 5 Oct 2017 09:10:55 -0700 Subject: [PATCH 22/54] Update baselines --- .../excessPropertyCheckWithUnions.symbols | 144 +++++++++++++ .../excessPropertyCheckWithUnions.types | 196 ++++++++++++++++++ 2 files changed, 340 insertions(+) create mode 100644 tests/baselines/reference/excessPropertyCheckWithUnions.symbols create mode 100644 tests/baselines/reference/excessPropertyCheckWithUnions.types diff --git a/tests/baselines/reference/excessPropertyCheckWithUnions.symbols b/tests/baselines/reference/excessPropertyCheckWithUnions.symbols new file mode 100644 index 00000000000..332166e396c --- /dev/null +++ b/tests/baselines/reference/excessPropertyCheckWithUnions.symbols @@ -0,0 +1,144 @@ +=== tests/cases/compiler/excessPropertyCheckWithUnions.ts === +type ADT = { +>ADT : Symbol(ADT, Decl(excessPropertyCheckWithUnions.ts, 0, 0)) + + tag: "A", +>tag : Symbol(tag, Decl(excessPropertyCheckWithUnions.ts, 0, 12)) + + a1: string +>a1 : Symbol(a1, Decl(excessPropertyCheckWithUnions.ts, 1, 13)) + +} | { + tag: "D", +>tag : Symbol(tag, Decl(excessPropertyCheckWithUnions.ts, 3, 5)) + + d20: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 +>d20 : Symbol(d20, Decl(excessPropertyCheckWithUnions.ts, 4, 13)) + +} | { + tag: "T", +>tag : Symbol(tag, Decl(excessPropertyCheckWithUnions.ts, 6, 5)) +} +let wrong: ADT = { tag: "T", a1: "extra" } +>wrong : Symbol(wrong, Decl(excessPropertyCheckWithUnions.ts, 9, 3)) +>ADT : Symbol(ADT, Decl(excessPropertyCheckWithUnions.ts, 0, 0)) +>tag : Symbol(tag, Decl(excessPropertyCheckWithUnions.ts, 9, 18)) +>a1 : Symbol(a1, Decl(excessPropertyCheckWithUnions.ts, 9, 28)) + +wrong = { tag: "A", d20: 12 } +>wrong : Symbol(wrong, Decl(excessPropertyCheckWithUnions.ts, 9, 3)) +>tag : Symbol(tag, Decl(excessPropertyCheckWithUnions.ts, 10, 9)) +>d20 : Symbol(d20, Decl(excessPropertyCheckWithUnions.ts, 10, 19)) + +wrong = { tag: "D" } +>wrong : Symbol(wrong, Decl(excessPropertyCheckWithUnions.ts, 9, 3)) +>tag : Symbol(tag, Decl(excessPropertyCheckWithUnions.ts, 11, 9)) + +type Ambiguous = { +>Ambiguous : Symbol(Ambiguous, Decl(excessPropertyCheckWithUnions.ts, 11, 20)) + + tag: "A", +>tag : Symbol(tag, Decl(excessPropertyCheckWithUnions.ts, 13, 18)) + + x: string +>x : Symbol(x, Decl(excessPropertyCheckWithUnions.ts, 14, 13)) + +} | { + tag: "A", +>tag : Symbol(tag, Decl(excessPropertyCheckWithUnions.ts, 16, 5)) + + y: number +>y : Symbol(y, Decl(excessPropertyCheckWithUnions.ts, 17, 13)) + +} | { + tag: "B", +>tag : Symbol(tag, Decl(excessPropertyCheckWithUnions.ts, 19, 5)) + + z: boolean +>z : Symbol(z, Decl(excessPropertyCheckWithUnions.ts, 20, 13)) + +} | { + tag: "C" +>tag : Symbol(tag, Decl(excessPropertyCheckWithUnions.ts, 22, 5)) +} +let amb: Ambiguous +>amb : Symbol(amb, Decl(excessPropertyCheckWithUnions.ts, 25, 3)) +>Ambiguous : Symbol(Ambiguous, Decl(excessPropertyCheckWithUnions.ts, 11, 20)) + +// no error for ambiguous tag, even when it could satisfy both constituents at once +amb = { tag: "A", x: "hi" } +>amb : Symbol(amb, Decl(excessPropertyCheckWithUnions.ts, 25, 3)) +>tag : Symbol(tag, Decl(excessPropertyCheckWithUnions.ts, 27, 7)) +>x : Symbol(x, Decl(excessPropertyCheckWithUnions.ts, 27, 17)) + +amb = { tag: "A", y: 12 } +>amb : Symbol(amb, Decl(excessPropertyCheckWithUnions.ts, 25, 3)) +>tag : Symbol(tag, Decl(excessPropertyCheckWithUnions.ts, 28, 7)) +>y : Symbol(y, Decl(excessPropertyCheckWithUnions.ts, 28, 17)) + +amb = { tag: "A", x: "hi", y: 12 } +>amb : Symbol(amb, Decl(excessPropertyCheckWithUnions.ts, 25, 3)) +>tag : Symbol(tag, Decl(excessPropertyCheckWithUnions.ts, 29, 7)) +>x : Symbol(x, Decl(excessPropertyCheckWithUnions.ts, 29, 17)) +>y : Symbol(y, Decl(excessPropertyCheckWithUnions.ts, 29, 26)) + +// correctly error on excess property 'extra', even when ambiguous +amb = { tag: "A", x: "hi", extra: 12 } +>amb : Symbol(amb, Decl(excessPropertyCheckWithUnions.ts, 25, 3)) +>tag : Symbol(tag, Decl(excessPropertyCheckWithUnions.ts, 32, 7)) +>x : Symbol(x, Decl(excessPropertyCheckWithUnions.ts, 32, 17)) +>extra : Symbol(extra, Decl(excessPropertyCheckWithUnions.ts, 32, 26)) + +amb = { tag: "A", y: 12, extra: 12 } +>amb : Symbol(amb, Decl(excessPropertyCheckWithUnions.ts, 25, 3)) +>tag : Symbol(tag, Decl(excessPropertyCheckWithUnions.ts, 33, 7)) +>y : Symbol(y, Decl(excessPropertyCheckWithUnions.ts, 33, 17)) +>extra : Symbol(extra, Decl(excessPropertyCheckWithUnions.ts, 33, 24)) + +// assignability errors still work. +// But note that the error for `z: true` is the fallback one of reporting on +// the last constituent since assignability error reporting can't find a single best discriminant either. +amb = { tag: "A" } +>amb : Symbol(amb, Decl(excessPropertyCheckWithUnions.ts, 25, 3)) +>tag : Symbol(tag, Decl(excessPropertyCheckWithUnions.ts, 38, 7)) + +amb = { tag: "A", z: true } +>amb : Symbol(amb, Decl(excessPropertyCheckWithUnions.ts, 25, 3)) +>tag : Symbol(tag, Decl(excessPropertyCheckWithUnions.ts, 39, 7)) +>z : Symbol(z, Decl(excessPropertyCheckWithUnions.ts, 39, 17)) + +type Overlapping = +>Overlapping : Symbol(Overlapping, Decl(excessPropertyCheckWithUnions.ts, 39, 27)) + + | { a: 1, b: 1, first: string } +>a : Symbol(a, Decl(excessPropertyCheckWithUnions.ts, 42, 7)) +>b : Symbol(b, Decl(excessPropertyCheckWithUnions.ts, 42, 13)) +>first : Symbol(first, Decl(excessPropertyCheckWithUnions.ts, 42, 19)) + + | { a: 2, second: string } +>a : Symbol(a, Decl(excessPropertyCheckWithUnions.ts, 43, 7)) +>second : Symbol(second, Decl(excessPropertyCheckWithUnions.ts, 43, 13)) + + | { b: 3, third: string } +>b : Symbol(b, Decl(excessPropertyCheckWithUnions.ts, 44, 7)) +>third : Symbol(third, Decl(excessPropertyCheckWithUnions.ts, 44, 13)) + +let over: Overlapping +>over : Symbol(over, Decl(excessPropertyCheckWithUnions.ts, 45, 3)) +>Overlapping : Symbol(Overlapping, Decl(excessPropertyCheckWithUnions.ts, 39, 27)) + +// these two are not reported because there are two discriminant properties +over = { a: 1, b: 1, first: "ok", second: "error" } +>over : Symbol(over, Decl(excessPropertyCheckWithUnions.ts, 45, 3)) +>a : Symbol(a, Decl(excessPropertyCheckWithUnions.ts, 48, 8)) +>b : Symbol(b, Decl(excessPropertyCheckWithUnions.ts, 48, 14)) +>first : Symbol(first, Decl(excessPropertyCheckWithUnions.ts, 48, 20)) +>second : Symbol(second, Decl(excessPropertyCheckWithUnions.ts, 48, 33)) + +over = { a: 1, b: 1, first: "ok", third: "error" } +>over : Symbol(over, Decl(excessPropertyCheckWithUnions.ts, 45, 3)) +>a : Symbol(a, Decl(excessPropertyCheckWithUnions.ts, 49, 8)) +>b : Symbol(b, Decl(excessPropertyCheckWithUnions.ts, 49, 14)) +>first : Symbol(first, Decl(excessPropertyCheckWithUnions.ts, 49, 20)) +>third : Symbol(third, Decl(excessPropertyCheckWithUnions.ts, 49, 33)) + diff --git a/tests/baselines/reference/excessPropertyCheckWithUnions.types b/tests/baselines/reference/excessPropertyCheckWithUnions.types new file mode 100644 index 00000000000..1d6bdd32eb2 --- /dev/null +++ b/tests/baselines/reference/excessPropertyCheckWithUnions.types @@ -0,0 +1,196 @@ +=== tests/cases/compiler/excessPropertyCheckWithUnions.ts === +type ADT = { +>ADT : ADT + + tag: "A", +>tag : "A" + + a1: string +>a1 : string + +} | { + tag: "D", +>tag : "D" + + d20: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 +>d20 : 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 + +} | { + tag: "T", +>tag : "T" +} +let wrong: ADT = { tag: "T", a1: "extra" } +>wrong : ADT +>ADT : ADT +>{ tag: "T", a1: "extra" } : { tag: "T"; a1: string; } +>tag : string +>"T" : "T" +>a1 : string +>"extra" : "extra" + +wrong = { tag: "A", d20: 12 } +>wrong = { tag: "A", d20: 12 } : { tag: "A"; d20: 12; } +>wrong : ADT +>{ tag: "A", d20: 12 } : { tag: "A"; d20: 12; } +>tag : string +>"A" : "A" +>d20 : number +>12 : 12 + +wrong = { tag: "D" } +>wrong = { tag: "D" } : { tag: "D"; } +>wrong : ADT +>{ tag: "D" } : { tag: "D"; } +>tag : string +>"D" : "D" + +type Ambiguous = { +>Ambiguous : Ambiguous + + tag: "A", +>tag : "A" + + x: string +>x : string + +} | { + tag: "A", +>tag : "A" + + y: number +>y : number + +} | { + tag: "B", +>tag : "B" + + z: boolean +>z : boolean + +} | { + tag: "C" +>tag : "C" +} +let amb: Ambiguous +>amb : Ambiguous +>Ambiguous : Ambiguous + +// no error for ambiguous tag, even when it could satisfy both constituents at once +amb = { tag: "A", x: "hi" } +>amb = { tag: "A", x: "hi" } : { tag: "A"; x: string; } +>amb : Ambiguous +>{ tag: "A", x: "hi" } : { tag: "A"; x: string; } +>tag : string +>"A" : "A" +>x : string +>"hi" : "hi" + +amb = { tag: "A", y: 12 } +>amb = { tag: "A", y: 12 } : { tag: "A"; y: number; } +>amb : Ambiguous +>{ tag: "A", y: 12 } : { tag: "A"; y: number; } +>tag : string +>"A" : "A" +>y : number +>12 : 12 + +amb = { tag: "A", x: "hi", y: 12 } +>amb = { tag: "A", x: "hi", y: 12 } : { tag: "A"; x: string; y: number; } +>amb : Ambiguous +>{ tag: "A", x: "hi", y: 12 } : { tag: "A"; x: string; y: number; } +>tag : string +>"A" : "A" +>x : string +>"hi" : "hi" +>y : number +>12 : 12 + +// correctly error on excess property 'extra', even when ambiguous +amb = { tag: "A", x: "hi", extra: 12 } +>amb = { tag: "A", x: "hi", extra: 12 } : { tag: "A"; x: string; extra: number; } +>amb : Ambiguous +>{ tag: "A", x: "hi", extra: 12 } : { tag: "A"; x: string; extra: number; } +>tag : string +>"A" : "A" +>x : string +>"hi" : "hi" +>extra : number +>12 : 12 + +amb = { tag: "A", y: 12, extra: 12 } +>amb = { tag: "A", y: 12, extra: 12 } : { tag: "A"; y: number; extra: number; } +>amb : Ambiguous +>{ tag: "A", y: 12, extra: 12 } : { tag: "A"; y: number; extra: number; } +>tag : string +>"A" : "A" +>y : number +>12 : 12 +>extra : number +>12 : 12 + +// assignability errors still work. +// But note that the error for `z: true` is the fallback one of reporting on +// the last constituent since assignability error reporting can't find a single best discriminant either. +amb = { tag: "A" } +>amb = { tag: "A" } : { tag: "A"; } +>amb : Ambiguous +>{ tag: "A" } : { tag: "A"; } +>tag : string +>"A" : "A" + +amb = { tag: "A", z: true } +>amb = { tag: "A", z: true } : { tag: "A"; z: true; } +>amb : Ambiguous +>{ tag: "A", z: true } : { tag: "A"; z: true; } +>tag : string +>"A" : "A" +>z : boolean +>true : true + +type Overlapping = +>Overlapping : Overlapping + + | { a: 1, b: 1, first: string } +>a : 1 +>b : 1 +>first : string + + | { a: 2, second: string } +>a : 2 +>second : string + + | { b: 3, third: string } +>b : 3 +>third : string + +let over: Overlapping +>over : Overlapping +>Overlapping : Overlapping + +// these two are not reported because there are two discriminant properties +over = { a: 1, b: 1, first: "ok", second: "error" } +>over = { a: 1, b: 1, first: "ok", second: "error" } : { a: 1; b: 1; first: string; second: string; } +>over : Overlapping +>{ a: 1, b: 1, first: "ok", second: "error" } : { a: 1; b: 1; first: string; second: string; } +>a : number +>1 : 1 +>b : number +>1 : 1 +>first : string +>"ok" : "ok" +>second : string +>"error" : "error" + +over = { a: 1, b: 1, first: "ok", third: "error" } +>over = { a: 1, b: 1, first: "ok", third: "error" } : { a: 1; b: 1; first: string; third: string; } +>over : Overlapping +>{ a: 1, b: 1, first: "ok", third: "error" } : { a: 1; b: 1; first: string; third: string; } +>a : number +>1 : 1 +>b : number +>1 : 1 +>first : string +>"ok" : "ok" +>third : string +>"error" : "error" + From 2facead886b2850df151dca9cf2852f0e68f800c Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Thu, 5 Oct 2017 09:54:21 -0700 Subject: [PATCH 23/54] Update tests after the merge from master --- src/harness/unittests/tsserverProjectSystem.ts | 11 +++++------ src/harness/virtualFileSystemWithWatch.ts | 12 ++++++++---- tests/baselines/reference/api/tsserverlibrary.d.ts | 7 +------ 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/harness/unittests/tsserverProjectSystem.ts b/src/harness/unittests/tsserverProjectSystem.ts index 06e2beeaaa6..e594093605a 100644 --- a/src/harness/unittests/tsserverProjectSystem.ts +++ b/src/harness/unittests/tsserverProjectSystem.ts @@ -352,8 +352,6 @@ namespace ts.projectSystem { verifyDiagnostics(actual, []); } - const typeRootFromTsserverLocation = "/node_modules/@types"; - export function getTypeRootsFromLocation(currentDirectory: string) { currentDirectory = normalizePath(currentDirectory); const result: string[] = []; @@ -401,7 +399,7 @@ namespace ts.projectSystem { const configFiles = flatMap(configFileLocations, location => [location + "tsconfig.json", location + "jsconfig.json"]); checkWatchedFiles(host, configFiles.concat(libFile.path, moduleFile.path)); checkWatchedDirectories(host, [], /*recursive*/ false); - checkWatchedDirectories(host, ["/a/b/c", typeRootFromTsserverLocation], /*recursive*/ true); + checkWatchedDirectories(host, ["/a/b/c", ...getTypeRootsFromLocation(getDirectoryPath(appFile.path))], /*recursive*/ true); }); it("can handle tsconfig file name with difference casing", () => { @@ -4331,7 +4329,7 @@ namespace ts.projectSystem { function verifyCalledOnEachEntry(callback: CalledMaps, expectedKeys: Map) { const calledMap = calledMaps[callback]; - assert.equal(calledMap.size, expectedKeys.size, `${callback}: incorrect size of map: Actual keys: ${arrayFrom(calledMap.keys())} Expected: ${arrayFrom(expectedKeys.keys())}`); + ts.TestFSWithWatch.verifyMapSize(callback, calledMap, arrayFrom(expectedKeys.keys())); expectedKeys.forEach((called, name) => { assert.isTrue(calledMap.has(name), `${callback} is expected to contain ${name}, actual keys: ${arrayFrom(calledMap.keys())}`); assert.equal(calledMap.get(name).length, called, `${callback} is expected to be called ${called} times with ${name}. Actual entry: ${calledMap.get(name)}`); @@ -4413,6 +4411,7 @@ namespace ts.projectSystem { } const f2Lookups = getLocationsForModuleLookup("f2"); callsTrackingHost.verifyCalledOnEachEntryNTimes(CalledMapsWithSingleArg.fileExists, f2Lookups, 1); + const typeRootLocations = getTypeRootsFromLocation(getDirectoryPath(root.path)); const f2DirLookups = getLocationsForDirectoryLookup(); callsTrackingHost.verifyCalledOnEachEntry(CalledMapsWithSingleArg.directoryExists, f2DirLookups); callsTrackingHost.verifyNoCall(CalledMapsWithSingleArg.getDirectories); @@ -4423,7 +4422,7 @@ namespace ts.projectSystem { verifyImportedDiagnostics(); const f1Lookups = f2Lookups.map(s => s.replace("f2", "f1")); f1Lookups.length = f1Lookups.indexOf(imported.path) + 1; - const f1DirLookups = ["/c/d", "/c", typeRootFromTsserverLocation]; + const f1DirLookups = ["/c/d", "/c", ...typeRootLocations]; vertifyF1Lookups(); // setting compiler options discards module resolution cache @@ -4475,7 +4474,7 @@ namespace ts.projectSystem { function getLocationsForDirectoryLookup() { const result = createMap(); // Type root - result.set(typeRootFromTsserverLocation, 1); + typeRootLocations.forEach(location => result.set(location, 1)); forEachAncestorDirectory(getDirectoryPath(root.path), ancestor => { // To resolve modules result.set(ancestor, 2); diff --git a/src/harness/virtualFileSystemWithWatch.ts b/src/harness/virtualFileSystemWithWatch.ts index c16f57235e4..ff782bbf7d2 100644 --- a/src/harness/virtualFileSystemWithWatch.ts +++ b/src/harness/virtualFileSystemWithWatch.ts @@ -95,7 +95,7 @@ namespace ts.TestFSWithWatch { } } - function getDiffInKeys(map: Map, expectedKeys: ReadonlyArray) { + function getDiffInKeys(map: Map, expectedKeys: ReadonlyArray) { if (map.size === expectedKeys.length) { return ""; } @@ -122,8 +122,12 @@ namespace ts.TestFSWithWatch { return `\n\nNotInActual: ${notInActual}\nDuplicates: ${duplicates}\nInActualButNotInExpected: ${inActualNotExpected}`; } - function checkMapKeys(caption: string, map: Map, expectedKeys: ReadonlyArray) { + export function verifyMapSize(caption: string, map: Map, expectedKeys: ReadonlyArray) { assert.equal(map.size, expectedKeys.length, `${caption}: incorrect size of map: Actual keys: ${arrayFrom(map.keys())} Expected: ${expectedKeys}${getDiffInKeys(map, expectedKeys)}`); + } + + function checkMapKeys(caption: string, map: Map, expectedKeys: ReadonlyArray) { + verifyMapSize(caption, map, expectedKeys); for (const name of expectedKeys) { assert.isTrue(map.has(name), `${caption} is expected to contain ${name}, actual keys: ${arrayFrom(map.keys())}`); } @@ -548,7 +552,7 @@ namespace ts.TestFSWithWatch { const folder = this.toFolder(directoryName); // base folder has to be present - const base = getDirectoryPath(folder.fullPath); + const base = getDirectoryPath(folder.path); const baseFolder = this.fs.get(base) as Folder; Debug.assert(isFolder(baseFolder)); @@ -560,7 +564,7 @@ namespace ts.TestFSWithWatch { const file = this.toFile({ path, content }); // base folder has to be present - const base = getDirectoryPath(file.fullPath); + const base = getDirectoryPath(file.path); const folder = this.fs.get(base) as Folder; Debug.assert(isFolder(folder)); diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 3bb2ed11674..713188e9da8 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -7131,7 +7131,6 @@ declare namespace ts.server { enableLanguageService(): void; disableLanguageService(): void; getProjectName(): string; - abstract getProjectRootPath(): string | undefined; abstract getTypeAcquisition(): TypeAcquisition; getExternalFiles(): SortedReadonlyArray; getSourceFile(path: Path): SourceFile; @@ -7184,7 +7183,6 @@ declare namespace ts.server { addRoot(info: ScriptInfo): void; removeRoot(info: ScriptInfo): void; isProjectWithSingleRoot(): boolean; - getProjectRootPath(): string; close(): void; getTypeAcquisition(): TypeAcquisition; } @@ -7211,7 +7209,6 @@ declare namespace ts.server { enablePlugins(): void; private enablePlugin(pluginConfigEntry, searchPaths); private enableProxy(pluginModuleFactory, configEntry); - getProjectRootPath(): string; /** * Get the errors that dont have any file name associated */ @@ -7237,11 +7234,9 @@ declare namespace ts.server { class ExternalProject extends Project { externalProjectName: string; compileOnSaveEnabled: boolean; - private readonly projectFilePath; excludedFiles: ReadonlyArray; private typeAcquisition; getExcludedFiles(): ReadonlyArray; - getProjectRootPath(): string; getTypeAcquisition(): TypeAcquisition; setTypeAcquisition(newTypeAcquisition: TypeAcquisition): void; } @@ -7519,7 +7514,7 @@ declare namespace ts.server { private updateNonInferredProject(project, newUncheckedFiles, propertyReader, newOptions, newTypeAcquisition, compileOnSave); private getOrCreateInferredProjectForProjectRootPathIfEnabled(info, projectRootPath); private getOrCreateSingleInferredProjectIfEnabled(); - private createInferredProject(rootDirectoryForResolution, isSingleInferredProject?, projectRootPath?); + private createInferredProject(currentDirectory, isSingleInferredProject?, projectRootPath?); getScriptInfo(uncheckedFileName: string): ScriptInfo; private watchClosedScriptInfo(info); private stopWatchingScriptInfo(info); From 32d705dbb53b48d2d473547d7ec566d3df818df8 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Thu, 5 Oct 2017 11:29:00 -0700 Subject: [PATCH 24/54] Fine tune logging so that only triggers in watch are logged in normal logging vs verbose --- src/compiler/watch.ts | 8 ++++---- src/compiler/watchUtilities.ts | 31 +++++++++++++++++++++++++------ src/server/editorServices.ts | 5 +++++ 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/compiler/watch.ts b/src/compiler/watch.ts index 779efb62c46..2ab3ccc5ce6 100644 --- a/src/compiler/watch.ts +++ b/src/compiler/watch.ts @@ -249,10 +249,10 @@ namespace ts { let hasChangedAutomaticTypeDirectiveNames = false; // True if the automatic type directives have changed const loggingEnabled = compilerOptions.diagnostics || compilerOptions.extendedDiagnostics; - const writeLog: (s: string) => void = loggingEnabled ? s => system.write(s) : noop; - const watchFile = loggingEnabled ? ts.addFileWatcherWithLogging : ts.addFileWatcher; - const watchFilePath = loggingEnabled ? ts.addFilePathWatcherWithLogging : ts.addFilePathWatcher; - const watchDirectoryWorker = loggingEnabled ? ts.addDirectoryWatcherWithLogging : ts.addDirectoryWatcher; + const writeLog: (s: string) => void = loggingEnabled ? s => { system.write(s); system.write(system.newLine); } : noop; + const watchFile = compilerOptions.extendedDiagnostics ? ts.addFileWatcherWithLogging : loggingEnabled ? ts.addFileWatcherWithOnlyTriggerLogging : ts.addFileWatcher; + const watchFilePath = compilerOptions.extendedDiagnostics ? ts.addFilePathWatcherWithLogging : ts.addFilePathWatcher; + const watchDirectoryWorker = compilerOptions.extendedDiagnostics ? ts.addDirectoryWatcherWithLogging : ts.addDirectoryWatcher; watchingHost = watchingHost || createWatchingSystemHost(compilerOptions.pretty); const { system, parseConfigFile, reportDiagnostic, reportWatchDiagnostic, beforeCompile, afterCompile } = watchingHost; diff --git a/src/compiler/watchUtilities.ts b/src/compiler/watchUtilities.ts index de415293954..26bf689401a 100644 --- a/src/compiler/watchUtilities.ts +++ b/src/compiler/watchUtilities.ts @@ -82,7 +82,12 @@ namespace ts { export function addFileWatcherWithLogging(host: System, file: string, cb: FileWatcherCallback, log: (s: string) => void): FileWatcher { const watcherCaption = `FileWatcher:: `; - return createWatcherWithLogging(addFileWatcher, watcherCaption, log, host, file, cb); + return createWatcherWithLogging(addFileWatcher, watcherCaption, log, /*logOnlyTrigger*/ false, host, file, cb); + } + + export function addFileWatcherWithOnlyTriggerLogging(host: System, file: string, cb: FileWatcherCallback, log: (s: string) => void): FileWatcher { + const watcherCaption = `FileWatcher:: `; + return createWatcherWithLogging(addFileWatcher, watcherCaption, log, /*logOnlyTrigger*/ true, host, file, cb); } export type FilePathWatcherCallback = (fileName: string, eventKind: FileWatcherEventKind, filePath: Path) => void; @@ -92,7 +97,12 @@ namespace ts { export function addFilePathWatcherWithLogging(host: System, file: string, cb: FilePathWatcherCallback, path: Path, log: (s: string) => void): FileWatcher { const watcherCaption = `FileWatcher:: `; - return createWatcherWithLogging(addFileWatcher, watcherCaption, log, host, file, cb, path); + return createWatcherWithLogging(addFileWatcher, watcherCaption, log, /*logOnlyTrigger*/ false, host, file, cb, path); + } + + export function addFilePathWatcherWithOnlyTriggerLogging(host: System, file: string, cb: FilePathWatcherCallback, path: Path, log: (s: string) => void): FileWatcher { + const watcherCaption = `FileWatcher:: `; + return createWatcherWithLogging(addFileWatcher, watcherCaption, log, /*logOnlyTrigger*/ true, host, file, cb, path); } export function addDirectoryWatcher(host: System, directory: string, cb: DirectoryWatcherCallback, flags: WatchDirectoryFlags): FileWatcher { @@ -102,14 +112,21 @@ namespace ts { export function addDirectoryWatcherWithLogging(host: System, directory: string, cb: DirectoryWatcherCallback, flags: WatchDirectoryFlags, log: (s: string) => void): FileWatcher { const watcherCaption = `DirectoryWatcher ${(flags & WatchDirectoryFlags.Recursive) !== 0 ? "recursive" : ""}:: `; - return createWatcherWithLogging(addDirectoryWatcher, watcherCaption, log, host, directory, cb, flags); + return createWatcherWithLogging(addDirectoryWatcher, watcherCaption, log, /*logOnlyTrigger*/ false, host, directory, cb, flags); + } + + export function addDirectoryWatcherWithOnlyTriggerLogging(host: System, directory: string, cb: DirectoryWatcherCallback, flags: WatchDirectoryFlags, log: (s: string) => void): FileWatcher { + const watcherCaption = `DirectoryWatcher ${(flags & WatchDirectoryFlags.Recursive) !== 0 ? "recursive" : ""}:: `; + return createWatcherWithLogging(addDirectoryWatcher, watcherCaption, log, /*logOnlyTrigger*/ true, host, directory, cb, flags); } type WatchCallback = (fileName: string, cbOptional1?: T, optional?: U) => void; type AddWatch = (host: System, file: string, cb: WatchCallback, optional?: U) => FileWatcher; - function createWatcherWithLogging(addWatch: AddWatch, watcherCaption: string, log: (s: string) => void, host: System, file: string, cb: WatchCallback, optional?: U): FileWatcher { + function createWatcherWithLogging(addWatch: AddWatch, watcherCaption: string, log: (s: string) => void, logOnlyTrigger: boolean, host: System, file: string, cb: WatchCallback, optional?: U): FileWatcher { const info = `PathInfo: ${file}`; - log(`${watcherCaption}Added: ${info}`); + if (!logOnlyTrigger) { + log(`${watcherCaption}Added: ${info}`); + } const watcher = addWatch(host, file, (fileName, cbOptional1?) => { const optionalInfo = cbOptional1 !== undefined ? ` ${cbOptional1}` : ""; log(`${watcherCaption}Trigger: ${fileName}${optionalInfo} ${info}`); @@ -120,7 +137,9 @@ namespace ts { }, optional); return { close: () => { - log(`${watcherCaption}Close: ${info}`); + if (!logOnlyTrigger) { + log(`${watcherCaption}Close: ${info}`); + } watcher.close(); } }; diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 25a285929be..2916fb60c57 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -431,6 +431,11 @@ namespace ts.server { this.watchFilePath = (host, file, cb, path, watchType, project) => ts.addFilePathWatcherWithLogging(host, file, cb, path, this.createWatcherLog(watchType, project)); this.watchDirectory = (host, dir, cb, flags, watchType, project) => ts.addDirectoryWatcherWithLogging(host, dir, cb, flags, this.createWatcherLog(watchType, project)); } + else if (this.logger.loggingEnabled()) { + this.watchFile = (host, file, cb, watchType, project) => ts.addFileWatcherWithOnlyTriggerLogging(host, file, cb, this.createWatcherLog(watchType, project)); + this.watchFilePath = (host, file, cb, path, watchType, project) => ts.addFilePathWatcherWithOnlyTriggerLogging(host, file, cb, path, this.createWatcherLog(watchType, project)); + this.watchDirectory = (host, dir, cb, flags, watchType, project) => ts.addDirectoryWatcherWithOnlyTriggerLogging(host, dir, cb, flags, this.createWatcherLog(watchType, project)); + } else { this.watchFile = ts.addFileWatcher; this.watchFilePath = ts.addFilePathWatcher; From e5eccf0a22104f2242cebbf636e6ad2336d328f1 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Sat, 30 Sep 2017 00:58:09 -0700 Subject: [PATCH 25/54] Added test. --- .../codeFixAddForgottenDecoratorCall01.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/cases/fourslash/codeFixAddForgottenDecoratorCall01.ts diff --git a/tests/cases/fourslash/codeFixAddForgottenDecoratorCall01.ts b/tests/cases/fourslash/codeFixAddForgottenDecoratorCall01.ts new file mode 100644 index 00000000000..17ae7f626f0 --- /dev/null +++ b/tests/cases/fourslash/codeFixAddForgottenDecoratorCall01.ts @@ -0,0 +1,14 @@ +/// + +////declare function foo(): (...args: any[]) => void; +////class C { +//// [|@foo|] +//// bar() { +//// +//// } +////} + +verify.codeFix({ + description: "Call decorator expression.", + newRangeContent: `@foo()` +}); From ea2021dd3e49a016dac58de326601e24c02f8a09 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Sat, 30 Sep 2017 00:58:46 -0700 Subject: [PATCH 26/54] Create fix for uninvoked decorators. --- src/compiler/diagnosticMessages.json | 4 ++++ .../addMissingInvocationForDecorator.ts | 20 +++++++++++++++++++ src/services/codefixes/fixes.ts | 1 + 3 files changed, 25 insertions(+) create mode 100644 src/services/codefixes/addMissingInvocationForDecorator.ts diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 17c23660820..f3d6d4fcc47 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3709,6 +3709,10 @@ "category": "Message", "code": 90027 }, + "Call decorator expression.": { + "category": "Message", + "code": 90028 + }, "Convert function to an ES2015 class": { "category": "Message", diff --git a/src/services/codefixes/addMissingInvocationForDecorator.ts b/src/services/codefixes/addMissingInvocationForDecorator.ts new file mode 100644 index 00000000000..7f17aab6db2 --- /dev/null +++ b/src/services/codefixes/addMissingInvocationForDecorator.ts @@ -0,0 +1,20 @@ +/* @internal */ +namespace ts.codefix { + registerCodeFix({ + errorCodes: [Diagnostics._0_accepts_too_few_arguments_to_be_used_as_a_decorator_here_Did_you_mean_to_call_it_first_and_write_0.code], + getCodeActions: (context: CodeFixContext) => { + const sourceFile = context.sourceFile; + const token = getTokenAtPosition(sourceFile, context.span.start, /*includeJsDocComment*/ false); + const decorator = getAncestor(token, SyntaxKind.Decorator) as Decorator; + Debug.assert(!!decorator, "Expected position to be owned by a decorator."); + const replacement = createCall(decorator.expression, /*typeArguments*/ undefined, /*argumentsArray*/ undefined); + const changeTracker = textChanges.ChangeTracker.fromContext(context); + changeTracker.replaceNode(sourceFile, decorator.expression, replacement); + + return [{ + description: getLocaleSpecificMessage(Diagnostics.Call_decorator_expression), + changes: changeTracker.getChanges() + }]; + } + }); +} diff --git a/src/services/codefixes/fixes.ts b/src/services/codefixes/fixes.ts index 9bc80cad691..b024dfae7cd 100644 --- a/src/services/codefixes/fixes.ts +++ b/src/services/codefixes/fixes.ts @@ -1,3 +1,4 @@ +/// /// /// /// From c3d1b027dcf27520a2a2e64a0e95aa64ea62505f Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 5 Oct 2017 13:18:38 -0700 Subject: [PATCH 27/54] Don't use callback parameter code path when strictly checking functions --- src/compiler/checker.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 58248a8a9ee..5ea946aa898 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8540,7 +8540,7 @@ namespace ts { } const kind = target.declaration ? target.declaration.kind : SyntaxKind.Unknown; - const strictVariance = strictFunctionTypes && kind !== SyntaxKind.MethodDeclaration && + const strictVariance = !checkAsCallback && strictFunctionTypes && kind !== SyntaxKind.MethodDeclaration && kind !== SyntaxKind.MethodSignature && kind !== SyntaxKind.Constructor; let result = Ternary.True; @@ -8579,7 +8579,7 @@ namespace ts { // similar to return values, callback parameters are output positions. This means that a Promise, // where T is used only in callback parameter positions, will be co-variant (as opposed to bi-variant) // with respect to T. - const callbacks = sourceSig && targetSig && !sourceSig.typePredicate && !targetSig.typePredicate && + const callbacks = !strictVariance && sourceSig && targetSig && !sourceSig.typePredicate && !targetSig.typePredicate && (getFalsyFlags(sourceType) & TypeFlags.Nullable) === (getFalsyFlags(targetType) & TypeFlags.Nullable); const related = callbacks ? compareSignaturesRelated(targetSig, sourceSig, /*checkAsCallback*/ true, /*ignoreReturnTypes*/ false, reportErrors, errorReporter, compareTypes) : From 0dc1c77f231762079ba106ea7fd828355c2a8852 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 5 Oct 2017 13:18:49 -0700 Subject: [PATCH 28/54] Accept new baselines --- .../strictFunctionTypesErrors.errors.txt | 32 +++++++------------ 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/tests/baselines/reference/strictFunctionTypesErrors.errors.txt b/tests/baselines/reference/strictFunctionTypesErrors.errors.txt index 2827983bd5a..661ad437ed2 100644 --- a/tests/baselines/reference/strictFunctionTypesErrors.errors.txt +++ b/tests/baselines/reference/strictFunctionTypesErrors.errors.txt @@ -40,26 +40,22 @@ tests/cases/compiler/strictFunctionTypesErrors.ts(58,1): error TS2322: Type 'Fun Type 'Object' is not assignable to type 'string'. tests/cases/compiler/strictFunctionTypesErrors.ts(61,1): error TS2322: Type 'Func, Object>' is not assignable to type 'Func, Object>'. Types of parameters 'x' and 'x' are incompatible. - Types of parameters 'x' and 'x' are incompatible. - Type 'Object' is not assignable to type 'string'. + Type 'Func' is not assignable to type 'Func'. + Types of parameters 'x' and 'x' are incompatible. + Type 'Object' is not assignable to type 'string'. tests/cases/compiler/strictFunctionTypesErrors.ts(62,1): error TS2322: Type 'Func, string>' is not assignable to type 'Func, Object>'. Types of parameters 'x' and 'x' are incompatible. - Types of parameters 'x' and 'x' are incompatible. - Type 'Object' is not assignable to type 'string'. + Type 'Func' is not assignable to type 'Func'. tests/cases/compiler/strictFunctionTypesErrors.ts(65,1): error TS2322: Type 'Func, Object>' is not assignable to type 'Func, string>'. Types of parameters 'x' and 'x' are incompatible. - Types of parameters 'x' and 'x' are incompatible. - Type 'Object' is not assignable to type 'string'. + Type 'Func' is not assignable to type 'Func'. tests/cases/compiler/strictFunctionTypesErrors.ts(66,1): error TS2322: Type 'Func, string>' is not assignable to type 'Func, string>'. Types of parameters 'x' and 'x' are incompatible. - Types of parameters 'x' and 'x' are incompatible. - Type 'Object' is not assignable to type 'string'. + Type 'Func' is not assignable to type 'Func'. tests/cases/compiler/strictFunctionTypesErrors.ts(67,1): error TS2322: Type 'Func, Object>' is not assignable to type 'Func, string>'. Type 'Object' is not assignable to type 'string'. tests/cases/compiler/strictFunctionTypesErrors.ts(74,1): error TS2322: Type 'Func>' is not assignable to type 'Func>'. Type 'Func' is not assignable to type 'Func'. - Types of parameters 'x' and 'x' are incompatible. - Type 'Object' is not assignable to type 'string'. tests/cases/compiler/strictFunctionTypesErrors.ts(75,1): error TS2322: Type 'Func>' is not assignable to type 'Func>'. Types of parameters 'x' and 'x' are incompatible. Type 'Object' is not assignable to type 'string'. @@ -210,28 +206,26 @@ tests/cases/compiler/strictFunctionTypesErrors.ts(127,1): error TS2322: Type 'Cr ~~ !!! error TS2322: Type 'Func, Object>' is not assignable to type 'Func, Object>'. !!! error TS2322: Types of parameters 'x' and 'x' are incompatible. -!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. -!!! error TS2322: Type 'Object' is not assignable to type 'string'. +!!! error TS2322: Type 'Func' is not assignable to type 'Func'. +!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. +!!! error TS2322: Type 'Object' is not assignable to type 'string'. h3 = h2; // Error ~~ !!! error TS2322: Type 'Func, string>' is not assignable to type 'Func, Object>'. !!! error TS2322: Types of parameters 'x' and 'x' are incompatible. -!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. -!!! error TS2322: Type 'Object' is not assignable to type 'string'. +!!! error TS2322: Type 'Func' is not assignable to type 'Func'. h3 = h4; // Ok h4 = h1; // Error ~~ !!! error TS2322: Type 'Func, Object>' is not assignable to type 'Func, string>'. !!! error TS2322: Types of parameters 'x' and 'x' are incompatible. -!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. -!!! error TS2322: Type 'Object' is not assignable to type 'string'. +!!! error TS2322: Type 'Func' is not assignable to type 'Func'. h4 = h2; // Error ~~ !!! error TS2322: Type 'Func, string>' is not assignable to type 'Func, string>'. !!! error TS2322: Types of parameters 'x' and 'x' are incompatible. -!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. -!!! error TS2322: Type 'Object' is not assignable to type 'string'. +!!! error TS2322: Type 'Func' is not assignable to type 'Func'. h4 = h3; // Error ~~ !!! error TS2322: Type 'Func, Object>' is not assignable to type 'Func, string>'. @@ -246,8 +240,6 @@ tests/cases/compiler/strictFunctionTypesErrors.ts(127,1): error TS2322: Type 'Cr ~~ !!! error TS2322: Type 'Func>' is not assignable to type 'Func>'. !!! error TS2322: Type 'Func' is not assignable to type 'Func'. -!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. -!!! error TS2322: Type 'Object' is not assignable to type 'string'. i1 = i3; // Error ~~ !!! error TS2322: Type 'Func>' is not assignable to type 'Func>'. From aae7572c4869dd5a62acd4311e8f7fdefd1f05f6 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 5 Oct 2017 13:25:23 -0700 Subject: [PATCH 29/54] Add test --- .../strictFunctionTypesErrors.errors.txt | 29 ++++++++++++++++++- .../reference/strictFunctionTypesErrors.js | 9 ++++++ .../strictFunctionTypesErrors.symbols | 24 +++++++++++++++ .../reference/strictFunctionTypesErrors.types | 26 +++++++++++++++++ .../compiler/strictFunctionTypesErrors.ts | 7 +++++ 5 files changed, 94 insertions(+), 1 deletion(-) diff --git a/tests/baselines/reference/strictFunctionTypesErrors.errors.txt b/tests/baselines/reference/strictFunctionTypesErrors.errors.txt index 661ad437ed2..6a8204fb147 100644 --- a/tests/baselines/reference/strictFunctionTypesErrors.errors.txt +++ b/tests/baselines/reference/strictFunctionTypesErrors.errors.txt @@ -83,9 +83,18 @@ tests/cases/compiler/strictFunctionTypesErrors.ts(126,1): error TS2322: Type 'Cr tests/cases/compiler/strictFunctionTypesErrors.ts(127,1): error TS2322: Type 'Crate' is not assignable to type 'Crate'. Types of property 'item' are incompatible. Type 'Animal' is not assignable to type 'Dog'. +tests/cases/compiler/strictFunctionTypesErrors.ts(133,1): error TS2322: Type '(f: (x: Dog) => Dog) => void' is not assignable to type '(f: (x: Animal) => Animal) => void'. + Types of parameters 'f' and 'f' are incompatible. + Type '(x: Animal) => Animal' is not assignable to type '(x: Dog) => Dog'. + Type 'Animal' is not assignable to type 'Dog'. +tests/cases/compiler/strictFunctionTypesErrors.ts(134,1): error TS2322: Type '(f: (x: Animal) => Animal) => void' is not assignable to type '(f: (x: Dog) => Dog) => void'. + Types of parameters 'f' and 'f' are incompatible. + Type '(x: Dog) => Dog' is not assignable to type '(x: Animal) => Animal'. + Types of parameters 'x' and 'x' are incompatible. + Type 'Animal' is not assignable to type 'Dog'. -==== tests/cases/compiler/strictFunctionTypesErrors.ts (31 errors) ==== +==== tests/cases/compiler/strictFunctionTypesErrors.ts (33 errors) ==== export {} @@ -329,4 +338,22 @@ tests/cases/compiler/strictFunctionTypesErrors.ts(127,1): error TS2322: Type 'Cr !!! error TS2322: Type 'Crate' is not assignable to type 'Crate'. !!! error TS2322: Types of property 'item' are incompatible. !!! error TS2322: Type 'Animal' is not assignable to type 'Dog'. + + // Verify that callback parameters are strictly checked + + declare let fc1: (f: (x: Animal) => Animal) => void; + declare let fc2: (f: (x: Dog) => Dog) => void; + fc1 = fc2; // Error + ~~~ +!!! error TS2322: Type '(f: (x: Dog) => Dog) => void' is not assignable to type '(f: (x: Animal) => Animal) => void'. +!!! error TS2322: Types of parameters 'f' and 'f' are incompatible. +!!! error TS2322: Type '(x: Animal) => Animal' is not assignable to type '(x: Dog) => Dog'. +!!! error TS2322: Type 'Animal' is not assignable to type 'Dog'. + fc2 = fc1; // Error + ~~~ +!!! error TS2322: Type '(f: (x: Animal) => Animal) => void' is not assignable to type '(f: (x: Dog) => Dog) => void'. +!!! error TS2322: Types of parameters 'f' and 'f' are incompatible. +!!! error TS2322: Type '(x: Dog) => Dog' is not assignable to type '(x: Animal) => Animal'. +!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. +!!! error TS2322: Type 'Animal' is not assignable to type 'Dog'. \ No newline at end of file diff --git a/tests/baselines/reference/strictFunctionTypesErrors.js b/tests/baselines/reference/strictFunctionTypesErrors.js index 2be598f0ef9..8e338fa030d 100644 --- a/tests/baselines/reference/strictFunctionTypesErrors.js +++ b/tests/baselines/reference/strictFunctionTypesErrors.js @@ -126,6 +126,13 @@ declare let dogCrate: Crate; animalCrate = dogCrate; // Error dogCrate = animalCrate; // Error + +// Verify that callback parameters are strictly checked + +declare let fc1: (f: (x: Animal) => Animal) => void; +declare let fc2: (f: (x: Dog) => Dog) => void; +fc1 = fc2; // Error +fc2 = fc1; // Error //// [strictFunctionTypesErrors.js] @@ -186,3 +193,5 @@ dogComparer2 = animalComparer2; // Ok // Errors below should elaborate the reason for invariance animalCrate = dogCrate; // Error dogCrate = animalCrate; // Error +fc1 = fc2; // Error +fc2 = fc1; // Error diff --git a/tests/baselines/reference/strictFunctionTypesErrors.symbols b/tests/baselines/reference/strictFunctionTypesErrors.symbols index 30faf83d87e..fd28775c5e1 100644 --- a/tests/baselines/reference/strictFunctionTypesErrors.symbols +++ b/tests/baselines/reference/strictFunctionTypesErrors.symbols @@ -400,3 +400,27 @@ dogCrate = animalCrate; // Error >dogCrate : Symbol(dogCrate, Decl(strictFunctionTypesErrors.ts, 121, 11)) >animalCrate : Symbol(animalCrate, Decl(strictFunctionTypesErrors.ts, 120, 11)) +// Verify that callback parameters are strictly checked + +declare let fc1: (f: (x: Animal) => Animal) => void; +>fc1 : Symbol(fc1, Decl(strictFunctionTypesErrors.ts, 130, 11)) +>f : Symbol(f, Decl(strictFunctionTypesErrors.ts, 130, 18)) +>x : Symbol(x, Decl(strictFunctionTypesErrors.ts, 130, 22)) +>Animal : Symbol(Animal, Decl(strictFunctionTypesErrors.ts, 87, 8)) +>Animal : Symbol(Animal, Decl(strictFunctionTypesErrors.ts, 87, 8)) + +declare let fc2: (f: (x: Dog) => Dog) => void; +>fc2 : Symbol(fc2, Decl(strictFunctionTypesErrors.ts, 131, 11)) +>f : Symbol(f, Decl(strictFunctionTypesErrors.ts, 131, 18)) +>x : Symbol(x, Decl(strictFunctionTypesErrors.ts, 131, 22)) +>Dog : Symbol(Dog, Decl(strictFunctionTypesErrors.ts, 89, 33)) +>Dog : Symbol(Dog, Decl(strictFunctionTypesErrors.ts, 89, 33)) + +fc1 = fc2; // Error +>fc1 : Symbol(fc1, Decl(strictFunctionTypesErrors.ts, 130, 11)) +>fc2 : Symbol(fc2, Decl(strictFunctionTypesErrors.ts, 131, 11)) + +fc2 = fc1; // Error +>fc2 : Symbol(fc2, Decl(strictFunctionTypesErrors.ts, 131, 11)) +>fc1 : Symbol(fc1, Decl(strictFunctionTypesErrors.ts, 130, 11)) + diff --git a/tests/baselines/reference/strictFunctionTypesErrors.types b/tests/baselines/reference/strictFunctionTypesErrors.types index e4372d4b8fa..45deeebabab 100644 --- a/tests/baselines/reference/strictFunctionTypesErrors.types +++ b/tests/baselines/reference/strictFunctionTypesErrors.types @@ -454,3 +454,29 @@ dogCrate = animalCrate; // Error >dogCrate : Crate >animalCrate : Crate +// Verify that callback parameters are strictly checked + +declare let fc1: (f: (x: Animal) => Animal) => void; +>fc1 : (f: (x: Animal) => Animal) => void +>f : (x: Animal) => Animal +>x : Animal +>Animal : Animal +>Animal : Animal + +declare let fc2: (f: (x: Dog) => Dog) => void; +>fc2 : (f: (x: Dog) => Dog) => void +>f : (x: Dog) => Dog +>x : Dog +>Dog : Dog +>Dog : Dog + +fc1 = fc2; // Error +>fc1 = fc2 : (f: (x: Dog) => Dog) => void +>fc1 : (f: (x: Animal) => Animal) => void +>fc2 : (f: (x: Dog) => Dog) => void + +fc2 = fc1; // Error +>fc2 = fc1 : (f: (x: Animal) => Animal) => void +>fc2 : (f: (x: Dog) => Dog) => void +>fc1 : (f: (x: Animal) => Animal) => void + diff --git a/tests/cases/compiler/strictFunctionTypesErrors.ts b/tests/cases/compiler/strictFunctionTypesErrors.ts index fbf1c0fa6da..95598d9d444 100644 --- a/tests/cases/compiler/strictFunctionTypesErrors.ts +++ b/tests/cases/compiler/strictFunctionTypesErrors.ts @@ -126,3 +126,10 @@ declare let dogCrate: Crate; animalCrate = dogCrate; // Error dogCrate = animalCrate; // Error + +// Verify that callback parameters are strictly checked + +declare let fc1: (f: (x: Animal) => Animal) => void; +declare let fc2: (f: (x: Dog) => Dog) => void; +fc1 = fc2; // Error +fc2 = fc1; // Error From 7a4c3314e816bc7a7589d1e6e52328c38f1fc693 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Thu, 5 Oct 2017 16:47:24 -0700 Subject: [PATCH 30/54] Visit default export expressions (#18977) --- src/compiler/transformers/module/module.ts | 4 ++-- ...amicImportInDefaultExportExpression.errors.txt | 11 +++++++++++ .../dynamicImportInDefaultExportExpression.js | 15 +++++++++++++++ ...dynamicImportInDefaultExportExpression.symbols | 8 ++++++++ .../dynamicImportInDefaultExportExpression.types | 13 +++++++++++++ .../dynamicImportInDefaultExportExpression.ts | 7 +++++++ 6 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/dynamicImportInDefaultExportExpression.errors.txt create mode 100644 tests/baselines/reference/dynamicImportInDefaultExportExpression.js create mode 100644 tests/baselines/reference/dynamicImportInDefaultExportExpression.symbols create mode 100644 tests/baselines/reference/dynamicImportInDefaultExportExpression.types create mode 100644 tests/cases/compiler/dynamicImportInDefaultExportExpression.ts diff --git a/src/compiler/transformers/module/module.ts b/src/compiler/transformers/module/module.ts index 145ac07c2f2..ecbef685649 100644 --- a/src/compiler/transformers/module/module.ts +++ b/src/compiler/transformers/module/module.ts @@ -861,10 +861,10 @@ namespace ts { if (original && hasAssociatedEndOfDeclarationMarker(original)) { // Defer exports until we encounter an EndOfDeclarationMarker node const id = getOriginalNodeId(node); - deferredExports[id] = appendExportStatement(deferredExports[id], createIdentifier("default"), node.expression, /*location*/ node, /*allowComments*/ true); + deferredExports[id] = appendExportStatement(deferredExports[id], createIdentifier("default"), visitNode(node.expression, importCallExpressionVisitor), /*location*/ node, /*allowComments*/ true); } else { - statements = appendExportStatement(statements, createIdentifier("default"), node.expression, /*location*/ node, /*allowComments*/ true); + statements = appendExportStatement(statements, createIdentifier("default"), visitNode(node.expression, importCallExpressionVisitor), /*location*/ node, /*allowComments*/ true); } return singleOrMany(statements); diff --git a/tests/baselines/reference/dynamicImportInDefaultExportExpression.errors.txt b/tests/baselines/reference/dynamicImportInDefaultExportExpression.errors.txt new file mode 100644 index 00000000000..8bf6704b743 --- /dev/null +++ b/tests/baselines/reference/dynamicImportInDefaultExportExpression.errors.txt @@ -0,0 +1,11 @@ +tests/cases/compiler/dynamicImportInDefaultExportExpression.ts(3,23): error TS2307: Cannot find module './foo2'. + + +==== tests/cases/compiler/dynamicImportInDefaultExportExpression.ts (1 errors) ==== + export default { + getInstance: function () { + return import('./foo2'); + ~~~~~~~~ +!!! error TS2307: Cannot find module './foo2'. + } + } \ No newline at end of file diff --git a/tests/baselines/reference/dynamicImportInDefaultExportExpression.js b/tests/baselines/reference/dynamicImportInDefaultExportExpression.js new file mode 100644 index 00000000000..75e5e0d57a2 --- /dev/null +++ b/tests/baselines/reference/dynamicImportInDefaultExportExpression.js @@ -0,0 +1,15 @@ +//// [dynamicImportInDefaultExportExpression.ts] +export default { + getInstance: function () { + return import('./foo2'); + } +} + +//// [dynamicImportInDefaultExportExpression.js] +"use strict"; +exports.__esModule = true; +exports["default"] = { + getInstance: function () { + return Promise.resolve().then(function () { return require('./foo2'); }); + } +}; diff --git a/tests/baselines/reference/dynamicImportInDefaultExportExpression.symbols b/tests/baselines/reference/dynamicImportInDefaultExportExpression.symbols new file mode 100644 index 00000000000..d16b5f0a374 --- /dev/null +++ b/tests/baselines/reference/dynamicImportInDefaultExportExpression.symbols @@ -0,0 +1,8 @@ +=== tests/cases/compiler/dynamicImportInDefaultExportExpression.ts === +export default { + getInstance: function () { +>getInstance : Symbol(getInstance, Decl(dynamicImportInDefaultExportExpression.ts, 0, 16)) + + return import('./foo2'); + } +} diff --git a/tests/baselines/reference/dynamicImportInDefaultExportExpression.types b/tests/baselines/reference/dynamicImportInDefaultExportExpression.types new file mode 100644 index 00000000000..c75894a17ff --- /dev/null +++ b/tests/baselines/reference/dynamicImportInDefaultExportExpression.types @@ -0,0 +1,13 @@ +=== tests/cases/compiler/dynamicImportInDefaultExportExpression.ts === +export default { +>{ getInstance: function () { return import('./foo2'); }} : { getInstance: () => Promise; } + + getInstance: function () { +>getInstance : () => Promise +>function () { return import('./foo2'); } : () => Promise + + return import('./foo2'); +>import('./foo2') : Promise +>'./foo2' : "./foo2" + } +} diff --git a/tests/cases/compiler/dynamicImportInDefaultExportExpression.ts b/tests/cases/compiler/dynamicImportInDefaultExportExpression.ts new file mode 100644 index 00000000000..0e6ad95886b --- /dev/null +++ b/tests/cases/compiler/dynamicImportInDefaultExportExpression.ts @@ -0,0 +1,7 @@ +// @skipLibCheck: true +// @lib: es6 +export default { + getInstance: function () { + return import('./foo2'); + } +} \ No newline at end of file From 70e259aba368349f075ebe389e2f5c16dfec41db Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 6 Oct 2017 09:16:57 -0700 Subject: [PATCH 31/54] Always use callback parameter code path, but stricter if necessary --- src/compiler/checker.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5ea946aa898..305e47374cb 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -503,6 +503,12 @@ namespace ts { Inferential = 2, // Inferential typing } + const enum CallbackCheck { + None, + Bivariant, + Strict, + } + const builtinGlobals = createSymbolTable(); builtinGlobals.set(undefinedSymbol.escapedName, undefinedSymbol); @@ -8510,7 +8516,7 @@ namespace ts { function isSignatureAssignableTo(source: Signature, target: Signature, ignoreReturnTypes: boolean): boolean { - return compareSignaturesRelated(source, target, /*checkAsCallback*/ false, ignoreReturnTypes, /*reportErrors*/ false, + return compareSignaturesRelated(source, target, CallbackCheck.None, ignoreReturnTypes, /*reportErrors*/ false, /*errorReporter*/ undefined, compareTypesAssignable) !== Ternary.False; } @@ -8521,7 +8527,7 @@ namespace ts { */ function compareSignaturesRelated(source: Signature, target: Signature, - checkAsCallback: boolean, + callbackCheck: CallbackCheck, ignoreReturnTypes: boolean, reportErrors: boolean, errorReporter: ErrorReporter, @@ -8540,7 +8546,7 @@ namespace ts { } const kind = target.declaration ? target.declaration.kind : SyntaxKind.Unknown; - const strictVariance = !checkAsCallback && strictFunctionTypes && kind !== SyntaxKind.MethodDeclaration && + const strictVariance = !callbackCheck && strictFunctionTypes && kind !== SyntaxKind.MethodDeclaration && kind !== SyntaxKind.MethodSignature && kind !== SyntaxKind.Constructor; let result = Ternary.True; @@ -8579,11 +8585,11 @@ namespace ts { // similar to return values, callback parameters are output positions. This means that a Promise, // where T is used only in callback parameter positions, will be co-variant (as opposed to bi-variant) // with respect to T. - const callbacks = !strictVariance && sourceSig && targetSig && !sourceSig.typePredicate && !targetSig.typePredicate && + const callbacks = sourceSig && targetSig && !sourceSig.typePredicate && !targetSig.typePredicate && (getFalsyFlags(sourceType) & TypeFlags.Nullable) === (getFalsyFlags(targetType) & TypeFlags.Nullable); const related = callbacks ? - compareSignaturesRelated(targetSig, sourceSig, /*checkAsCallback*/ true, /*ignoreReturnTypes*/ false, reportErrors, errorReporter, compareTypes) : - !checkAsCallback && !strictVariance && compareTypes(sourceType, targetType, /*reportErrors*/ false) || compareTypes(targetType, sourceType, reportErrors); + compareSignaturesRelated(targetSig, sourceSig, strictVariance ? CallbackCheck.Strict : CallbackCheck.Bivariant, /*ignoreReturnTypes*/ false, reportErrors, errorReporter, compareTypes) : + !callbackCheck && !strictVariance && compareTypes(sourceType, targetType, /*reportErrors*/ false) || compareTypes(targetType, sourceType, reportErrors); if (!related) { if (reportErrors) { errorReporter(Diagnostics.Types_of_parameters_0_and_1_are_incompatible, @@ -8618,7 +8624,7 @@ namespace ts { // When relating callback signatures, we still need to relate return types bi-variantly as otherwise // the containing type wouldn't be co-variant. For example, interface Foo { add(cb: () => T): void } // wouldn't be co-variant for T without this rule. - result &= checkAsCallback && compareTypes(targetReturnType, sourceReturnType, /*reportErrors*/ false) || + result &= callbackCheck === CallbackCheck.Bivariant && compareTypes(targetReturnType, sourceReturnType, /*reportErrors*/ false) || compareTypes(sourceReturnType, targetReturnType, reportErrors); } @@ -9715,7 +9721,7 @@ namespace ts { */ function signatureRelatedTo(source: Signature, target: Signature, erase: boolean, reportErrors: boolean): Ternary { return compareSignaturesRelated(erase ? getErasedSignature(source) : source, erase ? getErasedSignature(target) : target, - /*checkAsCallback*/ false, /*ignoreReturnTypes*/ false, reportErrors, reportError, isRelatedTo); + CallbackCheck.None, /*ignoreReturnTypes*/ false, reportErrors, reportError, isRelatedTo); } function signaturesIdenticalTo(source: Type, target: Type, kind: SignatureKind): Ternary { From c8d52609142cd61c50b9593ee42ba8a4f649d885 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 6 Oct 2017 09:17:18 -0700 Subject: [PATCH 32/54] Accept new baselines --- .../strictFunctionTypesErrors.errors.txt | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/tests/baselines/reference/strictFunctionTypesErrors.errors.txt b/tests/baselines/reference/strictFunctionTypesErrors.errors.txt index 6a8204fb147..600a58638ed 100644 --- a/tests/baselines/reference/strictFunctionTypesErrors.errors.txt +++ b/tests/baselines/reference/strictFunctionTypesErrors.errors.txt @@ -40,22 +40,26 @@ tests/cases/compiler/strictFunctionTypesErrors.ts(58,1): error TS2322: Type 'Fun Type 'Object' is not assignable to type 'string'. tests/cases/compiler/strictFunctionTypesErrors.ts(61,1): error TS2322: Type 'Func, Object>' is not assignable to type 'Func, Object>'. Types of parameters 'x' and 'x' are incompatible. - Type 'Func' is not assignable to type 'Func'. - Types of parameters 'x' and 'x' are incompatible. - Type 'Object' is not assignable to type 'string'. + Types of parameters 'x' and 'x' are incompatible. + Type 'Object' is not assignable to type 'string'. tests/cases/compiler/strictFunctionTypesErrors.ts(62,1): error TS2322: Type 'Func, string>' is not assignable to type 'Func, Object>'. Types of parameters 'x' and 'x' are incompatible. - Type 'Func' is not assignable to type 'Func'. + Types of parameters 'x' and 'x' are incompatible. + Type 'Object' is not assignable to type 'string'. tests/cases/compiler/strictFunctionTypesErrors.ts(65,1): error TS2322: Type 'Func, Object>' is not assignable to type 'Func, string>'. Types of parameters 'x' and 'x' are incompatible. - Type 'Func' is not assignable to type 'Func'. + Types of parameters 'x' and 'x' are incompatible. + Type 'Object' is not assignable to type 'string'. tests/cases/compiler/strictFunctionTypesErrors.ts(66,1): error TS2322: Type 'Func, string>' is not assignable to type 'Func, string>'. Types of parameters 'x' and 'x' are incompatible. - Type 'Func' is not assignable to type 'Func'. + Types of parameters 'x' and 'x' are incompatible. + Type 'Object' is not assignable to type 'string'. tests/cases/compiler/strictFunctionTypesErrors.ts(67,1): error TS2322: Type 'Func, Object>' is not assignable to type 'Func, string>'. Type 'Object' is not assignable to type 'string'. tests/cases/compiler/strictFunctionTypesErrors.ts(74,1): error TS2322: Type 'Func>' is not assignable to type 'Func>'. Type 'Func' is not assignable to type 'Func'. + Types of parameters 'x' and 'x' are incompatible. + Type 'Object' is not assignable to type 'string'. tests/cases/compiler/strictFunctionTypesErrors.ts(75,1): error TS2322: Type 'Func>' is not assignable to type 'Func>'. Types of parameters 'x' and 'x' are incompatible. Type 'Object' is not assignable to type 'string'. @@ -85,13 +89,11 @@ tests/cases/compiler/strictFunctionTypesErrors.ts(127,1): error TS2322: Type 'Cr Type 'Animal' is not assignable to type 'Dog'. tests/cases/compiler/strictFunctionTypesErrors.ts(133,1): error TS2322: Type '(f: (x: Dog) => Dog) => void' is not assignable to type '(f: (x: Animal) => Animal) => void'. Types of parameters 'f' and 'f' are incompatible. - Type '(x: Animal) => Animal' is not assignable to type '(x: Dog) => Dog'. - Type 'Animal' is not assignable to type 'Dog'. + Type 'Animal' is not assignable to type 'Dog'. tests/cases/compiler/strictFunctionTypesErrors.ts(134,1): error TS2322: Type '(f: (x: Animal) => Animal) => void' is not assignable to type '(f: (x: Dog) => Dog) => void'. Types of parameters 'f' and 'f' are incompatible. - Type '(x: Dog) => Dog' is not assignable to type '(x: Animal) => Animal'. - Types of parameters 'x' and 'x' are incompatible. - Type 'Animal' is not assignable to type 'Dog'. + Types of parameters 'x' and 'x' are incompatible. + Type 'Animal' is not assignable to type 'Dog'. ==== tests/cases/compiler/strictFunctionTypesErrors.ts (33 errors) ==== @@ -215,26 +217,28 @@ tests/cases/compiler/strictFunctionTypesErrors.ts(134,1): error TS2322: Type '(f ~~ !!! error TS2322: Type 'Func, Object>' is not assignable to type 'Func, Object>'. !!! error TS2322: Types of parameters 'x' and 'x' are incompatible. -!!! error TS2322: Type 'Func' is not assignable to type 'Func'. -!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. -!!! error TS2322: Type 'Object' is not assignable to type 'string'. +!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. +!!! error TS2322: Type 'Object' is not assignable to type 'string'. h3 = h2; // Error ~~ !!! error TS2322: Type 'Func, string>' is not assignable to type 'Func, Object>'. !!! error TS2322: Types of parameters 'x' and 'x' are incompatible. -!!! error TS2322: Type 'Func' is not assignable to type 'Func'. +!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. +!!! error TS2322: Type 'Object' is not assignable to type 'string'. h3 = h4; // Ok h4 = h1; // Error ~~ !!! error TS2322: Type 'Func, Object>' is not assignable to type 'Func, string>'. !!! error TS2322: Types of parameters 'x' and 'x' are incompatible. -!!! error TS2322: Type 'Func' is not assignable to type 'Func'. +!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. +!!! error TS2322: Type 'Object' is not assignable to type 'string'. h4 = h2; // Error ~~ !!! error TS2322: Type 'Func, string>' is not assignable to type 'Func, string>'. !!! error TS2322: Types of parameters 'x' and 'x' are incompatible. -!!! error TS2322: Type 'Func' is not assignable to type 'Func'. +!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. +!!! error TS2322: Type 'Object' is not assignable to type 'string'. h4 = h3; // Error ~~ !!! error TS2322: Type 'Func, Object>' is not assignable to type 'Func, string>'. @@ -249,6 +253,8 @@ tests/cases/compiler/strictFunctionTypesErrors.ts(134,1): error TS2322: Type '(f ~~ !!! error TS2322: Type 'Func>' is not assignable to type 'Func>'. !!! error TS2322: Type 'Func' is not assignable to type 'Func'. +!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. +!!! error TS2322: Type 'Object' is not assignable to type 'string'. i1 = i3; // Error ~~ !!! error TS2322: Type 'Func>' is not assignable to type 'Func>'. @@ -347,13 +353,11 @@ tests/cases/compiler/strictFunctionTypesErrors.ts(134,1): error TS2322: Type '(f ~~~ !!! error TS2322: Type '(f: (x: Dog) => Dog) => void' is not assignable to type '(f: (x: Animal) => Animal) => void'. !!! error TS2322: Types of parameters 'f' and 'f' are incompatible. -!!! error TS2322: Type '(x: Animal) => Animal' is not assignable to type '(x: Dog) => Dog'. -!!! error TS2322: Type 'Animal' is not assignable to type 'Dog'. +!!! error TS2322: Type 'Animal' is not assignable to type 'Dog'. fc2 = fc1; // Error ~~~ !!! error TS2322: Type '(f: (x: Animal) => Animal) => void' is not assignable to type '(f: (x: Dog) => Dog) => void'. !!! error TS2322: Types of parameters 'f' and 'f' are incompatible. -!!! error TS2322: Type '(x: Dog) => Dog' is not assignable to type '(x: Animal) => Animal'. -!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. -!!! error TS2322: Type 'Animal' is not assignable to type 'Dog'. +!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. +!!! error TS2322: Type 'Animal' is not assignable to type 'Dog'. \ No newline at end of file From 7fcf51960ddc753754876c9905c84726ff0ff97e Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 6 Oct 2017 09:22:10 -0700 Subject: [PATCH 33/54] Add tests --- .../strictFunctionTypesErrors.errors.txt | 43 +++++++++- .../reference/strictFunctionTypesErrors.js | 43 +++++++++- .../strictFunctionTypesErrors.symbols | 78 +++++++++++++++++ .../reference/strictFunctionTypesErrors.types | 84 +++++++++++++++++++ .../compiler/strictFunctionTypesErrors.ts | 22 +++++ 5 files changed, 267 insertions(+), 3 deletions(-) diff --git a/tests/baselines/reference/strictFunctionTypesErrors.errors.txt b/tests/baselines/reference/strictFunctionTypesErrors.errors.txt index 600a58638ed..d8e36e43060 100644 --- a/tests/baselines/reference/strictFunctionTypesErrors.errors.txt +++ b/tests/baselines/reference/strictFunctionTypesErrors.errors.txt @@ -94,9 +94,17 @@ tests/cases/compiler/strictFunctionTypesErrors.ts(134,1): error TS2322: Type '(f Types of parameters 'f' and 'f' are incompatible. Types of parameters 'x' and 'x' are incompatible. Type 'Animal' is not assignable to type 'Dog'. +tests/cases/compiler/strictFunctionTypesErrors.ts(147,5): error TS2322: Type '(cb: (x: Animal) => Animal) => void' is not assignable to type '(cb: (x: Dog) => Animal) => void'. + Types of parameters 'cb' and 'cb' are incompatible. + Types of parameters 'x' and 'x' are incompatible. + Type 'Animal' is not assignable to type 'Dog'. +tests/cases/compiler/strictFunctionTypesErrors.ts(155,5): error TS2322: Type '(cb: (x: Animal) => Animal) => void' is not assignable to type '(cb: (x: Dog) => Animal) => void'. + Types of parameters 'cb' and 'cb' are incompatible. + Types of parameters 'x' and 'x' are incompatible. + Type 'Animal' is not assignable to type 'Dog'. -==== tests/cases/compiler/strictFunctionTypesErrors.ts (33 errors) ==== +==== tests/cases/compiler/strictFunctionTypesErrors.ts (35 errors) ==== export {} @@ -360,4 +368,35 @@ tests/cases/compiler/strictFunctionTypesErrors.ts(134,1): error TS2322: Type '(f !!! error TS2322: Types of parameters 'f' and 'f' are incompatible. !!! error TS2322: Types of parameters 'x' and 'x' are incompatible. !!! error TS2322: Type 'Animal' is not assignable to type 'Dog'. - \ No newline at end of file + + // Verify that callback parameters aren't loosely checked when types + // originate in method declarations + + namespace n1 { + class Foo { + static f1(x: Animal): Animal { throw "wat"; } + static f2(x: Dog): Animal { throw "wat"; }; + } + declare let f1: (cb: typeof Foo.f1) => void; + declare let f2: (cb: typeof Foo.f2) => void; + f1 = f2; + f2 = f1; // Error + ~~ +!!! error TS2322: Type '(cb: (x: Animal) => Animal) => void' is not assignable to type '(cb: (x: Dog) => Animal) => void'. +!!! error TS2322: Types of parameters 'cb' and 'cb' are incompatible. +!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. +!!! error TS2322: Type 'Animal' is not assignable to type 'Dog'. + } + + namespace n2 { + type BivariantHack = { foo(x: Input): Output }["foo"]; + declare let f1: (cb: BivariantHack) => void; + declare let f2: (cb: BivariantHack) => void; + f1 = f2; + f2 = f1; // Error + ~~ +!!! error TS2322: Type '(cb: (x: Animal) => Animal) => void' is not assignable to type '(cb: (x: Dog) => Animal) => void'. +!!! error TS2322: Types of parameters 'cb' and 'cb' are incompatible. +!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. +!!! error TS2322: Type 'Animal' is not assignable to type 'Dog'. + } \ No newline at end of file diff --git a/tests/baselines/reference/strictFunctionTypesErrors.js b/tests/baselines/reference/strictFunctionTypesErrors.js index 8e338fa030d..d0608f6c1ac 100644 --- a/tests/baselines/reference/strictFunctionTypesErrors.js +++ b/tests/baselines/reference/strictFunctionTypesErrors.js @@ -133,7 +133,28 @@ declare let fc1: (f: (x: Animal) => Animal) => void; declare let fc2: (f: (x: Dog) => Dog) => void; fc1 = fc2; // Error fc2 = fc1; // Error - + +// Verify that callback parameters aren't loosely checked when types +// originate in method declarations + +namespace n1 { + class Foo { + static f1(x: Animal): Animal { throw "wat"; } + static f2(x: Dog): Animal { throw "wat"; }; + } + declare let f1: (cb: typeof Foo.f1) => void; + declare let f2: (cb: typeof Foo.f2) => void; + f1 = f2; + f2 = f1; // Error +} + +namespace n2 { + type BivariantHack = { foo(x: Input): Output }["foo"]; + declare let f1: (cb: BivariantHack) => void; + declare let f2: (cb: BivariantHack) => void; + f1 = f2; + f2 = f1; // Error +} //// [strictFunctionTypesErrors.js] "use strict"; @@ -195,3 +216,23 @@ animalCrate = dogCrate; // Error dogCrate = animalCrate; // Error fc1 = fc2; // Error fc2 = fc1; // Error +// Verify that callback parameters aren't loosely checked when types +// originate in method declarations +var n1; +(function (n1) { + var Foo = /** @class */ (function () { + function Foo() { + } + Foo.f1 = function (x) { throw "wat"; }; + Foo.f2 = function (x) { throw "wat"; }; + ; + return Foo; + }()); + f1 = f2; + f2 = f1; // Error +})(n1 || (n1 = {})); +var n2; +(function (n2) { + f1 = f2; + f2 = f1; // Error +})(n2 || (n2 = {})); diff --git a/tests/baselines/reference/strictFunctionTypesErrors.symbols b/tests/baselines/reference/strictFunctionTypesErrors.symbols index fd28775c5e1..c85c337ca44 100644 --- a/tests/baselines/reference/strictFunctionTypesErrors.symbols +++ b/tests/baselines/reference/strictFunctionTypesErrors.symbols @@ -424,3 +424,81 @@ fc2 = fc1; // Error >fc2 : Symbol(fc2, Decl(strictFunctionTypesErrors.ts, 131, 11)) >fc1 : Symbol(fc1, Decl(strictFunctionTypesErrors.ts, 130, 11)) +// Verify that callback parameters aren't loosely checked when types +// originate in method declarations + +namespace n1 { +>n1 : Symbol(n1, Decl(strictFunctionTypesErrors.ts, 133, 10)) + + class Foo { +>Foo : Symbol(Foo, Decl(strictFunctionTypesErrors.ts, 138, 14)) + + static f1(x: Animal): Animal { throw "wat"; } +>f1 : Symbol(Foo.f1, Decl(strictFunctionTypesErrors.ts, 139, 15)) +>x : Symbol(x, Decl(strictFunctionTypesErrors.ts, 140, 18)) +>Animal : Symbol(Animal, Decl(strictFunctionTypesErrors.ts, 87, 8)) +>Animal : Symbol(Animal, Decl(strictFunctionTypesErrors.ts, 87, 8)) + + static f2(x: Dog): Animal { throw "wat"; }; +>f2 : Symbol(Foo.f2, Decl(strictFunctionTypesErrors.ts, 140, 53)) +>x : Symbol(x, Decl(strictFunctionTypesErrors.ts, 141, 18)) +>Dog : Symbol(Dog, Decl(strictFunctionTypesErrors.ts, 89, 33)) +>Animal : Symbol(Animal, Decl(strictFunctionTypesErrors.ts, 87, 8)) + } + declare let f1: (cb: typeof Foo.f1) => void; +>f1 : Symbol(f1, Decl(strictFunctionTypesErrors.ts, 143, 15)) +>cb : Symbol(cb, Decl(strictFunctionTypesErrors.ts, 143, 21)) +>Foo.f1 : Symbol(Foo.f1, Decl(strictFunctionTypesErrors.ts, 139, 15)) +>Foo : Symbol(Foo, Decl(strictFunctionTypesErrors.ts, 138, 14)) +>f1 : Symbol(Foo.f1, Decl(strictFunctionTypesErrors.ts, 139, 15)) + + declare let f2: (cb: typeof Foo.f2) => void; +>f2 : Symbol(f2, Decl(strictFunctionTypesErrors.ts, 144, 15)) +>cb : Symbol(cb, Decl(strictFunctionTypesErrors.ts, 144, 21)) +>Foo.f2 : Symbol(Foo.f2, Decl(strictFunctionTypesErrors.ts, 140, 53)) +>Foo : Symbol(Foo, Decl(strictFunctionTypesErrors.ts, 138, 14)) +>f2 : Symbol(Foo.f2, Decl(strictFunctionTypesErrors.ts, 140, 53)) + + f1 = f2; +>f1 : Symbol(f1, Decl(strictFunctionTypesErrors.ts, 143, 15)) +>f2 : Symbol(f2, Decl(strictFunctionTypesErrors.ts, 144, 15)) + + f2 = f1; // Error +>f2 : Symbol(f2, Decl(strictFunctionTypesErrors.ts, 144, 15)) +>f1 : Symbol(f1, Decl(strictFunctionTypesErrors.ts, 143, 15)) +} + +namespace n2 { +>n2 : Symbol(n2, Decl(strictFunctionTypesErrors.ts, 147, 1)) + + type BivariantHack = { foo(x: Input): Output }["foo"]; +>BivariantHack : Symbol(BivariantHack, Decl(strictFunctionTypesErrors.ts, 149, 14)) +>Input : Symbol(Input, Decl(strictFunctionTypesErrors.ts, 150, 23)) +>Output : Symbol(Output, Decl(strictFunctionTypesErrors.ts, 150, 29)) +>foo : Symbol(foo, Decl(strictFunctionTypesErrors.ts, 150, 41)) +>x : Symbol(x, Decl(strictFunctionTypesErrors.ts, 150, 46)) +>Input : Symbol(Input, Decl(strictFunctionTypesErrors.ts, 150, 23)) +>Output : Symbol(Output, Decl(strictFunctionTypesErrors.ts, 150, 29)) + + declare let f1: (cb: BivariantHack) => void; +>f1 : Symbol(f1, Decl(strictFunctionTypesErrors.ts, 151, 15)) +>cb : Symbol(cb, Decl(strictFunctionTypesErrors.ts, 151, 21)) +>BivariantHack : Symbol(BivariantHack, Decl(strictFunctionTypesErrors.ts, 149, 14)) +>Animal : Symbol(Animal, Decl(strictFunctionTypesErrors.ts, 87, 8)) +>Animal : Symbol(Animal, Decl(strictFunctionTypesErrors.ts, 87, 8)) + + declare let f2: (cb: BivariantHack) => void; +>f2 : Symbol(f2, Decl(strictFunctionTypesErrors.ts, 152, 15)) +>cb : Symbol(cb, Decl(strictFunctionTypesErrors.ts, 152, 21)) +>BivariantHack : Symbol(BivariantHack, Decl(strictFunctionTypesErrors.ts, 149, 14)) +>Dog : Symbol(Dog, Decl(strictFunctionTypesErrors.ts, 89, 33)) +>Animal : Symbol(Animal, Decl(strictFunctionTypesErrors.ts, 87, 8)) + + f1 = f2; +>f1 : Symbol(f1, Decl(strictFunctionTypesErrors.ts, 151, 15)) +>f2 : Symbol(f2, Decl(strictFunctionTypesErrors.ts, 152, 15)) + + f2 = f1; // Error +>f2 : Symbol(f2, Decl(strictFunctionTypesErrors.ts, 152, 15)) +>f1 : Symbol(f1, Decl(strictFunctionTypesErrors.ts, 151, 15)) +} diff --git a/tests/baselines/reference/strictFunctionTypesErrors.types b/tests/baselines/reference/strictFunctionTypesErrors.types index 45deeebabab..1890279258c 100644 --- a/tests/baselines/reference/strictFunctionTypesErrors.types +++ b/tests/baselines/reference/strictFunctionTypesErrors.types @@ -480,3 +480,87 @@ fc2 = fc1; // Error >fc2 : (f: (x: Dog) => Dog) => void >fc1 : (f: (x: Animal) => Animal) => void +// Verify that callback parameters aren't loosely checked when types +// originate in method declarations + +namespace n1 { +>n1 : typeof n1 + + class Foo { +>Foo : Foo + + static f1(x: Animal): Animal { throw "wat"; } +>f1 : (x: Animal) => Animal +>x : Animal +>Animal : Animal +>Animal : Animal +>"wat" : "wat" + + static f2(x: Dog): Animal { throw "wat"; }; +>f2 : (x: Dog) => Animal +>x : Dog +>Dog : Dog +>Animal : Animal +>"wat" : "wat" + } + declare let f1: (cb: typeof Foo.f1) => void; +>f1 : (cb: (x: Animal) => Animal) => void +>cb : (x: Animal) => Animal +>Foo.f1 : (x: Animal) => Animal +>Foo : typeof Foo +>f1 : (x: Animal) => Animal + + declare let f2: (cb: typeof Foo.f2) => void; +>f2 : (cb: (x: Dog) => Animal) => void +>cb : (x: Dog) => Animal +>Foo.f2 : (x: Dog) => Animal +>Foo : typeof Foo +>f2 : (x: Dog) => Animal + + f1 = f2; +>f1 = f2 : (cb: (x: Dog) => Animal) => void +>f1 : (cb: (x: Animal) => Animal) => void +>f2 : (cb: (x: Dog) => Animal) => void + + f2 = f1; // Error +>f2 = f1 : (cb: (x: Animal) => Animal) => void +>f2 : (cb: (x: Dog) => Animal) => void +>f1 : (cb: (x: Animal) => Animal) => void +} + +namespace n2 { +>n2 : typeof n2 + + type BivariantHack = { foo(x: Input): Output }["foo"]; +>BivariantHack : (x: Input) => Output +>Input : Input +>Output : Output +>foo : (x: Input) => Output +>x : Input +>Input : Input +>Output : Output + + declare let f1: (cb: BivariantHack) => void; +>f1 : (cb: (x: Animal) => Animal) => void +>cb : (x: Animal) => Animal +>BivariantHack : (x: Input) => Output +>Animal : Animal +>Animal : Animal + + declare let f2: (cb: BivariantHack) => void; +>f2 : (cb: (x: Dog) => Animal) => void +>cb : (x: Dog) => Animal +>BivariantHack : (x: Input) => Output +>Dog : Dog +>Animal : Animal + + f1 = f2; +>f1 = f2 : (cb: (x: Dog) => Animal) => void +>f1 : (cb: (x: Animal) => Animal) => void +>f2 : (cb: (x: Dog) => Animal) => void + + f2 = f1; // Error +>f2 = f1 : (cb: (x: Animal) => Animal) => void +>f2 : (cb: (x: Dog) => Animal) => void +>f1 : (cb: (x: Animal) => Animal) => void +} diff --git a/tests/cases/compiler/strictFunctionTypesErrors.ts b/tests/cases/compiler/strictFunctionTypesErrors.ts index 95598d9d444..3f86f2529c0 100644 --- a/tests/cases/compiler/strictFunctionTypesErrors.ts +++ b/tests/cases/compiler/strictFunctionTypesErrors.ts @@ -133,3 +133,25 @@ declare let fc1: (f: (x: Animal) => Animal) => void; declare let fc2: (f: (x: Dog) => Dog) => void; fc1 = fc2; // Error fc2 = fc1; // Error + +// Verify that callback parameters aren't loosely checked when types +// originate in method declarations + +namespace n1 { + class Foo { + static f1(x: Animal): Animal { throw "wat"; } + static f2(x: Dog): Animal { throw "wat"; }; + } + declare let f1: (cb: typeof Foo.f1) => void; + declare let f2: (cb: typeof Foo.f2) => void; + f1 = f2; + f2 = f1; // Error +} + +namespace n2 { + type BivariantHack = { foo(x: Input): Output }["foo"]; + declare let f1: (cb: BivariantHack) => void; + declare let f2: (cb: BivariantHack) => void; + f1 = f2; + f2 = f1; // Error +} \ No newline at end of file From 3b9bbb3e55c91775b9b1633bcd4cf6417d280b6b Mon Sep 17 00:00:00 2001 From: Andy Date: Fri, 6 Oct 2017 10:31:45 -0700 Subject: [PATCH 34/54] Remove duplicate assignment (#18994) --- src/compiler/checker.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 58248a8a9ee..bb37d09888b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -18451,9 +18451,8 @@ namespace ts { checkGrammarDecorators(node) || checkGrammarModifiers(node); checkVariableLikeDeclaration(node); - let func = getContainingFunction(node); + const func = getContainingFunction(node); if (hasModifier(node, ModifierFlags.ParameterPropertyModifier)) { - func = getContainingFunction(node); if (!(func.kind === SyntaxKind.Constructor && nodeIsPresent(func.body))) { error(node, Diagnostics.A_parameter_property_is_only_allowed_in_a_constructor_implementation); } From fbf8df66f006553bc720751389e18d98a3f9b3cf Mon Sep 17 00:00:00 2001 From: Arthur Ozga Date: Fri, 6 Oct 2017 14:27:32 -0700 Subject: [PATCH 35/54] accept baselines --- tests/baselines/reference/APISample_jsdoc.js | 4 ++-- tests/baselines/reference/api/tsserverlibrary.d.ts | 4 ++++ tests/baselines/reference/api/typescript.d.ts | 4 ++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/baselines/reference/APISample_jsdoc.js b/tests/baselines/reference/APISample_jsdoc.js index 33857d06a6a..c74e188f38b 100644 --- a/tests/baselines/reference/APISample_jsdoc.js +++ b/tests/baselines/reference/APISample_jsdoc.js @@ -101,7 +101,7 @@ function getAllTags(node: ts.Node) { function getSomeOtherTags(node: ts.Node) { const tags: (ts.JSDocTag | undefined)[] = []; - tags.push(ts.getJSDocAugmentsOrExtendsTag(node)); + tags.push(ts.getJSDocAugmentsTag(node)); tags.push(ts.getJSDocClassTag(node)); tags.push(ts.getJSDocReturnTag(node)); const type = ts.getJSDocTypeTag(node); @@ -200,7 +200,7 @@ function getAllTags(node) { } function getSomeOtherTags(node) { var tags = []; - tags.push(ts.getJSDocAugmentsOrExtendsTag(node)); + tags.push(ts.getJSDocAugmentsTag(node)); tags.push(ts.getJSDocClassTag(node)); tags.push(ts.getJSDocReturnTag(node)); var type = ts.getJSDocTypeTag(node); diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 3bb2ed11674..ca8696b11fb 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -1442,6 +1442,10 @@ declare namespace ts { interface JSDocUnknownTag extends JSDocTag { kind: SyntaxKind.JSDocTag; } + /** + * Note that `@extends` is a synonym of `@augments`. + * Both tags are represented by this interface. + */ interface JSDocAugmentsTag extends JSDocTag { kind: SyntaxKind.JSDocAugmentsTag; class: ExpressionWithTypeArguments & { diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index d41db2eb413..820be44f1f1 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -1442,6 +1442,10 @@ declare namespace ts { interface JSDocUnknownTag extends JSDocTag { kind: SyntaxKind.JSDocTag; } + /** + * Note that `@extends` is a synonym of `@augments`. + * Both tags are represented by this interface. + */ interface JSDocAugmentsTag extends JSDocTag { kind: SyntaxKind.JSDocAugmentsTag; class: ExpressionWithTypeArguments & { From 71f885212451e09da16c8808361c46f3fc38227f Mon Sep 17 00:00:00 2001 From: Andy Date: Fri, 6 Oct 2017 14:29:45 -0700 Subject: [PATCH 36/54] Have getNameOfDeclaration return `x` for `export default x`. (#18616) --- src/compiler/binder.ts | 6 ++- src/compiler/utilities.ts | 46 +++++++++++-------- src/services/findAllReferences.ts | 5 +- src/services/importTracker.ts | 3 -- src/services/symbolDisplay.ts | 20 +++++--- .../duplicateExportAssignments.errors.txt | 44 +++++++++--------- .../baselines/reference/es5-commonjs7.symbols | 2 +- .../reference/exportDefaultVariable.symbols | 2 +- ...ationBindMultipleDefaultExports.errors.txt | 4 +- .../multipleDefaultExports01.errors.txt | 4 +- .../multipleExportAssignments.errors.txt | 8 ++-- ...AssignmentsInAmbientDeclaration.errors.txt | 8 ++-- .../reference/typeAliasExport.symbols | 2 +- ...indAllRefsDefaultImportThroughNamespace.ts | 15 +++--- .../findAllRefsForDefaultExport04.ts | 6 +-- ...currencesIsDefinitionOfComputedProperty.ts | 6 ++- 16 files changed, 100 insertions(+), 81 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 796c9603423..28193535ce9 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -230,6 +230,10 @@ namespace ts { // Should not be called on a declaration with a computed property name, // unless it is a well known Symbol. function getDeclarationName(node: Declaration): __String { + if (node.kind === SyntaxKind.ExportAssignment) { + return (node).isExportEquals ? InternalSymbolName.ExportEquals : InternalSymbolName.Default; + } + const name = getNameOfDeclaration(node); if (name) { if (isAmbientModule(node)) { @@ -261,8 +265,6 @@ namespace ts { return InternalSymbolName.Index; case SyntaxKind.ExportDeclaration: return InternalSymbolName.ExportStar; - case SyntaxKind.ExportAssignment: - return (node).isExportEquals ? InternalSymbolName.ExportEquals : InternalSymbolName.Default; case SyntaxKind.BinaryExpression: if (getSpecialPropertyAssignmentKind(node as BinaryExpression) === SpecialPropertyAssignmentKind.ModuleExports) { // module.exports = ... diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index f0eb394adb7..ce209cbd811 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -4112,27 +4112,35 @@ namespace ts { if (!declaration) { return undefined; } - if (isJSDocPropertyLikeTag(declaration) && declaration.name.kind === SyntaxKind.QualifiedName) { - return declaration.name.right; - } - if (declaration.kind === SyntaxKind.BinaryExpression) { - const expr = declaration as BinaryExpression; - switch (getSpecialPropertyAssignmentKind(expr)) { - case SpecialPropertyAssignmentKind.ExportsProperty: - case SpecialPropertyAssignmentKind.ThisProperty: - case SpecialPropertyAssignmentKind.Property: - case SpecialPropertyAssignmentKind.PrototypeProperty: - return (expr.left as PropertyAccessExpression).name; - default: - return undefined; + switch (declaration.kind) { + case SyntaxKind.JSDocPropertyTag: + case SyntaxKind.JSDocParameterTag: { + const { name } = declaration as JSDocPropertyLikeTag; + if (name.kind === SyntaxKind.QualifiedName) { + return name.right; + } + break; + } + case SyntaxKind.BinaryExpression: { + const expr = declaration as BinaryExpression; + switch (getSpecialPropertyAssignmentKind(expr)) { + case SpecialPropertyAssignmentKind.ExportsProperty: + case SpecialPropertyAssignmentKind.ThisProperty: + case SpecialPropertyAssignmentKind.Property: + case SpecialPropertyAssignmentKind.PrototypeProperty: + return (expr.left as PropertyAccessExpression).name; + default: + return undefined; + } + } + case SyntaxKind.JSDocTypedefTag: + return getNameOfJSDocTypedef(declaration as JSDocTypedefTag); + case SyntaxKind.ExportAssignment: { + const { expression } = declaration as ExportAssignment; + return isIdentifier(expression) ? expression : undefined; } } - else if (declaration.kind === SyntaxKind.JSDocTypedefTag) { - return getNameOfJSDocTypedef(declaration as JSDocTypedefTag); - } - else { - return (declaration as NamedDeclaration).name; - } + return (declaration as NamedDeclaration).name; } /** diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index d4e660295a0..e43a2cbfd8e 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -482,7 +482,10 @@ namespace ts.FindAllReferences.Core { /** @param allSearchSymbols set of additinal symbols for use by `includes`. */ createSearch(location: Node, symbol: Symbol, comingFrom: ImportExport | undefined, searchOptions: { text?: string, allSearchSymbols?: Symbol[] } = {}): Search { // Note: if this is an external module symbol, the name doesn't include quotes. - const { text = stripQuotes(getDeclaredName(this.checker, symbol, location)), allSearchSymbols = undefined } = searchOptions; + const { + text = stripQuotes(unescapeLeadingUnderscores((getLocalSymbolForExportDefault(symbol) || symbol).escapedName)), + allSearchSymbols = undefined, + } = searchOptions; const escapedText = escapeLeadingUnderscores(text); const parents = this.options.implementations && getParentSymbolsOfPropertyAccess(location, symbol, this.checker); return { diff --git a/src/services/importTracker.ts b/src/services/importTracker.ts index a6152230218..f12df4613c0 100644 --- a/src/services/importTracker.ts +++ b/src/services/importTracker.ts @@ -609,9 +609,6 @@ namespace ts.FindAllReferences { } return forEach(symbol.declarations, decl => { - if (isExportAssignment(decl)) { - return isIdentifier(decl.expression) ? decl.expression.escapedText : undefined; - } const name = getNameOfDeclaration(decl); return name && name.kind === SyntaxKind.Identifier && name.escapedText; }); diff --git a/src/services/symbolDisplay.ts b/src/services/symbolDisplay.ts index a399610d823..d38d21f1092 100644 --- a/src/services/symbolDisplay.ts +++ b/src/services/symbolDisplay.ts @@ -341,13 +341,19 @@ namespace ts.SymbolDisplay { } if (symbolFlags & SymbolFlags.Alias) { addNewLineIfDisplayPartsExist(); - if (symbol.declarations[0].kind === SyntaxKind.NamespaceExportDeclaration) { - displayParts.push(keywordPart(SyntaxKind.ExportKeyword)); - displayParts.push(spacePart()); - displayParts.push(keywordPart(SyntaxKind.NamespaceKeyword)); - } - else { - displayParts.push(keywordPart(SyntaxKind.ImportKeyword)); + switch (symbol.declarations[0].kind) { + case SyntaxKind.NamespaceExportDeclaration: + displayParts.push(keywordPart(SyntaxKind.ExportKeyword)); + displayParts.push(spacePart()); + displayParts.push(keywordPart(SyntaxKind.NamespaceKeyword)); + break; + case SyntaxKind.ExportAssignment: + displayParts.push(keywordPart(SyntaxKind.ExportKeyword)); + displayParts.push(spacePart()); + displayParts.push(keywordPart((symbol.declarations[0] as ExportAssignment).isExportEquals ? SyntaxKind.EqualsToken : SyntaxKind.DefaultKeyword)); + break; + default: + displayParts.push(keywordPart(SyntaxKind.ImportKeyword)); } displayParts.push(spacePart()); addFullSymbolName(symbol); diff --git a/tests/baselines/reference/duplicateExportAssignments.errors.txt b/tests/baselines/reference/duplicateExportAssignments.errors.txt index 17ad0a39847..0ad9507c9a6 100644 --- a/tests/baselines/reference/duplicateExportAssignments.errors.txt +++ b/tests/baselines/reference/duplicateExportAssignments.errors.txt @@ -1,34 +1,34 @@ -tests/cases/conformance/externalModules/foo1.ts(3,1): error TS2300: Duplicate identifier 'export='. -tests/cases/conformance/externalModules/foo1.ts(4,1): error TS2300: Duplicate identifier 'export='. -tests/cases/conformance/externalModules/foo2.ts(3,1): error TS2300: Duplicate identifier 'export='. -tests/cases/conformance/externalModules/foo2.ts(4,1): error TS2300: Duplicate identifier 'export='. -tests/cases/conformance/externalModules/foo3.ts(7,1): error TS2300: Duplicate identifier 'export='. -tests/cases/conformance/externalModules/foo3.ts(8,1): error TS2300: Duplicate identifier 'export='. -tests/cases/conformance/externalModules/foo4.ts(1,1): error TS2300: Duplicate identifier 'export='. -tests/cases/conformance/externalModules/foo4.ts(8,1): error TS2300: Duplicate identifier 'export='. -tests/cases/conformance/externalModules/foo5.ts(4,1): error TS2300: Duplicate identifier 'export='. -tests/cases/conformance/externalModules/foo5.ts(5,1): error TS2300: Duplicate identifier 'export='. -tests/cases/conformance/externalModules/foo5.ts(6,1): error TS2300: Duplicate identifier 'export='. +tests/cases/conformance/externalModules/foo1.ts(3,10): error TS2300: Duplicate identifier 'export='. +tests/cases/conformance/externalModules/foo1.ts(4,10): error TS2300: Duplicate identifier 'export='. +tests/cases/conformance/externalModules/foo2.ts(3,10): error TS2300: Duplicate identifier 'export='. +tests/cases/conformance/externalModules/foo2.ts(4,10): error TS2300: Duplicate identifier 'export='. +tests/cases/conformance/externalModules/foo3.ts(7,10): error TS2300: Duplicate identifier 'export='. +tests/cases/conformance/externalModules/foo3.ts(8,10): error TS2300: Duplicate identifier 'export='. +tests/cases/conformance/externalModules/foo4.ts(1,10): error TS2300: Duplicate identifier 'export='. +tests/cases/conformance/externalModules/foo4.ts(8,10): error TS2300: Duplicate identifier 'export='. +tests/cases/conformance/externalModules/foo5.ts(4,10): error TS2300: Duplicate identifier 'export='. +tests/cases/conformance/externalModules/foo5.ts(5,10): error TS2300: Duplicate identifier 'export='. +tests/cases/conformance/externalModules/foo5.ts(6,10): error TS2300: Duplicate identifier 'export='. ==== tests/cases/conformance/externalModules/foo1.ts (2 errors) ==== var x = 10; var y = 20; export = x; - ~~~~~~~~~~~ + ~ !!! error TS2300: Duplicate identifier 'export='. export = y; - ~~~~~~~~~~~ + ~ !!! error TS2300: Duplicate identifier 'export='. ==== tests/cases/conformance/externalModules/foo2.ts (2 errors) ==== var x = 10; class y {}; export = x; - ~~~~~~~~~~~ + ~ !!! error TS2300: Duplicate identifier 'export='. export = y; - ~~~~~~~~~~~ + ~ !!! error TS2300: Duplicate identifier 'export='. ==== tests/cases/conformance/externalModules/foo3.ts (2 errors) ==== @@ -39,15 +39,15 @@ tests/cases/conformance/externalModules/foo5.ts(6,1): error TS2300: Duplicate id y: number; } export = x; - ~~~~~~~~~~~ + ~ !!! error TS2300: Duplicate identifier 'export='. export = y; - ~~~~~~~~~~~ + ~ !!! error TS2300: Duplicate identifier 'export='. ==== tests/cases/conformance/externalModules/foo4.ts (2 errors) ==== export = x; - ~~~~~~~~~~~ + ~ !!! error TS2300: Duplicate identifier 'export='. function x(){ return 42; @@ -56,7 +56,7 @@ tests/cases/conformance/externalModules/foo5.ts(6,1): error TS2300: Duplicate id return 42; } export = y; - ~~~~~~~~~~~ + ~ !!! error TS2300: Duplicate identifier 'export='. ==== tests/cases/conformance/externalModules/foo5.ts (3 errors) ==== @@ -64,12 +64,12 @@ tests/cases/conformance/externalModules/foo5.ts(6,1): error TS2300: Duplicate id var y = "test"; var z = {}; export = x; - ~~~~~~~~~~~ + ~ !!! error TS2300: Duplicate identifier 'export='. export = y; - ~~~~~~~~~~~ + ~ !!! error TS2300: Duplicate identifier 'export='. export = z; - ~~~~~~~~~~~ + ~ !!! error TS2300: Duplicate identifier 'export='. \ No newline at end of file diff --git a/tests/baselines/reference/es5-commonjs7.symbols b/tests/baselines/reference/es5-commonjs7.symbols index ed8247a6878..eb6dc062573 100644 --- a/tests/baselines/reference/es5-commonjs7.symbols +++ b/tests/baselines/reference/es5-commonjs7.symbols @@ -1,6 +1,6 @@ === tests/cases/compiler/test.d.ts === export default undefined; ->undefined : Symbol(default) +>undefined : Symbol(undefined) export var __esModule; >__esModule : Symbol(__esModule, Decl(test.d.ts, 1, 10)) diff --git a/tests/baselines/reference/exportDefaultVariable.symbols b/tests/baselines/reference/exportDefaultVariable.symbols index cd7ebed926c..9c924c32f4e 100644 --- a/tests/baselines/reference/exportDefaultVariable.symbols +++ b/tests/baselines/reference/exportDefaultVariable.symbols @@ -6,6 +6,6 @@ declare var io: any; declare module 'module' { export default io; ->io : Symbol(default, Decl(exportDefaultVariable.ts, 2, 11)) +>io : Symbol(io, Decl(exportDefaultVariable.ts, 2, 11)) } diff --git a/tests/baselines/reference/jsFileCompilationBindMultipleDefaultExports.errors.txt b/tests/baselines/reference/jsFileCompilationBindMultipleDefaultExports.errors.txt index 9a9a7a12db9..246b8f20cac 100644 --- a/tests/baselines/reference/jsFileCompilationBindMultipleDefaultExports.errors.txt +++ b/tests/baselines/reference/jsFileCompilationBindMultipleDefaultExports.errors.txt @@ -1,6 +1,6 @@ tests/cases/compiler/a.js(1,22): error TS2528: A module cannot have multiple default exports. tests/cases/compiler/a.js(1,22): error TS2652: Merged declaration 'a' cannot include a default export declaration. Consider adding a separate 'export default a' declaration instead. -tests/cases/compiler/a.js(3,1): error TS2528: A module cannot have multiple default exports. +tests/cases/compiler/a.js(3,15): error TS2528: A module cannot have multiple default exports. tests/cases/compiler/a.js(3,16): error TS1109: Expression expected. tests/cases/compiler/a.js(3,20): error TS2652: Merged declaration 'a' cannot include a default export declaration. Consider adding a separate 'export default a' declaration instead. @@ -13,7 +13,7 @@ tests/cases/compiler/a.js(3,20): error TS2652: Merged declaration 'a' cannot inc !!! error TS2652: Merged declaration 'a' cannot include a default export declaration. Consider adding a separate 'export default a' declaration instead. } export default var a = 10; - ~~~~~~~~~~~~~~ + !!! error TS2528: A module cannot have multiple default exports. ~~~ !!! error TS1109: Expression expected. diff --git a/tests/baselines/reference/multipleDefaultExports01.errors.txt b/tests/baselines/reference/multipleDefaultExports01.errors.txt index b53d4113ded..eff0203387c 100644 --- a/tests/baselines/reference/multipleDefaultExports01.errors.txt +++ b/tests/baselines/reference/multipleDefaultExports01.errors.txt @@ -1,6 +1,6 @@ tests/cases/conformance/es6/modules/m1.ts(1,22): error TS2528: A module cannot have multiple default exports. tests/cases/conformance/es6/modules/m1.ts(5,25): error TS2528: A module cannot have multiple default exports. -tests/cases/conformance/es6/modules/m1.ts(10,1): error TS2528: A module cannot have multiple default exports. +tests/cases/conformance/es6/modules/m1.ts(10,16): error TS2528: A module cannot have multiple default exports. tests/cases/conformance/es6/modules/m2.ts(3,1): error TS2348: Value of type 'typeof foo' is not callable. Did you mean to include 'new'? @@ -19,7 +19,7 @@ tests/cases/conformance/es6/modules/m2.ts(3,1): error TS2348: Value of type 'typ var x = 10; export default x; - ~~~~~~~~~~~~~~~~~ + ~ !!! error TS2528: A module cannot have multiple default exports. ==== tests/cases/conformance/es6/modules/m2.ts (1 errors) ==== diff --git a/tests/baselines/reference/multipleExportAssignments.errors.txt b/tests/baselines/reference/multipleExportAssignments.errors.txt index 5fbabc2b4db..c9f5eb7cf79 100644 --- a/tests/baselines/reference/multipleExportAssignments.errors.txt +++ b/tests/baselines/reference/multipleExportAssignments.errors.txt @@ -1,5 +1,5 @@ -tests/cases/compiler/multipleExportAssignments.ts(13,1): error TS2300: Duplicate identifier 'export='. -tests/cases/compiler/multipleExportAssignments.ts(14,1): error TS2300: Duplicate identifier 'export='. +tests/cases/compiler/multipleExportAssignments.ts(13,10): error TS2300: Duplicate identifier 'export='. +tests/cases/compiler/multipleExportAssignments.ts(14,10): error TS2300: Duplicate identifier 'export='. ==== tests/cases/compiler/multipleExportAssignments.ts (2 errors) ==== @@ -16,10 +16,10 @@ tests/cases/compiler/multipleExportAssignments.ts(14,1): error TS2300: Duplicate test2(): connectModule; }; export = server; - ~~~~~~~~~~~~~~~~ + ~~~~~~ !!! error TS2300: Duplicate identifier 'export='. export = connectExport; - ~~~~~~~~~~~~~~~~~~~~~~~ + ~~~~~~~~~~~~~ !!! error TS2300: Duplicate identifier 'export='. \ No newline at end of file diff --git a/tests/baselines/reference/multipleExportAssignmentsInAmbientDeclaration.errors.txt b/tests/baselines/reference/multipleExportAssignmentsInAmbientDeclaration.errors.txt index c6427a5cc28..792e2dd5968 100644 --- a/tests/baselines/reference/multipleExportAssignmentsInAmbientDeclaration.errors.txt +++ b/tests/baselines/reference/multipleExportAssignmentsInAmbientDeclaration.errors.txt @@ -1,5 +1,5 @@ -tests/cases/compiler/multipleExportAssignmentsInAmbientDeclaration.ts(4,5): error TS2300: Duplicate identifier 'export='. -tests/cases/compiler/multipleExportAssignmentsInAmbientDeclaration.ts(5,5): error TS2300: Duplicate identifier 'export='. +tests/cases/compiler/multipleExportAssignmentsInAmbientDeclaration.ts(4,14): error TS2300: Duplicate identifier 'export='. +tests/cases/compiler/multipleExportAssignmentsInAmbientDeclaration.ts(5,14): error TS2300: Duplicate identifier 'export='. ==== tests/cases/compiler/multipleExportAssignmentsInAmbientDeclaration.ts (2 errors) ==== @@ -7,9 +7,9 @@ tests/cases/compiler/multipleExportAssignmentsInAmbientDeclaration.ts(5,5): erro var a: number var b: number; export = a; - ~~~~~~~~~~~ + ~ !!! error TS2300: Duplicate identifier 'export='. export = b; - ~~~~~~~~~~~ + ~ !!! error TS2300: Duplicate identifier 'export='. } \ No newline at end of file diff --git a/tests/baselines/reference/typeAliasExport.symbols b/tests/baselines/reference/typeAliasExport.symbols index e019ca7a10b..5ffe181ca70 100644 --- a/tests/baselines/reference/typeAliasExport.symbols +++ b/tests/baselines/reference/typeAliasExport.symbols @@ -1,7 +1,7 @@ === tests/cases/compiler/typeAliasExport.ts === declare module "a" { export default undefined ->undefined : Symbol(default) +>undefined : Symbol(undefined) export var a; >a : Symbol(a, Decl(typeAliasExport.ts, 2, 12), Decl(typeAliasExport.ts, 2, 15)) diff --git a/tests/cases/fourslash/findAllRefsDefaultImportThroughNamespace.ts b/tests/cases/fourslash/findAllRefsDefaultImportThroughNamespace.ts index e5f77631324..1411a106c89 100644 --- a/tests/cases/fourslash/findAllRefsDefaultImportThroughNamespace.ts +++ b/tests/cases/fourslash/findAllRefsDefaultImportThroughNamespace.ts @@ -1,7 +1,7 @@ /// // @Filename: /a.ts -////export default function [|{| "isWriteAccess": true, "isDefinition": true |}f|]() {} +////export [|{| "isWriteAccess": true, "isDefinition": true |}default|] function [|{| "isWriteAccess": true, "isDefinition": true |}f|]() {} // @Filename: /b.ts ////export import a = require("./a"); @@ -13,16 +13,17 @@ ////declare const x: { [|{| "isWriteAccess": true, "isDefinition": true |}default|]: number }; ////x.[|default|]; -const [r0, r1, r2, r3] = test.ranges(); +const [r0, r1, r2, r3, r4] = test.ranges(); -verify.singleReferenceGroup("function f(): void", [r0, r1]); -verify.singleReferenceGroup("(property) default: number", [r2, r3]); +verify.referenceGroups([r0], [{ definition: "function f(): void", ranges: [r1, r2] }]); +verify.singleReferenceGroup("function f(): void", [r1, r2]); +verify.singleReferenceGroup("(property) default: number", [r3, r4]); -verify.rangesAreRenameLocations([r0]); +verify.rangesAreRenameLocations([r1]); // Can't rename a default import. -goTo.rangeStart(r1); +goTo.rangeStart(r2); verify.renameInfoFailed(); // Can rename a default property. -verify.rangesAreRenameLocations([r2, r3]); +verify.rangesAreRenameLocations([r3, r4]); diff --git a/tests/cases/fourslash/findAllRefsForDefaultExport04.ts b/tests/cases/fourslash/findAllRefsForDefaultExport04.ts index 1b5eb7a282d..093910d1cb5 100644 --- a/tests/cases/fourslash/findAllRefsForDefaultExport04.ts +++ b/tests/cases/fourslash/findAllRefsForDefaultExport04.ts @@ -14,12 +14,10 @@ verify.referenceGroups([r0, r2], [ { definition: "import a", ranges: [r3, r4] } ]); verify.referenceGroups(r1, [ - // TODO:GH#17990 - { definition: "import default", ranges: [r1] }, + { definition: "export default a", ranges: [r1] }, { definition: "import a", ranges: [r3, r4] }, ]); verify.referenceGroups([r3, r4], [ { definition: "import a", ranges: [r3, r4] }, - // TODO:GH#17990 - { definition: "import default", ranges: [r1] }, + { definition: "export default a", ranges: [r1] }, ]); diff --git a/tests/cases/fourslash/getOccurrencesIsDefinitionOfComputedProperty.ts b/tests/cases/fourslash/getOccurrencesIsDefinitionOfComputedProperty.ts index 7d6cb0a48af..329a72c30fd 100644 --- a/tests/cases/fourslash/getOccurrencesIsDefinitionOfComputedProperty.ts +++ b/tests/cases/fourslash/getOccurrencesIsDefinitionOfComputedProperty.ts @@ -6,4 +6,8 @@ const ranges = test.ranges(); const [r0, r1, r2] = ranges; verify.referenceGroups(r0, [{ definition: '(property) ["foo"]: number', ranges }]); -verify.referenceGroups([r1, r2], undefined); // TODO: fix +verify.referenceGroups([r1, r2], [ + // TODO: these are the same thing, should be in the same group. + { definition: "(property) [\"foo\"]: number", ranges: [r0] }, + { definition: "(property) [\"foo\"]: number", ranges: [r1, r2] }, +]); From e821c2b6e9f244264779045fd1720fb9d16c0bb2 Mon Sep 17 00:00:00 2001 From: Andy Date: Fri, 6 Oct 2017 15:05:00 -0700 Subject: [PATCH 37/54] A parameter not declared as a rest parameter is not one (#18825) --- src/compiler/checker.ts | 2 +- src/compiler/utilities.ts | 22 ++++--------------- ...ileCompilationRestParamJsDocFunction.types | 4 ++-- .../reference/jsdocPrefixPostfixParsing.types | 2 +- 4 files changed, 8 insertions(+), 22 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e1a60c3c5e2..a8e18c432a6 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -20298,7 +20298,7 @@ namespace ts { function checkCollisionWithArgumentsInGeneratedCode(node: SignatureDeclaration) { // no rest parameters \ declaration context \ overload - no codegen impact - if (!hasDeclaredRestParameter(node) || isInAmbientContext(node) || nodeIsMissing((node).body)) { + if (!hasRestParameter(node) || isInAmbientContext(node) || nodeIsMissing((node).body)) { return; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index ce209cbd811..bdf8d81aa96 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1604,26 +1604,12 @@ namespace ts { } export function hasRestParameter(s: SignatureDeclaration): boolean { - return isRestParameter(lastOrUndefined(s.parameters)); + const last = lastOrUndefined(s.parameters); + return last && isRestParameter(last); } - export function hasDeclaredRestParameter(s: SignatureDeclaration): boolean { - return isDeclaredRestParam(lastOrUndefined(s.parameters)); - } - - export function isRestParameter(node: ParameterDeclaration) { - if (isInJavaScriptFile(node)) { - if (node.type && node.type.kind === SyntaxKind.JSDocVariadicType || - forEach(getJSDocParameterTags(node), - t => t.typeExpression && t.typeExpression.type.kind === SyntaxKind.JSDocVariadicType)) { - return true; - } - } - return isDeclaredRestParam(node); - } - - export function isDeclaredRestParam(node: ParameterDeclaration) { - return node && node.dotDotDotToken !== undefined; + export function isRestParameter(node: ParameterDeclaration): boolean { + return node.dotDotDotToken !== undefined; } export const enum AssignmentKind { diff --git a/tests/baselines/reference/jsFileCompilationRestParamJsDocFunction.types b/tests/baselines/reference/jsFileCompilationRestParamJsDocFunction.types index 54bc7938689..72c3388f226 100644 --- a/tests/baselines/reference/jsFileCompilationRestParamJsDocFunction.types +++ b/tests/baselines/reference/jsFileCompilationRestParamJsDocFunction.types @@ -10,7 +10,7 @@ * @returns {*} Returns the result of `func`. */ function apply(func, thisArg, args) { ->apply : (func: Function, thisArg: any, ...args: any[]) => any +>apply : (func: Function, thisArg: any, args: any[]) => any >func : Function >thisArg : any >args : any[] @@ -84,5 +84,5 @@ function apply(func, thisArg, args) { } export default apply; ->apply : (func: Function, thisArg: any, ...args: any[]) => any +>apply : (func: Function, thisArg: any, args: any[]) => any diff --git a/tests/baselines/reference/jsdocPrefixPostfixParsing.types b/tests/baselines/reference/jsdocPrefixPostfixParsing.types index b5ddec68cba..9961af48e27 100644 --- a/tests/baselines/reference/jsdocPrefixPostfixParsing.types +++ b/tests/baselines/reference/jsdocPrefixPostfixParsing.types @@ -16,7 +16,7 @@ * @param {...number?[]!} k - (number[] | null)[] */ function f(x, y, z, a, b, c, d, e, f, g, h, i, j, k) { ->f : (x: number[], y: number[], z: number[], a: (number | null)[], b: number[] | null, c: number[] | null, d: number[] | null, ...e: (number | null)[], f: number[] | null, g: number[] | null, h: number[] | null, i: number[][], j: number[][] | null, k: (number[] | null)[]) => void +>f : (x: number[], y: number[], z: number[], a: (number | null)[], b: number[] | null, c: number[] | null, d: number[] | null, e: (number | null)[], f: number[] | null, g: number[] | null, h: number[] | null, i: number[][], j: number[][] | null, k: (number[] | null)[]) => void >x : number[] >y : number[] >z : number[] From 0afaadba3b83dfbad89a8c2c5d812ef8ab783361 Mon Sep 17 00:00:00 2001 From: Arthur Ozga Date: Fri, 6 Oct 2017 15:56:39 -0700 Subject: [PATCH 38/54] add error for multiple tags --- src/compiler/checker.ts | 14 ++++-- src/compiler/diagnosticMessages.json | 4 ++ src/compiler/utilities.ts | 6 +++ .../fourslash/jsDocAugmentsAndExtends.ts | 50 +++++++++++++++++++ 4 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 tests/cases/fourslash/jsDocAugmentsAndExtends.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 718fd76625f..31c9acd8fb1 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -20009,14 +20009,20 @@ namespace ts { } function checkJSDocAugmentsTag(node: JSDocAugmentsTag): void { - const cls = getJSDocHost(node); - if (!isClassDeclaration(cls) && !isClassExpression(cls)) { - error(cls, Diagnostics.JSDoc_augments_is_not_attached_to_a_class_declaration); + const classLike = getJSDocHost(node); + if (!isClassDeclaration(classLike) && !isClassExpression(classLike)) { + error(classLike, Diagnostics.JSDoc_augments_is_not_attached_to_a_class_declaration); return; } + const augmentsTags = getAllJSDocTagsOfKind(classLike, SyntaxKind.JSDocAugmentsTag); + Debug.assert(augmentsTags.length > 0); + if (augmentsTags.length > 1) { + error(augmentsTags[1], Diagnostics.The_total_number_of_augments_and_extends_tags_allowed_for_a_single_class_declaration_is_at_most_1); + } + const name = getIdentifierFromEntityNameExpression(node.class.expression); - const extend = getClassExtendsHeritageClauseElement(cls); + const extend = getClassExtendsHeritageClauseElement(classLike); if (extend) { const className = getIdentifierFromEntityNameExpression(extend.expression); if (className && name.escapedText !== className.escapedText) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index f3d6d4fcc47..64c4d99d349 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3527,6 +3527,10 @@ "category": "Error", "code": 8024 }, + "The total number of `@augments` and `@extends` tags allowed for a single class declaration is at most 1.": { + "category": "Error", + "code": 8025 + }, "Only identifiers/qualified-names with optional type arguments are currently supported in a class 'extends' clause.": { "category": "Error", "code": 9002 diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index f0eb394adb7..825b310ddeb 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -4247,6 +4247,12 @@ namespace ts { return find(tags, doc => doc.kind === kind); } + /** Gets all JSDoc tags of a specified kind, or undefined if not present. */ + export function getAllJSDocTagsOfKind(node: Node, kind: SyntaxKind): ReadonlyArray | undefined { + const tags = getJSDocTags(node); + return filter(tags, doc => doc.kind === kind); + } + } // Simple node tests of the form `node.kind === SyntaxKind.Foo`. diff --git a/tests/cases/fourslash/jsDocAugmentsAndExtends.ts b/tests/cases/fourslash/jsDocAugmentsAndExtends.ts new file mode 100644 index 00000000000..e76a617a3cf --- /dev/null +++ b/tests/cases/fourslash/jsDocAugmentsAndExtends.ts @@ -0,0 +1,50 @@ +/// + +// @allowJs: true +// @checkJs: true +// @Filename: dummy.js + +//// /** +//// * @augments {Thing} +//// * @extends {Thing} +//// */ +//// class MyStringThing extends Thing { +//// constructor() { +//// var x = this.mine; +//// x/**/; +//// } +//// } + +// @Filename: declarations.d.ts +//// declare class Thing { +//// mine: T; +//// } + +// if more than one tag is present, report an error and take the type of the first entry. + +goTo.marker(); +verify.quickInfoIs("(local var) x: number"); +verify.getSemanticDiagnostics( +`[ + { + "message": "The total number of \`@augments\` and \`@extends\` tags allowed for a single class declaration is at most 1.", + "start": 36, + "length": 24, + "category": "error", + "code": 8025 + }, + { + "message": "Constructors for derived classes must contain a \'super\' call.", + "start": 105, + "length": 59, + "category": "error", + "code": 2377 + }, + { + "message": "\'super\' must be called before accessing \'this\' in the constructor of a derived class.", + "start": 137, + "length": 4, + "category": "error", + "code": 17009 + } +]`); \ No newline at end of file From 932b1b038c712b73eda432b5296263fe32af6a6d Mon Sep 17 00:00:00 2001 From: Arthur Ozga Date: Fri, 6 Oct 2017 16:16:37 -0700 Subject: [PATCH 39/54] better error message --- src/compiler/checker.ts | 2 +- src/compiler/diagnosticMessages.json | 2 +- .../cases/fourslash/jsDocAugmentsAndExtends.ts | 17 ++--------------- 3 files changed, 4 insertions(+), 17 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 31c9acd8fb1..aa5d10c475c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -20018,7 +20018,7 @@ namespace ts { const augmentsTags = getAllJSDocTagsOfKind(classLike, SyntaxKind.JSDocAugmentsTag); Debug.assert(augmentsTags.length > 0); if (augmentsTags.length > 1) { - error(augmentsTags[1], Diagnostics.The_total_number_of_augments_and_extends_tags_allowed_for_a_single_class_declaration_is_at_most_1); + error(augmentsTags[1], Diagnostics.Class_declarations_cannot_have_more_than_one_augments_or_extends_tag); } const name = getIdentifierFromEntityNameExpression(node.class.expression); diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 64c4d99d349..e0de43a97db 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3527,7 +3527,7 @@ "category": "Error", "code": 8024 }, - "The total number of `@augments` and `@extends` tags allowed for a single class declaration is at most 1.": { + "Class declarations cannot have more than one `@augments` or `@extends` tag.": { "category": "Error", "code": 8025 }, diff --git a/tests/cases/fourslash/jsDocAugmentsAndExtends.ts b/tests/cases/fourslash/jsDocAugmentsAndExtends.ts index e76a617a3cf..10f33260268 100644 --- a/tests/cases/fourslash/jsDocAugmentsAndExtends.ts +++ b/tests/cases/fourslash/jsDocAugmentsAndExtends.ts @@ -10,6 +10,7 @@ //// */ //// class MyStringThing extends Thing { //// constructor() { +//// super(); //// var x = this.mine; //// x/**/; //// } @@ -27,24 +28,10 @@ verify.quickInfoIs("(local var) x: number"); verify.getSemanticDiagnostics( `[ { - "message": "The total number of \`@augments\` and \`@extends\` tags allowed for a single class declaration is at most 1.", + "message": "Class declarations cannot have more than one \`@augments\` or \`@extends\` tag.", "start": 36, "length": 24, "category": "error", "code": 8025 - }, - { - "message": "Constructors for derived classes must contain a \'super\' call.", - "start": 105, - "length": 59, - "category": "error", - "code": 2377 - }, - { - "message": "\'super\' must be called before accessing \'this\' in the constructor of a derived class.", - "start": 137, - "length": 4, - "category": "error", - "code": 17009 } ]`); \ No newline at end of file From 9e00df590d638cad1266e388385396aea2879cc3 Mon Sep 17 00:00:00 2001 From: Charles Pierce Date: Fri, 6 Oct 2017 19:46:29 -0700 Subject: [PATCH 40/54] Error when accessing abstract property in constructor #9230 --- src/compiler/checker.ts | 28 ++++++++-- src/compiler/diagnosticMessages.json | 4 ++ .../abstractPropertyInConstructor.errors.txt | 25 +++++++++ .../abstractPropertyInConstructor.js | 30 ++++++++++ .../abstractPropertyInConstructor.symbols | 48 ++++++++++++++++ .../abstractPropertyInConstructor.types | 56 +++++++++++++++++++ .../compiler/abstractPropertyInConstructor.ts | 15 +++++ 7 files changed, 201 insertions(+), 5 deletions(-) create mode 100644 tests/baselines/reference/abstractPropertyInConstructor.errors.txt create mode 100644 tests/baselines/reference/abstractPropertyInConstructor.js create mode 100644 tests/baselines/reference/abstractPropertyInConstructor.symbols create mode 100644 tests/baselines/reference/abstractPropertyInConstructor.types create mode 100644 tests/cases/compiler/abstractPropertyInConstructor.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a8e18c432a6..9a483ae50ed 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14826,11 +14826,7 @@ namespace ts { // where this references the constructor function object of a derived class, // a super property access is permitted and must specify a public static member function of the base class. if (languageVersion < ScriptTarget.ES2015) { - const hasNonMethodDeclaration = forEachProperty(prop, p => { - const propKind = getDeclarationKindFromSymbol(p); - return propKind !== SyntaxKind.MethodDeclaration && propKind !== SyntaxKind.MethodSignature; - }); - if (hasNonMethodDeclaration) { + if (symbolHasNonMethodDeclaration(prop)) { error(errorNode, Diagnostics.Only_public_and_protected_methods_of_the_base_class_are_accessible_via_the_super_keyword); return false; } @@ -14845,6 +14841,17 @@ namespace ts { } } + // Referencing Abstract Properties within Constructors is not allowed + if ((flags & ModifierFlags.Abstract) && symbolHasNonMethodDeclaration(prop)) { + const declaringClassDeclaration = getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop)); + const declaringClassConstructor = declaringClassDeclaration && findConstructorDeclaration(declaringClassDeclaration); + + if (declaringClassConstructor && isNodeWithinFunction(node, declaringClassConstructor)) { + error(errorNode, Diagnostics.Abstract_property_0_in_class_1_cannot_be_accessed_in_constructor, symbolToString(prop), typeToString(getDeclaringClass(prop))); + return false; + } + } + // Public properties are otherwise accessible. if (!(flags & ModifierFlags.NonPublicAccessibilityModifier)) { return true; @@ -14896,6 +14903,13 @@ namespace ts { return true; } + function symbolHasNonMethodDeclaration(symbol: Symbol) { + return forEachProperty(symbol, prop => { + const propKind = getDeclarationKindFromSymbol(prop); + return propKind !== SyntaxKind.MethodDeclaration && propKind !== SyntaxKind.MethodSignature; + }); + } + function checkNonNullExpression(node: Expression | QualifiedName) { return checkNonNullType(checkExpression(node), node); } @@ -23139,6 +23153,10 @@ namespace ts { return result; } + function isNodeWithinFunction(node: Node, functionDeclaration: FunctionLike) { + return getContainingFunction(node) === functionDeclaration; + } + function isNodeWithinClass(node: Node, classDeclaration: ClassLikeDeclaration) { return !!forEachEnclosingClass(node, n => n === classDeclaration); } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index f3d6d4fcc47..e2d514ba268 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2220,6 +2220,10 @@ "category": "Error", "code": 2714 }, + "Abstract property '{0}' in class '{1}' cannot be accessed in constructor.": { + "category": "Error", + "code": 2715 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", diff --git a/tests/baselines/reference/abstractPropertyInConstructor.errors.txt b/tests/baselines/reference/abstractPropertyInConstructor.errors.txt new file mode 100644 index 00000000000..7e654b440c6 --- /dev/null +++ b/tests/baselines/reference/abstractPropertyInConstructor.errors.txt @@ -0,0 +1,25 @@ +tests/cases/compiler/abstractPropertyInConstructor.ts(4,24): error TS2715: Abstract property 'prop' in class 'AbstractClass' cannot be accessed in constructor. +tests/cases/compiler/abstractPropertyInConstructor.ts(5,14): error TS2715: Abstract property 'prop' in class 'AbstractClass' cannot be accessed in constructor. + + +==== tests/cases/compiler/abstractPropertyInConstructor.ts (2 errors) ==== + abstract class AbstractClass { + constructor(str: string) { + this.method(parseInt(str)); + let val = this.prop.toLowerCase(); + ~~~~ +!!! error TS2715: Abstract property 'prop' in class 'AbstractClass' cannot be accessed in constructor. + this.prop = "Hello World"; + ~~~~ +!!! error TS2715: Abstract property 'prop' in class 'AbstractClass' cannot be accessed in constructor. + } + + abstract prop: string; + + abstract method(num: number): void; + + method2() { + this.prop = this.prop + "!"; + } + } + \ No newline at end of file diff --git a/tests/baselines/reference/abstractPropertyInConstructor.js b/tests/baselines/reference/abstractPropertyInConstructor.js new file mode 100644 index 00000000000..c6d7de6c037 --- /dev/null +++ b/tests/baselines/reference/abstractPropertyInConstructor.js @@ -0,0 +1,30 @@ +//// [abstractPropertyInConstructor.ts] +abstract class AbstractClass { + constructor(str: string) { + this.method(parseInt(str)); + let val = this.prop.toLowerCase(); + this.prop = "Hello World"; + } + + abstract prop: string; + + abstract method(num: number): void; + + method2() { + this.prop = this.prop + "!"; + } +} + + +//// [abstractPropertyInConstructor.js] +var AbstractClass = /** @class */ (function () { + function AbstractClass(str) { + this.method(parseInt(str)); + var val = this.prop.toLowerCase(); + this.prop = "Hello World"; + } + AbstractClass.prototype.method2 = function () { + this.prop = this.prop + "!"; + }; + return AbstractClass; +}()); diff --git a/tests/baselines/reference/abstractPropertyInConstructor.symbols b/tests/baselines/reference/abstractPropertyInConstructor.symbols new file mode 100644 index 00000000000..7d634f80267 --- /dev/null +++ b/tests/baselines/reference/abstractPropertyInConstructor.symbols @@ -0,0 +1,48 @@ +=== tests/cases/compiler/abstractPropertyInConstructor.ts === +abstract class AbstractClass { +>AbstractClass : Symbol(AbstractClass, Decl(abstractPropertyInConstructor.ts, 0, 0)) + + constructor(str: string) { +>str : Symbol(str, Decl(abstractPropertyInConstructor.ts, 1, 16)) + + this.method(parseInt(str)); +>this.method : Symbol(AbstractClass.method, Decl(abstractPropertyInConstructor.ts, 7, 26)) +>this : Symbol(AbstractClass, Decl(abstractPropertyInConstructor.ts, 0, 0)) +>method : Symbol(AbstractClass.method, Decl(abstractPropertyInConstructor.ts, 7, 26)) +>parseInt : Symbol(parseInt, Decl(lib.d.ts, --, --)) +>str : Symbol(str, Decl(abstractPropertyInConstructor.ts, 1, 16)) + + let val = this.prop.toLowerCase(); +>val : Symbol(val, Decl(abstractPropertyInConstructor.ts, 3, 11)) +>this.prop.toLowerCase : Symbol(String.toLowerCase, Decl(lib.d.ts, --, --)) +>this.prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 5, 5)) +>this : Symbol(AbstractClass, Decl(abstractPropertyInConstructor.ts, 0, 0)) +>prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 5, 5)) +>toLowerCase : Symbol(String.toLowerCase, Decl(lib.d.ts, --, --)) + + this.prop = "Hello World"; +>this.prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 5, 5)) +>this : Symbol(AbstractClass, Decl(abstractPropertyInConstructor.ts, 0, 0)) +>prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 5, 5)) + } + + abstract prop: string; +>prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 5, 5)) + + abstract method(num: number): void; +>method : Symbol(AbstractClass.method, Decl(abstractPropertyInConstructor.ts, 7, 26)) +>num : Symbol(num, Decl(abstractPropertyInConstructor.ts, 9, 20)) + + method2() { +>method2 : Symbol(AbstractClass.method2, Decl(abstractPropertyInConstructor.ts, 9, 39)) + + this.prop = this.prop + "!"; +>this.prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 5, 5)) +>this : Symbol(AbstractClass, Decl(abstractPropertyInConstructor.ts, 0, 0)) +>prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 5, 5)) +>this.prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 5, 5)) +>this : Symbol(AbstractClass, Decl(abstractPropertyInConstructor.ts, 0, 0)) +>prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 5, 5)) + } +} + diff --git a/tests/baselines/reference/abstractPropertyInConstructor.types b/tests/baselines/reference/abstractPropertyInConstructor.types new file mode 100644 index 00000000000..05f7a7752e6 --- /dev/null +++ b/tests/baselines/reference/abstractPropertyInConstructor.types @@ -0,0 +1,56 @@ +=== tests/cases/compiler/abstractPropertyInConstructor.ts === +abstract class AbstractClass { +>AbstractClass : AbstractClass + + constructor(str: string) { +>str : string + + this.method(parseInt(str)); +>this.method(parseInt(str)) : void +>this.method : (num: number) => void +>this : this +>method : (num: number) => void +>parseInt(str) : number +>parseInt : (s: string, radix?: number) => number +>str : string + + let val = this.prop.toLowerCase(); +>val : string +>this.prop.toLowerCase() : string +>this.prop.toLowerCase : () => string +>this.prop : string +>this : this +>prop : string +>toLowerCase : () => string + + this.prop = "Hello World"; +>this.prop = "Hello World" : "Hello World" +>this.prop : string +>this : this +>prop : string +>"Hello World" : "Hello World" + } + + abstract prop: string; +>prop : string + + abstract method(num: number): void; +>method : (num: number) => void +>num : number + + method2() { +>method2 : () => void + + this.prop = this.prop + "!"; +>this.prop = this.prop + "!" : string +>this.prop : string +>this : this +>prop : string +>this.prop + "!" : string +>this.prop : string +>this : this +>prop : string +>"!" : "!" + } +} + diff --git a/tests/cases/compiler/abstractPropertyInConstructor.ts b/tests/cases/compiler/abstractPropertyInConstructor.ts new file mode 100644 index 00000000000..5376aae9d6f --- /dev/null +++ b/tests/cases/compiler/abstractPropertyInConstructor.ts @@ -0,0 +1,15 @@ +abstract class AbstractClass { + constructor(str: string) { + this.method(parseInt(str)); + let val = this.prop.toLowerCase(); + this.prop = "Hello World"; + } + + abstract prop: string; + + abstract method(num: number): void; + + method2() { + this.prop = this.prop + "!"; + } +} From 8a55baf9a3caf88c7e89e563ec51466644981df4 Mon Sep 17 00:00:00 2001 From: Andy Date: Mon, 9 Oct 2017 09:58:02 -0700 Subject: [PATCH 41/54] In @typedef tag, handle property with no type (#19013) --- src/compiler/binder.ts | 2 +- src/compiler/parser.ts | 2 +- src/compiler/types.ts | 2 +- tests/baselines/reference/api/tsserverlibrary.d.ts | 2 +- tests/baselines/reference/api/typescript.d.ts | 2 +- .../jsdocTypedef_propertyWithNoType.symbols | 11 +++++++++++ .../reference/jsdocTypedef_propertyWithNoType.types | 13 +++++++++++++ .../compiler/jsdocTypedef_propertyWithNoType.ts | 12 ++++++++++++ 8 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 tests/baselines/reference/jsdocTypedef_propertyWithNoType.symbols create mode 100644 tests/baselines/reference/jsdocTypedef_propertyWithNoType.types create mode 100644 tests/cases/compiler/jsdocTypedef_propertyWithNoType.ts diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 28193535ce9..9b977eb6bfc 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -2146,7 +2146,7 @@ namespace ts { // falls through case SyntaxKind.JSDocPropertyTag: const propTag = node as JSDocPropertyLikeTag; - const flags = propTag.isBracketed || propTag.typeExpression.type.kind === SyntaxKind.JSDocOptionalType ? + const flags = propTag.isBracketed || propTag.typeExpression && propTag.typeExpression.type.kind === SyntaxKind.JSDocOptionalType ? SymbolFlags.Property | SymbolFlags.Optional : SymbolFlags.Property; return declareSymbolAndAddToSymbolTable(propTag, flags, SymbolFlags.PropertyExcludes); diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index ebdf390f5b2..db6c5f23cfd 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -6699,7 +6699,7 @@ namespace ts { if (typeExpression && typeExpression.type.kind === SyntaxKind.ArrayType) { jsdocTypeLiteral.isArrayType = true; } - typedefTag.typeExpression = childTypeTag && !isObjectOrObjectArrayTypeReference(childTypeTag.typeExpression.type) ? + typedefTag.typeExpression = childTypeTag && childTypeTag.typeExpression && !isObjectOrObjectArrayTypeReference(childTypeTag.typeExpression.type) ? childTypeTag.typeExpression : finishNode(jsdocTypeLiteral); } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 578c2d23c4f..f1d4740ccac 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2194,7 +2194,7 @@ namespace ts { export interface JSDocPropertyLikeTag extends JSDocTag, Declaration { parent: JSDoc; name: EntityName; - typeExpression: JSDocTypeExpression; + typeExpression?: JSDocTypeExpression; /** Whether the property name came before the type -- non-standard for JSDoc, but Typescript-like */ isNameFirst: boolean; isBracketed: boolean; diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 3bb2ed11674..7fe07813adc 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -1473,7 +1473,7 @@ declare namespace ts { interface JSDocPropertyLikeTag extends JSDocTag, Declaration { parent: JSDoc; name: EntityName; - typeExpression: JSDocTypeExpression; + typeExpression?: JSDocTypeExpression; /** Whether the property name came before the type -- non-standard for JSDoc, but Typescript-like */ isNameFirst: boolean; isBracketed: boolean; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index d41db2eb413..a5bb674d511 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -1473,7 +1473,7 @@ declare namespace ts { interface JSDocPropertyLikeTag extends JSDocTag, Declaration { parent: JSDoc; name: EntityName; - typeExpression: JSDocTypeExpression; + typeExpression?: JSDocTypeExpression; /** Whether the property name came before the type -- non-standard for JSDoc, but Typescript-like */ isNameFirst: boolean; isBracketed: boolean; diff --git a/tests/baselines/reference/jsdocTypedef_propertyWithNoType.symbols b/tests/baselines/reference/jsdocTypedef_propertyWithNoType.symbols new file mode 100644 index 00000000000..0bac06e2e61 --- /dev/null +++ b/tests/baselines/reference/jsdocTypedef_propertyWithNoType.symbols @@ -0,0 +1,11 @@ +=== /a.js === +/** + * @typedef Foo + * @property foo + */ + +/** @type {Foo} */ +const x = { foo: 0 }; +>x : Symbol(x, Decl(a.js, 6, 5)) +>foo : Symbol(foo, Decl(a.js, 6, 11)) + diff --git a/tests/baselines/reference/jsdocTypedef_propertyWithNoType.types b/tests/baselines/reference/jsdocTypedef_propertyWithNoType.types new file mode 100644 index 00000000000..7d7326fc80c --- /dev/null +++ b/tests/baselines/reference/jsdocTypedef_propertyWithNoType.types @@ -0,0 +1,13 @@ +=== /a.js === +/** + * @typedef Foo + * @property foo + */ + +/** @type {Foo} */ +const x = { foo: 0 }; +>x : { foo: any; } +>{ foo: 0 } : { foo: number; } +>foo : number +>0 : 0 + diff --git a/tests/cases/compiler/jsdocTypedef_propertyWithNoType.ts b/tests/cases/compiler/jsdocTypedef_propertyWithNoType.ts new file mode 100644 index 00000000000..bcabe980915 --- /dev/null +++ b/tests/cases/compiler/jsdocTypedef_propertyWithNoType.ts @@ -0,0 +1,12 @@ +// @allowJs: true +// @checkJs: true +// @noEmit: true + +// @Filename: /a.js +/** + * @typedef Foo + * @property foo + */ + +/** @type {Foo} */ +const x = { foo: 0 }; From 79f5d968a120e469dbaf432aaad101a708989a6d Mon Sep 17 00:00:00 2001 From: Charles Pierce Date: Mon, 9 Oct 2017 10:57:08 -0700 Subject: [PATCH 42/54] Use ancestor walk to determine if property access is within constructor #9230 --- src/compiler/checker.ts | 17 ++++-- src/compiler/diagnosticMessages.json | 2 +- .../abstractPropertyInConstructor.errors.txt | 26 +++++++--- .../abstractPropertyInConstructor.js | 20 ++++++- .../abstractPropertyInConstructor.symbols | 52 +++++++++++++------ .../abstractPropertyInConstructor.types | 27 +++++++++- .../compiler/abstractPropertyInConstructor.ts | 11 +++- 7 files changed, 123 insertions(+), 32 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9a483ae50ed..8afee9455cf 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14844,10 +14844,9 @@ namespace ts { // Referencing Abstract Properties within Constructors is not allowed if ((flags & ModifierFlags.Abstract) && symbolHasNonMethodDeclaration(prop)) { const declaringClassDeclaration = getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop)); - const declaringClassConstructor = declaringClassDeclaration && findConstructorDeclaration(declaringClassDeclaration); - if (declaringClassConstructor && isNodeWithinFunction(node, declaringClassConstructor)) { - error(errorNode, Diagnostics.Abstract_property_0_in_class_1_cannot_be_accessed_in_constructor, symbolToString(prop), typeToString(getDeclaringClass(prop))); + if (declaringClassDeclaration && isNodeWithinConstructor(node, declaringClassDeclaration)) { + error(errorNode, Diagnostics.Abstract_property_0_in_class_1_cannot_be_accessed_in_the_constructor, symbolToString(prop), typeToString(getDeclaringClass(prop))); return false; } } @@ -23153,8 +23152,16 @@ namespace ts { return result; } - function isNodeWithinFunction(node: Node, functionDeclaration: FunctionLike) { - return getContainingFunction(node) === functionDeclaration; + function isNodeWithinConstructor(node: Node, classDeclaration: ClassLikeDeclaration) { + return findAncestor(node, element => { + if (isConstructorDeclaration(element) && nodeIsPresent(element.body)) { + return true; + } else if (element === classDeclaration || isFunctionLikeDeclaration(element)) { + return "quit"; + } + + return false; + }); } function isNodeWithinClass(node: Node, classDeclaration: ClassLikeDeclaration) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index e2d514ba268..9b220a880b0 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2220,7 +2220,7 @@ "category": "Error", "code": 2714 }, - "Abstract property '{0}' in class '{1}' cannot be accessed in constructor.": { + "Abstract property '{0}' in class '{1}' cannot be accessed in the constructor.": { "category": "Error", "code": 2715 }, diff --git a/tests/baselines/reference/abstractPropertyInConstructor.errors.txt b/tests/baselines/reference/abstractPropertyInConstructor.errors.txt index 7e654b440c6..461dd713d3b 100644 --- a/tests/baselines/reference/abstractPropertyInConstructor.errors.txt +++ b/tests/baselines/reference/abstractPropertyInConstructor.errors.txt @@ -1,20 +1,32 @@ -tests/cases/compiler/abstractPropertyInConstructor.ts(4,24): error TS2715: Abstract property 'prop' in class 'AbstractClass' cannot be accessed in constructor. -tests/cases/compiler/abstractPropertyInConstructor.ts(5,14): error TS2715: Abstract property 'prop' in class 'AbstractClass' cannot be accessed in constructor. +tests/cases/compiler/abstractPropertyInConstructor.ts(4,24): error TS2715: Abstract property 'prop' in class 'AbstractClass' cannot be accessed in the constructor. +tests/cases/compiler/abstractPropertyInConstructor.ts(7,18): error TS2715: Abstract property 'prop' in class 'AbstractClass' cannot be accessed in the constructor. +tests/cases/compiler/abstractPropertyInConstructor.ts(9,14): error TS2715: Abstract property 'cb' in class 'AbstractClass' cannot be accessed in the constructor. -==== tests/cases/compiler/abstractPropertyInConstructor.ts (2 errors) ==== +==== tests/cases/compiler/abstractPropertyInConstructor.ts (3 errors) ==== abstract class AbstractClass { constructor(str: string) { this.method(parseInt(str)); let val = this.prop.toLowerCase(); ~~~~ -!!! error TS2715: Abstract property 'prop' in class 'AbstractClass' cannot be accessed in constructor. - this.prop = "Hello World"; - ~~~~ -!!! error TS2715: Abstract property 'prop' in class 'AbstractClass' cannot be accessed in constructor. +!!! error TS2715: Abstract property 'prop' in class 'AbstractClass' cannot be accessed in the constructor. + + if (!str) { + this.prop = "Hello World"; + ~~~~ +!!! error TS2715: Abstract property 'prop' in class 'AbstractClass' cannot be accessed in the constructor. + } + this.cb(str); + ~~ +!!! error TS2715: Abstract property 'cb' in class 'AbstractClass' cannot be accessed in the constructor. + + const innerFunction = () => { + return this.prop; + } } abstract prop: string; + abstract cb: (s: string) => void; abstract method(num: number): void; diff --git a/tests/baselines/reference/abstractPropertyInConstructor.js b/tests/baselines/reference/abstractPropertyInConstructor.js index c6d7de6c037..18a2937a191 100644 --- a/tests/baselines/reference/abstractPropertyInConstructor.js +++ b/tests/baselines/reference/abstractPropertyInConstructor.js @@ -3,10 +3,19 @@ abstract class AbstractClass { constructor(str: string) { this.method(parseInt(str)); let val = this.prop.toLowerCase(); - this.prop = "Hello World"; + + if (!str) { + this.prop = "Hello World"; + } + this.cb(str); + + const innerFunction = () => { + return this.prop; + } } abstract prop: string; + abstract cb: (s: string) => void; abstract method(num: number): void; @@ -19,9 +28,16 @@ abstract class AbstractClass { //// [abstractPropertyInConstructor.js] var AbstractClass = /** @class */ (function () { function AbstractClass(str) { + var _this = this; this.method(parseInt(str)); var val = this.prop.toLowerCase(); - this.prop = "Hello World"; + if (!str) { + this.prop = "Hello World"; + } + this.cb(str); + var innerFunction = function () { + return _this.prop; + }; } AbstractClass.prototype.method2 = function () { this.prop = this.prop + "!"; diff --git a/tests/baselines/reference/abstractPropertyInConstructor.symbols b/tests/baselines/reference/abstractPropertyInConstructor.symbols index 7d634f80267..0d542ffb0a8 100644 --- a/tests/baselines/reference/abstractPropertyInConstructor.symbols +++ b/tests/baselines/reference/abstractPropertyInConstructor.symbols @@ -6,43 +6,65 @@ abstract class AbstractClass { >str : Symbol(str, Decl(abstractPropertyInConstructor.ts, 1, 16)) this.method(parseInt(str)); ->this.method : Symbol(AbstractClass.method, Decl(abstractPropertyInConstructor.ts, 7, 26)) +>this.method : Symbol(AbstractClass.method, Decl(abstractPropertyInConstructor.ts, 16, 37)) >this : Symbol(AbstractClass, Decl(abstractPropertyInConstructor.ts, 0, 0)) ->method : Symbol(AbstractClass.method, Decl(abstractPropertyInConstructor.ts, 7, 26)) +>method : Symbol(AbstractClass.method, Decl(abstractPropertyInConstructor.ts, 16, 37)) >parseInt : Symbol(parseInt, Decl(lib.d.ts, --, --)) >str : Symbol(str, Decl(abstractPropertyInConstructor.ts, 1, 16)) let val = this.prop.toLowerCase(); >val : Symbol(val, Decl(abstractPropertyInConstructor.ts, 3, 11)) >this.prop.toLowerCase : Symbol(String.toLowerCase, Decl(lib.d.ts, --, --)) ->this.prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 5, 5)) +>this.prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 13, 5)) >this : Symbol(AbstractClass, Decl(abstractPropertyInConstructor.ts, 0, 0)) ->prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 5, 5)) +>prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 13, 5)) >toLowerCase : Symbol(String.toLowerCase, Decl(lib.d.ts, --, --)) - this.prop = "Hello World"; ->this.prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 5, 5)) + if (!str) { +>str : Symbol(str, Decl(abstractPropertyInConstructor.ts, 1, 16)) + + this.prop = "Hello World"; +>this.prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 13, 5)) >this : Symbol(AbstractClass, Decl(abstractPropertyInConstructor.ts, 0, 0)) ->prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 5, 5)) +>prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 13, 5)) + } + this.cb(str); +>this.cb : Symbol(AbstractClass.cb, Decl(abstractPropertyInConstructor.ts, 15, 26)) +>this : Symbol(AbstractClass, Decl(abstractPropertyInConstructor.ts, 0, 0)) +>cb : Symbol(AbstractClass.cb, Decl(abstractPropertyInConstructor.ts, 15, 26)) +>str : Symbol(str, Decl(abstractPropertyInConstructor.ts, 1, 16)) + + const innerFunction = () => { +>innerFunction : Symbol(innerFunction, Decl(abstractPropertyInConstructor.ts, 10, 13)) + + return this.prop; +>this.prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 13, 5)) +>this : Symbol(AbstractClass, Decl(abstractPropertyInConstructor.ts, 0, 0)) +>prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 13, 5)) + } } abstract prop: string; ->prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 5, 5)) +>prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 13, 5)) + + abstract cb: (s: string) => void; +>cb : Symbol(AbstractClass.cb, Decl(abstractPropertyInConstructor.ts, 15, 26)) +>s : Symbol(s, Decl(abstractPropertyInConstructor.ts, 16, 18)) abstract method(num: number): void; ->method : Symbol(AbstractClass.method, Decl(abstractPropertyInConstructor.ts, 7, 26)) ->num : Symbol(num, Decl(abstractPropertyInConstructor.ts, 9, 20)) +>method : Symbol(AbstractClass.method, Decl(abstractPropertyInConstructor.ts, 16, 37)) +>num : Symbol(num, Decl(abstractPropertyInConstructor.ts, 18, 20)) method2() { ->method2 : Symbol(AbstractClass.method2, Decl(abstractPropertyInConstructor.ts, 9, 39)) +>method2 : Symbol(AbstractClass.method2, Decl(abstractPropertyInConstructor.ts, 18, 39)) this.prop = this.prop + "!"; ->this.prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 5, 5)) +>this.prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 13, 5)) >this : Symbol(AbstractClass, Decl(abstractPropertyInConstructor.ts, 0, 0)) ->prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 5, 5)) ->this.prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 5, 5)) +>prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 13, 5)) +>this.prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 13, 5)) >this : Symbol(AbstractClass, Decl(abstractPropertyInConstructor.ts, 0, 0)) ->prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 5, 5)) +>prop : Symbol(AbstractClass.prop, Decl(abstractPropertyInConstructor.ts, 13, 5)) } } diff --git a/tests/baselines/reference/abstractPropertyInConstructor.types b/tests/baselines/reference/abstractPropertyInConstructor.types index 05f7a7752e6..0ffb5f1bdfd 100644 --- a/tests/baselines/reference/abstractPropertyInConstructor.types +++ b/tests/baselines/reference/abstractPropertyInConstructor.types @@ -23,17 +23,42 @@ abstract class AbstractClass { >prop : string >toLowerCase : () => string - this.prop = "Hello World"; + if (!str) { +>!str : boolean +>str : string + + this.prop = "Hello World"; >this.prop = "Hello World" : "Hello World" >this.prop : string >this : this >prop : string >"Hello World" : "Hello World" + } + this.cb(str); +>this.cb(str) : void +>this.cb : (s: string) => void +>this : this +>cb : (s: string) => void +>str : string + + const innerFunction = () => { +>innerFunction : () => string +>() => { return this.prop; } : () => string + + return this.prop; +>this.prop : string +>this : this +>prop : string + } } abstract prop: string; >prop : string + abstract cb: (s: string) => void; +>cb : (s: string) => void +>s : string + abstract method(num: number): void; >method : (num: number) => void >num : number diff --git a/tests/cases/compiler/abstractPropertyInConstructor.ts b/tests/cases/compiler/abstractPropertyInConstructor.ts index 5376aae9d6f..457fdb473b1 100644 --- a/tests/cases/compiler/abstractPropertyInConstructor.ts +++ b/tests/cases/compiler/abstractPropertyInConstructor.ts @@ -2,10 +2,19 @@ abstract class AbstractClass { constructor(str: string) { this.method(parseInt(str)); let val = this.prop.toLowerCase(); - this.prop = "Hello World"; + + if (!str) { + this.prop = "Hello World"; + } + this.cb(str); + + const innerFunction = () => { + return this.prop; + } } abstract prop: string; + abstract cb: (s: string) => void; abstract method(num: number): void; From 2796ebfe35ca08518ed196b522181bc0deff2373 Mon Sep 17 00:00:00 2001 From: Andy Date: Mon, 9 Oct 2017 11:04:28 -0700 Subject: [PATCH 43/54] In resolveNameHelper, use a lastNonBlockLocation (#18918) --- src/compiler/checker.ts | 6 ++- .../noUnusedLocals_selfReference.errors.txt | 16 ++++-- .../reference/noUnusedLocals_selfReference.js | 14 ++++- .../noUnusedLocals_selfReference.symbols | 53 +++++++++++-------- .../noUnusedLocals_selfReference.types | 13 ++++- .../compiler/noUnusedLocals_selfReference.ts | 7 ++- 6 files changed, 77 insertions(+), 32 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a8e18c432a6..58ac3bc52c0 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -908,6 +908,7 @@ namespace ts { const originalLocation = location; // needed for did-you-mean error reporting, which gathers candidates starting from the original location let result: Symbol; let lastLocation: Node; + let lastNonBlockLocation: Node; let propertyWithInvalidInitializer: Node; const errorLocation = location; let grandparent: Node; @@ -1126,6 +1127,9 @@ namespace ts { } break; } + if (location.kind !== SyntaxKind.Block) { + lastNonBlockLocation = location; + } lastLocation = location; location = location.parent; } @@ -1133,7 +1137,7 @@ namespace ts { // We just climbed up parents looking for the name, meaning that we started in a descendant node of `lastLocation`. // If `result === lastLocation.symbol`, that means that we are somewhere inside `lastLocation` looking up a name, and resolving to `lastLocation` itself. // That means that this is a self-reference of `lastLocation`, and shouldn't count this when considering whether `lastLocation` is used. - if (isUse && result && nameNotFoundMessage && noUnusedIdentifiers && result !== lastLocation.symbol) { + if (isUse && result && nameNotFoundMessage && noUnusedIdentifiers && result !== lastNonBlockLocation.symbol) { result.isReferenced = true; } diff --git a/tests/baselines/reference/noUnusedLocals_selfReference.errors.txt b/tests/baselines/reference/noUnusedLocals_selfReference.errors.txt index e4a0d478cb4..603bc54d448 100644 --- a/tests/baselines/reference/noUnusedLocals_selfReference.errors.txt +++ b/tests/baselines/reference/noUnusedLocals_selfReference.errors.txt @@ -1,14 +1,22 @@ tests/cases/compiler/noUnusedLocals_selfReference.ts(3,10): error TS6133: 'f' is declared but its value is never read. -tests/cases/compiler/noUnusedLocals_selfReference.ts(4,7): error TS6133: 'C' is declared but its value is never read. -tests/cases/compiler/noUnusedLocals_selfReference.ts(7,6): error TS6133: 'E' is declared but its value is never read. +tests/cases/compiler/noUnusedLocals_selfReference.ts(5,14): error TS6133: 'g' is declared but its value is never read. +tests/cases/compiler/noUnusedLocals_selfReference.ts(9,7): error TS6133: 'C' is declared but its value is never read. +tests/cases/compiler/noUnusedLocals_selfReference.ts(12,6): error TS6133: 'E' is declared but its value is never read. -==== tests/cases/compiler/noUnusedLocals_selfReference.ts (3 errors) ==== +==== tests/cases/compiler/noUnusedLocals_selfReference.ts (4 errors) ==== export {}; // Make this a module scope, so these are local variables. - function f() { f; } + function f() { ~ !!! error TS6133: 'f' is declared but its value is never read. + f; + function g() { + ~ +!!! error TS6133: 'g' is declared but its value is never read. + g; + } + } class C { ~ !!! error TS6133: 'C' is declared but its value is never read. diff --git a/tests/baselines/reference/noUnusedLocals_selfReference.js b/tests/baselines/reference/noUnusedLocals_selfReference.js index 5f206fbc3dc..a8f3d6a8aed 100644 --- a/tests/baselines/reference/noUnusedLocals_selfReference.js +++ b/tests/baselines/reference/noUnusedLocals_selfReference.js @@ -1,7 +1,12 @@ //// [noUnusedLocals_selfReference.ts] export {}; // Make this a module scope, so these are local variables. -function f() { f; } +function f() { + f; + function g() { + g; + } +} class C { m() { C; } } @@ -19,7 +24,12 @@ P; //// [noUnusedLocals_selfReference.js] "use strict"; exports.__esModule = true; -function f() { f; } +function f() { + f; + function g() { + g; + } +} var C = /** @class */ (function () { function C() { } diff --git a/tests/baselines/reference/noUnusedLocals_selfReference.symbols b/tests/baselines/reference/noUnusedLocals_selfReference.symbols index dcd815b2619..015a78d87d3 100644 --- a/tests/baselines/reference/noUnusedLocals_selfReference.symbols +++ b/tests/baselines/reference/noUnusedLocals_selfReference.symbols @@ -1,43 +1,52 @@ === tests/cases/compiler/noUnusedLocals_selfReference.ts === export {}; // Make this a module scope, so these are local variables. -function f() { f; } ->f : Symbol(f, Decl(noUnusedLocals_selfReference.ts, 0, 10)) +function f() { >f : Symbol(f, Decl(noUnusedLocals_selfReference.ts, 0, 10)) + f; +>f : Symbol(f, Decl(noUnusedLocals_selfReference.ts, 0, 10)) + + function g() { +>g : Symbol(g, Decl(noUnusedLocals_selfReference.ts, 3, 6)) + + g; +>g : Symbol(g, Decl(noUnusedLocals_selfReference.ts, 3, 6)) + } +} class C { ->C : Symbol(C, Decl(noUnusedLocals_selfReference.ts, 2, 19)) +>C : Symbol(C, Decl(noUnusedLocals_selfReference.ts, 7, 1)) m() { C; } ->m : Symbol(C.m, Decl(noUnusedLocals_selfReference.ts, 3, 9)) ->C : Symbol(C, Decl(noUnusedLocals_selfReference.ts, 2, 19)) +>m : Symbol(C.m, Decl(noUnusedLocals_selfReference.ts, 8, 9)) +>C : Symbol(C, Decl(noUnusedLocals_selfReference.ts, 7, 1)) } enum E { A = 0, B = E.A } ->E : Symbol(E, Decl(noUnusedLocals_selfReference.ts, 5, 1)) ->A : Symbol(E.A, Decl(noUnusedLocals_selfReference.ts, 6, 8)) ->B : Symbol(E.B, Decl(noUnusedLocals_selfReference.ts, 6, 15)) ->E.A : Symbol(E.A, Decl(noUnusedLocals_selfReference.ts, 6, 8)) ->E : Symbol(E, Decl(noUnusedLocals_selfReference.ts, 5, 1)) ->A : Symbol(E.A, Decl(noUnusedLocals_selfReference.ts, 6, 8)) +>E : Symbol(E, Decl(noUnusedLocals_selfReference.ts, 10, 1)) +>A : Symbol(E.A, Decl(noUnusedLocals_selfReference.ts, 11, 8)) +>B : Symbol(E.B, Decl(noUnusedLocals_selfReference.ts, 11, 15)) +>E.A : Symbol(E.A, Decl(noUnusedLocals_selfReference.ts, 11, 8)) +>E : Symbol(E, Decl(noUnusedLocals_selfReference.ts, 10, 1)) +>A : Symbol(E.A, Decl(noUnusedLocals_selfReference.ts, 11, 8)) // Does not detect mutual recursion. function g() { D; } ->g : Symbol(g, Decl(noUnusedLocals_selfReference.ts, 6, 25)) ->D : Symbol(D, Decl(noUnusedLocals_selfReference.ts, 9, 19)) +>g : Symbol(g, Decl(noUnusedLocals_selfReference.ts, 11, 25)) +>D : Symbol(D, Decl(noUnusedLocals_selfReference.ts, 14, 19)) class D { m() { g; } } ->D : Symbol(D, Decl(noUnusedLocals_selfReference.ts, 9, 19)) ->m : Symbol(D.m, Decl(noUnusedLocals_selfReference.ts, 10, 9)) ->g : Symbol(g, Decl(noUnusedLocals_selfReference.ts, 6, 25)) +>D : Symbol(D, Decl(noUnusedLocals_selfReference.ts, 14, 19)) +>m : Symbol(D.m, Decl(noUnusedLocals_selfReference.ts, 15, 9)) +>g : Symbol(g, Decl(noUnusedLocals_selfReference.ts, 11, 25)) // Does not work on private methods. class P { private m() { this.m; } } ->P : Symbol(P, Decl(noUnusedLocals_selfReference.ts, 10, 22)) ->m : Symbol(P.m, Decl(noUnusedLocals_selfReference.ts, 13, 9)) ->this.m : Symbol(P.m, Decl(noUnusedLocals_selfReference.ts, 13, 9)) ->this : Symbol(P, Decl(noUnusedLocals_selfReference.ts, 10, 22)) ->m : Symbol(P.m, Decl(noUnusedLocals_selfReference.ts, 13, 9)) +>P : Symbol(P, Decl(noUnusedLocals_selfReference.ts, 15, 22)) +>m : Symbol(P.m, Decl(noUnusedLocals_selfReference.ts, 18, 9)) +>this.m : Symbol(P.m, Decl(noUnusedLocals_selfReference.ts, 18, 9)) +>this : Symbol(P, Decl(noUnusedLocals_selfReference.ts, 15, 22)) +>m : Symbol(P.m, Decl(noUnusedLocals_selfReference.ts, 18, 9)) P; ->P : Symbol(P, Decl(noUnusedLocals_selfReference.ts, 10, 22)) +>P : Symbol(P, Decl(noUnusedLocals_selfReference.ts, 15, 22)) diff --git a/tests/baselines/reference/noUnusedLocals_selfReference.types b/tests/baselines/reference/noUnusedLocals_selfReference.types index 7d2741c5681..7e75062db34 100644 --- a/tests/baselines/reference/noUnusedLocals_selfReference.types +++ b/tests/baselines/reference/noUnusedLocals_selfReference.types @@ -1,10 +1,19 @@ === tests/cases/compiler/noUnusedLocals_selfReference.ts === export {}; // Make this a module scope, so these are local variables. -function f() { f; } ->f : () => void +function f() { >f : () => void + f; +>f : () => void + + function g() { +>g : () => void + + g; +>g : () => void + } +} class C { >C : C diff --git a/tests/cases/compiler/noUnusedLocals_selfReference.ts b/tests/cases/compiler/noUnusedLocals_selfReference.ts index 8eb528743c0..fc6b02b6006 100644 --- a/tests/cases/compiler/noUnusedLocals_selfReference.ts +++ b/tests/cases/compiler/noUnusedLocals_selfReference.ts @@ -2,7 +2,12 @@ export {}; // Make this a module scope, so these are local variables. -function f() { f; } +function f() { + f; + function g() { + g; + } +} class C { m() { C; } } From 8486c482371e800fcaa83c655f837f70d8ac02af Mon Sep 17 00:00:00 2001 From: Charles Pierce Date: Mon, 9 Oct 2017 13:01:30 -0700 Subject: [PATCH 44/54] Fix linting error in new function --- src/compiler/checker.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8afee9455cf..26e851eb53d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -23156,7 +23156,8 @@ namespace ts { return findAncestor(node, element => { if (isConstructorDeclaration(element) && nodeIsPresent(element.body)) { return true; - } else if (element === classDeclaration || isFunctionLikeDeclaration(element)) { + } + else if (element === classDeclaration || isFunctionLikeDeclaration(element)) { return "quit"; } From 8b60736b61b8559fc7bd542a5e8954e258f51bd3 Mon Sep 17 00:00:00 2001 From: Andy Date: Mon, 9 Oct 2017 13:39:15 -0700 Subject: [PATCH 45/54] importFixes: Remove unnecessary undefined check (#19045) --- src/services/codefixes/importFixes.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/services/codefixes/importFixes.ts b/src/services/codefixes/importFixes.ts index f516bc15de3..9b54543231d 100644 --- a/src/services/codefixes/importFixes.ts +++ b/src/services/codefixes/importFixes.ts @@ -16,7 +16,7 @@ namespace ts.codefix { moduleSpecifier?: string; } - enum ModuleSpecifierComparison { + const enum ModuleSpecifierComparison { Better, Equal, Worse @@ -26,10 +26,6 @@ namespace ts.codefix { private symbolIdToActionMap: ImportCodeAction[][] = []; addAction(symbolId: number, newAction: ImportCodeAction) { - if (!newAction) { - return; - } - const actions = this.symbolIdToActionMap[symbolId]; if (!actions) { this.symbolIdToActionMap[symbolId] = [newAction]; From 5f3d6e753e0c89419bd3734ccf991c26ba772131 Mon Sep 17 00:00:00 2001 From: Arthur Ozga Date: Mon, 9 Oct 2017 14:43:51 -0700 Subject: [PATCH 46/54] update baselines --- tests/baselines/reference/api/tsserverlibrary.d.ts | 2 ++ tests/baselines/reference/api/typescript.d.ts | 2 ++ 2 files changed, 4 insertions(+) diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index ca8696b11fb..5f4ef1dbdfe 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -2870,6 +2870,8 @@ declare namespace ts { function getJSDocReturnType(node: Node): TypeNode | undefined; /** Get all JSDoc tags related to a node, including those on parent nodes. */ function getJSDocTags(node: Node): ReadonlyArray | undefined; + /** Gets all JSDoc tags of a specified kind, or undefined if not present. */ + function getAllJSDocTagsOfKind(node: Node, kind: SyntaxKind): ReadonlyArray | undefined; } declare namespace ts { function isNumericLiteral(node: Node): node is NumericLiteral; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 820be44f1f1..e608c7ffc3d 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2925,6 +2925,8 @@ declare namespace ts { function getJSDocReturnType(node: Node): TypeNode | undefined; /** Get all JSDoc tags related to a node, including those on parent nodes. */ function getJSDocTags(node: Node): ReadonlyArray | undefined; + /** Gets all JSDoc tags of a specified kind, or undefined if not present. */ + function getAllJSDocTagsOfKind(node: Node, kind: SyntaxKind): ReadonlyArray | undefined; } declare namespace ts { function isNumericLiteral(node: Node): node is NumericLiteral; From bb3467b8e1c2b7897bc30e282e59bd85a8b8c714 Mon Sep 17 00:00:00 2001 From: Joe Calzaretta Date: Mon, 9 Oct 2017 17:58:41 -0400 Subject: [PATCH 47/54] Handle type guard predicates on `Array.find` (#18160) * Handle type guard predicates on `Array.find` If the `predicate` function passed to `Array.find` or `ReadonlyArray.find` is a type guard narrowing `value` to type `S`, then any returned element should also be narrowed to `S`. Adding test case and associated baselines * trailing whitespace after merge conflict --- src/lib/es2015.core.d.ts | 2 + tests/baselines/reference/arrayFind.js | 22 ++++++++++ tests/baselines/reference/arrayFind.symbols | 33 +++++++++++++++ tests/baselines/reference/arrayFind.types | 46 +++++++++++++++++++++ tests/cases/compiler/arrayFind.ts | 12 ++++++ 5 files changed, 115 insertions(+) create mode 100644 tests/baselines/reference/arrayFind.js create mode 100644 tests/baselines/reference/arrayFind.symbols create mode 100644 tests/baselines/reference/arrayFind.types create mode 100644 tests/cases/compiler/arrayFind.ts diff --git a/src/lib/es2015.core.d.ts b/src/lib/es2015.core.d.ts index 5c2438d9052..9ea773e3eef 100644 --- a/src/lib/es2015.core.d.ts +++ b/src/lib/es2015.core.d.ts @@ -10,6 +10,7 @@ interface Array { * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. */ + find(predicate: (this: void, value: T, index: number, obj: T[]) => value is S, thisArg?: any): S | undefined; find(predicate: (value: T, index: number, obj: T[]) => boolean, thisArg?: any): T | undefined; /** @@ -350,6 +351,7 @@ interface ReadonlyArray { * @param thisArg If provided, it will be used as the this value for each invocation of * predicate. If it is not provided, undefined is used instead. */ + find(predicate: (this: void, value: T, index: number, obj: ReadonlyArray) => value is S, thisArg?: any): S | undefined; find(predicate: (value: T, index: number, obj: ReadonlyArray) => boolean, thisArg?: any): T | undefined; /** diff --git a/tests/baselines/reference/arrayFind.js b/tests/baselines/reference/arrayFind.js new file mode 100644 index 00000000000..1926c3a8dcc --- /dev/null +++ b/tests/baselines/reference/arrayFind.js @@ -0,0 +1,22 @@ +//// [arrayFind.ts] +// test fix for #18112, type guard predicates should narrow returned element +function isNumber(x: any): x is number { + return typeof x === "number"; +} + +const arrayOfStringsNumbersAndBooleans = ["string", false, 0, "strung", 1, true]; +const foundNumber: number | undefined = arrayOfStringsNumbersAndBooleans.find(isNumber); + +const readonlyArrayOfStringsNumbersAndBooleans = arrayOfStringsNumbersAndBooleans as ReadonlyArray; +const readonlyFoundNumber: number | undefined = readonlyArrayOfStringsNumbersAndBooleans.find(isNumber); + + +//// [arrayFind.js] +// test fix for #18112, type guard predicates should narrow returned element +function isNumber(x) { + return typeof x === "number"; +} +var arrayOfStringsNumbersAndBooleans = ["string", false, 0, "strung", 1, true]; +var foundNumber = arrayOfStringsNumbersAndBooleans.find(isNumber); +var readonlyArrayOfStringsNumbersAndBooleans = arrayOfStringsNumbersAndBooleans; +var readonlyFoundNumber = readonlyArrayOfStringsNumbersAndBooleans.find(isNumber); diff --git a/tests/baselines/reference/arrayFind.symbols b/tests/baselines/reference/arrayFind.symbols new file mode 100644 index 00000000000..163d5d818ba --- /dev/null +++ b/tests/baselines/reference/arrayFind.symbols @@ -0,0 +1,33 @@ +=== tests/cases/compiler/arrayFind.ts === +// test fix for #18112, type guard predicates should narrow returned element +function isNumber(x: any): x is number { +>isNumber : Symbol(isNumber, Decl(arrayFind.ts, 0, 0)) +>x : Symbol(x, Decl(arrayFind.ts, 1, 18)) +>x : Symbol(x, Decl(arrayFind.ts, 1, 18)) + + return typeof x === "number"; +>x : Symbol(x, Decl(arrayFind.ts, 1, 18)) +} + +const arrayOfStringsNumbersAndBooleans = ["string", false, 0, "strung", 1, true]; +>arrayOfStringsNumbersAndBooleans : Symbol(arrayOfStringsNumbersAndBooleans, Decl(arrayFind.ts, 5, 5)) + +const foundNumber: number | undefined = arrayOfStringsNumbersAndBooleans.find(isNumber); +>foundNumber : Symbol(foundNumber, Decl(arrayFind.ts, 6, 5)) +>arrayOfStringsNumbersAndBooleans.find : Symbol(Array.find, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --)) +>arrayOfStringsNumbersAndBooleans : Symbol(arrayOfStringsNumbersAndBooleans, Decl(arrayFind.ts, 5, 5)) +>find : Symbol(Array.find, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --)) +>isNumber : Symbol(isNumber, Decl(arrayFind.ts, 0, 0)) + +const readonlyArrayOfStringsNumbersAndBooleans = arrayOfStringsNumbersAndBooleans as ReadonlyArray; +>readonlyArrayOfStringsNumbersAndBooleans : Symbol(readonlyArrayOfStringsNumbersAndBooleans, Decl(arrayFind.ts, 8, 5)) +>arrayOfStringsNumbersAndBooleans : Symbol(arrayOfStringsNumbersAndBooleans, Decl(arrayFind.ts, 5, 5)) +>ReadonlyArray : Symbol(ReadonlyArray, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --)) + +const readonlyFoundNumber: number | undefined = readonlyArrayOfStringsNumbersAndBooleans.find(isNumber); +>readonlyFoundNumber : Symbol(readonlyFoundNumber, Decl(arrayFind.ts, 9, 5)) +>readonlyArrayOfStringsNumbersAndBooleans.find : Symbol(ReadonlyArray.find, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --)) +>readonlyArrayOfStringsNumbersAndBooleans : Symbol(readonlyArrayOfStringsNumbersAndBooleans, Decl(arrayFind.ts, 8, 5)) +>find : Symbol(ReadonlyArray.find, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --)) +>isNumber : Symbol(isNumber, Decl(arrayFind.ts, 0, 0)) + diff --git a/tests/baselines/reference/arrayFind.types b/tests/baselines/reference/arrayFind.types new file mode 100644 index 00000000000..5c0769cb606 --- /dev/null +++ b/tests/baselines/reference/arrayFind.types @@ -0,0 +1,46 @@ +=== tests/cases/compiler/arrayFind.ts === +// test fix for #18112, type guard predicates should narrow returned element +function isNumber(x: any): x is number { +>isNumber : (x: any) => x is number +>x : any +>x : any + + return typeof x === "number"; +>typeof x === "number" : boolean +>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function" +>x : any +>"number" : "number" +} + +const arrayOfStringsNumbersAndBooleans = ["string", false, 0, "strung", 1, true]; +>arrayOfStringsNumbersAndBooleans : (string | number | boolean)[] +>["string", false, 0, "strung", 1, true] : (string | number | boolean)[] +>"string" : "string" +>false : false +>0 : 0 +>"strung" : "strung" +>1 : 1 +>true : true + +const foundNumber: number | undefined = arrayOfStringsNumbersAndBooleans.find(isNumber); +>foundNumber : number +>arrayOfStringsNumbersAndBooleans.find(isNumber) : number +>arrayOfStringsNumbersAndBooleans.find : { (predicate: (this: void, value: string | number | boolean, index: number, obj: (string | number | boolean)[]) => value is S, thisArg?: any): S; (predicate: (value: string | number | boolean, index: number, obj: (string | number | boolean)[]) => boolean, thisArg?: any): string | number | boolean; } +>arrayOfStringsNumbersAndBooleans : (string | number | boolean)[] +>find : { (predicate: (this: void, value: string | number | boolean, index: number, obj: (string | number | boolean)[]) => value is S, thisArg?: any): S; (predicate: (value: string | number | boolean, index: number, obj: (string | number | boolean)[]) => boolean, thisArg?: any): string | number | boolean; } +>isNumber : (x: any) => x is number + +const readonlyArrayOfStringsNumbersAndBooleans = arrayOfStringsNumbersAndBooleans as ReadonlyArray; +>readonlyArrayOfStringsNumbersAndBooleans : ReadonlyArray +>arrayOfStringsNumbersAndBooleans as ReadonlyArray : ReadonlyArray +>arrayOfStringsNumbersAndBooleans : (string | number | boolean)[] +>ReadonlyArray : ReadonlyArray + +const readonlyFoundNumber: number | undefined = readonlyArrayOfStringsNumbersAndBooleans.find(isNumber); +>readonlyFoundNumber : number +>readonlyArrayOfStringsNumbersAndBooleans.find(isNumber) : number +>readonlyArrayOfStringsNumbersAndBooleans.find : { (predicate: (this: void, value: string | number | boolean, index: number, obj: ReadonlyArray) => value is S, thisArg?: any): S; (predicate: (value: string | number | boolean, index: number, obj: ReadonlyArray) => boolean, thisArg?: any): string | number | boolean; } +>readonlyArrayOfStringsNumbersAndBooleans : ReadonlyArray +>find : { (predicate: (this: void, value: string | number | boolean, index: number, obj: ReadonlyArray) => value is S, thisArg?: any): S; (predicate: (value: string | number | boolean, index: number, obj: ReadonlyArray) => boolean, thisArg?: any): string | number | boolean; } +>isNumber : (x: any) => x is number + diff --git a/tests/cases/compiler/arrayFind.ts b/tests/cases/compiler/arrayFind.ts new file mode 100644 index 00000000000..90883974766 --- /dev/null +++ b/tests/cases/compiler/arrayFind.ts @@ -0,0 +1,12 @@ +// @lib: es2015 + +// test fix for #18112, type guard predicates should narrow returned element +function isNumber(x: any): x is number { + return typeof x === "number"; +} + +const arrayOfStringsNumbersAndBooleans = ["string", false, 0, "strung", 1, true]; +const foundNumber: number | undefined = arrayOfStringsNumbersAndBooleans.find(isNumber); + +const readonlyArrayOfStringsNumbersAndBooleans = arrayOfStringsNumbersAndBooleans as ReadonlyArray; +const readonlyFoundNumber: number | undefined = readonlyArrayOfStringsNumbersAndBooleans.find(isNumber); From 661ecc241ebd2ac6f29f6cd7d37273e4b963be09 Mon Sep 17 00:00:00 2001 From: falsandtru Date: Tue, 10 Oct 2017 07:08:22 +0900 Subject: [PATCH 48/54] Improve Object.{values,entries} static methods (#18875) --- src/lib/es2017.object.d.ts | 4 ++-- .../useObjectValuesAndEntries1.types | 20 +++++++++---------- .../useObjectValuesAndEntries4.types | 8 ++++---- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/lib/es2017.object.d.ts b/src/lib/es2017.object.d.ts index 1d8a52da758..4014e8c2927 100644 --- a/src/lib/es2017.object.d.ts +++ b/src/lib/es2017.object.d.ts @@ -3,7 +3,7 @@ interface ObjectConstructor { * Returns an array of values of the enumerable properties of an object * @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object. */ - values(o: { [s: string]: T }): T[]; + values(o: { [s: string]: T } | { [n: number]: T }): T[]; /** * Returns an array of values of the enumerable properties of an object @@ -15,7 +15,7 @@ interface ObjectConstructor { * Returns an array of key/values of the enumerable properties of an object * @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object. */ - entries(o: { [s: string]: T }): [string, T][]; + entries(o: { [s: string]: T } | { [n: number]: T }): [string, T][]; /** * Returns an array of key/values of the enumerable properties of an object diff --git a/tests/baselines/reference/useObjectValuesAndEntries1.types b/tests/baselines/reference/useObjectValuesAndEntries1.types index 1b537ed063d..6ea45385bbc 100644 --- a/tests/baselines/reference/useObjectValuesAndEntries1.types +++ b/tests/baselines/reference/useObjectValuesAndEntries1.types @@ -10,9 +10,9 @@ var o = { a: 1, b: 2 }; for (var x of Object.values(o)) { >x : number >Object.values(o) : number[] ->Object.values : { (o: { [s: string]: T; }): T[]; (o: any): any[]; } +>Object.values : { (o: { [s: string]: T; } | { [n: number]: T; }): T[]; (o: any): any[]; } >Object : ObjectConstructor ->values : { (o: { [s: string]: T; }): T[]; (o: any): any[]; } +>values : { (o: { [s: string]: T; } | { [n: number]: T; }): T[]; (o: any): any[]; } >o : { a: number; b: number; } let y = x; @@ -23,25 +23,25 @@ for (var x of Object.values(o)) { var entries = Object.entries(o); // <-- entries: ['a' | 'b', number][] >entries : [string, number][] >Object.entries(o) : [string, number][] ->Object.entries : { (o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; } +>Object.entries : { (o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: any): [string, any][]; } >Object : ObjectConstructor ->entries : { (o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; } +>entries : { (o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: any): [string, any][]; } >o : { a: number; b: number; } var entries1 = Object.entries(1); // <-- entries: [string, any][] >entries1 : [string, any][] >Object.entries(1) : [string, any][] ->Object.entries : { (o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; } +>Object.entries : { (o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: any): [string, any][]; } >Object : ObjectConstructor ->entries : { (o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; } +>entries : { (o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: any): [string, any][]; } >1 : 1 var entries2 = Object.entries({a: true, b: 2}) // ['a' | 'b', number | boolean][] >entries2 : [string, number | boolean][] >Object.entries({a: true, b: 2}) : [string, number | boolean][] ->Object.entries : { (o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; } +>Object.entries : { (o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: any): [string, any][]; } >Object : ObjectConstructor ->entries : { (o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; } +>entries : { (o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: any): [string, any][]; } >{a: true, b: 2} : { a: true; b: 2; } >a : boolean >true : true @@ -51,8 +51,8 @@ var entries2 = Object.entries({a: true, b: 2}) // ['a' | 'b', number | boolean][ var entries3 = Object.entries({}) // [never, any][] >entries3 : [string, {}][] >Object.entries({}) : [string, {}][] ->Object.entries : { (o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; } +>Object.entries : { (o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: any): [string, any][]; } >Object : ObjectConstructor ->entries : { (o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; } +>entries : { (o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: any): [string, any][]; } >{} : {} diff --git a/tests/baselines/reference/useObjectValuesAndEntries4.types b/tests/baselines/reference/useObjectValuesAndEntries4.types index 85810bccd26..245803a24d4 100644 --- a/tests/baselines/reference/useObjectValuesAndEntries4.types +++ b/tests/baselines/reference/useObjectValuesAndEntries4.types @@ -10,9 +10,9 @@ var o = { a: 1, b: 2 }; for (var x of Object.values(o)) { >x : number >Object.values(o) : number[] ->Object.values : { (o: { [s: string]: T; }): T[]; (o: any): any[]; } +>Object.values : { (o: { [s: string]: T; } | { [n: number]: T; }): T[]; (o: any): any[]; } >Object : ObjectConstructor ->values : { (o: { [s: string]: T; }): T[]; (o: any): any[]; } +>values : { (o: { [s: string]: T; } | { [n: number]: T; }): T[]; (o: any): any[]; } >o : { a: number; b: number; } let y = x; @@ -23,8 +23,8 @@ for (var x of Object.values(o)) { var entries = Object.entries(o); >entries : [string, number][] >Object.entries(o) : [string, number][] ->Object.entries : { (o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; } +>Object.entries : { (o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: any): [string, any][]; } >Object : ObjectConstructor ->entries : { (o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; } +>entries : { (o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: any): [string, any][]; } >o : { a: number; b: number; } From 17a1cd069dc4d1f45f09f57186f94e45505b5ed6 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Mon, 9 Oct 2017 16:55:20 -0700 Subject: [PATCH 49/54] Add deprecation warning to getSymbolDisplayBuilder (#18953) * Add deprecation warning to getSymbolDisplayBuilder * Accept API baselines --- src/compiler/types.ts | 4 ++++ tests/baselines/reference/api/tsserverlibrary.d.ts | 4 ++++ tests/baselines/reference/api/typescript.d.ts | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 13a23b77a0a..67b8606df5e 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2653,6 +2653,10 @@ namespace ts { signatureToString(signature: Signature, enclosingDeclaration?: Node, flags?: TypeFormatFlags, kind?: SignatureKind): string; typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string; symbolToString(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags): string; + /** + * @deprecated Use the createX factory functions or XToY typechecker methods and `createPrinter` or the `xToString` methods instead + * This will be removed in a future version. + */ getSymbolDisplayBuilder(): SymbolDisplayBuilder; getFullyQualifiedName(symbol: Symbol): string; getAugmentedPropertiesOfType(type: Type): Symbol[]; diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index ce926dfc040..6da4262e0b7 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -1731,6 +1731,10 @@ declare namespace ts { signatureToString(signature: Signature, enclosingDeclaration?: Node, flags?: TypeFormatFlags, kind?: SignatureKind): string; typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string; symbolToString(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags): string; + /** + * @deprecated Use the createX factory functions or XToY typechecker methods and `createPrinter` or the `xToString` methods instead + * This will be removed in a future version. + */ getSymbolDisplayBuilder(): SymbolDisplayBuilder; getFullyQualifiedName(symbol: Symbol): string; getAugmentedPropertiesOfType(type: Type): Symbol[]; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index b8fd072c852..0c74f74c741 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -1731,6 +1731,10 @@ declare namespace ts { signatureToString(signature: Signature, enclosingDeclaration?: Node, flags?: TypeFormatFlags, kind?: SignatureKind): string; typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string; symbolToString(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags): string; + /** + * @deprecated Use the createX factory functions or XToY typechecker methods and `createPrinter` or the `xToString` methods instead + * This will be removed in a future version. + */ getSymbolDisplayBuilder(): SymbolDisplayBuilder; getFullyQualifiedName(symbol: Symbol): string; getAugmentedPropertiesOfType(type: Type): Symbol[]; From d23e5f1ee2fbe67db4ed0a5ac6dc856dfc7ce9c8 Mon Sep 17 00:00:00 2001 From: falsandtru Date: Tue, 10 Oct 2017 09:11:31 +0900 Subject: [PATCH 50/54] Fix Array.{reduce,reduceRight} methods (#18987) --- src/lib/es5.d.ts | 66 ++++++++++++------- .../anyInferenceAnonymousFunctions.symbols | 12 ++-- .../anyInferenceAnonymousFunctions.types | 12 ++-- ...plicateOverloadInTypeAugmentation1.symbols | 8 +-- ...duplicateOverloadInTypeAugmentation1.types | 8 +-- ...ericContextualTypingSpecialization.symbols | 4 +- ...enericContextualTypingSpecialization.types | 4 +- .../baselines/reference/genericReduce.symbols | 12 ++-- tests/baselines/reference/genericReduce.types | 12 ++-- ...ferFromGenericFunctionReturnTypes1.symbols | 4 +- ...inferFromGenericFunctionReturnTypes1.types | 4 +- ...ferFromGenericFunctionReturnTypes2.symbols | 4 +- ...inferFromGenericFunctionReturnTypes2.types | 4 +- .../baselines/reference/parserharness.symbols | 12 ++-- tests/baselines/reference/parserharness.types | 12 ++-- .../reference/recursiveTypeRelations.symbols | 4 +- .../reference/recursiveTypeRelations.types | 4 +- .../reference/restInvalidArgumentType.types | 2 +- .../returnTypeParameterWithModules.symbols | 4 +- .../returnTypeParameterWithModules.types | 4 +- .../reference/spreadInvalidArgumentType.types | 4 +- .../unknownSymbolOffContextualType1.symbols | 4 +- .../unknownSymbolOffContextualType1.types | 4 +- 23 files changed, 115 insertions(+), 93 deletions(-) diff --git a/src/lib/es5.d.ts b/src/lib/es5.d.ts index e08534d8ba9..fd2ae5b3fdf 100644 --- a/src/lib/es5.d.ts +++ b/src/lib/es5.d.ts @@ -1050,7 +1050,8 @@ interface ReadonlyArray { * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ - reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: ReadonlyArray) => T, initialValue?: T): T; + reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: ReadonlyArray) => T): T; + reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: ReadonlyArray) => T, initialValue: T): T; /** * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. @@ -1062,7 +1063,8 @@ interface ReadonlyArray { * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ - reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: ReadonlyArray) => T, initialValue?: T): T; + reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: ReadonlyArray) => T): T; + reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: ReadonlyArray) => T, initialValue: T): T; /** * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. @@ -1200,7 +1202,8 @@ interface Array { * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ - reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; + reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; + reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; /** * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. @@ -1212,7 +1215,8 @@ interface Array { * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ - reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; + reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; + reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; /** * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. @@ -1647,7 +1651,8 @@ interface Int8Array { * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. */ - reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Int8Array) => number, initialValue?: number): number; + reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Int8Array) => number): number; + reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Int8Array) => number, initialValue: number): number; /** * Calls the specified callback function for all the elements in an array. The return value of @@ -1671,7 +1676,8 @@ interface Int8Array { * the accumulation. The first call to the callbackfn function provides this value as an * argument instead of an array value. */ - reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Int8Array) => number, initialValue?: number): number; + reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Int8Array) => number): number; + reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Int8Array) => number, initialValue: number): number; /** * Calls the specified callback function for all the elements in an array, in descending order. @@ -1914,7 +1920,8 @@ interface Uint8Array { * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. */ - reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array) => number, initialValue?: number): number; + reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array) => number): number; + reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array) => number, initialValue: number): number; /** * Calls the specified callback function for all the elements in an array. The return value of @@ -1938,7 +1945,8 @@ interface Uint8Array { * the accumulation. The first call to the callbackfn function provides this value as an * argument instead of an array value. */ - reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array) => number, initialValue?: number): number; + reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array) => number): number; + reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array) => number, initialValue: number): number; /** * Calls the specified callback function for all the elements in an array, in descending order. @@ -2181,7 +2189,8 @@ interface Uint8ClampedArray { * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. */ - reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8ClampedArray) => number, initialValue?: number): number; + reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8ClampedArray) => number): number; + reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8ClampedArray) => number, initialValue: number): number; /** * Calls the specified callback function for all the elements in an array. The return value of @@ -2205,7 +2214,8 @@ interface Uint8ClampedArray { * the accumulation. The first call to the callbackfn function provides this value as an * argument instead of an array value. */ - reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8ClampedArray) => number, initialValue?: number): number; + reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8ClampedArray) => number): number; + reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8ClampedArray) => number, initialValue: number): number; /** * Calls the specified callback function for all the elements in an array, in descending order. @@ -2446,7 +2456,8 @@ interface Int16Array { * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. */ - reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Int16Array) => number, initialValue?: number): number; + reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Int16Array) => number): number; + reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Int16Array) => number, initialValue: number): number; /** * Calls the specified callback function for all the elements in an array. The return value of @@ -2470,7 +2481,8 @@ interface Int16Array { * the accumulation. The first call to the callbackfn function provides this value as an * argument instead of an array value. */ - reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Int16Array) => number, initialValue?: number): number; + reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Int16Array) => number): number; + reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Int16Array) => number, initialValue: number): number; /** * Calls the specified callback function for all the elements in an array, in descending order. @@ -2714,7 +2726,8 @@ interface Uint16Array { * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. */ - reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint16Array) => number, initialValue?: number): number; + reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint16Array) => number): number; + reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint16Array) => number, initialValue: number): number; /** * Calls the specified callback function for all the elements in an array. The return value of @@ -2738,7 +2751,8 @@ interface Uint16Array { * the accumulation. The first call to the callbackfn function provides this value as an * argument instead of an array value. */ - reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint16Array) => number, initialValue?: number): number; + reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint16Array) => number): number; + reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint16Array) => number, initialValue: number): number; /** * Calls the specified callback function for all the elements in an array, in descending order. @@ -2981,7 +2995,8 @@ interface Int32Array { * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. */ - reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Int32Array) => number, initialValue?: number): number; + reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Int32Array) => number): number; + reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Int32Array) => number, initialValue: number): number; /** * Calls the specified callback function for all the elements in an array. The return value of @@ -3005,7 +3020,8 @@ interface Int32Array { * the accumulation. The first call to the callbackfn function provides this value as an * argument instead of an array value. */ - reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Int32Array) => number, initialValue?: number): number; + reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Int32Array) => number): number; + reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Int32Array) => number, initialValue: number): number; /** * Calls the specified callback function for all the elements in an array, in descending order. @@ -3247,7 +3263,8 @@ interface Uint32Array { * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. */ - reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint32Array) => number, initialValue?: number): number; + reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint32Array) => number): number; + reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint32Array) => number, initialValue: number): number; /** * Calls the specified callback function for all the elements in an array. The return value of @@ -3271,7 +3288,8 @@ interface Uint32Array { * the accumulation. The first call to the callbackfn function provides this value as an * argument instead of an array value. */ - reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint32Array) => number, initialValue?: number): number; + reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint32Array) => number): number; + reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint32Array) => number, initialValue: number): number; /** * Calls the specified callback function for all the elements in an array, in descending order. @@ -3514,7 +3532,8 @@ interface Float32Array { * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. */ - reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Float32Array) => number, initialValue?: number): number; + reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Float32Array) => number): number; + reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Float32Array) => number, initialValue: number): number; /** * Calls the specified callback function for all the elements in an array. The return value of @@ -3538,7 +3557,8 @@ interface Float32Array { * the accumulation. The first call to the callbackfn function provides this value as an * argument instead of an array value. */ - reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Float32Array) => number, initialValue?: number): number; + reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Float32Array) => number): number; + reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Float32Array) => number, initialValue: number): number; /** * Calls the specified callback function for all the elements in an array, in descending order. @@ -3782,7 +3802,8 @@ interface Float64Array { * the accumulation. The first call to the callbackfn function provides this value as an argument * instead of an array value. */ - reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Float64Array) => number, initialValue?: number): number; + reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Float64Array) => number): number; + reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Float64Array) => number, initialValue: number): number; /** * Calls the specified callback function for all the elements in an array. The return value of @@ -3806,7 +3827,8 @@ interface Float64Array { * the accumulation. The first call to the callbackfn function provides this value as an * argument instead of an array value. */ - reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Float64Array) => number, initialValue?: number): number; + reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Float64Array) => number): number; + reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Float64Array) => number, initialValue: number): number; /** * Calls the specified callback function for all the elements in an array, in descending order. diff --git a/tests/baselines/reference/anyInferenceAnonymousFunctions.symbols b/tests/baselines/reference/anyInferenceAnonymousFunctions.symbols index c1b5df88fb0..4aad165f40a 100644 --- a/tests/baselines/reference/anyInferenceAnonymousFunctions.symbols +++ b/tests/baselines/reference/anyInferenceAnonymousFunctions.symbols @@ -3,9 +3,9 @@ var paired: any[]; >paired : Symbol(paired, Decl(anyInferenceAnonymousFunctions.ts, 0, 3)) paired.reduce(function (a1, a2) { ->paired.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>paired.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >paired : Symbol(paired, Decl(anyInferenceAnonymousFunctions.ts, 0, 3)) ->reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >a1 : Symbol(a1, Decl(anyInferenceAnonymousFunctions.ts, 2, 24)) >a2 : Symbol(a2, Decl(anyInferenceAnonymousFunctions.ts, 2, 27)) @@ -15,9 +15,9 @@ paired.reduce(function (a1, a2) { } , []); paired.reduce((b1, b2) => { ->paired.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>paired.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >paired : Symbol(paired, Decl(anyInferenceAnonymousFunctions.ts, 0, 3)) ->reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >b1 : Symbol(b1, Decl(anyInferenceAnonymousFunctions.ts, 8, 15)) >b2 : Symbol(b2, Decl(anyInferenceAnonymousFunctions.ts, 8, 18)) @@ -27,9 +27,9 @@ paired.reduce((b1, b2) => { } , []); paired.reduce((b3, b4) => b3.concat({}), []); ->paired.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>paired.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >paired : Symbol(paired, Decl(anyInferenceAnonymousFunctions.ts, 0, 3)) ->reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >b3 : Symbol(b3, Decl(anyInferenceAnonymousFunctions.ts, 13, 15)) >b4 : Symbol(b4, Decl(anyInferenceAnonymousFunctions.ts, 13, 18)) >b3 : Symbol(b3, Decl(anyInferenceAnonymousFunctions.ts, 13, 15)) diff --git a/tests/baselines/reference/anyInferenceAnonymousFunctions.types b/tests/baselines/reference/anyInferenceAnonymousFunctions.types index 8dc7fdcb90f..d5f693b5453 100644 --- a/tests/baselines/reference/anyInferenceAnonymousFunctions.types +++ b/tests/baselines/reference/anyInferenceAnonymousFunctions.types @@ -4,9 +4,9 @@ var paired: any[]; paired.reduce(function (a1, a2) { >paired.reduce(function (a1, a2) { return a1.concat({});} , []) : any ->paired.reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue?: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } +>paired.reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } >paired : any[] ->reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue?: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } +>reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } >function (a1, a2) { return a1.concat({});} : (a1: any, a2: any) => any >a1 : any >a2 : any @@ -23,9 +23,9 @@ paired.reduce(function (a1, a2) { paired.reduce((b1, b2) => { >paired.reduce((b1, b2) => { return b1.concat({});} , []) : any ->paired.reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue?: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } +>paired.reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } >paired : any[] ->reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue?: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } +>reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } >(b1, b2) => { return b1.concat({});} : (b1: any, b2: any) => any >b1 : any >b2 : any @@ -42,9 +42,9 @@ paired.reduce((b1, b2) => { paired.reduce((b3, b4) => b3.concat({}), []); >paired.reduce((b3, b4) => b3.concat({}), []) : any ->paired.reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue?: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } +>paired.reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } >paired : any[] ->reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue?: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } +>reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } >(b3, b4) => b3.concat({}) : (b3: any, b4: any) => any >b3 : any >b4 : any diff --git a/tests/baselines/reference/duplicateOverloadInTypeAugmentation1.symbols b/tests/baselines/reference/duplicateOverloadInTypeAugmentation1.symbols index 02589e011c4..31efb739e4c 100644 --- a/tests/baselines/reference/duplicateOverloadInTypeAugmentation1.symbols +++ b/tests/baselines/reference/duplicateOverloadInTypeAugmentation1.symbols @@ -4,7 +4,7 @@ interface Array { >T : Symbol(T, Decl(lib.d.ts, --, --), Decl(duplicateOverloadInTypeAugmentation1.ts, 0, 16)) reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, ->reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(duplicateOverloadInTypeAugmentation1.ts, 0, 20), Decl(duplicateOverloadInTypeAugmentation1.ts, 2, 29)) +>reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(duplicateOverloadInTypeAugmentation1.ts, 0, 20), Decl(duplicateOverloadInTypeAugmentation1.ts, 2, 29)) >callbackfn : Symbol(callbackfn, Decl(duplicateOverloadInTypeAugmentation1.ts, 1, 11)) >previousValue : Symbol(previousValue, Decl(duplicateOverloadInTypeAugmentation1.ts, 1, 24)) >T : Symbol(T, Decl(lib.d.ts, --, --), Decl(duplicateOverloadInTypeAugmentation1.ts, 0, 16)) @@ -21,7 +21,7 @@ interface Array { >T : Symbol(T, Decl(lib.d.ts, --, --), Decl(duplicateOverloadInTypeAugmentation1.ts, 0, 16)) reduce(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, ->reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(duplicateOverloadInTypeAugmentation1.ts, 0, 20), Decl(duplicateOverloadInTypeAugmentation1.ts, 2, 29)) +>reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(duplicateOverloadInTypeAugmentation1.ts, 0, 20), Decl(duplicateOverloadInTypeAugmentation1.ts, 2, 29)) >U : Symbol(U, Decl(duplicateOverloadInTypeAugmentation1.ts, 3, 11)) >callbackfn : Symbol(callbackfn, Decl(duplicateOverloadInTypeAugmentation1.ts, 3, 14)) >previousValue : Symbol(previousValue, Decl(duplicateOverloadInTypeAugmentation1.ts, 3, 27)) @@ -44,9 +44,9 @@ var a: Array; var r5 = a.reduce((x, y) => x + y); >r5 : Symbol(r5, Decl(duplicateOverloadInTypeAugmentation1.ts, 7, 3)) ->a.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(duplicateOverloadInTypeAugmentation1.ts, 0, 20), Decl(duplicateOverloadInTypeAugmentation1.ts, 2, 29)) +>a.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(duplicateOverloadInTypeAugmentation1.ts, 0, 20), Decl(duplicateOverloadInTypeAugmentation1.ts, 2, 29)) >a : Symbol(a, Decl(duplicateOverloadInTypeAugmentation1.ts, 6, 3)) ->reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(duplicateOverloadInTypeAugmentation1.ts, 0, 20), Decl(duplicateOverloadInTypeAugmentation1.ts, 2, 29)) +>reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(duplicateOverloadInTypeAugmentation1.ts, 0, 20), Decl(duplicateOverloadInTypeAugmentation1.ts, 2, 29)) >x : Symbol(x, Decl(duplicateOverloadInTypeAugmentation1.ts, 7, 19)) >y : Symbol(y, Decl(duplicateOverloadInTypeAugmentation1.ts, 7, 21)) >x : Symbol(x, Decl(duplicateOverloadInTypeAugmentation1.ts, 7, 19)) diff --git a/tests/baselines/reference/duplicateOverloadInTypeAugmentation1.types b/tests/baselines/reference/duplicateOverloadInTypeAugmentation1.types index bc7214eb6b9..ea07ece4314 100644 --- a/tests/baselines/reference/duplicateOverloadInTypeAugmentation1.types +++ b/tests/baselines/reference/duplicateOverloadInTypeAugmentation1.types @@ -4,7 +4,7 @@ interface Array { >T : T reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, ->reduce : { (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; (callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; (callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; } +>reduce : { (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; (callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; (callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; } >callbackfn : (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T >previousValue : T >T : T @@ -21,7 +21,7 @@ interface Array { >T : T reduce(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, ->reduce : { (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; (callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; (callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; } +>reduce : { (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; (callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; (callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; } >U : U >callbackfn : (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U >previousValue : U @@ -45,9 +45,9 @@ var a: Array; var r5 = a.reduce((x, y) => x + y); >r5 : string >a.reduce((x, y) => x + y) : string ->a.reduce : { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string, initialValue?: string): string; (callbackfn: (previousValue: U, currentValue: string, currentIndex: number, array: string[]) => U, initialValue: U): U; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string, initialValue?: string): string; (callbackfn: (previousValue: U, currentValue: string, currentIndex: number, array: string[]) => U, initialValue: U): U; } +>a.reduce : { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string): string; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string, initialValue: string): string; (callbackfn: (previousValue: U, currentValue: string, currentIndex: number, array: string[]) => U, initialValue: U): U; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string, initialValue?: string): string; (callbackfn: (previousValue: U, currentValue: string, currentIndex: number, array: string[]) => U, initialValue: U): U; } >a : string[] ->reduce : { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string, initialValue?: string): string; (callbackfn: (previousValue: U, currentValue: string, currentIndex: number, array: string[]) => U, initialValue: U): U; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string, initialValue?: string): string; (callbackfn: (previousValue: U, currentValue: string, currentIndex: number, array: string[]) => U, initialValue: U): U; } +>reduce : { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string): string; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string, initialValue: string): string; (callbackfn: (previousValue: U, currentValue: string, currentIndex: number, array: string[]) => U, initialValue: U): U; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string, initialValue?: string): string; (callbackfn: (previousValue: U, currentValue: string, currentIndex: number, array: string[]) => U, initialValue: U): U; } >(x, y) => x + y : (x: string, y: string) => string >x : string >y : string diff --git a/tests/baselines/reference/genericContextualTypingSpecialization.symbols b/tests/baselines/reference/genericContextualTypingSpecialization.symbols index b244ab496be..0dcc8cb5899 100644 --- a/tests/baselines/reference/genericContextualTypingSpecialization.symbols +++ b/tests/baselines/reference/genericContextualTypingSpecialization.symbols @@ -3,9 +3,9 @@ var b: number[]; >b : Symbol(b, Decl(genericContextualTypingSpecialization.ts, 0, 3)) b.reduce((c, d) => c + d, 0); // should not error on '+' ->b.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>b.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >b : Symbol(b, Decl(genericContextualTypingSpecialization.ts, 0, 3)) ->reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >c : Symbol(c, Decl(genericContextualTypingSpecialization.ts, 1, 18)) >d : Symbol(d, Decl(genericContextualTypingSpecialization.ts, 1, 20)) >c : Symbol(c, Decl(genericContextualTypingSpecialization.ts, 1, 18)) diff --git a/tests/baselines/reference/genericContextualTypingSpecialization.types b/tests/baselines/reference/genericContextualTypingSpecialization.types index 82255020347..d7d61010507 100644 --- a/tests/baselines/reference/genericContextualTypingSpecialization.types +++ b/tests/baselines/reference/genericContextualTypingSpecialization.types @@ -4,9 +4,9 @@ var b: number[]; b.reduce((c, d) => c + d, 0); // should not error on '+' >b.reduce((c, d) => c + d, 0) : number ->b.reduce : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue?: number): number; (callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } +>b.reduce : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number): number; (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number; (callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } >b : number[] ->reduce : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue?: number): number; (callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } +>reduce : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number): number; (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number; (callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } >(c, d) => c + d : (c: number, d: number) => number >c : number >d : number diff --git a/tests/baselines/reference/genericReduce.symbols b/tests/baselines/reference/genericReduce.symbols index f220972a350..a939c5cf92d 100644 --- a/tests/baselines/reference/genericReduce.symbols +++ b/tests/baselines/reference/genericReduce.symbols @@ -14,9 +14,9 @@ var b = a.map(s => s.length); var n1 = b.reduce((x, y) => x + y); >n1 : Symbol(n1, Decl(genericReduce.ts, 2, 3)) ->b.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>b.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >b : Symbol(b, Decl(genericReduce.ts, 1, 3)) ->reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >x : Symbol(x, Decl(genericReduce.ts, 2, 19)) >y : Symbol(y, Decl(genericReduce.ts, 2, 21)) >x : Symbol(x, Decl(genericReduce.ts, 2, 19)) @@ -24,9 +24,9 @@ var n1 = b.reduce((x, y) => x + y); var n2 = b.reduceRight((x, y) => x + y); >n2 : Symbol(n2, Decl(genericReduce.ts, 3, 3)) ->b.reduceRight : Symbol(Array.reduceRight, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>b.reduceRight : Symbol(Array.reduceRight, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >b : Symbol(b, Decl(genericReduce.ts, 1, 3)) ->reduceRight : Symbol(Array.reduceRight, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>reduceRight : Symbol(Array.reduceRight, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >x : Symbol(x, Decl(genericReduce.ts, 3, 24)) >y : Symbol(y, Decl(genericReduce.ts, 3, 26)) >x : Symbol(x, Decl(genericReduce.ts, 3, 24)) @@ -50,9 +50,9 @@ n2.toExponential(2); // should not error if 'n2' is correctly number. var n3 = b.reduce( (x, y) => x + y, ""); // Initial value is of type string >n3 : Symbol(n3, Decl(genericReduce.ts, 10, 3)) ->b.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>b.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >b : Symbol(b, Decl(genericReduce.ts, 1, 3)) ->reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >x : Symbol(x, Decl(genericReduce.ts, 10, 28)) >y : Symbol(y, Decl(genericReduce.ts, 10, 30)) >x : Symbol(x, Decl(genericReduce.ts, 10, 28)) diff --git a/tests/baselines/reference/genericReduce.types b/tests/baselines/reference/genericReduce.types index 628398f86fd..65a069426a7 100644 --- a/tests/baselines/reference/genericReduce.types +++ b/tests/baselines/reference/genericReduce.types @@ -22,9 +22,9 @@ var b = a.map(s => s.length); var n1 = b.reduce((x, y) => x + y); >n1 : number >b.reduce((x, y) => x + y) : number ->b.reduce : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue?: number): number; (callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } +>b.reduce : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number): number; (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number; (callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } >b : number[] ->reduce : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue?: number): number; (callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } +>reduce : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number): number; (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number; (callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } >(x, y) => x + y : (x: number, y: number) => number >x : number >y : number @@ -35,9 +35,9 @@ var n1 = b.reduce((x, y) => x + y); var n2 = b.reduceRight((x, y) => x + y); >n2 : number >b.reduceRight((x, y) => x + y) : number ->b.reduceRight : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue?: number): number; (callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } +>b.reduceRight : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number): number; (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number; (callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } >b : number[] ->reduceRight : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue?: number): number; (callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } +>reduceRight : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number): number; (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number; (callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } >(x, y) => x + y : (x: number, y: number) => number >x : number >y : number @@ -76,9 +76,9 @@ n2.toExponential(2); // should not error if 'n2' is correctly number. var n3 = b.reduce( (x, y) => x + y, ""); // Initial value is of type string >n3 : string >b.reduce( (x, y) => x + y, "") : string ->b.reduce : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue?: number): number; (callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } +>b.reduce : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number): number; (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number; (callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } >b : number[] ->reduce : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue?: number): number; (callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } +>reduce : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number): number; (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number; (callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } >(x, y) => x + y : (x: string, y: number) => string >x : string >y : number diff --git a/tests/baselines/reference/inferFromGenericFunctionReturnTypes1.symbols b/tests/baselines/reference/inferFromGenericFunctionReturnTypes1.symbols index e18c80afcde..3060f44009b 100644 --- a/tests/baselines/reference/inferFromGenericFunctionReturnTypes1.symbols +++ b/tests/baselines/reference/inferFromGenericFunctionReturnTypes1.symbols @@ -124,9 +124,9 @@ function compose(...fns: ((x: T) => T)[]): (x: T) => T { return (x: T) => fns.reduce((prev, fn) => fn(prev), x); >x : Symbol(x, Decl(inferFromGenericFunctionReturnTypes1.ts, 27, 10)) >T : Symbol(T, Decl(inferFromGenericFunctionReturnTypes1.ts, 26, 17)) ->fns.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>fns.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >fns : Symbol(fns, Decl(inferFromGenericFunctionReturnTypes1.ts, 26, 20)) ->reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >prev : Symbol(prev, Decl(inferFromGenericFunctionReturnTypes1.ts, 27, 31)) >fn : Symbol(fn, Decl(inferFromGenericFunctionReturnTypes1.ts, 27, 36)) >fn : Symbol(fn, Decl(inferFromGenericFunctionReturnTypes1.ts, 27, 36)) diff --git a/tests/baselines/reference/inferFromGenericFunctionReturnTypes1.types b/tests/baselines/reference/inferFromGenericFunctionReturnTypes1.types index dfbe6c2f2b6..9484b3aaa19 100644 --- a/tests/baselines/reference/inferFromGenericFunctionReturnTypes1.types +++ b/tests/baselines/reference/inferFromGenericFunctionReturnTypes1.types @@ -131,9 +131,9 @@ function compose(...fns: ((x: T) => T)[]): (x: T) => T { >x : T >T : T >fns.reduce((prev, fn) => fn(prev), x) : T ->fns.reduce : { (callbackfn: (previousValue: (x: T) => T, currentValue: (x: T) => T, currentIndex: number, array: ((x: T) => T)[]) => (x: T) => T, initialValue?: (x: T) => T): (x: T) => T; (callbackfn: (previousValue: U, currentValue: (x: T) => T, currentIndex: number, array: ((x: T) => T)[]) => U, initialValue: U): U; } +>fns.reduce : { (callbackfn: (previousValue: (x: T) => T, currentValue: (x: T) => T, currentIndex: number, array: ((x: T) => T)[]) => (x: T) => T): (x: T) => T; (callbackfn: (previousValue: (x: T) => T, currentValue: (x: T) => T, currentIndex: number, array: ((x: T) => T)[]) => (x: T) => T, initialValue: (x: T) => T): (x: T) => T; (callbackfn: (previousValue: U, currentValue: (x: T) => T, currentIndex: number, array: ((x: T) => T)[]) => U, initialValue: U): U; } >fns : ((x: T) => T)[] ->reduce : { (callbackfn: (previousValue: (x: T) => T, currentValue: (x: T) => T, currentIndex: number, array: ((x: T) => T)[]) => (x: T) => T, initialValue?: (x: T) => T): (x: T) => T; (callbackfn: (previousValue: U, currentValue: (x: T) => T, currentIndex: number, array: ((x: T) => T)[]) => U, initialValue: U): U; } +>reduce : { (callbackfn: (previousValue: (x: T) => T, currentValue: (x: T) => T, currentIndex: number, array: ((x: T) => T)[]) => (x: T) => T): (x: T) => T; (callbackfn: (previousValue: (x: T) => T, currentValue: (x: T) => T, currentIndex: number, array: ((x: T) => T)[]) => (x: T) => T, initialValue: (x: T) => T): (x: T) => T; (callbackfn: (previousValue: U, currentValue: (x: T) => T, currentIndex: number, array: ((x: T) => T)[]) => U, initialValue: U): U; } >(prev, fn) => fn(prev) : (prev: T, fn: (x: T) => T) => T >prev : T >fn : (x: T) => T diff --git a/tests/baselines/reference/inferFromGenericFunctionReturnTypes2.symbols b/tests/baselines/reference/inferFromGenericFunctionReturnTypes2.symbols index 7e4fe043b93..f7189682273 100644 --- a/tests/baselines/reference/inferFromGenericFunctionReturnTypes2.symbols +++ b/tests/baselines/reference/inferFromGenericFunctionReturnTypes2.symbols @@ -292,9 +292,9 @@ function compose(...fns: ((x: T) => T)[]): (x: T) => T { return (x: T) => fns.reduce((prev, fn) => fn(prev), x); >x : Symbol(x, Decl(inferFromGenericFunctionReturnTypes2.ts, 49, 10)) >T : Symbol(T, Decl(inferFromGenericFunctionReturnTypes2.ts, 48, 17)) ->fns.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>fns.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >fns : Symbol(fns, Decl(inferFromGenericFunctionReturnTypes2.ts, 48, 20)) ->reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >prev : Symbol(prev, Decl(inferFromGenericFunctionReturnTypes2.ts, 49, 31)) >fn : Symbol(fn, Decl(inferFromGenericFunctionReturnTypes2.ts, 49, 36)) >fn : Symbol(fn, Decl(inferFromGenericFunctionReturnTypes2.ts, 49, 36)) diff --git a/tests/baselines/reference/inferFromGenericFunctionReturnTypes2.types b/tests/baselines/reference/inferFromGenericFunctionReturnTypes2.types index cf7752e237e..a36b7ed35be 100644 --- a/tests/baselines/reference/inferFromGenericFunctionReturnTypes2.types +++ b/tests/baselines/reference/inferFromGenericFunctionReturnTypes2.types @@ -358,9 +358,9 @@ function compose(...fns: ((x: T) => T)[]): (x: T) => T { >x : T >T : T >fns.reduce((prev, fn) => fn(prev), x) : T ->fns.reduce : { (callbackfn: (previousValue: (x: T) => T, currentValue: (x: T) => T, currentIndex: number, array: ((x: T) => T)[]) => (x: T) => T, initialValue?: (x: T) => T): (x: T) => T; (callbackfn: (previousValue: U, currentValue: (x: T) => T, currentIndex: number, array: ((x: T) => T)[]) => U, initialValue: U): U; } +>fns.reduce : { (callbackfn: (previousValue: (x: T) => T, currentValue: (x: T) => T, currentIndex: number, array: ((x: T) => T)[]) => (x: T) => T): (x: T) => T; (callbackfn: (previousValue: (x: T) => T, currentValue: (x: T) => T, currentIndex: number, array: ((x: T) => T)[]) => (x: T) => T, initialValue: (x: T) => T): (x: T) => T; (callbackfn: (previousValue: U, currentValue: (x: T) => T, currentIndex: number, array: ((x: T) => T)[]) => U, initialValue: U): U; } >fns : ((x: T) => T)[] ->reduce : { (callbackfn: (previousValue: (x: T) => T, currentValue: (x: T) => T, currentIndex: number, array: ((x: T) => T)[]) => (x: T) => T, initialValue?: (x: T) => T): (x: T) => T; (callbackfn: (previousValue: U, currentValue: (x: T) => T, currentIndex: number, array: ((x: T) => T)[]) => U, initialValue: U): U; } +>reduce : { (callbackfn: (previousValue: (x: T) => T, currentValue: (x: T) => T, currentIndex: number, array: ((x: T) => T)[]) => (x: T) => T): (x: T) => T; (callbackfn: (previousValue: (x: T) => T, currentValue: (x: T) => T, currentIndex: number, array: ((x: T) => T)[]) => (x: T) => T, initialValue: (x: T) => T): (x: T) => T; (callbackfn: (previousValue: U, currentValue: (x: T) => T, currentIndex: number, array: ((x: T) => T)[]) => U, initialValue: U): U; } >(prev, fn) => fn(prev) : (prev: T, fn: (x: T) => T) => T >prev : T >fn : (x: T) => T diff --git a/tests/baselines/reference/parserharness.symbols b/tests/baselines/reference/parserharness.symbols index 94ae5c87901..8382d6ab41a 100644 --- a/tests/baselines/reference/parserharness.symbols +++ b/tests/baselines/reference/parserharness.symbols @@ -4692,7 +4692,7 @@ module Harness { var minDistFromStart = entries.map(x => x.editRange.minChar).reduce((prev, current) => Math.min(prev, current)); >minDistFromStart : Symbol(minDistFromStart, Decl(parserharness.ts, 1595, 15)) ->entries.map(x => x.editRange.minChar).reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>entries.map(x => x.editRange.minChar).reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >entries.map : Symbol(Array.map, Decl(lib.d.ts, --, --)) >entries : Symbol(entries, Decl(parserharness.ts, 1593, 15)) >map : Symbol(Array.map, Decl(lib.d.ts, --, --)) @@ -4700,7 +4700,7 @@ module Harness { >x.editRange : Symbol(editRange, Decl(parserharness.ts, 1547, 44)) >x : Symbol(x, Decl(parserharness.ts, 1595, 47)) >editRange : Symbol(editRange, Decl(parserharness.ts, 1547, 44)) ->reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >prev : Symbol(prev, Decl(parserharness.ts, 1595, 81)) >current : Symbol(current, Decl(parserharness.ts, 1595, 86)) >Math.min : Symbol(Math.min, Decl(lib.d.ts, --, --)) @@ -4711,7 +4711,7 @@ module Harness { var minDistFromEnd = entries.map(x => x.length - x.editRange.limChar).reduce((prev, current) => Math.min(prev, current)); >minDistFromEnd : Symbol(minDistFromEnd, Decl(parserharness.ts, 1596, 15)) ->entries.map(x => x.length - x.editRange.limChar).reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>entries.map(x => x.length - x.editRange.limChar).reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >entries.map : Symbol(Array.map, Decl(lib.d.ts, --, --)) >entries : Symbol(entries, Decl(parserharness.ts, 1593, 15)) >map : Symbol(Array.map, Decl(lib.d.ts, --, --)) @@ -4722,7 +4722,7 @@ module Harness { >x.editRange : Symbol(editRange, Decl(parserharness.ts, 1547, 44)) >x : Symbol(x, Decl(parserharness.ts, 1596, 45)) >editRange : Symbol(editRange, Decl(parserharness.ts, 1547, 44)) ->reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >prev : Symbol(prev, Decl(parserharness.ts, 1596, 90)) >current : Symbol(current, Decl(parserharness.ts, 1596, 95)) >Math.min : Symbol(Math.min, Decl(lib.d.ts, --, --)) @@ -4733,7 +4733,7 @@ module Harness { var aggDelta = entries.map(x => x.editRange.delta).reduce((prev, current) => prev + current); >aggDelta : Symbol(aggDelta, Decl(parserharness.ts, 1597, 15)) ->entries.map(x => x.editRange.delta).reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>entries.map(x => x.editRange.delta).reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >entries.map : Symbol(Array.map, Decl(lib.d.ts, --, --)) >entries : Symbol(entries, Decl(parserharness.ts, 1593, 15)) >map : Symbol(Array.map, Decl(lib.d.ts, --, --)) @@ -4741,7 +4741,7 @@ module Harness { >x.editRange : Symbol(editRange, Decl(parserharness.ts, 1547, 44)) >x : Symbol(x, Decl(parserharness.ts, 1597, 39)) >editRange : Symbol(editRange, Decl(parserharness.ts, 1547, 44)) ->reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >prev : Symbol(prev, Decl(parserharness.ts, 1597, 71)) >current : Symbol(current, Decl(parserharness.ts, 1597, 76)) >prev : Symbol(prev, Decl(parserharness.ts, 1597, 71)) diff --git a/tests/baselines/reference/parserharness.types b/tests/baselines/reference/parserharness.types index 2fd260f5d2b..6ea78e22f2f 100644 --- a/tests/baselines/reference/parserharness.types +++ b/tests/baselines/reference/parserharness.types @@ -6604,7 +6604,7 @@ module Harness { var minDistFromStart = entries.map(x => x.editRange.minChar).reduce((prev, current) => Math.min(prev, current)); >minDistFromStart : any >entries.map(x => x.editRange.minChar).reduce((prev, current) => Math.min(prev, current)) : any ->entries.map(x => x.editRange.minChar).reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue?: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } +>entries.map(x => x.editRange.minChar).reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } >entries.map(x => x.editRange.minChar) : any[] >entries.map : (callbackfn: (value: { length: number; editRange: any; }, index: number, array: { length: number; editRange: any; }[]) => U, thisArg?: any) => U[] >entries : { length: number; editRange: any; }[] @@ -6616,7 +6616,7 @@ module Harness { >x : { length: number; editRange: any; } >editRange : any >minChar : any ->reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue?: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } +>reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } >(prev, current) => Math.min(prev, current) : (prev: any, current: any) => number >prev : any >current : any @@ -6630,7 +6630,7 @@ module Harness { var minDistFromEnd = entries.map(x => x.length - x.editRange.limChar).reduce((prev, current) => Math.min(prev, current)); >minDistFromEnd : number >entries.map(x => x.length - x.editRange.limChar).reduce((prev, current) => Math.min(prev, current)) : number ->entries.map(x => x.length - x.editRange.limChar).reduce : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue?: number): number; (callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } +>entries.map(x => x.length - x.editRange.limChar).reduce : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number): number; (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number; (callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } >entries.map(x => x.length - x.editRange.limChar) : number[] >entries.map : (callbackfn: (value: { length: number; editRange: any; }, index: number, array: { length: number; editRange: any; }[]) => U, thisArg?: any) => U[] >entries : { length: number; editRange: any; }[] @@ -6646,7 +6646,7 @@ module Harness { >x : { length: number; editRange: any; } >editRange : any >limChar : any ->reduce : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue?: number): number; (callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } +>reduce : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number): number; (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number; (callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } >(prev, current) => Math.min(prev, current) : (prev: number, current: number) => number >prev : number >current : number @@ -6660,7 +6660,7 @@ module Harness { var aggDelta = entries.map(x => x.editRange.delta).reduce((prev, current) => prev + current); >aggDelta : any >entries.map(x => x.editRange.delta).reduce((prev, current) => prev + current) : any ->entries.map(x => x.editRange.delta).reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue?: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } +>entries.map(x => x.editRange.delta).reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } >entries.map(x => x.editRange.delta) : any[] >entries.map : (callbackfn: (value: { length: number; editRange: any; }, index: number, array: { length: number; editRange: any; }[]) => U, thisArg?: any) => U[] >entries : { length: number; editRange: any; }[] @@ -6672,7 +6672,7 @@ module Harness { >x : { length: number; editRange: any; } >editRange : any >delta : any ->reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue?: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } +>reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } >(prev, current) => prev + current : (prev: any, current: any) => any >prev : any >current : any diff --git a/tests/baselines/reference/recursiveTypeRelations.symbols b/tests/baselines/reference/recursiveTypeRelations.symbols index 9f656a789ed..2c940df2487 100644 --- a/tests/baselines/reference/recursiveTypeRelations.symbols +++ b/tests/baselines/reference/recursiveTypeRelations.symbols @@ -89,12 +89,12 @@ export function css(styles: S, ...classNam >arg : Symbol(arg, Decl(recursiveTypeRelations.ts, 18, 30)) return Object.keys(arg).reduce((obj: ClassNameObject, key: keyof S) => { ->Object.keys(arg).reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>Object.keys(arg).reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >Object.keys : Symbol(ObjectConstructor.keys, Decl(lib.d.ts, --, --)) >Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >keys : Symbol(ObjectConstructor.keys, Decl(lib.d.ts, --, --)) >arg : Symbol(arg, Decl(recursiveTypeRelations.ts, 18, 30)) ->reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >obj : Symbol(obj, Decl(recursiveTypeRelations.ts, 26, 55)) >key : Symbol(key, Decl(recursiveTypeRelations.ts, 26, 76)) >S : Symbol(S, Decl(recursiveTypeRelations.ts, 17, 20)) diff --git a/tests/baselines/reference/recursiveTypeRelations.types b/tests/baselines/reference/recursiveTypeRelations.types index 6def28d4461..110ff8175c4 100644 --- a/tests/baselines/reference/recursiveTypeRelations.types +++ b/tests/baselines/reference/recursiveTypeRelations.types @@ -102,13 +102,13 @@ export function css(styles: S, ...classNam return Object.keys(arg).reduce((obj: ClassNameObject, key: keyof S) => { >Object.keys(arg).reduce((obj: ClassNameObject, key: keyof S) => { const exportedClassName = styles[key]; obj[exportedClassName] = (arg as ClassNameMap)[key]; return obj; }, {}) : any ->Object.keys(arg).reduce : { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string, initialValue?: string): string; (callbackfn: (previousValue: U, currentValue: string, currentIndex: number, array: string[]) => U, initialValue: U): U; } +>Object.keys(arg).reduce : { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string): string; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string, initialValue: string): string; (callbackfn: (previousValue: U, currentValue: string, currentIndex: number, array: string[]) => U, initialValue: U): U; } >Object.keys(arg) : string[] >Object.keys : (o: {}) => string[] >Object : ObjectConstructor >keys : (o: {}) => string[] >arg : keyof S | (object & { [K in keyof S]?: boolean; }) ->reduce : { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string, initialValue?: string): string; (callbackfn: (previousValue: U, currentValue: string, currentIndex: number, array: string[]) => U, initialValue: U): U; } +>reduce : { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string): string; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string, initialValue: string): string; (callbackfn: (previousValue: U, currentValue: string, currentIndex: number, array: string[]) => U, initialValue: U): U; } >ClassNameObject : No type information available! >(obj: ClassNameObject, key: keyof S) => { const exportedClassName = styles[key]; obj[exportedClassName] = (arg as ClassNameMap)[key]; return obj; } : (obj: any, key: keyof S) => any >obj : any diff --git a/tests/baselines/reference/restInvalidArgumentType.types b/tests/baselines/reference/restInvalidArgumentType.types index 28162bcc7cc..39495008877 100644 --- a/tests/baselines/reference/restInvalidArgumentType.types +++ b/tests/baselines/reference/restInvalidArgumentType.types @@ -87,7 +87,7 @@ function f(p1: T, p2: T[]) { >p1 : T var {...r2} = p2; // OK ->r2 : { [n: number]: T; length: number; toString(): string; toLocaleString(): string; push(...items: T[]): number; pop(): T; concat(...items: ReadonlyArray[]): T[]; concat(...items: (T | ReadonlyArray)[]): T[]; join(separator?: string): string; reverse(): T[]; shift(): T; slice(start?: number, end?: number): T[]; sort(compareFn?: (a: T, b: T) => number): T[]; splice(start: number, deleteCount?: number): T[]; splice(start: number, deleteCount: number, ...items: T[]): T[]; unshift(...items: T[]): number; indexOf(searchElement: T, fromIndex?: number): number; lastIndexOf(searchElement: T, fromIndex?: number): number; every(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; some(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void; map(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[]; filter(callbackfn: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[]; filter(callbackfn: (value: T, index: number, array: T[]) => any, thisArg?: any): T[]; reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; reduce(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; reduceRight(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; } +>r2 : { [n: number]: T; length: number; toString(): string; toLocaleString(): string; push(...items: T[]): number; pop(): T; concat(...items: ReadonlyArray[]): T[]; concat(...items: (T | ReadonlyArray)[]): T[]; join(separator?: string): string; reverse(): T[]; shift(): T; slice(start?: number, end?: number): T[]; sort(compareFn?: (a: T, b: T) => number): T[]; splice(start: number, deleteCount?: number): T[]; splice(start: number, deleteCount: number, ...items: T[]): T[]; unshift(...items: T[]): number; indexOf(searchElement: T, fromIndex?: number): number; lastIndexOf(searchElement: T, fromIndex?: number): number; every(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; some(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void; map(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[]; filter(callbackfn: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[]; filter(callbackfn: (value: T, index: number, array: T[]) => any, thisArg?: any): T[]; reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; reduce(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; reduceRight(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; } >p2 : T[] var {...r3} = t; // Error, generic type paramter diff --git a/tests/baselines/reference/returnTypeParameterWithModules.symbols b/tests/baselines/reference/returnTypeParameterWithModules.symbols index 7f8fab382a3..eb56940b604 100644 --- a/tests/baselines/reference/returnTypeParameterWithModules.symbols +++ b/tests/baselines/reference/returnTypeParameterWithModules.symbols @@ -13,11 +13,11 @@ module M1 { return Array.prototype.reduce.apply(ar, e ? [f, e] : [f]); >Array.prototype.reduce.apply : Symbol(Function.apply, Decl(lib.d.ts, --, --)) ->Array.prototype.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>Array.prototype.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >Array.prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, --, --)) >Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, --, --)) ->reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >apply : Symbol(Function.apply, Decl(lib.d.ts, --, --)) >ar : Symbol(ar, Decl(returnTypeParameterWithModules.ts, 1, 30)) >e : Symbol(e, Decl(returnTypeParameterWithModules.ts, 1, 36)) diff --git a/tests/baselines/reference/returnTypeParameterWithModules.types b/tests/baselines/reference/returnTypeParameterWithModules.types index 584c962a170..3aec0f14fcf 100644 --- a/tests/baselines/reference/returnTypeParameterWithModules.types +++ b/tests/baselines/reference/returnTypeParameterWithModules.types @@ -14,11 +14,11 @@ module M1 { return Array.prototype.reduce.apply(ar, e ? [f, e] : [f]); >Array.prototype.reduce.apply(ar, e ? [f, e] : [f]) : any >Array.prototype.reduce.apply : (this: Function, thisArg: any, argArray?: any) => any ->Array.prototype.reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue?: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } +>Array.prototype.reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } >Array.prototype : any[] >Array : ArrayConstructor >prototype : any[] ->reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue?: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } +>reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } >apply : (this: Function, thisArg: any, argArray?: any) => any >ar : any >e ? [f, e] : [f] : any[] diff --git a/tests/baselines/reference/spreadInvalidArgumentType.types b/tests/baselines/reference/spreadInvalidArgumentType.types index 1eebc00850f..244d8515893 100644 --- a/tests/baselines/reference/spreadInvalidArgumentType.types +++ b/tests/baselines/reference/spreadInvalidArgumentType.types @@ -89,8 +89,8 @@ function f(p1: T, p2: T[]) { >p1 : T var o2 = { ...p2 }; // OK ->o2 : { [n: number]: T; length: number; toString(): string; toLocaleString(): string; push(...items: T[]): number; pop(): T; concat(...items: ReadonlyArray[]): T[]; concat(...items: (T | ReadonlyArray)[]): T[]; join(separator?: string): string; reverse(): T[]; shift(): T; slice(start?: number, end?: number): T[]; sort(compareFn?: (a: T, b: T) => number): T[]; splice(start: number, deleteCount?: number): T[]; splice(start: number, deleteCount: number, ...items: T[]): T[]; unshift(...items: T[]): number; indexOf(searchElement: T, fromIndex?: number): number; lastIndexOf(searchElement: T, fromIndex?: number): number; every(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; some(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void; map(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[]; filter(callbackfn: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[]; filter(callbackfn: (value: T, index: number, array: T[]) => any, thisArg?: any): T[]; reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; reduce(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; reduceRight(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; } ->{ ...p2 } : { [n: number]: T; length: number; toString(): string; toLocaleString(): string; push(...items: T[]): number; pop(): T; concat(...items: ReadonlyArray[]): T[]; concat(...items: (T | ReadonlyArray)[]): T[]; join(separator?: string): string; reverse(): T[]; shift(): T; slice(start?: number, end?: number): T[]; sort(compareFn?: (a: T, b: T) => number): T[]; splice(start: number, deleteCount?: number): T[]; splice(start: number, deleteCount: number, ...items: T[]): T[]; unshift(...items: T[]): number; indexOf(searchElement: T, fromIndex?: number): number; lastIndexOf(searchElement: T, fromIndex?: number): number; every(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; some(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void; map(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[]; filter(callbackfn: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[]; filter(callbackfn: (value: T, index: number, array: T[]) => any, thisArg?: any): T[]; reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; reduce(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; reduceRight(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; } +>o2 : { [n: number]: T; length: number; toString(): string; toLocaleString(): string; push(...items: T[]): number; pop(): T; concat(...items: ReadonlyArray[]): T[]; concat(...items: (T | ReadonlyArray)[]): T[]; join(separator?: string): string; reverse(): T[]; shift(): T; slice(start?: number, end?: number): T[]; sort(compareFn?: (a: T, b: T) => number): T[]; splice(start: number, deleteCount?: number): T[]; splice(start: number, deleteCount: number, ...items: T[]): T[]; unshift(...items: T[]): number; indexOf(searchElement: T, fromIndex?: number): number; lastIndexOf(searchElement: T, fromIndex?: number): number; every(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; some(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void; map(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[]; filter(callbackfn: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[]; filter(callbackfn: (value: T, index: number, array: T[]) => any, thisArg?: any): T[]; reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; reduce(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; reduceRight(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; } +>{ ...p2 } : { [n: number]: T; length: number; toString(): string; toLocaleString(): string; push(...items: T[]): number; pop(): T; concat(...items: ReadonlyArray[]): T[]; concat(...items: (T | ReadonlyArray)[]): T[]; join(separator?: string): string; reverse(): T[]; shift(): T; slice(start?: number, end?: number): T[]; sort(compareFn?: (a: T, b: T) => number): T[]; splice(start: number, deleteCount?: number): T[]; splice(start: number, deleteCount: number, ...items: T[]): T[]; unshift(...items: T[]): number; indexOf(searchElement: T, fromIndex?: number): number; lastIndexOf(searchElement: T, fromIndex?: number): number; every(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; some(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void; map(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[]; filter(callbackfn: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[]; filter(callbackfn: (value: T, index: number, array: T[]) => any, thisArg?: any): T[]; reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; reduce(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; reduceRight(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; } >p2 : T[] var o3 = { ...t }; // Error, generic type paramter diff --git a/tests/baselines/reference/unknownSymbolOffContextualType1.symbols b/tests/baselines/reference/unknownSymbolOffContextualType1.symbols index 1e19afeb6a0..3acf34ec9bf 100644 --- a/tests/baselines/reference/unknownSymbolOffContextualType1.symbols +++ b/tests/baselines/reference/unknownSymbolOffContextualType1.symbols @@ -61,9 +61,9 @@ function getMaxWidth(elementNames: string[]) { }); var maxWidth = widths.reduce(function (a, b) { >maxWidth : Symbol(maxWidth, Decl(unknownSymbolOffContextualType1.ts, 17, 7)) ->widths.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>widths.reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >widths : Symbol(widths, Decl(unknownSymbolOffContextualType1.ts, 14, 7)) ->reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>reduce : Symbol(Array.reduce, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) >a : Symbol(a, Decl(unknownSymbolOffContextualType1.ts, 17, 43)) >b : Symbol(b, Decl(unknownSymbolOffContextualType1.ts, 17, 45)) diff --git a/tests/baselines/reference/unknownSymbolOffContextualType1.types b/tests/baselines/reference/unknownSymbolOffContextualType1.types index c6f05b69b74..7df3bd52cc9 100644 --- a/tests/baselines/reference/unknownSymbolOffContextualType1.types +++ b/tests/baselines/reference/unknownSymbolOffContextualType1.types @@ -72,9 +72,9 @@ function getMaxWidth(elementNames: string[]) { var maxWidth = widths.reduce(function (a, b) { >maxWidth : any >widths.reduce(function (a, b) { return a > b ? a : b; }) : any ->widths.reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue?: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } +>widths.reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } >widths : any[] ->reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue?: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } +>reduce : { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue: any): any; (callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U; } >function (a, b) { return a > b ? a : b; } : (a: any, b: any) => any >a : any >b : any From dc607c29b4f281d8734a70264525ecf3fbe64c25 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Mon, 9 Oct 2017 17:15:13 -0700 Subject: [PATCH 51/54] Fix 'this' capturing for dynamic import --- src/compiler/binder.ts | 6 ++ src/compiler/transformers/module/module.ts | 89 ++++++++++++++----- .../dynamicImportWithNestedThis_es2015.js | 37 ++++++++ ...dynamicImportWithNestedThis_es2015.symbols | 27 ++++++ .../dynamicImportWithNestedThis_es2015.types | 31 +++++++ .../dynamicImportWithNestedThis_es5.js | 39 ++++++++ .../dynamicImportWithNestedThis_es5.symbols | 27 ++++++ .../dynamicImportWithNestedThis_es5.types | 31 +++++++ .../dynamicImportWithNestedThis_es2015.ts | 14 +++ .../dynamicImportWithNestedThis_es5.ts | 14 +++ 10 files changed, 292 insertions(+), 23 deletions(-) create mode 100644 tests/baselines/reference/dynamicImportWithNestedThis_es2015.js create mode 100644 tests/baselines/reference/dynamicImportWithNestedThis_es2015.symbols create mode 100644 tests/baselines/reference/dynamicImportWithNestedThis_es2015.types create mode 100644 tests/baselines/reference/dynamicImportWithNestedThis_es5.js create mode 100644 tests/baselines/reference/dynamicImportWithNestedThis_es5.symbols create mode 100644 tests/baselines/reference/dynamicImportWithNestedThis_es5.types create mode 100644 tests/cases/compiler/dynamicImportWithNestedThis_es2015.ts create mode 100644 tests/cases/compiler/dynamicImportWithNestedThis_es5.ts diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 9b977eb6bfc..48cace44841 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -2699,6 +2699,12 @@ namespace ts { if (expression.kind === SyntaxKind.ImportKeyword) { transformFlags |= TransformFlags.ContainsDynamicImport; + + // A dynamic 'import()' call that contains a lexical 'this' will + // require a captured 'this' when emitting down-level. + if (subtreeFlags & TransformFlags.ContainsLexicalThis) { + transformFlags |= TransformFlags.ContainsCapturedLexicalThis; + } } node.transformFlags = transformFlags | TransformFlags.HasComputedFlags; diff --git a/src/compiler/transformers/module/module.ts b/src/compiler/transformers/module/module.ts index ecbef685649..ba262bf2c59 100644 --- a/src/compiler/transformers/module/module.ts +++ b/src/compiler/transformers/module/module.ts @@ -561,46 +561,89 @@ namespace ts { // }); const resolve = createUniqueName("resolve"); const reject = createUniqueName("reject"); - return createNew( - createIdentifier("Promise"), - /*typeArguments*/ undefined, - [createFunctionExpression( + const parameters = [ + createParameter(/*decorator*/ undefined, /*modifiers*/ undefined, /*dotDotDotToken*/ undefined, /*name*/ resolve), + createParameter(/*decorator*/ undefined, /*modifiers*/ undefined, /*dotDotDotToken*/ undefined, /*name*/ reject) + ]; + const body = createBlock([ + createStatement( + createCall( + createIdentifier("require"), + /*typeArguments*/ undefined, + [createArrayLiteral([firstOrUndefined(node.arguments) || createOmittedExpression()]), resolve, reject] + ) + ) + ]); + + let func: FunctionExpression | ArrowFunction; + if (languageVersion >= ScriptTarget.ES2015) { + func = createArrowFunction( + /*modifiers*/ undefined, + /*typeParameters*/ undefined, + parameters, + /*type*/ undefined, + /*equalsGreaterThanToken*/ undefined, + body); + } + else { + func = createFunctionExpression( /*modifiers*/ undefined, /*asteriskToken*/ undefined, /*name*/ undefined, /*typeParameters*/ undefined, - [createParameter(/*decorator*/ undefined, /*modifiers*/ undefined, /*dotDotDotToken*/ undefined, /*name*/ resolve), - createParameter(/*decorator*/ undefined, /*modifiers*/ undefined, /*dotDotDotToken*/ undefined, /*name*/ reject)], + parameters, /*type*/ undefined, - createBlock([createStatement( - createCall( - createIdentifier("require"), - /*typeArguments*/ undefined, - [createArrayLiteral([firstOrUndefined(node.arguments) || createOmittedExpression()]), resolve, reject] - ))]) - )]); + body); + + // if there is a lexical 'this' in the import call arguments, ensure we indicate + // that this new function expression indicates it captures 'this' so that the + // es2015 transformer will properly substitute 'this' with '_this'. + if (node.transformFlags & TransformFlags.ContainsLexicalThis) { + setEmitFlags(func, EmitFlags.CapturesThis); + } + } + + return createNew(createIdentifier("Promise"), /*typeArguments*/ undefined, [func]); } - function transformImportCallExpressionCommonJS(node: ImportCall): Expression { + function transformImportCallExpressionCommonJS(node: ImportCall): Expression { // import("./blah") // emit as // Promise.resolve().then(function () { return require(x); }) /*CommonJs Require*/ // We have to wrap require in then callback so that require is done in asynchronously // if we simply do require in resolve callback in Promise constructor. We will execute the loading immediately - return createCall( - createPropertyAccess( - createCall(createPropertyAccess(createIdentifier("Promise"), "resolve"), /*typeArguments*/ undefined, /*argumentsArray*/ []), - "then"), - /*typeArguments*/ undefined, - [createFunctionExpression( + const promiseResolveCall = createCall(createPropertyAccess(createIdentifier("Promise"), "resolve"), /*typeArguments*/ undefined, /*argumentsArray*/ []); + const requireCall = createCall(createIdentifier("require"), /*typeArguments*/ undefined, node.arguments); + + let func: FunctionExpression | ArrowFunction; + if (languageVersion >= ScriptTarget.ES2015) { + func = createArrowFunction( + /*modifiers*/ undefined, + /*typeParameters*/ undefined, + /*parameters*/ [], + /*type*/ undefined, + /*equalsGreaterThanToken*/ undefined, + requireCall); + } + else { + func = createFunctionExpression( /*modifiers*/ undefined, /*asteriskToken*/ undefined, /*name*/ undefined, /*typeParameters*/ undefined, - /*parameters*/ undefined, + /*parameters*/ [], /*type*/ undefined, - createBlock([createReturn(createCall(createIdentifier("require"), /*typeArguments*/ undefined, node.arguments))]) - )]); + createBlock([createReturn(requireCall)])); + + // if there is a lexical 'this' in the import call arguments, ensure we indicate + // that this new function expression indicates it captures 'this' so that the + // es2015 transformer will properly substitute 'this' with '_this'. + if (node.transformFlags & TransformFlags.ContainsLexicalThis) { + setEmitFlags(func, EmitFlags.CapturesThis); + } + } + + return createCall(createPropertyAccess(promiseResolveCall, "then"), /*typeArguments*/ undefined, [func]); } /** diff --git a/tests/baselines/reference/dynamicImportWithNestedThis_es2015.js b/tests/baselines/reference/dynamicImportWithNestedThis_es2015.js new file mode 100644 index 00000000000..86fba0c0b5d --- /dev/null +++ b/tests/baselines/reference/dynamicImportWithNestedThis_es2015.js @@ -0,0 +1,37 @@ +//// [dynamicImportWithNestedThis_es2015.ts] +// https://github.com/Microsoft/TypeScript/issues/17564 +class C { + private _path = './other'; + + dynamic() { + return import(this._path); + } +} + +const c = new C(); +c.dynamic(); + +//// [dynamicImportWithNestedThis_es2015.js] +(function (factory) { + if (typeof module === "object" && typeof module.exports === "object") { + var v = factory(require, exports); + if (v !== undefined) module.exports = v; + } + else if (typeof define === "function" && define.amd) { + define(["require", "exports"], factory); + } +})(function (require, exports) { + "use strict"; + var __syncRequire = typeof module === "object" && typeof module.exports === "object"; + // https://github.com/Microsoft/TypeScript/issues/17564 + class C { + constructor() { + this._path = './other'; + } + dynamic() { + return __syncRequire ? Promise.resolve().then(() => require(this._path)) : new Promise((resolve_1, reject_1) => { require([this._path], resolve_1, reject_1); }); + } + } + const c = new C(); + c.dynamic(); +}); diff --git a/tests/baselines/reference/dynamicImportWithNestedThis_es2015.symbols b/tests/baselines/reference/dynamicImportWithNestedThis_es2015.symbols new file mode 100644 index 00000000000..7043a071124 --- /dev/null +++ b/tests/baselines/reference/dynamicImportWithNestedThis_es2015.symbols @@ -0,0 +1,27 @@ +=== tests/cases/compiler/dynamicImportWithNestedThis_es2015.ts === +// https://github.com/Microsoft/TypeScript/issues/17564 +class C { +>C : Symbol(C, Decl(dynamicImportWithNestedThis_es2015.ts, 0, 0)) + + private _path = './other'; +>_path : Symbol(C._path, Decl(dynamicImportWithNestedThis_es2015.ts, 1, 9)) + + dynamic() { +>dynamic : Symbol(C.dynamic, Decl(dynamicImportWithNestedThis_es2015.ts, 2, 27)) + + return import(this._path); +>this._path : Symbol(C._path, Decl(dynamicImportWithNestedThis_es2015.ts, 1, 9)) +>this : Symbol(C, Decl(dynamicImportWithNestedThis_es2015.ts, 0, 0)) +>_path : Symbol(C._path, Decl(dynamicImportWithNestedThis_es2015.ts, 1, 9)) + } +} + +const c = new C(); +>c : Symbol(c, Decl(dynamicImportWithNestedThis_es2015.ts, 9, 5)) +>C : Symbol(C, Decl(dynamicImportWithNestedThis_es2015.ts, 0, 0)) + +c.dynamic(); +>c.dynamic : Symbol(C.dynamic, Decl(dynamicImportWithNestedThis_es2015.ts, 2, 27)) +>c : Symbol(c, Decl(dynamicImportWithNestedThis_es2015.ts, 9, 5)) +>dynamic : Symbol(C.dynamic, Decl(dynamicImportWithNestedThis_es2015.ts, 2, 27)) + diff --git a/tests/baselines/reference/dynamicImportWithNestedThis_es2015.types b/tests/baselines/reference/dynamicImportWithNestedThis_es2015.types new file mode 100644 index 00000000000..165929a43cf --- /dev/null +++ b/tests/baselines/reference/dynamicImportWithNestedThis_es2015.types @@ -0,0 +1,31 @@ +=== tests/cases/compiler/dynamicImportWithNestedThis_es2015.ts === +// https://github.com/Microsoft/TypeScript/issues/17564 +class C { +>C : C + + private _path = './other'; +>_path : string +>'./other' : "./other" + + dynamic() { +>dynamic : () => Promise + + return import(this._path); +>import(this._path) : Promise +>this._path : string +>this : this +>_path : string + } +} + +const c = new C(); +>c : C +>new C() : C +>C : typeof C + +c.dynamic(); +>c.dynamic() : Promise +>c.dynamic : () => Promise +>c : C +>dynamic : () => Promise + diff --git a/tests/baselines/reference/dynamicImportWithNestedThis_es5.js b/tests/baselines/reference/dynamicImportWithNestedThis_es5.js new file mode 100644 index 00000000000..cde1979b25b --- /dev/null +++ b/tests/baselines/reference/dynamicImportWithNestedThis_es5.js @@ -0,0 +1,39 @@ +//// [dynamicImportWithNestedThis_es5.ts] +// https://github.com/Microsoft/TypeScript/issues/17564 +class C { + private _path = './other'; + + dynamic() { + return import(this._path); + } +} + +const c = new C(); +c.dynamic(); + +//// [dynamicImportWithNestedThis_es5.js] +(function (factory) { + if (typeof module === "object" && typeof module.exports === "object") { + var v = factory(require, exports); + if (v !== undefined) module.exports = v; + } + else if (typeof define === "function" && define.amd) { + define(["require", "exports"], factory); + } +})(function (require, exports) { + "use strict"; + var __syncRequire = typeof module === "object" && typeof module.exports === "object"; + // https://github.com/Microsoft/TypeScript/issues/17564 + var C = /** @class */ (function () { + function C() { + this._path = './other'; + } + C.prototype.dynamic = function () { + var _this = this; + return __syncRequire ? Promise.resolve().then(function () { return require(_this._path); }) : new Promise(function (resolve_1, reject_1) { require([_this._path], resolve_1, reject_1); }); + }; + return C; + }()); + var c = new C(); + c.dynamic(); +}); diff --git a/tests/baselines/reference/dynamicImportWithNestedThis_es5.symbols b/tests/baselines/reference/dynamicImportWithNestedThis_es5.symbols new file mode 100644 index 00000000000..6a127548030 --- /dev/null +++ b/tests/baselines/reference/dynamicImportWithNestedThis_es5.symbols @@ -0,0 +1,27 @@ +=== tests/cases/compiler/dynamicImportWithNestedThis_es5.ts === +// https://github.com/Microsoft/TypeScript/issues/17564 +class C { +>C : Symbol(C, Decl(dynamicImportWithNestedThis_es5.ts, 0, 0)) + + private _path = './other'; +>_path : Symbol(C._path, Decl(dynamicImportWithNestedThis_es5.ts, 1, 9)) + + dynamic() { +>dynamic : Symbol(C.dynamic, Decl(dynamicImportWithNestedThis_es5.ts, 2, 27)) + + return import(this._path); +>this._path : Symbol(C._path, Decl(dynamicImportWithNestedThis_es5.ts, 1, 9)) +>this : Symbol(C, Decl(dynamicImportWithNestedThis_es5.ts, 0, 0)) +>_path : Symbol(C._path, Decl(dynamicImportWithNestedThis_es5.ts, 1, 9)) + } +} + +const c = new C(); +>c : Symbol(c, Decl(dynamicImportWithNestedThis_es5.ts, 9, 5)) +>C : Symbol(C, Decl(dynamicImportWithNestedThis_es5.ts, 0, 0)) + +c.dynamic(); +>c.dynamic : Symbol(C.dynamic, Decl(dynamicImportWithNestedThis_es5.ts, 2, 27)) +>c : Symbol(c, Decl(dynamicImportWithNestedThis_es5.ts, 9, 5)) +>dynamic : Symbol(C.dynamic, Decl(dynamicImportWithNestedThis_es5.ts, 2, 27)) + diff --git a/tests/baselines/reference/dynamicImportWithNestedThis_es5.types b/tests/baselines/reference/dynamicImportWithNestedThis_es5.types new file mode 100644 index 00000000000..78b0f472971 --- /dev/null +++ b/tests/baselines/reference/dynamicImportWithNestedThis_es5.types @@ -0,0 +1,31 @@ +=== tests/cases/compiler/dynamicImportWithNestedThis_es5.ts === +// https://github.com/Microsoft/TypeScript/issues/17564 +class C { +>C : C + + private _path = './other'; +>_path : string +>'./other' : "./other" + + dynamic() { +>dynamic : () => Promise + + return import(this._path); +>import(this._path) : Promise +>this._path : string +>this : this +>_path : string + } +} + +const c = new C(); +>c : C +>new C() : C +>C : typeof C + +c.dynamic(); +>c.dynamic() : Promise +>c.dynamic : () => Promise +>c : C +>dynamic : () => Promise + diff --git a/tests/cases/compiler/dynamicImportWithNestedThis_es2015.ts b/tests/cases/compiler/dynamicImportWithNestedThis_es2015.ts new file mode 100644 index 00000000000..3c7f2936931 --- /dev/null +++ b/tests/cases/compiler/dynamicImportWithNestedThis_es2015.ts @@ -0,0 +1,14 @@ +// @lib: es2015 +// @target: es2015 +// @module: umd +// https://github.com/Microsoft/TypeScript/issues/17564 +class C { + private _path = './other'; + + dynamic() { + return import(this._path); + } +} + +const c = new C(); +c.dynamic(); \ No newline at end of file diff --git a/tests/cases/compiler/dynamicImportWithNestedThis_es5.ts b/tests/cases/compiler/dynamicImportWithNestedThis_es5.ts new file mode 100644 index 00000000000..5740c3f6694 --- /dev/null +++ b/tests/cases/compiler/dynamicImportWithNestedThis_es5.ts @@ -0,0 +1,14 @@ +// @lib: es2015 +// @target: es5 +// @module: umd +// https://github.com/Microsoft/TypeScript/issues/17564 +class C { + private _path = './other'; + + dynamic() { + return import(this._path); + } +} + +const c = new C(); +c.dynamic(); \ No newline at end of file From dca6e33ac7ba655af0d27a9d2e9555424c9cc67e Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Tue, 10 Oct 2017 10:03:18 -0700 Subject: [PATCH 52/54] baseline updates --- .../reference/importCallExpressionAsyncES6AMD.js | 10 +++++----- .../reference/importCallExpressionAsyncES6CJS.js | 10 +++++----- .../reference/importCallExpressionAsyncES6UMD.js | 10 +++++----- .../importCallExpressionCheckReturntype1.js | 6 +++--- .../importCallExpressionDeclarationEmit1.js | 10 +++++----- .../reference/importCallExpressionES6AMD.js | 12 ++++++------ .../reference/importCallExpressionES6CJS.js | 12 ++++++------ .../reference/importCallExpressionES6UMD.js | 12 ++++++------ .../importCallExpressionGrammarError.js | 10 +++++----- .../reference/importCallExpressionInAMD1.js | 8 ++++---- .../reference/importCallExpressionInAMD2.js | 2 +- .../reference/importCallExpressionInAMD3.js | 2 +- .../reference/importCallExpressionInAMD4.js | 12 ++++++------ .../reference/importCallExpressionInCJS1.js | 8 ++++---- .../reference/importCallExpressionInCJS2.js | 4 ++-- .../reference/importCallExpressionInCJS3.js | 2 +- .../reference/importCallExpressionInCJS4.js | 2 +- .../reference/importCallExpressionInCJS5.js | 12 ++++++------ .../importCallExpressionInExportEqualsAMD.js | 2 +- .../importCallExpressionInExportEqualsCJS.js | 2 +- .../importCallExpressionInExportEqualsUMD.js | 2 +- .../importCallExpressionInScriptContext1.js | 2 +- .../importCallExpressionInScriptContext2.js | 2 +- .../reference/importCallExpressionInUMD1.js | 8 ++++---- .../reference/importCallExpressionInUMD2.js | 2 +- .../reference/importCallExpressionInUMD3.js | 2 +- .../reference/importCallExpressionInUMD4.js | 12 ++++++------ .../importCallExpressionReturnPromiseOfAny.js | 16 ++++++++-------- ...tCallExpressionSpecifierNotStringTypeError.js | 10 +++++----- .../importCallExpressionWithTypeArgument.js | 4 ++-- 30 files changed, 104 insertions(+), 104 deletions(-) diff --git a/tests/baselines/reference/importCallExpressionAsyncES6AMD.js b/tests/baselines/reference/importCallExpressionAsyncES6AMD.js index 9819da8369b..7f86625bbfc 100644 --- a/tests/baselines/reference/importCallExpressionAsyncES6AMD.js +++ b/tests/baselines/reference/importCallExpressionAsyncES6AMD.js @@ -42,34 +42,34 @@ define(["require", "exports"], function (require, exports) { Object.defineProperty(exports, "__esModule", { value: true }); function fn() { return __awaiter(this, void 0, void 0, function* () { - const req = yield new Promise(function (resolve_1, reject_1) { require(['./test'], resolve_1, reject_1); }); // ONE + const req = yield new Promise((resolve_1, reject_1) => { require(['./test'], resolve_1, reject_1); }); // ONE }); } exports.fn = fn; class cl1 { m() { return __awaiter(this, void 0, void 0, function* () { - const req = yield new Promise(function (resolve_2, reject_2) { require(['./test'], resolve_2, reject_2); }); // TWO + const req = yield new Promise((resolve_2, reject_2) => { require(['./test'], resolve_2, reject_2); }); // TWO }); } } exports.cl1 = cl1; exports.obj = { m: () => __awaiter(this, void 0, void 0, function* () { - const req = yield new Promise(function (resolve_3, reject_3) { require(['./test'], resolve_3, reject_3); }); // THREE + const req = yield new Promise((resolve_3, reject_3) => { require(['./test'], resolve_3, reject_3); }); // THREE }) }; class cl2 { constructor() { this.p = { m: () => __awaiter(this, void 0, void 0, function* () { - const req = yield new Promise(function (resolve_4, reject_4) { require(['./test'], resolve_4, reject_4); }); // FOUR + const req = yield new Promise((resolve_4, reject_4) => { require(['./test'], resolve_4, reject_4); }); // FOUR }) }; } } exports.cl2 = cl2; exports.l = () => __awaiter(this, void 0, void 0, function* () { - const req = yield new Promise(function (resolve_5, reject_5) { require(['./test'], resolve_5, reject_5); }); // FIVE + const req = yield new Promise((resolve_5, reject_5) => { require(['./test'], resolve_5, reject_5); }); // FIVE }); }); diff --git a/tests/baselines/reference/importCallExpressionAsyncES6CJS.js b/tests/baselines/reference/importCallExpressionAsyncES6CJS.js index b512ae94b48..20961f96330 100644 --- a/tests/baselines/reference/importCallExpressionAsyncES6CJS.js +++ b/tests/baselines/reference/importCallExpressionAsyncES6CJS.js @@ -41,33 +41,33 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge Object.defineProperty(exports, "__esModule", { value: true }); function fn() { return __awaiter(this, void 0, void 0, function* () { - const req = yield Promise.resolve().then(function () { return require('./test'); }); // ONE + const req = yield Promise.resolve().then(() => require('./test')); // ONE }); } exports.fn = fn; class cl1 { m() { return __awaiter(this, void 0, void 0, function* () { - const req = yield Promise.resolve().then(function () { return require('./test'); }); // TWO + const req = yield Promise.resolve().then(() => require('./test')); // TWO }); } } exports.cl1 = cl1; exports.obj = { m: () => __awaiter(this, void 0, void 0, function* () { - const req = yield Promise.resolve().then(function () { return require('./test'); }); // THREE + const req = yield Promise.resolve().then(() => require('./test')); // THREE }) }; class cl2 { constructor() { this.p = { m: () => __awaiter(this, void 0, void 0, function* () { - const req = yield Promise.resolve().then(function () { return require('./test'); }); // FOUR + const req = yield Promise.resolve().then(() => require('./test')); // FOUR }) }; } } exports.cl2 = cl2; exports.l = () => __awaiter(this, void 0, void 0, function* () { - const req = yield Promise.resolve().then(function () { return require('./test'); }); // FIVE + const req = yield Promise.resolve().then(() => require('./test')); // FIVE }); diff --git a/tests/baselines/reference/importCallExpressionAsyncES6UMD.js b/tests/baselines/reference/importCallExpressionAsyncES6UMD.js index f77d5150118..1d4aff02670 100644 --- a/tests/baselines/reference/importCallExpressionAsyncES6UMD.js +++ b/tests/baselines/reference/importCallExpressionAsyncES6UMD.js @@ -51,34 +51,34 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge Object.defineProperty(exports, "__esModule", { value: true }); function fn() { return __awaiter(this, void 0, void 0, function* () { - const req = yield __syncRequire ? Promise.resolve().then(function () { return require('./test'); }) : new Promise(function (resolve_1, reject_1) { require(['./test'], resolve_1, reject_1); }); // ONE + const req = yield __syncRequire ? Promise.resolve().then(() => require('./test')) : new Promise((resolve_1, reject_1) => { require(['./test'], resolve_1, reject_1); }); // ONE }); } exports.fn = fn; class cl1 { m() { return __awaiter(this, void 0, void 0, function* () { - const req = yield __syncRequire ? Promise.resolve().then(function () { return require('./test'); }) : new Promise(function (resolve_2, reject_2) { require(['./test'], resolve_2, reject_2); }); // TWO + const req = yield __syncRequire ? Promise.resolve().then(() => require('./test')) : new Promise((resolve_2, reject_2) => { require(['./test'], resolve_2, reject_2); }); // TWO }); } } exports.cl1 = cl1; exports.obj = { m: () => __awaiter(this, void 0, void 0, function* () { - const req = yield __syncRequire ? Promise.resolve().then(function () { return require('./test'); }) : new Promise(function (resolve_3, reject_3) { require(['./test'], resolve_3, reject_3); }); // THREE + const req = yield __syncRequire ? Promise.resolve().then(() => require('./test')) : new Promise((resolve_3, reject_3) => { require(['./test'], resolve_3, reject_3); }); // THREE }) }; class cl2 { constructor() { this.p = { m: () => __awaiter(this, void 0, void 0, function* () { - const req = yield __syncRequire ? Promise.resolve().then(function () { return require('./test'); }) : new Promise(function (resolve_4, reject_4) { require(['./test'], resolve_4, reject_4); }); // FOUR + const req = yield __syncRequire ? Promise.resolve().then(() => require('./test')) : new Promise((resolve_4, reject_4) => { require(['./test'], resolve_4, reject_4); }); // FOUR }) }; } } exports.cl2 = cl2; exports.l = () => __awaiter(this, void 0, void 0, function* () { - const req = yield __syncRequire ? Promise.resolve().then(function () { return require('./test'); }) : new Promise(function (resolve_5, reject_5) { require(['./test'], resolve_5, reject_5); }); // FIVE + const req = yield __syncRequire ? Promise.resolve().then(() => require('./test')) : new Promise((resolve_5, reject_5) => { require(['./test'], resolve_5, reject_5); }); // FIVE }); }); diff --git a/tests/baselines/reference/importCallExpressionCheckReturntype1.js b/tests/baselines/reference/importCallExpressionCheckReturntype1.js index facb6913388..3cc4893ff67 100644 --- a/tests/baselines/reference/importCallExpressionCheckReturntype1.js +++ b/tests/baselines/reference/importCallExpressionCheckReturntype1.js @@ -30,6 +30,6 @@ exports.C = C; //// [1.js] "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -let p1 = Promise.resolve().then(function () { return require("./defaultPath"); }); -let p2 = Promise.resolve().then(function () { return require("./defaultPath"); }); -let p3 = Promise.resolve().then(function () { return require("./defaultPath"); }); +let p1 = Promise.resolve().then(() => require("./defaultPath")); +let p2 = Promise.resolve().then(() => require("./defaultPath")); +let p3 = Promise.resolve().then(() => require("./defaultPath")); diff --git a/tests/baselines/reference/importCallExpressionDeclarationEmit1.js b/tests/baselines/reference/importCallExpressionDeclarationEmit1.js index 07f95d3b3b2..721d85abe71 100644 --- a/tests/baselines/reference/importCallExpressionDeclarationEmit1.js +++ b/tests/baselines/reference/importCallExpressionDeclarationEmit1.js @@ -15,12 +15,12 @@ function returnDynamicLoad(path: string) { } //// [importCallExpressionDeclarationEmit1.js] -Promise.resolve().then(function () { return require(getSpecifier()); }); -var p0 = Promise.resolve().then(function () { return require(`${directory}\${moduleFile}`); }); -var p1 = Promise.resolve().then(function () { return require(getSpecifier()); }); -const p2 = Promise.resolve().then(function () { return require(whatToLoad ? getSpecifier() : "defaulPath"); }); +Promise.resolve().then(() => require(getSpecifier())); +var p0 = Promise.resolve().then(() => require(`${directory}\${moduleFile}`)); +var p1 = Promise.resolve().then(() => require(getSpecifier())); +const p2 = Promise.resolve().then(() => require(whatToLoad ? getSpecifier() : "defaulPath")); function returnDynamicLoad(path) { - return Promise.resolve().then(function () { return require(path); }); + return Promise.resolve().then(() => require(path)); } diff --git a/tests/baselines/reference/importCallExpressionES6AMD.js b/tests/baselines/reference/importCallExpressionES6AMD.js index 08fec3b27fd..1c5430c9f04 100644 --- a/tests/baselines/reference/importCallExpressionES6AMD.js +++ b/tests/baselines/reference/importCallExpressionES6AMD.js @@ -39,23 +39,23 @@ define(["require", "exports"], function (require, exports) { define(["require", "exports"], function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); - new Promise(function (resolve_1, reject_1) { require(["./0"], resolve_1, reject_1); }); - var p1 = new Promise(function (resolve_2, reject_2) { require(["./0"], resolve_2, reject_2); }); + new Promise((resolve_1, reject_1) => { require(["./0"], resolve_1, reject_1); }); + var p1 = new Promise((resolve_2, reject_2) => { require(["./0"], resolve_2, reject_2); }); p1.then(zero => { return zero.foo(); }); - exports.p2 = new Promise(function (resolve_3, reject_3) { require(["./0"], resolve_3, reject_3); }); + exports.p2 = new Promise((resolve_3, reject_3) => { require(["./0"], resolve_3, reject_3); }); function foo() { - const p2 = new Promise(function (resolve_4, reject_4) { require(["./0"], resolve_4, reject_4); }); + const p2 = new Promise((resolve_4, reject_4) => { require(["./0"], resolve_4, reject_4); }); } class C { method() { - const loadAsync = new Promise(function (resolve_5, reject_5) { require(["./0"], resolve_5, reject_5); }); + const loadAsync = new Promise((resolve_5, reject_5) => { require(["./0"], resolve_5, reject_5); }); } } class D { method() { - const loadAsync = new Promise(function (resolve_6, reject_6) { require(["./0"], resolve_6, reject_6); }); + const loadAsync = new Promise((resolve_6, reject_6) => { require(["./0"], resolve_6, reject_6); }); } } exports.D = D; diff --git a/tests/baselines/reference/importCallExpressionES6CJS.js b/tests/baselines/reference/importCallExpressionES6CJS.js index 28833e17479..a1d8108bdea 100644 --- a/tests/baselines/reference/importCallExpressionES6CJS.js +++ b/tests/baselines/reference/importCallExpressionES6CJS.js @@ -36,23 +36,23 @@ exports.foo = foo; //// [1.js] "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -Promise.resolve().then(function () { return require("./0"); }); -var p1 = Promise.resolve().then(function () { return require("./0"); }); +Promise.resolve().then(() => require("./0")); +var p1 = Promise.resolve().then(() => require("./0")); p1.then(zero => { return zero.foo(); }); -exports.p2 = Promise.resolve().then(function () { return require("./0"); }); +exports.p2 = Promise.resolve().then(() => require("./0")); function foo() { - const p2 = Promise.resolve().then(function () { return require("./0"); }); + const p2 = Promise.resolve().then(() => require("./0")); } class C { method() { - const loadAsync = Promise.resolve().then(function () { return require("./0"); }); + const loadAsync = Promise.resolve().then(() => require("./0")); } } class D { method() { - const loadAsync = Promise.resolve().then(function () { return require("./0"); }); + const loadAsync = Promise.resolve().then(() => require("./0")); } } exports.D = D; diff --git a/tests/baselines/reference/importCallExpressionES6UMD.js b/tests/baselines/reference/importCallExpressionES6UMD.js index cc7dfef0074..750a1a7cc0c 100644 --- a/tests/baselines/reference/importCallExpressionES6UMD.js +++ b/tests/baselines/reference/importCallExpressionES6UMD.js @@ -56,23 +56,23 @@ export class D { "use strict"; var __syncRequire = typeof module === "object" && typeof module.exports === "object"; Object.defineProperty(exports, "__esModule", { value: true }); - __syncRequire ? Promise.resolve().then(function () { return require("./0"); }) : new Promise(function (resolve_1, reject_1) { require(["./0"], resolve_1, reject_1); }); - var p1 = __syncRequire ? Promise.resolve().then(function () { return require("./0"); }) : new Promise(function (resolve_2, reject_2) { require(["./0"], resolve_2, reject_2); }); + __syncRequire ? Promise.resolve().then(() => require("./0")) : new Promise((resolve_1, reject_1) => { require(["./0"], resolve_1, reject_1); }); + var p1 = __syncRequire ? Promise.resolve().then(() => require("./0")) : new Promise((resolve_2, reject_2) => { require(["./0"], resolve_2, reject_2); }); p1.then(zero => { return zero.foo(); }); - exports.p2 = __syncRequire ? Promise.resolve().then(function () { return require("./0"); }) : new Promise(function (resolve_3, reject_3) { require(["./0"], resolve_3, reject_3); }); + exports.p2 = __syncRequire ? Promise.resolve().then(() => require("./0")) : new Promise((resolve_3, reject_3) => { require(["./0"], resolve_3, reject_3); }); function foo() { - const p2 = __syncRequire ? Promise.resolve().then(function () { return require("./0"); }) : new Promise(function (resolve_4, reject_4) { require(["./0"], resolve_4, reject_4); }); + const p2 = __syncRequire ? Promise.resolve().then(() => require("./0")) : new Promise((resolve_4, reject_4) => { require(["./0"], resolve_4, reject_4); }); } class C { method() { - const loadAsync = __syncRequire ? Promise.resolve().then(function () { return require("./0"); }) : new Promise(function (resolve_5, reject_5) { require(["./0"], resolve_5, reject_5); }); + const loadAsync = __syncRequire ? Promise.resolve().then(() => require("./0")) : new Promise((resolve_5, reject_5) => { require(["./0"], resolve_5, reject_5); }); } } class D { method() { - const loadAsync = __syncRequire ? Promise.resolve().then(function () { return require("./0"); }) : new Promise(function (resolve_6, reject_6) { require(["./0"], resolve_6, reject_6); }); + const loadAsync = __syncRequire ? Promise.resolve().then(() => require("./0")) : new Promise((resolve_6, reject_6) => { require(["./0"], resolve_6, reject_6); }); } } exports.D = D; diff --git a/tests/baselines/reference/importCallExpressionGrammarError.js b/tests/baselines/reference/importCallExpressionGrammarError.js index b30b0c9ddd5..e2ffc55577d 100644 --- a/tests/baselines/reference/importCallExpressionGrammarError.js +++ b/tests/baselines/reference/importCallExpressionGrammarError.js @@ -12,8 +12,8 @@ const p4 = import("pathToModule", "secondModule"); //// [importCallExpressionGrammarError.js] var a = ["./0"]; -Promise.resolve().then(function () { return require(...["PathModule"]); }); -var p1 = Promise.resolve().then(function () { return require(...a); }); -const p2 = Promise.resolve().then(function () { return require(); }); -const p3 = Promise.resolve().then(function () { return require(); }); -const p4 = Promise.resolve().then(function () { return require("pathToModule", "secondModule"); }); +Promise.resolve().then(() => require(...["PathModule"])); +var p1 = Promise.resolve().then(() => require(...a)); +const p2 = Promise.resolve().then(() => require()); +const p3 = Promise.resolve().then(() => require()); +const p4 = Promise.resolve().then(() => require("pathToModule", "secondModule")); diff --git a/tests/baselines/reference/importCallExpressionInAMD1.js b/tests/baselines/reference/importCallExpressionInAMD1.js index 5c858160353..64e5aee2dda 100644 --- a/tests/baselines/reference/importCallExpressionInAMD1.js +++ b/tests/baselines/reference/importCallExpressionInAMD1.js @@ -27,13 +27,13 @@ define(["require", "exports"], function (require, exports) { define(["require", "exports"], function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); - new Promise(function (resolve_1, reject_1) { require(["./0"], resolve_1, reject_1); }); - var p1 = new Promise(function (resolve_2, reject_2) { require(["./0"], resolve_2, reject_2); }); + new Promise((resolve_1, reject_1) => { require(["./0"], resolve_1, reject_1); }); + var p1 = new Promise((resolve_2, reject_2) => { require(["./0"], resolve_2, reject_2); }); p1.then(zero => { return zero.foo(); }); - exports.p2 = new Promise(function (resolve_3, reject_3) { require(["./0"], resolve_3, reject_3); }); + exports.p2 = new Promise((resolve_3, reject_3) => { require(["./0"], resolve_3, reject_3); }); function foo() { - const p2 = new Promise(function (resolve_4, reject_4) { require(["./0"], resolve_4, reject_4); }); + const p2 = new Promise((resolve_4, reject_4) => { require(["./0"], resolve_4, reject_4); }); } }); diff --git a/tests/baselines/reference/importCallExpressionInAMD2.js b/tests/baselines/reference/importCallExpressionInAMD2.js index 7347e2f8105..0d3f0e08d21 100644 --- a/tests/baselines/reference/importCallExpressionInAMD2.js +++ b/tests/baselines/reference/importCallExpressionInAMD2.js @@ -35,5 +35,5 @@ define(["require", "exports"], function (require, exports) { b.print(); }); } - foo(new Promise(function (resolve_1, reject_1) { require(["./0"], resolve_1, reject_1); })); + foo(new Promise((resolve_1, reject_1) => { require(["./0"], resolve_1, reject_1); })); }); diff --git a/tests/baselines/reference/importCallExpressionInAMD3.js b/tests/baselines/reference/importCallExpressionInAMD3.js index 471f35a6415..07e7e922541 100644 --- a/tests/baselines/reference/importCallExpressionInAMD3.js +++ b/tests/baselines/reference/importCallExpressionInAMD3.js @@ -26,7 +26,7 @@ define(["require", "exports"], function (require, exports) { define(["require", "exports"], function (require, exports) { "use strict"; async function foo() { - class C extends (await new Promise(function (resolve_1, reject_1) { require(["./0"], resolve_1, reject_1); })).B { + class C extends (await new Promise((resolve_1, reject_1) => { require(["./0"], resolve_1, reject_1); })).B { } var c = new C(); c.print(); diff --git a/tests/baselines/reference/importCallExpressionInAMD4.js b/tests/baselines/reference/importCallExpressionInAMD4.js index 43ba5afcd30..2fe29e5ae06 100644 --- a/tests/baselines/reference/importCallExpressionInAMD4.js +++ b/tests/baselines/reference/importCallExpressionInAMD4.js @@ -64,30 +64,30 @@ define(["require", "exports"], function (require, exports) { Object.defineProperty(exports, "__esModule", { value: true }); class C { constructor() { - this.myModule = new Promise(function (resolve_1, reject_1) { require(["./0"], resolve_1, reject_1); }); + this.myModule = new Promise((resolve_1, reject_1) => { require(["./0"], resolve_1, reject_1); }); } method() { - const loadAsync = new Promise(function (resolve_2, reject_2) { require(["./0"], resolve_2, reject_2); }); + const loadAsync = new Promise((resolve_2, reject_2) => { require(["./0"], resolve_2, reject_2); }); this.myModule.then(Zero => { console.log(Zero.foo()); }, async (err) => { console.log(err); - let one = await new Promise(function (resolve_3, reject_3) { require(["./1"], resolve_3, reject_3); }); + let one = await new Promise((resolve_3, reject_3) => { require(["./1"], resolve_3, reject_3); }); console.log(one.backup()); }); } } class D { constructor() { - this.myModule = new Promise(function (resolve_4, reject_4) { require(["./0"], resolve_4, reject_4); }); + this.myModule = new Promise((resolve_4, reject_4) => { require(["./0"], resolve_4, reject_4); }); } method() { - const loadAsync = new Promise(function (resolve_5, reject_5) { require(["./0"], resolve_5, reject_5); }); + const loadAsync = new Promise((resolve_5, reject_5) => { require(["./0"], resolve_5, reject_5); }); this.myModule.then(Zero => { console.log(Zero.foo()); }, async (err) => { console.log(err); - let one = await new Promise(function (resolve_6, reject_6) { require(["./1"], resolve_6, reject_6); }); + let one = await new Promise((resolve_6, reject_6) => { require(["./1"], resolve_6, reject_6); }); console.log(one.backup()); }); } diff --git a/tests/baselines/reference/importCallExpressionInCJS1.js b/tests/baselines/reference/importCallExpressionInCJS1.js index 359e743144b..c814f5e5671 100644 --- a/tests/baselines/reference/importCallExpressionInCJS1.js +++ b/tests/baselines/reference/importCallExpressionInCJS1.js @@ -24,12 +24,12 @@ exports.foo = foo; //// [1.js] "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -Promise.resolve().then(function () { return require("./0"); }); -var p1 = Promise.resolve().then(function () { return require("./0"); }); +Promise.resolve().then(() => require("./0")); +var p1 = Promise.resolve().then(() => require("./0")); p1.then(zero => { return zero.foo(); }); -exports.p2 = Promise.resolve().then(function () { return require("./0"); }); +exports.p2 = Promise.resolve().then(() => require("./0")); function foo() { - const p2 = Promise.resolve().then(function () { return require("./0"); }); + const p2 = Promise.resolve().then(() => require("./0")); } diff --git a/tests/baselines/reference/importCallExpressionInCJS2.js b/tests/baselines/reference/importCallExpressionInCJS2.js index aa983a7a2fe..fb559cb1193 100644 --- a/tests/baselines/reference/importCallExpressionInCJS2.js +++ b/tests/baselines/reference/importCallExpressionInCJS2.js @@ -32,9 +32,9 @@ exports.backup = backup; async function compute(promise) { let j = await promise; if (!j) { - j = await Promise.resolve().then(function () { return require("./1"); }); + j = await Promise.resolve().then(() => require("./1")); return j.backup(); } return j.foo(); } -compute(Promise.resolve().then(function () { return require("./0"); })); +compute(Promise.resolve().then(() => require("./0"))); diff --git a/tests/baselines/reference/importCallExpressionInCJS3.js b/tests/baselines/reference/importCallExpressionInCJS3.js index 2f956d9ac3a..616fbc9c3f9 100644 --- a/tests/baselines/reference/importCallExpressionInCJS3.js +++ b/tests/baselines/reference/importCallExpressionInCJS3.js @@ -31,4 +31,4 @@ function foo(x) { b.print(); }); } -foo(Promise.resolve().then(function () { return require("./0"); })); +foo(Promise.resolve().then(() => require("./0"))); diff --git a/tests/baselines/reference/importCallExpressionInCJS4.js b/tests/baselines/reference/importCallExpressionInCJS4.js index 554a0b222ab..b88295110b8 100644 --- a/tests/baselines/reference/importCallExpressionInCJS4.js +++ b/tests/baselines/reference/importCallExpressionInCJS4.js @@ -22,7 +22,7 @@ class B { exports.B = B; //// [2.js] async function foo() { - class C extends (await Promise.resolve().then(function () { return require("./0"); })).B { + class C extends (await Promise.resolve().then(() => require("./0"))).B { } var c = new C(); c.print(); diff --git a/tests/baselines/reference/importCallExpressionInCJS5.js b/tests/baselines/reference/importCallExpressionInCJS5.js index eeb4db275fa..b32b0e52c50 100644 --- a/tests/baselines/reference/importCallExpressionInCJS5.js +++ b/tests/baselines/reference/importCallExpressionInCJS5.js @@ -59,30 +59,30 @@ exports.backup = backup; Object.defineProperty(exports, "__esModule", { value: true }); class C { constructor() { - this.myModule = Promise.resolve().then(function () { return require("./0"); }); + this.myModule = Promise.resolve().then(() => require("./0")); } method() { - const loadAsync = Promise.resolve().then(function () { return require("./0"); }); + const loadAsync = Promise.resolve().then(() => require("./0")); this.myModule.then(Zero => { console.log(Zero.foo()); }, async (err) => { console.log(err); - let one = await Promise.resolve().then(function () { return require("./1"); }); + let one = await Promise.resolve().then(() => require("./1")); console.log(one.backup()); }); } } class D { constructor() { - this.myModule = Promise.resolve().then(function () { return require("./0"); }); + this.myModule = Promise.resolve().then(() => require("./0")); } method() { - const loadAsync = Promise.resolve().then(function () { return require("./0"); }); + const loadAsync = Promise.resolve().then(() => require("./0")); this.myModule.then(Zero => { console.log(Zero.foo()); }, async (err) => { console.log(err); - let one = await Promise.resolve().then(function () { return require("./1"); }); + let one = await Promise.resolve().then(() => require("./1")); console.log(one.backup()); }); } diff --git a/tests/baselines/reference/importCallExpressionInExportEqualsAMD.js b/tests/baselines/reference/importCallExpressionInExportEqualsAMD.js index f2fda1fadd7..1fcef2bde39 100644 --- a/tests/baselines/reference/importCallExpressionInExportEqualsAMD.js +++ b/tests/baselines/reference/importCallExpressionInExportEqualsAMD.js @@ -17,6 +17,6 @@ define(["require", "exports"], function (require, exports) { define(["require", "exports"], function (require, exports) { "use strict"; return async function () { - const something = await new Promise(function (resolve_1, reject_1) { require(["./something"], resolve_1, reject_1); }); + const something = await new Promise((resolve_1, reject_1) => { require(["./something"], resolve_1, reject_1); }); }; }); diff --git a/tests/baselines/reference/importCallExpressionInExportEqualsCJS.js b/tests/baselines/reference/importCallExpressionInExportEqualsCJS.js index 5d7e2816116..72e3a0ec0af 100644 --- a/tests/baselines/reference/importCallExpressionInExportEqualsCJS.js +++ b/tests/baselines/reference/importCallExpressionInExportEqualsCJS.js @@ -14,5 +14,5 @@ module.exports = 42; //// [index.js] "use strict"; module.exports = async function () { - const something = await Promise.resolve().then(function () { return require("./something"); }); + const something = await Promise.resolve().then(() => require("./something")); }; diff --git a/tests/baselines/reference/importCallExpressionInExportEqualsUMD.js b/tests/baselines/reference/importCallExpressionInExportEqualsUMD.js index e0c6e2a925f..5f70891b09e 100644 --- a/tests/baselines/reference/importCallExpressionInExportEqualsUMD.js +++ b/tests/baselines/reference/importCallExpressionInExportEqualsUMD.js @@ -34,6 +34,6 @@ export = async function() { "use strict"; var __syncRequire = typeof module === "object" && typeof module.exports === "object"; return async function () { - const something = await (__syncRequire ? Promise.resolve().then(function () { return require("./something"); }) : new Promise(function (resolve_1, reject_1) { require(["./something"], resolve_1, reject_1); })); + const something = await (__syncRequire ? Promise.resolve().then(() => require("./something")) : new Promise((resolve_1, reject_1) => { require(["./something"], resolve_1, reject_1); })); }; }); diff --git a/tests/baselines/reference/importCallExpressionInScriptContext1.js b/tests/baselines/reference/importCallExpressionInScriptContext1.js index 2c2d2f904d5..53c6118f61b 100644 --- a/tests/baselines/reference/importCallExpressionInScriptContext1.js +++ b/tests/baselines/reference/importCallExpressionInScriptContext1.js @@ -13,5 +13,5 @@ Object.defineProperty(exports, "__esModule", { value: true }); function foo() { return "foo"; } exports.foo = foo; //// [1.js] -var p1 = Promise.resolve().then(function () { return require("./0"); }); +var p1 = Promise.resolve().then(() => require("./0")); function arguments() { } // this is allow as the file doesn't have implicit "use strict" diff --git a/tests/baselines/reference/importCallExpressionInScriptContext2.js b/tests/baselines/reference/importCallExpressionInScriptContext2.js index 6b6e0109fda..4a0d4a1bf5a 100644 --- a/tests/baselines/reference/importCallExpressionInScriptContext2.js +++ b/tests/baselines/reference/importCallExpressionInScriptContext2.js @@ -15,5 +15,5 @@ function foo() { return "foo"; } exports.foo = foo; //// [1.js] "use strict"; -var p1 = Promise.resolve().then(function () { return require("./0"); }); +var p1 = Promise.resolve().then(() => require("./0")); function arguments() { } diff --git a/tests/baselines/reference/importCallExpressionInUMD1.js b/tests/baselines/reference/importCallExpressionInUMD1.js index ee99468f7f3..597e68e2d6f 100644 --- a/tests/baselines/reference/importCallExpressionInUMD1.js +++ b/tests/baselines/reference/importCallExpressionInUMD1.js @@ -44,13 +44,13 @@ function foo() { "use strict"; var __syncRequire = typeof module === "object" && typeof module.exports === "object"; Object.defineProperty(exports, "__esModule", { value: true }); - __syncRequire ? Promise.resolve().then(function () { return require("./0"); }) : new Promise(function (resolve_1, reject_1) { require(["./0"], resolve_1, reject_1); }); - var p1 = __syncRequire ? Promise.resolve().then(function () { return require("./0"); }) : new Promise(function (resolve_2, reject_2) { require(["./0"], resolve_2, reject_2); }); + __syncRequire ? Promise.resolve().then(() => require("./0")) : new Promise((resolve_1, reject_1) => { require(["./0"], resolve_1, reject_1); }); + var p1 = __syncRequire ? Promise.resolve().then(() => require("./0")) : new Promise((resolve_2, reject_2) => { require(["./0"], resolve_2, reject_2); }); p1.then(zero => { return zero.foo(); }); - exports.p2 = __syncRequire ? Promise.resolve().then(function () { return require("./0"); }) : new Promise(function (resolve_3, reject_3) { require(["./0"], resolve_3, reject_3); }); + exports.p2 = __syncRequire ? Promise.resolve().then(() => require("./0")) : new Promise((resolve_3, reject_3) => { require(["./0"], resolve_3, reject_3); }); function foo() { - const p2 = __syncRequire ? Promise.resolve().then(function () { return require("./0"); }) : new Promise(function (resolve_4, reject_4) { require(["./0"], resolve_4, reject_4); }); + const p2 = __syncRequire ? Promise.resolve().then(() => require("./0")) : new Promise((resolve_4, reject_4) => { require(["./0"], resolve_4, reject_4); }); } }); diff --git a/tests/baselines/reference/importCallExpressionInUMD2.js b/tests/baselines/reference/importCallExpressionInUMD2.js index db8b87a2f79..516800968c1 100644 --- a/tests/baselines/reference/importCallExpressionInUMD2.js +++ b/tests/baselines/reference/importCallExpressionInUMD2.js @@ -52,5 +52,5 @@ foo(import("./0")); b.print(); }); } - foo(__syncRequire ? Promise.resolve().then(function () { return require("./0"); }) : new Promise(function (resolve_1, reject_1) { require(["./0"], resolve_1, reject_1); })); + foo(__syncRequire ? Promise.resolve().then(() => require("./0")) : new Promise((resolve_1, reject_1) => { require(["./0"], resolve_1, reject_1); })); }); diff --git a/tests/baselines/reference/importCallExpressionInUMD3.js b/tests/baselines/reference/importCallExpressionInUMD3.js index 41106e3ab78..57d200ca70c 100644 --- a/tests/baselines/reference/importCallExpressionInUMD3.js +++ b/tests/baselines/reference/importCallExpressionInUMD3.js @@ -43,7 +43,7 @@ foo(); "use strict"; var __syncRequire = typeof module === "object" && typeof module.exports === "object"; async function foo() { - class C extends (await (__syncRequire ? Promise.resolve().then(function () { return require("./0"); }) : new Promise(function (resolve_1, reject_1) { require(["./0"], resolve_1, reject_1); }))).B { + class C extends (await (__syncRequire ? Promise.resolve().then(() => require("./0")) : new Promise((resolve_1, reject_1) => { require(["./0"], resolve_1, reject_1); }))).B { } var c = new C(); c.print(); diff --git a/tests/baselines/reference/importCallExpressionInUMD4.js b/tests/baselines/reference/importCallExpressionInUMD4.js index 477a7826bc0..70a574f0302 100644 --- a/tests/baselines/reference/importCallExpressionInUMD4.js +++ b/tests/baselines/reference/importCallExpressionInUMD4.js @@ -89,30 +89,30 @@ export class D { Object.defineProperty(exports, "__esModule", { value: true }); class C { constructor() { - this.myModule = __syncRequire ? Promise.resolve().then(function () { return require("./0"); }) : new Promise(function (resolve_1, reject_1) { require(["./0"], resolve_1, reject_1); }); + this.myModule = __syncRequire ? Promise.resolve().then(() => require("./0")) : new Promise((resolve_1, reject_1) => { require(["./0"], resolve_1, reject_1); }); } method() { - const loadAsync = __syncRequire ? Promise.resolve().then(function () { return require("./0"); }) : new Promise(function (resolve_2, reject_2) { require(["./0"], resolve_2, reject_2); }); + const loadAsync = __syncRequire ? Promise.resolve().then(() => require("./0")) : new Promise((resolve_2, reject_2) => { require(["./0"], resolve_2, reject_2); }); this.myModule.then(Zero => { console.log(Zero.foo()); }, async (err) => { console.log(err); - let one = await (__syncRequire ? Promise.resolve().then(function () { return require("./1"); }) : new Promise(function (resolve_3, reject_3) { require(["./1"], resolve_3, reject_3); })); + let one = await (__syncRequire ? Promise.resolve().then(() => require("./1")) : new Promise((resolve_3, reject_3) => { require(["./1"], resolve_3, reject_3); })); console.log(one.backup()); }); } } class D { constructor() { - this.myModule = __syncRequire ? Promise.resolve().then(function () { return require("./0"); }) : new Promise(function (resolve_4, reject_4) { require(["./0"], resolve_4, reject_4); }); + this.myModule = __syncRequire ? Promise.resolve().then(() => require("./0")) : new Promise((resolve_4, reject_4) => { require(["./0"], resolve_4, reject_4); }); } method() { - const loadAsync = __syncRequire ? Promise.resolve().then(function () { return require("./0"); }) : new Promise(function (resolve_5, reject_5) { require(["./0"], resolve_5, reject_5); }); + const loadAsync = __syncRequire ? Promise.resolve().then(() => require("./0")) : new Promise((resolve_5, reject_5) => { require(["./0"], resolve_5, reject_5); }); this.myModule.then(Zero => { console.log(Zero.foo()); }, async (err) => { console.log(err); - let one = await (__syncRequire ? Promise.resolve().then(function () { return require("./1"); }) : new Promise(function (resolve_6, reject_6) { require(["./1"], resolve_6, reject_6); })); + let one = await (__syncRequire ? Promise.resolve().then(() => require("./1")) : new Promise((resolve_6, reject_6) => { require(["./1"], resolve_6, reject_6); })); console.log(one.backup()); }); } diff --git a/tests/baselines/reference/importCallExpressionReturnPromiseOfAny.js b/tests/baselines/reference/importCallExpressionReturnPromiseOfAny.js index 728d6636953..8c94510b609 100644 --- a/tests/baselines/reference/importCallExpressionReturnPromiseOfAny.js +++ b/tests/baselines/reference/importCallExpressionReturnPromiseOfAny.js @@ -42,20 +42,20 @@ exports.C = C; //// [1.js] "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -Promise.resolve().then(function () { return require(`${directory}\${moduleFile}`); }); -Promise.resolve().then(function () { return require(getSpecifier()); }); -var p1 = Promise.resolve().then(function () { return require(ValidSomeCondition() ? "./0" : "externalModule"); }); -var p1 = Promise.resolve().then(function () { return require(getSpecifier()); }); -var p11 = Promise.resolve().then(function () { return require(getSpecifier()); }); -const p2 = Promise.resolve().then(function () { return require(whatToLoad ? getSpecifier() : "defaulPath"); }); +Promise.resolve().then(() => require(`${directory}\${moduleFile}`)); +Promise.resolve().then(() => require(getSpecifier())); +var p1 = Promise.resolve().then(() => require(ValidSomeCondition() ? "./0" : "externalModule")); +var p1 = Promise.resolve().then(() => require(getSpecifier())); +var p11 = Promise.resolve().then(() => require(getSpecifier())); +const p2 = Promise.resolve().then(() => require(whatToLoad ? getSpecifier() : "defaulPath")); p1.then(zero => { return zero.foo(); // ok, zero is any }); let j; -var p3 = Promise.resolve().then(function () { return require(j = getSpecifier()); }); +var p3 = Promise.resolve().then(() => require(j = getSpecifier())); function* loadModule(directories) { for (const directory of directories) { const path = `${directory}\moduleFile`; - Promise.resolve().then(function () { return require(yield path); }); + Promise.resolve().then(() => require(yield path)); } } diff --git a/tests/baselines/reference/importCallExpressionSpecifierNotStringTypeError.js b/tests/baselines/reference/importCallExpressionSpecifierNotStringTypeError.js index dde35d8048b..5e2ace1c401 100644 --- a/tests/baselines/reference/importCallExpressionSpecifierNotStringTypeError.js +++ b/tests/baselines/reference/importCallExpressionSpecifierNotStringTypeError.js @@ -15,11 +15,11 @@ var p4 = import(()=>"PathToModule"); //// [importCallExpressionSpecifierNotStringTypeError.js] // Error specifier is not assignable to string -Promise.resolve().then(function () { return require(getSpecifier()); }); -var p1 = Promise.resolve().then(function () { return require(getSpecifier()); }); -const p2 = Promise.resolve().then(function () { return require(whatToLoad ? getSpecifier() : "defaulPath"); }); +Promise.resolve().then(() => require(getSpecifier())); +var p1 = Promise.resolve().then(() => require(getSpecifier())); +const p2 = Promise.resolve().then(() => require(whatToLoad ? getSpecifier() : "defaulPath")); p1.then(zero => { return zero.foo(); // ok, zero is any }); -var p3 = Promise.resolve().then(function () { return require(["path1", "path2"]); }); -var p4 = Promise.resolve().then(function () { return require(() => "PathToModule"); }); +var p3 = Promise.resolve().then(() => require(["path1", "path2"])); +var p4 = Promise.resolve().then(() => require(() => "PathToModule")); diff --git a/tests/baselines/reference/importCallExpressionWithTypeArgument.js b/tests/baselines/reference/importCallExpressionWithTypeArgument.js index 2915669eae5..885992a798b 100644 --- a/tests/baselines/reference/importCallExpressionWithTypeArgument.js +++ b/tests/baselines/reference/importCallExpressionWithTypeArgument.js @@ -15,5 +15,5 @@ function foo() { return "foo"; } exports.foo = foo; //// [1.js] "use strict"; -var p1 = Promise.resolve().then(function () { return require("./0"); }); // error -var p2 = Promise.resolve().then(function () { return require("./0"); }); // error +var p1 = Promise.resolve().then(() => require("./0")); // error +var p2 = Promise.resolve().then(() => require("./0")); // error From 3eeb54861d310a37518e81642bee77120c097e00 Mon Sep 17 00:00:00 2001 From: Andy Date: Tue, 10 Oct 2017 10:53:43 -0700 Subject: [PATCH 53/54] Fix invalid cast (#18821) --- src/compiler/binder.ts | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 48cace44841..4cacb5765db 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -2271,16 +2271,13 @@ namespace ts { function isExportsOrModuleExportsOrAlias(node: Node): boolean { return isExportsIdentifier(node) || isModuleExportsPropertyAccessExpression(node) || - isNameOfExportsOrModuleExportsAliasDeclaration(node); + isIdentifier(node) && isNameOfExportsOrModuleExportsAliasDeclaration(node); } - function isNameOfExportsOrModuleExportsAliasDeclaration(node: Node) { - if (isIdentifier(node)) { - const symbol = lookupSymbolForName(node.escapedText); - return symbol && symbol.valueDeclaration && isVariableDeclaration(symbol.valueDeclaration) && - symbol.valueDeclaration.initializer && isExportsOrModuleExportsOrAliasOrAssignment(symbol.valueDeclaration.initializer); - } - return false; + function isNameOfExportsOrModuleExportsAliasDeclaration(node: Identifier): boolean { + const symbol = lookupSymbolForName(node.escapedText); + return symbol && symbol.valueDeclaration && isVariableDeclaration(symbol.valueDeclaration) && + symbol.valueDeclaration.initializer && isExportsOrModuleExportsOrAliasOrAssignment(symbol.valueDeclaration.initializer); } function isExportsOrModuleExportsOrAliasOrAssignment(node: Node): boolean { @@ -2354,20 +2351,22 @@ namespace ts { // Look up the function in the local scope, since prototype assignments should // follow the function declaration const leftSideOfAssignment = node.left as PropertyAccessExpression; - const target = leftSideOfAssignment.expression as Identifier; + const target = leftSideOfAssignment.expression; - // Fix up parent pointers since we're going to use these nodes before we bind into them - leftSideOfAssignment.parent = node; - target.parent = leftSideOfAssignment; + if (isIdentifier(target)) { + // Fix up parent pointers since we're going to use these nodes before we bind into them + leftSideOfAssignment.parent = node; + target.parent = leftSideOfAssignment; - if (isNameOfExportsOrModuleExportsAliasDeclaration(target)) { - // This can be an alias for the 'exports' or 'module.exports' names, e.g. - // var util = module.exports; - // util.property = function ... - bindExportsPropertyAssignment(node); - } - else { - bindPropertyAssignment(target.escapedText, leftSideOfAssignment, /*isPrototypeProperty*/ false); + if (isNameOfExportsOrModuleExportsAliasDeclaration(target)) { + // This can be an alias for the 'exports' or 'module.exports' names, e.g. + // var util = module.exports; + // util.property = function ... + bindExportsPropertyAssignment(node); + } + else { + bindPropertyAssignment(target.escapedText, leftSideOfAssignment, /*isPrototypeProperty*/ false); + } } } From 9ccc1b48873bb2cd1e1a0cf9eabc0232c26c200d Mon Sep 17 00:00:00 2001 From: Andy Date: Tue, 10 Oct 2017 10:54:29 -0700 Subject: [PATCH 54/54] Remove unnecessary uses of `any` in shims.ts (#19038) --- src/services/shims.ts | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/services/shims.ts b/src/services/shims.ts index 737db44b83f..9d4baccc3c4 100644 --- a/src/services/shims.ts +++ b/src/services/shims.ts @@ -16,7 +16,7 @@ /// /* @internal */ -let debugObjectHost = (function (this: any) { return this; })(); +let debugObjectHost: { CollectGarbage(): void } = (function (this: any) { return this; })(); // We need to use 'null' to interface with the managed side. /* tslint:disable:no-null-keyword */ @@ -119,13 +119,13 @@ namespace ts { } export interface Shim { - dispose(_dummy: any): void; + dispose(_dummy: {}): void; } export interface LanguageServiceShim extends Shim { languageService: LanguageService; - dispose(_dummy: any): void; + dispose(_dummy: {}): void; refresh(throwOnError: boolean): void; @@ -417,7 +417,7 @@ namespace ts { return this.shimHost.getScriptVersion(fileName); } - public getLocalizedDiagnosticMessages(): any { + public getLocalizedDiagnosticMessages() { const diagnosticMessagesJson = this.shimHost.getLocalizedDiagnosticMessages(); if (diagnosticMessagesJson === null || diagnosticMessagesJson === "") { return null; @@ -515,7 +515,7 @@ namespace ts { } } - function simpleForwardCall(logger: Logger, actionDescription: string, action: () => any, logPerformance: boolean): any { + function simpleForwardCall(logger: Logger, actionDescription: string, action: () => {}, logPerformance: boolean): {} { let start: number; if (logPerformance) { logger.log(actionDescription); @@ -539,14 +539,14 @@ namespace ts { return result; } - function forwardJSONCall(logger: Logger, actionDescription: string, action: () => any, logPerformance: boolean): string { + function forwardJSONCall(logger: Logger, actionDescription: string, action: () => {}, logPerformance: boolean): string { return forwardCall(logger, actionDescription, /*returnJson*/ true, action, logPerformance); } function forwardCall(logger: Logger, actionDescription: string, returnJson: boolean, action: () => T, logPerformance: boolean): T | string { try { const result = simpleForwardCall(logger, actionDescription, action, logPerformance); - return returnJson ? JSON.stringify({ result }) : result; + return returnJson ? JSON.stringify({ result }) : result as T; } catch (err) { if (err instanceof OperationCanceledException) { @@ -563,7 +563,7 @@ namespace ts { constructor(private factory: ShimFactory) { factory.registerShim(this); } - public dispose(_dummy: any): void { + public dispose(_dummy: {}): void { this.factory.unregisterShim(this); } } @@ -601,7 +601,7 @@ namespace ts { this.logger = this.host; } - public forwardJSONCall(actionDescription: string, action: () => any): string { + public forwardJSONCall(actionDescription: string, action: () => {}): string { return forwardJSONCall(this.logger, actionDescription, action, this.logPerformance); } @@ -611,7 +611,7 @@ namespace ts { * Ensure (almost) deterministic release of internal Javascript resources when * some external native objects holds onto us (e.g. Com/Interop). */ - public dispose(dummy: any): void { + public dispose(dummy: {}): void { this.logger.log("dispose()"); this.languageService.dispose(); this.languageService = null; @@ -635,7 +635,7 @@ namespace ts { public refresh(throwOnError: boolean): void { this.forwardJSONCall( `refresh(${throwOnError})`, - () => null + () => null ); } @@ -644,7 +644,7 @@ namespace ts { "cleanupSemanticCache()", () => { this.languageService.cleanupSemanticCache(); - return null; + return null; }); } @@ -980,13 +980,13 @@ namespace ts { ); } - public getEmitOutputObject(fileName: string): any { + public getEmitOutputObject(fileName: string): EmitOutput { return forwardCall( this.logger, `getEmitOutput('${fileName}')`, /*returnJson*/ false, () => this.languageService.getEmitOutput(fileName), - this.logPerformance); + this.logPerformance) as EmitOutput; } } @@ -1030,7 +1030,7 @@ namespace ts { super(factory); } - private forwardJSONCall(actionDescription: string, action: () => any): any { + private forwardJSONCall(actionDescription: string, action: () => {}): string { return forwardJSONCall(this.logger, actionDescription, action, this.logPerformance); } @@ -1221,7 +1221,7 @@ namespace ts { // Here we expose the TypeScript services as an external module // so that it may be consumed easily like a node module. - declare const module: any; + declare const module: { exports: {} }; if (typeof module !== "undefined" && module.exports) { module.exports = ts; }