From dfee3de3a664d2925110b919e3f3d0b780732fe4 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Thu, 21 Apr 2016 10:57:54 -0700 Subject: [PATCH 01/24] Add test case for #8229 --- ...tAnyDestructuringVarDeclaration.errors.txt | 18 ++++-- ...oImplicitAnyDestructuringVarDeclaration.js | 4 +- ...AnyDestructuringVarDeclaration2.errors.txt | 63 +++++++++++++++++++ ...ImplicitAnyDestructuringVarDeclaration2.js | 25 ++++++++ ...oImplicitAnyDestructuringVarDeclaration.ts | 2 +- ...ImplicitAnyDestructuringVarDeclaration2.ts | 12 ++++ 6 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.errors.txt create mode 100644 tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.js create mode 100644 tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts diff --git a/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration.errors.txt b/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration.errors.txt index 10d6b05c62203..e54261b117f39 100644 --- a/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration.errors.txt +++ b/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration.errors.txt @@ -15,11 +15,13 @@ tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(5,18): error TS tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(7,5): error TS1182: A destructuring declaration must have an initializer. tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(7,13): error TS7008: Member 'b3' implicitly has an 'any' type. tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(7,25): error TS7008: Member 'b3' implicitly has an 'any' type. -tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(9,6): error TS7031: Binding element 'a1' implicitly has an 'any' type. -tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(9,26): error TS7031: Binding element 'b1' implicitly has an 'any' type. +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(9,6): error TS7031: Binding element 'a4' implicitly has an 'any' type. +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(9,26): error TS7031: Binding element 'b4' implicitly has an 'any' type. +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(9,46): error TS7005: Variable 'c4' implicitly has an 'any' type. +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(9,62): error TS7005: Variable 'd4' implicitly has an 'any' type. -==== tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts (19 errors) ==== +==== tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts (21 errors) ==== var [a], {b}, c, d; // error ~~~ !!! error TS1182: A destructuring declaration must have an initializer. @@ -62,8 +64,12 @@ tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(9,26): error TS ~~ !!! error TS7008: Member 'b3' implicitly has an 'any' type. - var [a1] = [undefined], {b1} = { b1: null }, c1 = undefined, d1 = null; // error + var [a4] = [undefined], {b4} = { b4: null }, c4 = undefined, d4 = null; // error ~~ -!!! error TS7031: Binding element 'a1' implicitly has an 'any' type. +!!! error TS7031: Binding element 'a4' implicitly has an 'any' type. ~~ -!!! error TS7031: Binding element 'b1' implicitly has an 'any' type. \ No newline at end of file +!!! error TS7031: Binding element 'b4' implicitly has an 'any' type. + ~~ +!!! error TS7005: Variable 'c4' implicitly has an 'any' type. + ~~ +!!! error TS7005: Variable 'd4' implicitly has an 'any' type. \ No newline at end of file diff --git a/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration.js b/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration.js index cbc15c01e9e6d..4e9606d9c5380 100644 --- a/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration.js +++ b/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration.js @@ -7,11 +7,11 @@ var [a2]: [any], {b2}: { b2: any }, c2: any, d2: any; var {b3}: { b3 }, c3: { b3 }; // error in type instead -var [a1] = [undefined], {b1} = { b1: null }, c1 = undefined, d1 = null; // error +var [a4] = [undefined], {b4} = { b4: null }, c4 = undefined, d4 = null; // error //// [noImplicitAnyDestructuringVarDeclaration.js] var a = (void 0)[0], b = (void 0).b, c, d; // error var _a = (void 0)[0], a1 = _a === void 0 ? undefined : _a, _b = (void 0).b1, b1 = _b === void 0 ? null : _b, c1 = undefined, d1 = null; // error var a2 = (void 0)[0], b2 = (void 0).b2, c2, d2; var b3 = (void 0).b3, c3; // error in type instead -var a1 = [undefined][0], b1 = { b1: null }.b1, c1 = undefined, d1 = null; // error +var a4 = [undefined][0], b4 = { b4: null }.b4, c4 = undefined, d4 = null; // error diff --git a/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.errors.txt b/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.errors.txt new file mode 100644 index 0000000000000..d84a297566582 --- /dev/null +++ b/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.errors.txt @@ -0,0 +1,63 @@ +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(1,6): error TS7031: Binding element 'a' implicitly has an 'any' type. +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(1,9): error TS7031: Binding element 'b' implicitly has an 'any' type. +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(1,12): error TS7031: Binding element 'c' implicitly has an 'any' type. +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(3,6): error TS7031: Binding element 'a2' implicitly has an 'any' type. +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(3,22): error TS7031: Binding element 'b2' implicitly has an 'any' type. +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(3,38): error TS7031: Binding element 'c2' implicitly has an 'any' type. +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(5,6): error TS7031: Binding element 'a4' implicitly has an 'any' type. +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(5,31): error TS7031: Binding element 'b4' implicitly has an 'any' type. +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(7,6): error TS7031: Binding element 'x' implicitly has an 'any' type. +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(7,9): error TS7031: Binding element 'y' implicitly has an 'any' type. +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(7,12): error TS7031: Binding element 'z' implicitly has an 'any' type. +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(9,6): error TS7031: Binding element 'x2' implicitly has an 'any' type. +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(9,22): error TS7031: Binding element 'y2' implicitly has an 'any' type. +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(9,38): error TS7031: Binding element 'z2' implicitly has an 'any' type. +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(11,6): error TS7031: Binding element 'x4' implicitly has an 'any' type. +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(11,37): error TS7031: Binding element 'y4' implicitly has an 'any' type. + + +==== tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts (16 errors) ==== + let [a, b, c] = [1, 2, 3]; // no error + ~ +!!! error TS7031: Binding element 'a' implicitly has an 'any' type. + ~ +!!! error TS7031: Binding element 'b' implicitly has an 'any' type. + ~ +!!! error TS7031: Binding element 'c' implicitly has an 'any' type. + let [a1 = 10, b1 = 10, c1 = 10] = [1, 2, 3]; // no error + let [a2 = undefined, b2 = undefined, c2 = undefined] = [1, 2, 3]; // no error + ~~ +!!! error TS7031: Binding element 'a2' implicitly has an 'any' type. + ~~ +!!! error TS7031: Binding element 'b2' implicitly has an 'any' type. + ~~ +!!! error TS7031: Binding element 'c2' implicitly has an 'any' type. + let [a3 = undefined, b3 = null, c3 = undefined] = [1, 2, 3]; // no error + let [a4] = [undefined], [b4] = [null], c4 = undefined, d4 = null; // no error + ~~ +!!! error TS7031: Binding element 'a4' implicitly has an 'any' type. + ~~ +!!! error TS7031: Binding element 'b4' implicitly has an 'any' type. + + let {x, y, z} = { x: 1, y: 2, z: 3 }; // no error + ~ +!!! error TS7031: Binding element 'x' implicitly has an 'any' type. + ~ +!!! error TS7031: Binding element 'y' implicitly has an 'any' type. + ~ +!!! error TS7031: Binding element 'z' implicitly has an 'any' type. + let {x1 = 10, y1 = 10, z1 = 10} = { x1: 1, y1: 2, z1: 3 }; // no error + let {x2 = undefined, y2 = undefined, z2 = undefined} = { x2: 1, y2: 2, z2: 3 }; // no error + ~~ +!!! error TS7031: Binding element 'x2' implicitly has an 'any' type. + ~~ +!!! error TS7031: Binding element 'y2' implicitly has an 'any' type. + ~~ +!!! error TS7031: Binding element 'z2' implicitly has an 'any' type. + let {x3 = undefined, y3 = null, z3 = undefined} = { x3: 1, y3: 2, z3: 3 }; // no error + let {x4} = { x4: undefined }, {y4} = { y4: null }; // no error + ~~ +!!! error TS7031: Binding element 'x4' implicitly has an 'any' type. + ~~ +!!! error TS7031: Binding element 'y4' implicitly has an 'any' type. + \ No newline at end of file diff --git a/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.js b/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.js new file mode 100644 index 0000000000000..79ee0b8830ffc --- /dev/null +++ b/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.js @@ -0,0 +1,25 @@ +//// [noImplicitAnyDestructuringVarDeclaration2.ts] +let [a, b, c] = [1, 2, 3]; // no error +let [a1 = 10, b1 = 10, c1 = 10] = [1, 2, 3]; // no error +let [a2 = undefined, b2 = undefined, c2 = undefined] = [1, 2, 3]; // no error +let [a3 = undefined, b3 = null, c3 = undefined] = [1, 2, 3]; // no error +let [a4] = [undefined], [b4] = [null], c4 = undefined, d4 = null; // no error + +let {x, y, z} = { x: 1, y: 2, z: 3 }; // no error +let {x1 = 10, y1 = 10, z1 = 10} = { x1: 1, y1: 2, z1: 3 }; // no error +let {x2 = undefined, y2 = undefined, z2 = undefined} = { x2: 1, y2: 2, z2: 3 }; // no error +let {x3 = undefined, y3 = null, z3 = undefined} = { x3: 1, y3: 2, z3: 3 }; // no error +let {x4} = { x4: undefined }, {y4} = { y4: null }; // no error + + +//// [noImplicitAnyDestructuringVarDeclaration2.js] +var _a = [1, 2, 3], a = _a[0], b = _a[1], c = _a[2]; // no error +var _b = [1, 2, 3], _c = _b[0], a1 = _c === void 0 ? 10 : _c, _d = _b[1], b1 = _d === void 0 ? 10 : _d, _e = _b[2], c1 = _e === void 0 ? 10 : _e; // no error +var _f = [1, 2, 3], _g = _f[0], a2 = _g === void 0 ? undefined : _g, _h = _f[1], b2 = _h === void 0 ? undefined : _h, _j = _f[2], c2 = _j === void 0 ? undefined : _j; // no error +var _k = [1, 2, 3], _l = _k[0], a3 = _l === void 0 ? undefined : _l, _m = _k[1], b3 = _m === void 0 ? null : _m, _o = _k[2], c3 = _o === void 0 ? undefined : _o; // no error +var a4 = [undefined][0], b4 = [null][0], c4 = undefined, d4 = null; // no error +var _p = { x: 1, y: 2, z: 3 }, x = _p.x, y = _p.y, z = _p.z; // no error +var _q = { x1: 1, y1: 2, z1: 3 }, _r = _q.x1, x1 = _r === void 0 ? 10 : _r, _s = _q.y1, y1 = _s === void 0 ? 10 : _s, _t = _q.z1, z1 = _t === void 0 ? 10 : _t; // no error +var _u = { x2: 1, y2: 2, z2: 3 }, _v = _u.x2, x2 = _v === void 0 ? undefined : _v, _w = _u.y2, y2 = _w === void 0 ? undefined : _w, _x = _u.z2, z2 = _x === void 0 ? undefined : _x; // no error +var _y = { x3: 1, y3: 2, z3: 3 }, _z = _y.x3, x3 = _z === void 0 ? undefined : _z, _0 = _y.y3, y3 = _0 === void 0 ? null : _0, _1 = _y.z3, z3 = _1 === void 0 ? undefined : _1; // no error +var x4 = { x4: undefined }.x4, y4 = { y4: null }.y4; // no error diff --git a/tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts b/tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts index 6988416d4de3f..b9ecd8e72a8b4 100644 --- a/tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts +++ b/tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts @@ -7,4 +7,4 @@ var [a2]: [any], {b2}: { b2: any }, c2: any, d2: any; var {b3}: { b3 }, c3: { b3 }; // error in type instead -var [a1] = [undefined], {b1} = { b1: null }, c1 = undefined, d1 = null; // error \ No newline at end of file +var [a4] = [undefined], {b4} = { b4: null }, c4 = undefined, d4 = null; // error \ No newline at end of file diff --git a/tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts b/tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts new file mode 100644 index 0000000000000..a964bd96dba0d --- /dev/null +++ b/tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts @@ -0,0 +1,12 @@ +// @noimplicitany: true +let [a, b, c] = [1, 2, 3]; // no error +let [a1 = 10, b1 = 10, c1 = 10] = [1, 2, 3]; // no error +let [a2 = undefined, b2 = undefined, c2 = undefined] = [1, 2, 3]; // no error +let [a3 = undefined, b3 = null, c3 = undefined] = [1, 2, 3]; // no error +let [a4] = [undefined], [b4] = [null], c4 = undefined, d4 = null; // no error + +let {x, y, z} = { x: 1, y: 2, z: 3 }; // no error +let {x1 = 10, y1 = 10, z1 = 10} = { x1: 1, y1: 2, z1: 3 }; // no error +let {x2 = undefined, y2 = undefined, z2 = undefined} = { x2: 1, y2: 2, z2: 3 }; // no error +let {x3 = undefined, y3 = null, z3 = undefined} = { x3: 1, y3: 2, z3: 3 }; // no error +let {x4} = { x4: undefined }, {y4} = { y4: null }; // no error From 90d347e855d94bfb24ef644731cc623552a0c854 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Thu, 21 Apr 2016 13:25:43 -0700 Subject: [PATCH 02/24] Do not report errors during contextual typecheck Fixes #8229 --- src/compiler/checker.ts | 28 ++-- ...AnyDestructuringVarDeclaration2.errors.txt | 63 -------- ...citAnyDestructuringVarDeclaration2.symbols | 78 ++++++++++ ...licitAnyDestructuringVarDeclaration2.types | 137 ++++++++++++++++++ 4 files changed, 230 insertions(+), 76 deletions(-) delete mode 100644 tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.errors.txt create mode 100644 tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.symbols create mode 100644 tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.types diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 899ee479578b2..f84988e68ba8d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2873,7 +2873,7 @@ namespace ts { // If the declaration specifies a binding pattern, use the type implied by the binding pattern if (isBindingPattern(declaration.name)) { - return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ false); + return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ false, /*reportErrors*/ true); } // No type specified and nothing can be inferred @@ -2883,23 +2883,25 @@ namespace ts { // Return the type implied by a binding pattern element. This is the type of the initializer of the element if // one is present. Otherwise, if the element is itself a binding pattern, it is the type implied by the binding // pattern. Otherwise, it is the type any. - function getTypeFromBindingElement(element: BindingElement, includePatternInType?: boolean): Type { + function getTypeFromBindingElement(element: BindingElement, includePatternInType?: boolean, reportErrors?: boolean): Type { if (element.initializer) { const type = checkExpressionCached(element.initializer); - reportErrorsFromWidening(element, type); + if (reportErrors) { + reportErrorsFromWidening(element, type); + } return getWidenedType(type); } if (isBindingPattern(element.name)) { - return getTypeFromBindingPattern(element.name, includePatternInType); + return getTypeFromBindingPattern(element.name, includePatternInType, reportErrors); } - if (compilerOptions.noImplicitAny && !declarationBelongsToPrivateAmbientMember(element)) { + if (reportErrors && compilerOptions.noImplicitAny && !declarationBelongsToPrivateAmbientMember(element)) { reportImplicitAnyError(element, anyType); } return anyType; } // Return the type implied by an object binding pattern - function getTypeFromObjectBindingPattern(pattern: BindingPattern, includePatternInType: boolean): Type { + function getTypeFromObjectBindingPattern(pattern: BindingPattern, includePatternInType: boolean, reportErrors: boolean): Type { const members: SymbolTable = {}; let hasComputedProperties = false; forEach(pattern.elements, e => { @@ -2913,7 +2915,7 @@ namespace ts { const text = getTextOfPropertyName(name); const flags = SymbolFlags.Property | SymbolFlags.Transient | (e.initializer ? SymbolFlags.Optional : 0); const symbol = createSymbol(flags, text); - symbol.type = getTypeFromBindingElement(e, includePatternInType); + symbol.type = getTypeFromBindingElement(e, includePatternInType, reportErrors); symbol.bindingElement = e; members[symbol.name] = symbol; }); @@ -2928,13 +2930,13 @@ namespace ts { } // Return the type implied by an array binding pattern - function getTypeFromArrayBindingPattern(pattern: BindingPattern, includePatternInType: boolean): Type { + function getTypeFromArrayBindingPattern(pattern: BindingPattern, includePatternInType: boolean, reportErrors: boolean): Type { const elements = pattern.elements; if (elements.length === 0 || elements[elements.length - 1].dotDotDotToken) { return languageVersion >= ScriptTarget.ES6 ? createIterableType(anyType) : anyArrayType; } // If the pattern has at least one element, and no rest element, then it should imply a tuple type. - const elementTypes = map(elements, e => e.kind === SyntaxKind.OmittedExpression ? anyType : getTypeFromBindingElement(e, includePatternInType)); + const elementTypes = map(elements, e => e.kind === SyntaxKind.OmittedExpression ? anyType : getTypeFromBindingElement(e, includePatternInType, reportErrors)); if (includePatternInType) { const result = createNewTupleType(elementTypes); result.pattern = pattern; @@ -2950,10 +2952,10 @@ namespace ts { // used as the contextual type of an initializer associated with the binding pattern. Also, for a destructuring // parameter with no type annotation or initializer, the type implied by the binding pattern becomes the type of // the parameter. - function getTypeFromBindingPattern(pattern: BindingPattern, includePatternInType?: boolean): Type { + function getTypeFromBindingPattern(pattern: BindingPattern, includePatternInType?: boolean, reportErrors?: boolean): Type { return pattern.kind === SyntaxKind.ObjectBindingPattern - ? getTypeFromObjectBindingPattern(pattern, includePatternInType) - : getTypeFromArrayBindingPattern(pattern, includePatternInType); + ? getTypeFromObjectBindingPattern(pattern, includePatternInType, reportErrors) + : getTypeFromArrayBindingPattern(pattern, includePatternInType, reportErrors); } // Return the type associated with a variable, parameter, or property declaration. In the simple case this is the type @@ -8467,7 +8469,7 @@ namespace ts { } } if (isBindingPattern(declaration.name)) { - return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ true); + return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ true, /*reportErrors*/ false); } if (isBindingPattern(declaration.parent)) { const parentDeclaration = declaration.parent.parent; diff --git a/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.errors.txt b/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.errors.txt deleted file mode 100644 index d84a297566582..0000000000000 --- a/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.errors.txt +++ /dev/null @@ -1,63 +0,0 @@ -tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(1,6): error TS7031: Binding element 'a' implicitly has an 'any' type. -tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(1,9): error TS7031: Binding element 'b' implicitly has an 'any' type. -tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(1,12): error TS7031: Binding element 'c' implicitly has an 'any' type. -tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(3,6): error TS7031: Binding element 'a2' implicitly has an 'any' type. -tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(3,22): error TS7031: Binding element 'b2' implicitly has an 'any' type. -tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(3,38): error TS7031: Binding element 'c2' implicitly has an 'any' type. -tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(5,6): error TS7031: Binding element 'a4' implicitly has an 'any' type. -tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(5,31): error TS7031: Binding element 'b4' implicitly has an 'any' type. -tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(7,6): error TS7031: Binding element 'x' implicitly has an 'any' type. -tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(7,9): error TS7031: Binding element 'y' implicitly has an 'any' type. -tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(7,12): error TS7031: Binding element 'z' implicitly has an 'any' type. -tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(9,6): error TS7031: Binding element 'x2' implicitly has an 'any' type. -tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(9,22): error TS7031: Binding element 'y2' implicitly has an 'any' type. -tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(9,38): error TS7031: Binding element 'z2' implicitly has an 'any' type. -tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(11,6): error TS7031: Binding element 'x4' implicitly has an 'any' type. -tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts(11,37): error TS7031: Binding element 'y4' implicitly has an 'any' type. - - -==== tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts (16 errors) ==== - let [a, b, c] = [1, 2, 3]; // no error - ~ -!!! error TS7031: Binding element 'a' implicitly has an 'any' type. - ~ -!!! error TS7031: Binding element 'b' implicitly has an 'any' type. - ~ -!!! error TS7031: Binding element 'c' implicitly has an 'any' type. - let [a1 = 10, b1 = 10, c1 = 10] = [1, 2, 3]; // no error - let [a2 = undefined, b2 = undefined, c2 = undefined] = [1, 2, 3]; // no error - ~~ -!!! error TS7031: Binding element 'a2' implicitly has an 'any' type. - ~~ -!!! error TS7031: Binding element 'b2' implicitly has an 'any' type. - ~~ -!!! error TS7031: Binding element 'c2' implicitly has an 'any' type. - let [a3 = undefined, b3 = null, c3 = undefined] = [1, 2, 3]; // no error - let [a4] = [undefined], [b4] = [null], c4 = undefined, d4 = null; // no error - ~~ -!!! error TS7031: Binding element 'a4' implicitly has an 'any' type. - ~~ -!!! error TS7031: Binding element 'b4' implicitly has an 'any' type. - - let {x, y, z} = { x: 1, y: 2, z: 3 }; // no error - ~ -!!! error TS7031: Binding element 'x' implicitly has an 'any' type. - ~ -!!! error TS7031: Binding element 'y' implicitly has an 'any' type. - ~ -!!! error TS7031: Binding element 'z' implicitly has an 'any' type. - let {x1 = 10, y1 = 10, z1 = 10} = { x1: 1, y1: 2, z1: 3 }; // no error - let {x2 = undefined, y2 = undefined, z2 = undefined} = { x2: 1, y2: 2, z2: 3 }; // no error - ~~ -!!! error TS7031: Binding element 'x2' implicitly has an 'any' type. - ~~ -!!! error TS7031: Binding element 'y2' implicitly has an 'any' type. - ~~ -!!! error TS7031: Binding element 'z2' implicitly has an 'any' type. - let {x3 = undefined, y3 = null, z3 = undefined} = { x3: 1, y3: 2, z3: 3 }; // no error - let {x4} = { x4: undefined }, {y4} = { y4: null }; // no error - ~~ -!!! error TS7031: Binding element 'x4' implicitly has an 'any' type. - ~~ -!!! error TS7031: Binding element 'y4' implicitly has an 'any' type. - \ No newline at end of file diff --git a/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.symbols b/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.symbols new file mode 100644 index 0000000000000..ee7fe9f7edd9f --- /dev/null +++ b/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.symbols @@ -0,0 +1,78 @@ +=== tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts === +let [a, b, c] = [1, 2, 3]; // no error +>a : Symbol(a, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 0, 5)) +>b : Symbol(b, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 0, 7)) +>c : Symbol(c, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 0, 10)) + +let [a1 = 10, b1 = 10, c1 = 10] = [1, 2, 3]; // no error +>a1 : Symbol(a1, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 1, 5)) +>b1 : Symbol(b1, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 1, 13)) +>c1 : Symbol(c1, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 1, 22)) + +let [a2 = undefined, b2 = undefined, c2 = undefined] = [1, 2, 3]; // no error +>a2 : Symbol(a2, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 2, 5)) +>undefined : Symbol(undefined) +>b2 : Symbol(b2, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 2, 20)) +>undefined : Symbol(undefined) +>c2 : Symbol(c2, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 2, 36)) +>undefined : Symbol(undefined) + +let [a3 = undefined, b3 = null, c3 = undefined] = [1, 2, 3]; // no error +>a3 : Symbol(a3, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 3, 5)) +>undefined : Symbol(undefined) +>b3 : Symbol(b3, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 3, 25)) +>c3 : Symbol(c3, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 3, 41)) +>undefined : Symbol(undefined) + +let [a4] = [undefined], [b4] = [null], c4 = undefined, d4 = null; // no error +>a4 : Symbol(a4, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 4, 5)) +>undefined : Symbol(undefined) +>b4 : Symbol(b4, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 4, 30)) +>c4 : Symbol(c4, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 4, 48)) +>undefined : Symbol(undefined) +>d4 : Symbol(d4, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 4, 69)) + +let {x, y, z} = { x: 1, y: 2, z: 3 }; // no error +>x : Symbol(x, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 6, 5)) +>y : Symbol(y, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 6, 7)) +>z : Symbol(z, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 6, 10)) +>x : Symbol(x, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 6, 17)) +>y : Symbol(y, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 6, 23)) +>z : Symbol(z, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 6, 29)) + +let {x1 = 10, y1 = 10, z1 = 10} = { x1: 1, y1: 2, z1: 3 }; // no error +>x1 : Symbol(x1, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 7, 5)) +>y1 : Symbol(y1, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 7, 13)) +>z1 : Symbol(z1, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 7, 22)) +>x1 : Symbol(x1, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 7, 35)) +>y1 : Symbol(y1, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 7, 42)) +>z1 : Symbol(z1, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 7, 49)) + +let {x2 = undefined, y2 = undefined, z2 = undefined} = { x2: 1, y2: 2, z2: 3 }; // no error +>x2 : Symbol(x2, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 8, 5)) +>undefined : Symbol(undefined) +>y2 : Symbol(y2, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 8, 20)) +>undefined : Symbol(undefined) +>z2 : Symbol(z2, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 8, 36)) +>undefined : Symbol(undefined) +>x2 : Symbol(x2, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 8, 56)) +>y2 : Symbol(y2, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 8, 63)) +>z2 : Symbol(z2, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 8, 70)) + +let {x3 = undefined, y3 = null, z3 = undefined} = { x3: 1, y3: 2, z3: 3 }; // no error +>x3 : Symbol(x3, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 9, 5)) +>undefined : Symbol(undefined) +>y3 : Symbol(y3, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 9, 25)) +>z3 : Symbol(z3, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 9, 41)) +>undefined : Symbol(undefined) +>x3 : Symbol(x3, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 9, 66)) +>y3 : Symbol(y3, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 9, 73)) +>z3 : Symbol(z3, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 9, 80)) + +let {x4} = { x4: undefined }, {y4} = { y4: null }; // no error +>x4 : Symbol(x4, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 10, 5)) +>x4 : Symbol(x4, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 10, 12)) +>undefined : Symbol(undefined) +>y4 : Symbol(y4, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 10, 36)) +>y4 : Symbol(y4, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 10, 43)) + diff --git a/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.types b/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.types new file mode 100644 index 0000000000000..ef0a3f2c25b63 --- /dev/null +++ b/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.types @@ -0,0 +1,137 @@ +=== tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts === +let [a, b, c] = [1, 2, 3]; // no error +>a : number +>b : number +>c : number +>[1, 2, 3] : [number, number, number] +>1 : number +>2 : number +>3 : number + +let [a1 = 10, b1 = 10, c1 = 10] = [1, 2, 3]; // no error +>a1 : number +>10 : number +>b1 : number +>10 : number +>c1 : number +>10 : number +>[1, 2, 3] : [number, number, number] +>1 : number +>2 : number +>3 : number + +let [a2 = undefined, b2 = undefined, c2 = undefined] = [1, 2, 3]; // no error +>a2 : number +>undefined : undefined +>b2 : number +>undefined : undefined +>c2 : number +>undefined : undefined +>[1, 2, 3] : [number, number, number] +>1 : number +>2 : number +>3 : number + +let [a3 = undefined, b3 = null, c3 = undefined] = [1, 2, 3]; // no error +>a3 : number +>undefined : any +>undefined : undefined +>b3 : number +>null : any +>null : null +>c3 : number +>undefined : any +>undefined : undefined +>[1, 2, 3] : [number, number, number] +>1 : number +>2 : number +>3 : number + +let [a4] = [undefined], [b4] = [null], c4 = undefined, d4 = null; // no error +>a4 : any +>[undefined] : [any] +>undefined : any +>undefined : undefined +>b4 : any +>[null] : [any] +>null : any +>null : null +>c4 : any +>undefined : any +>undefined : undefined +>d4 : any +>null : any +>null : null + +let {x, y, z} = { x: 1, y: 2, z: 3 }; // no error +>x : number +>y : number +>z : number +>{ x: 1, y: 2, z: 3 } : { x: number; y: number; z: number; } +>x : number +>1 : number +>y : number +>2 : number +>z : number +>3 : number + +let {x1 = 10, y1 = 10, z1 = 10} = { x1: 1, y1: 2, z1: 3 }; // no error +>x1 : number +>10 : number +>y1 : number +>10 : number +>z1 : number +>10 : number +>{ x1: 1, y1: 2, z1: 3 } : { x1?: number; y1?: number; z1?: number; } +>x1 : number +>1 : number +>y1 : number +>2 : number +>z1 : number +>3 : number + +let {x2 = undefined, y2 = undefined, z2 = undefined} = { x2: 1, y2: 2, z2: 3 }; // no error +>x2 : number +>undefined : undefined +>y2 : number +>undefined : undefined +>z2 : number +>undefined : undefined +>{ x2: 1, y2: 2, z2: 3 } : { x2?: number; y2?: number; z2?: number; } +>x2 : number +>1 : number +>y2 : number +>2 : number +>z2 : number +>3 : number + +let {x3 = undefined, y3 = null, z3 = undefined} = { x3: 1, y3: 2, z3: 3 }; // no error +>x3 : number +>undefined : any +>undefined : undefined +>y3 : number +>null : any +>null : null +>z3 : number +>undefined : any +>undefined : undefined +>{ x3: 1, y3: 2, z3: 3 } : { x3?: number; y3?: number; z3?: number; } +>x3 : number +>1 : number +>y3 : number +>2 : number +>z3 : number +>3 : number + +let {x4} = { x4: undefined }, {y4} = { y4: null }; // no error +>x4 : any +>{ x4: undefined } : { x4: any; } +>x4 : any +>undefined : any +>undefined : undefined +>y4 : any +>{ y4: null } : { y4: any; } +>y4 : any +>null : any +>null : null + From 34f7f2caed8fe1198e095332500c6bb6f34de819 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Fri, 22 Apr 2016 10:40:06 -0700 Subject: [PATCH 03/24] Handle the scenario when let [a=undefined]=[] --- src/compiler/checker.ts | 6 +----- .../noImplicitAnyDestructuringVarDeclaration.errors.txt | 9 +++++++-- .../noImplicitAnyDestructuringVarDeclaration.js | 5 ++++- .../compiler/noImplicitAnyDestructuringVarDeclaration.ts | 4 +++- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f84988e68ba8d..a6d1680a6e313 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2885,11 +2885,7 @@ namespace ts { // pattern. Otherwise, it is the type any. function getTypeFromBindingElement(element: BindingElement, includePatternInType?: boolean, reportErrors?: boolean): Type { if (element.initializer) { - const type = checkExpressionCached(element.initializer); - if (reportErrors) { - reportErrorsFromWidening(element, type); - } - return getWidenedType(type); + return checkExpressionCached(element.initializer); } if (isBindingPattern(element.name)) { return getTypeFromBindingPattern(element.name, includePatternInType, reportErrors); diff --git a/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration.errors.txt b/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration.errors.txt index e54261b117f39..f7e6475b971ca 100644 --- a/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration.errors.txt +++ b/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration.errors.txt @@ -19,9 +19,10 @@ tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(9,6): error TS7 tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(9,26): error TS7031: Binding element 'b4' implicitly has an 'any' type. tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(9,46): error TS7005: Variable 'c4' implicitly has an 'any' type. tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(9,62): error TS7005: Variable 'd4' implicitly has an 'any' type. +tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(11,6): error TS7031: Binding element 'a5' implicitly has an 'any' type. -==== tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts (21 errors) ==== +==== tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts (22 errors) ==== var [a], {b}, c, d; // error ~~~ !!! error TS1182: A destructuring declaration must have an initializer. @@ -72,4 +73,8 @@ tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(9,62): error TS ~~ !!! error TS7005: Variable 'c4' implicitly has an 'any' type. ~~ -!!! error TS7005: Variable 'd4' implicitly has an 'any' type. \ No newline at end of file +!!! error TS7005: Variable 'd4' implicitly has an 'any' type. + + var [a5 = undefined] = []; // error + ~~ +!!! error TS7031: Binding element 'a5' implicitly has an 'any' type. \ No newline at end of file diff --git a/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration.js b/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration.js index 4e9606d9c5380..85358df666ede 100644 --- a/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration.js +++ b/tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration.js @@ -7,7 +7,9 @@ var [a2]: [any], {b2}: { b2: any }, c2: any, d2: any; var {b3}: { b3 }, c3: { b3 }; // error in type instead -var [a4] = [undefined], {b4} = { b4: null }, c4 = undefined, d4 = null; // error +var [a4] = [undefined], {b4} = { b4: null }, c4 = undefined, d4 = null; // error + +var [a5 = undefined] = []; // error //// [noImplicitAnyDestructuringVarDeclaration.js] var a = (void 0)[0], b = (void 0).b, c, d; // error @@ -15,3 +17,4 @@ var _a = (void 0)[0], a1 = _a === void 0 ? undefined : _a, _b = (void 0).b1, b1 var a2 = (void 0)[0], b2 = (void 0).b2, c2, d2; var b3 = (void 0).b3, c3; // error in type instead var a4 = [undefined][0], b4 = { b4: null }.b4, c4 = undefined, d4 = null; // error +var _c = [][0], a5 = _c === void 0 ? undefined : _c; // error diff --git a/tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts b/tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts index b9ecd8e72a8b4..ea99d03bd7cc5 100644 --- a/tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts +++ b/tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts @@ -7,4 +7,6 @@ var [a2]: [any], {b2}: { b2: any }, c2: any, d2: any; var {b3}: { b3 }, c3: { b3 }; // error in type instead -var [a4] = [undefined], {b4} = { b4: null }, c4 = undefined, d4 = null; // error \ No newline at end of file +var [a4] = [undefined], {b4} = { b4: null }, c4 = undefined, d4 = null; // error + +var [a5 = undefined] = []; // error \ No newline at end of file From 448a480aa8d592378732b1052c645b5c681fa7e3 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 12 Jul 2016 08:37:52 -0700 Subject: [PATCH 04/24] Don't allow `.ts` to appear in an import --- src/compiler/program.ts | 61 +++++++++++-------- src/harness/unittests/moduleResolution.ts | 8 +-- .../missingFunctionImplementation2.errors.txt | 2 +- .../missingFunctionImplementation2.js | 2 +- .../reference/moduleResolutionNoTs.errors.txt | 11 ++++ .../reference/moduleResolutionNoTs.js | 15 +++++ .../moduleResolutionWithExtensions.js | 7 --- .../moduleResolutionWithExtensions.symbols | 5 -- .../moduleResolutionWithExtensions.trace.json | 6 -- .../moduleResolutionWithExtensions.types | 5 -- .../staticInstanceResolution5.errors.txt | 2 +- .../reference/staticInstanceResolution5.js | 2 +- .../missingFunctionImplementation2.ts | 2 +- tests/cases/compiler/moduleResolutionNoTs.ts | 5 ++ .../compiler/staticInstanceResolution5.ts | 2 +- .../moduleResolutionWithExtensions.ts | 4 -- 16 files changed, 77 insertions(+), 62 deletions(-) create mode 100644 tests/baselines/reference/moduleResolutionNoTs.errors.txt create mode 100644 tests/baselines/reference/moduleResolutionNoTs.js create mode 100644 tests/cases/compiler/moduleResolutionNoTs.ts diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 988714be2ab1b..5ddf5f7d065c5 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -668,24 +668,37 @@ namespace ts { * @param {boolean} onlyRecordFailures - if true then function won't try to actually load files but instead record all attempts as failures. This flag is necessary * in cases when we know upfront that all load attempts will fail (because containing folder does not exists) however we still need to record all failed lookup locations. */ - function loadModuleFromFile(candidate: string, extensions: string[], failedLookupLocation: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): string { - // First try to keep/add an extension: importing "./foo.ts" can be matched by a file "./foo.ts", and "./foo" by "./foo.d.ts" - const resolvedByAddingOrKeepingExtension = loadModuleFromFileWorker(candidate, extensions, failedLookupLocation, onlyRecordFailures, state); - if (resolvedByAddingOrKeepingExtension) { - return resolvedByAddingOrKeepingExtension; + function loadModuleFromFile(candidate: string, extensions: string[], failedLookupLocation: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): string | undefined { + // If the candidate already has an extension load that or quit. + if (hasTypeScriptFileExtension(candidate)) { + // Don't allow `.ts` to appear at the end + if (!fileExtensionIs(candidate, ".d.ts")) { + return undefined; + } + return tryFile(candidate, failedLookupLocation, onlyRecordFailures, state); } - // Then try stripping a ".js" or ".jsx" extension and replacing it with a TypeScript one, e.g. "./foo.js" can be matched by "./foo.ts" or "./foo.d.ts" + + // Next, try adding an extension. + // We don't allow an import of "foo.ts" to be matched by "foo.ts.ts", but we do allow "foo.js" to be matched by "foo.js.ts". + const resolvedByAddingExtension = tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state); + if (resolvedByAddingExtension) { + return resolvedByAddingExtension; + } + + // If that didn't work, try stripping a ".js" or ".jsx" extension and replacing it with a TypeScript one; + // e.g. "./foo.js" can be matched by "./foo.ts" or "./foo.d.ts" if (hasJavaScriptFileExtension(candidate)) { const extensionless = removeFileExtension(candidate); if (state.traceEnabled) { const extension = candidate.substring(extensionless.length); trace(state.host, Diagnostics.File_name_0_has_a_1_extension_stripping_it, candidate, extension); } - return loadModuleFromFileWorker(extensionless, extensions, failedLookupLocation, onlyRecordFailures, state); + return tryAddingExtensions(extensionless, extensions, failedLookupLocation, onlyRecordFailures, state); } } - function loadModuleFromFileWorker(candidate: string, extensions: string[], failedLookupLocation: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): string { + /** Try to return an existing file that adds one of the `extensions` to `candidate`. */ + function tryAddingExtensions(candidate: string, extensions: string[], failedLookupLocation: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): string | undefined { if (!onlyRecordFailures) { // check if containing folder exists - if it doesn't then just record failures for all supported extensions without disk probing const directory = getDirectoryPath(candidate); @@ -693,26 +706,24 @@ namespace ts { onlyRecordFailures = !directoryProbablyExists(directory, state.host); } } - return forEach(extensions, tryLoad); + return forEach(extensions, ext => + !(state.skipTsx && isJsxOrTsxExtension(ext)) && tryFile(candidate + ext, failedLookupLocation, onlyRecordFailures, state)); + } - function tryLoad(ext: string): string { - if (state.skipTsx && isJsxOrTsxExtension(ext)) { - return undefined; - } - const fileName = fileExtensionIs(candidate, ext) ? candidate : candidate + ext; - if (!onlyRecordFailures && state.host.fileExists(fileName)) { - if (state.traceEnabled) { - trace(state.host, Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName); - } - return fileName; + /** Return the file if it exists. */ + function tryFile(fileName: string, failedLookupLocation: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): string | undefined { + if (!onlyRecordFailures && state.host.fileExists(fileName)) { + if (state.traceEnabled) { + trace(state.host, Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName); } - else { - if (state.traceEnabled) { - trace(state.host, Diagnostics.File_0_does_not_exist, fileName); - } - failedLookupLocation.push(fileName); - return undefined; + return fileName; + } + else { + if (state.traceEnabled) { + trace(state.host, Diagnostics.File_0_does_not_exist, fileName); } + failedLookupLocation.push(fileName); + return undefined; } } diff --git a/src/harness/unittests/moduleResolution.ts b/src/harness/unittests/moduleResolution.ts index b3f2102d903de..0c312f4701a66 100644 --- a/src/harness/unittests/moduleResolution.ts +++ b/src/harness/unittests/moduleResolution.ts @@ -465,8 +465,8 @@ export = C; "/a/B/c/moduleB.ts": `import a = require("./moduleC")`, "/a/B/c/moduleC.ts": "export var x", "/a/B/c/moduleD.ts": ` -import a = require("./moduleA.ts"); -import b = require("./moduleB.ts"); +import a = require("./moduleA"); +import b = require("./moduleB"); ` }; test(files, { module: ts.ModuleKind.CommonJS, forceConsistentCasingInFileNames: true }, "/a/B/c", /*useCaseSensitiveFileNames*/ false, ["moduleD.ts"], [1149]); @@ -477,8 +477,8 @@ import b = require("./moduleB.ts"); "/a/B/c/moduleB.ts": `import a = require("./moduleC")`, "/a/B/c/moduleC.ts": "export var x", "/a/B/c/moduleD.ts": ` -import a = require("./moduleA.ts"); -import b = require("./moduleB.ts"); +import a = require("./moduleA"); +import b = require("./moduleB"); ` }; test(files, { module: ts.ModuleKind.CommonJS, forceConsistentCasingInFileNames: true }, "/a/B/c", /*useCaseSensitiveFileNames*/ false, ["moduleD.ts"], []); diff --git a/tests/baselines/reference/missingFunctionImplementation2.errors.txt b/tests/baselines/reference/missingFunctionImplementation2.errors.txt index a695482e1fba3..86d8baba6f780 100644 --- a/tests/baselines/reference/missingFunctionImplementation2.errors.txt +++ b/tests/baselines/reference/missingFunctionImplementation2.errors.txt @@ -4,7 +4,7 @@ tests/cases/compiler/missingFunctionImplementation2_b.ts(1,17): error TS2391: Fu ==== tests/cases/compiler/missingFunctionImplementation2_a.ts (1 errors) ==== export {}; - declare module "./missingFunctionImplementation2_b.ts" { + declare module "./missingFunctionImplementation2_b" { export function f(a, b): void; ~ !!! error TS2384: Overload signatures must all be ambient or non-ambient. diff --git a/tests/baselines/reference/missingFunctionImplementation2.js b/tests/baselines/reference/missingFunctionImplementation2.js index 104b6dd9443d6..7b3456015c832 100644 --- a/tests/baselines/reference/missingFunctionImplementation2.js +++ b/tests/baselines/reference/missingFunctionImplementation2.js @@ -2,7 +2,7 @@ //// [missingFunctionImplementation2_a.ts] export {}; -declare module "./missingFunctionImplementation2_b.ts" { +declare module "./missingFunctionImplementation2_b" { export function f(a, b): void; } diff --git a/tests/baselines/reference/moduleResolutionNoTs.errors.txt b/tests/baselines/reference/moduleResolutionNoTs.errors.txt new file mode 100644 index 0000000000000..d1d6270ae136f --- /dev/null +++ b/tests/baselines/reference/moduleResolutionNoTs.errors.txt @@ -0,0 +1,11 @@ +tests/cases/compiler/user.ts(1,15): error TS2307: Cannot find module './m.ts'. + + +==== tests/cases/compiler/m.ts (0 errors) ==== + export default 0; + +==== tests/cases/compiler/user.ts (1 errors) ==== + import x from "./m.ts"; + ~~~~~~~~ +!!! error TS2307: Cannot find module './m.ts'. + \ No newline at end of file diff --git a/tests/baselines/reference/moduleResolutionNoTs.js b/tests/baselines/reference/moduleResolutionNoTs.js new file mode 100644 index 0000000000000..2a56947ce198a --- /dev/null +++ b/tests/baselines/reference/moduleResolutionNoTs.js @@ -0,0 +1,15 @@ +//// [tests/cases/compiler/moduleResolutionNoTs.ts] //// + +//// [m.ts] +export default 0; + +//// [user.ts] +import x from "./m.ts"; + + +//// [m.js] +"use strict"; +exports.__esModule = true; +exports["default"] = 0; +//// [user.js] +"use strict"; diff --git a/tests/baselines/reference/moduleResolutionWithExtensions.js b/tests/baselines/reference/moduleResolutionWithExtensions.js index df12a3531bcfa..16d176872cac1 100644 --- a/tests/baselines/reference/moduleResolutionWithExtensions.js +++ b/tests/baselines/reference/moduleResolutionWithExtensions.js @@ -8,10 +8,6 @@ export default 0; //// [b.ts] import a from './a'; -// Matching extension -//// [c.ts] -import a from './a.ts'; - // '.js' extension: stripped and replaced with '.ts' //// [d.ts] import a from './a.js'; @@ -36,9 +32,6 @@ exports["default"] = 0; // No extension: '.ts' added //// [b.js] "use strict"; -// Matching extension -//// [c.js] -"use strict"; // '.js' extension: stripped and replaced with '.ts' //// [d.js] "use strict"; diff --git a/tests/baselines/reference/moduleResolutionWithExtensions.symbols b/tests/baselines/reference/moduleResolutionWithExtensions.symbols index e9934946a64fd..eaaa1d51cc097 100644 --- a/tests/baselines/reference/moduleResolutionWithExtensions.symbols +++ b/tests/baselines/reference/moduleResolutionWithExtensions.symbols @@ -7,11 +7,6 @@ No type information for this code.=== /src/b.ts === import a from './a'; >a : Symbol(a, Decl(b.ts, 0, 6)) -// Matching extension -=== /src/c.ts === -import a from './a.ts'; ->a : Symbol(a, Decl(c.ts, 0, 6)) - // '.js' extension: stripped and replaced with '.ts' === /src/d.ts === import a from './a.js'; diff --git a/tests/baselines/reference/moduleResolutionWithExtensions.trace.json b/tests/baselines/reference/moduleResolutionWithExtensions.trace.json index 7dc9e8c104b8c..5060006ac56bd 100644 --- a/tests/baselines/reference/moduleResolutionWithExtensions.trace.json +++ b/tests/baselines/reference/moduleResolutionWithExtensions.trace.json @@ -5,12 +5,6 @@ "File '/src/a.ts' exist - use it as a name resolution result.", "Resolving real path for '/src/a.ts', result '/src/a.ts'", "======== Module name './a' was successfully resolved to '/src/a.ts'. ========", - "======== Resolving module './a.ts' from '/src/c.ts'. ========", - "Module resolution kind is not specified, using 'NodeJs'.", - "Loading module as file / folder, candidate module location '/src/a.ts'.", - "File '/src/a.ts' exist - use it as a name resolution result.", - "Resolving real path for '/src/a.ts', result '/src/a.ts'", - "======== Module name './a.ts' was successfully resolved to '/src/a.ts'. ========", "======== Resolving module './a.js' from '/src/d.ts'. ========", "Module resolution kind is not specified, using 'NodeJs'.", "Loading module as file / folder, candidate module location '/src/a.js'.", diff --git a/tests/baselines/reference/moduleResolutionWithExtensions.types b/tests/baselines/reference/moduleResolutionWithExtensions.types index fbc0091ee3b95..f59eda1a81e79 100644 --- a/tests/baselines/reference/moduleResolutionWithExtensions.types +++ b/tests/baselines/reference/moduleResolutionWithExtensions.types @@ -7,11 +7,6 @@ No type information for this code.=== /src/b.ts === import a from './a'; >a : number -// Matching extension -=== /src/c.ts === -import a from './a.ts'; ->a : number - // '.js' extension: stripped and replaced with '.ts' === /src/d.ts === import a from './a.js'; diff --git a/tests/baselines/reference/staticInstanceResolution5.errors.txt b/tests/baselines/reference/staticInstanceResolution5.errors.txt index da6f0fde1433a..f36851b2b97e8 100644 --- a/tests/baselines/reference/staticInstanceResolution5.errors.txt +++ b/tests/baselines/reference/staticInstanceResolution5.errors.txt @@ -4,7 +4,7 @@ tests/cases/compiler/staticInstanceResolution5_1.ts(6,16): error TS2304: Cannot ==== tests/cases/compiler/staticInstanceResolution5_1.ts (3 errors) ==== - import WinJS = require('staticInstanceResolution5_0.ts'); + import WinJS = require('staticInstanceResolution5_0'); // these 3 should be errors var x = (w1: WinJS) => { }; diff --git a/tests/baselines/reference/staticInstanceResolution5.js b/tests/baselines/reference/staticInstanceResolution5.js index 0809d1af5f059..b3514e00aad87 100644 --- a/tests/baselines/reference/staticInstanceResolution5.js +++ b/tests/baselines/reference/staticInstanceResolution5.js @@ -8,7 +8,7 @@ export class Promise { } //// [staticInstanceResolution5_1.ts] -import WinJS = require('staticInstanceResolution5_0.ts'); +import WinJS = require('staticInstanceResolution5_0'); // these 3 should be errors var x = (w1: WinJS) => { }; diff --git a/tests/cases/compiler/missingFunctionImplementation2.ts b/tests/cases/compiler/missingFunctionImplementation2.ts index 25909b6add4c2..1717bc663f270 100644 --- a/tests/cases/compiler/missingFunctionImplementation2.ts +++ b/tests/cases/compiler/missingFunctionImplementation2.ts @@ -1,6 +1,6 @@ // @Filename: missingFunctionImplementation2_a.ts export {}; -declare module "./missingFunctionImplementation2_b.ts" { +declare module "./missingFunctionImplementation2_b" { export function f(a, b): void; } diff --git a/tests/cases/compiler/moduleResolutionNoTs.ts b/tests/cases/compiler/moduleResolutionNoTs.ts new file mode 100644 index 0000000000000..18b042d905fd9 --- /dev/null +++ b/tests/cases/compiler/moduleResolutionNoTs.ts @@ -0,0 +1,5 @@ +// @filename: m.ts +export default 0; + +// @filename: user.ts +import x from "./m.ts"; diff --git a/tests/cases/compiler/staticInstanceResolution5.ts b/tests/cases/compiler/staticInstanceResolution5.ts index 3adb8667be578..ed34dbd535f27 100644 --- a/tests/cases/compiler/staticInstanceResolution5.ts +++ b/tests/cases/compiler/staticInstanceResolution5.ts @@ -7,7 +7,7 @@ export class Promise { } // @Filename: staticInstanceResolution5_1.ts -import WinJS = require('staticInstanceResolution5_0.ts'); +import WinJS = require('staticInstanceResolution5_0'); // these 3 should be errors var x = (w1: WinJS) => { }; diff --git a/tests/cases/conformance/externalModules/moduleResolutionWithExtensions.ts b/tests/cases/conformance/externalModules/moduleResolutionWithExtensions.ts index 6ad3565862337..429289546c5ea 100644 --- a/tests/cases/conformance/externalModules/moduleResolutionWithExtensions.ts +++ b/tests/cases/conformance/externalModules/moduleResolutionWithExtensions.ts @@ -7,10 +7,6 @@ export default 0; // @Filename: /src/b.ts import a from './a'; -// Matching extension -// @Filename: /src/c.ts -import a from './a.ts'; - // '.js' extension: stripped and replaced with '.ts' // @Filename: /src/d.ts import a from './a.js'; From a8c05a98e98b0e2f7d50d983ac08d385d6dd3641 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 12 Jul 2016 11:11:03 -0700 Subject: [PATCH 05/24] Add specific error message for unwanted '.ts' extension --- src/compiler/checker.ts | 7 ++++++- src/compiler/core.ts | 3 ++- src/compiler/diagnosticMessages.json | 4 ++++ src/compiler/utilities.ts | 4 ++++ .../reference/moduleResolutionNoTs.errors.txt | 13 ++++++++----- tests/baselines/reference/moduleResolutionNoTs.js | 5 +++++ tests/cases/compiler/moduleResolutionNoTs.ts | 3 +++ 7 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 1cebd4693885b..88565bcba92c3 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1342,7 +1342,12 @@ namespace ts { if (moduleNotFoundError) { // report errors only if it was requested - error(moduleReferenceLiteral, moduleNotFoundError, moduleName); + if (hasTypeScriptFileExtensionNonDts(moduleName)) { + error(moduleReferenceLiteral, Diagnostics.Module_name_should_not_include_a_ts_extension_Colon_0, moduleName); + } + else { + error(moduleReferenceLiteral, moduleNotFoundError, moduleName); + } } return undefined; } diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 926ee38a79514..9b12f6a7f6861 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -1144,7 +1144,8 @@ namespace ts { /** * List of supported extensions in order of file resolution precedence. */ - export const supportedTypeScriptExtensions = [".ts", ".tsx", ".d.ts"]; + export const supportedTypeScriptExtensionsNonDts = [".ts", ".tsx"]; + export const supportedTypeScriptExtensions = supportedTypeScriptExtensionsNonDts.concat([".d.ts"]); export const supportedJavascriptExtensions = [".js", ".jsx"]; const allSupportedExtensions = supportedTypeScriptExtensions.concat(supportedJavascriptExtensions); diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 8ffb02a89743e..d85dc20afdfdd 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1943,6 +1943,10 @@ "category": "Error", "code": 2689 }, + "Module name should not include a '.ts' extension: '{0}'.": { + "category": "Error", + "code": 2690 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", "code": 4000 diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 7219a5bbd24ab..bfe8e4bd657b8 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2726,6 +2726,10 @@ namespace ts { return forEach(supportedTypeScriptExtensions, extension => fileExtensionIs(fileName, extension)); } + export function hasTypeScriptFileExtensionNonDts(fileName: string) { + return forEach(supportedTypeScriptExtensionsNonDts, extension => fileExtensionIs(fileName, extension)); + } + /** * Replace each instance of non-ascii characters by one, two, three, or four escape sequences * representing the UTF-8 encoding of the character, and return the expanded char code list. diff --git a/tests/baselines/reference/moduleResolutionNoTs.errors.txt b/tests/baselines/reference/moduleResolutionNoTs.errors.txt index d1d6270ae136f..de105f7353965 100644 --- a/tests/baselines/reference/moduleResolutionNoTs.errors.txt +++ b/tests/baselines/reference/moduleResolutionNoTs.errors.txt @@ -1,11 +1,14 @@ -tests/cases/compiler/user.ts(1,15): error TS2307: Cannot find module './m.ts'. +tests/cases/compiler/user.ts(4,15): error TS2690: Module name should not include a '.ts' extension: './m.ts'. -==== tests/cases/compiler/m.ts (0 errors) ==== - export default 0; - ==== tests/cases/compiler/user.ts (1 errors) ==== + // '.ts' extension is OK in a reference + /// + import x from "./m.ts"; ~~~~~~~~ -!!! error TS2307: Cannot find module './m.ts'. +!!! error TS2690: Module name should not include a '.ts' extension: './m.ts'. + +==== tests/cases/compiler/m.ts (0 errors) ==== + export default 0; \ No newline at end of file diff --git a/tests/baselines/reference/moduleResolutionNoTs.js b/tests/baselines/reference/moduleResolutionNoTs.js index 2a56947ce198a..8a912f33011f6 100644 --- a/tests/baselines/reference/moduleResolutionNoTs.js +++ b/tests/baselines/reference/moduleResolutionNoTs.js @@ -4,6 +4,9 @@ export default 0; //// [user.ts] +// '.ts' extension is OK in a reference +/// + import x from "./m.ts"; @@ -12,4 +15,6 @@ import x from "./m.ts"; exports.__esModule = true; exports["default"] = 0; //// [user.js] +// '.ts' extension is OK in a reference +/// "use strict"; diff --git a/tests/cases/compiler/moduleResolutionNoTs.ts b/tests/cases/compiler/moduleResolutionNoTs.ts index 18b042d905fd9..29d81589cd92c 100644 --- a/tests/cases/compiler/moduleResolutionNoTs.ts +++ b/tests/cases/compiler/moduleResolutionNoTs.ts @@ -2,4 +2,7 @@ export default 0; // @filename: user.ts +// '.ts' extension is OK in a reference +/// + import x from "./m.ts"; From 95e391ec72f289e5049d7601af758bdc7b0dcc69 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 22 Jul 2016 05:56:05 -0700 Subject: [PATCH 06/24] Allow `await` in a simple unary expression --- src/compiler/parser.ts | 2 ++ tests/baselines/reference/castOfAwait.js | 20 +++++++++++++++++++ tests/baselines/reference/castOfAwait.symbols | 7 +++++++ tests/baselines/reference/castOfAwait.types | 10 ++++++++++ tests/cases/compiler/castOfAwait.ts | 4 ++++ 5 files changed, 43 insertions(+) create mode 100644 tests/baselines/reference/castOfAwait.js create mode 100644 tests/baselines/reference/castOfAwait.symbols create mode 100644 tests/baselines/reference/castOfAwait.types create mode 100644 tests/cases/compiler/castOfAwait.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 774cea60e09a8..8545273afecc9 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -3400,6 +3400,8 @@ namespace ts { return parseTypeOfExpression(); case SyntaxKind.VoidKeyword: return parseVoidExpression(); + case SyntaxKind.AwaitKeyword: + return parseAwaitExpression(); case SyntaxKind.LessThanToken: // This is modified UnaryExpression grammar in TypeScript // UnaryExpression (modified): diff --git a/tests/baselines/reference/castOfAwait.js b/tests/baselines/reference/castOfAwait.js new file mode 100644 index 0000000000000..8833a6aa97224 --- /dev/null +++ b/tests/baselines/reference/castOfAwait.js @@ -0,0 +1,20 @@ +//// [castOfAwait.ts] +async function f() { + return await 0; +} + + +//// [castOfAwait.js] +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments)).next()); + }); +}; +function f() { + return __awaiter(this, void 0, void 0, function* () { + return yield 0; + }); +} diff --git a/tests/baselines/reference/castOfAwait.symbols b/tests/baselines/reference/castOfAwait.symbols new file mode 100644 index 0000000000000..b6ee77ac793f9 --- /dev/null +++ b/tests/baselines/reference/castOfAwait.symbols @@ -0,0 +1,7 @@ +=== tests/cases/compiler/castOfAwait.ts === +async function f() { +>f : Symbol(f, Decl(castOfAwait.ts, 0, 0)) + + return await 0; +} + diff --git a/tests/baselines/reference/castOfAwait.types b/tests/baselines/reference/castOfAwait.types new file mode 100644 index 0000000000000..7b6d8ef035bb7 --- /dev/null +++ b/tests/baselines/reference/castOfAwait.types @@ -0,0 +1,10 @@ +=== tests/cases/compiler/castOfAwait.ts === +async function f() { +>f : () => Promise + + return await 0; +> await 0 : number +>await 0 : number +>0 : number +} + diff --git a/tests/cases/compiler/castOfAwait.ts b/tests/cases/compiler/castOfAwait.ts new file mode 100644 index 0000000000000..64e96be70f327 --- /dev/null +++ b/tests/cases/compiler/castOfAwait.ts @@ -0,0 +1,4 @@ +// @target: es6 +async function f() { + return await 0; +} From bc5c7b654ad876f7a75422c58cbbc2d331bdf5f7 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 22 Jul 2016 13:33:22 -0700 Subject: [PATCH 07/24] More tests --- src/compiler/parser.ts | 1 + tests/baselines/reference/castOfAwait.js | 10 ++++++-- tests/baselines/reference/castOfAwait.symbols | 5 +++- tests/baselines/reference/castOfAwait.types | 24 +++++++++++++++++-- .../reference/castOfYield.errors.txt | 12 ++++++++++ tests/baselines/reference/castOfYield.js | 15 ++++++++++++ tests/cases/compiler/castOfAwait.ts | 5 +++- tests/cases/compiler/castOfYield.ts | 5 ++++ 8 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 tests/baselines/reference/castOfYield.errors.txt create mode 100644 tests/baselines/reference/castOfYield.js create mode 100644 tests/cases/compiler/castOfYield.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 8545273afecc9..61c7698c59d0d 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -3386,6 +3386,7 @@ namespace ts { * 6) - UnaryExpression[?yield] * 7) ~ UnaryExpression[?yield] * 8) ! UnaryExpression[?yield] + * 9) await AwaitExpression[?yield] */ function parseSimpleUnaryExpression(): UnaryExpression { switch (token) { diff --git a/tests/baselines/reference/castOfAwait.js b/tests/baselines/reference/castOfAwait.js index 8833a6aa97224..9d1399d9d65f3 100644 --- a/tests/baselines/reference/castOfAwait.js +++ b/tests/baselines/reference/castOfAwait.js @@ -1,6 +1,9 @@ //// [castOfAwait.ts] async function f() { - return await 0; + await 0; + typeof await 0; + void await 0; + await void typeof void await 0; } @@ -15,6 +18,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }; function f() { return __awaiter(this, void 0, void 0, function* () { - return yield 0; + yield 0; + typeof yield 0; + void yield 0; + yield void typeof void yield 0; }); } diff --git a/tests/baselines/reference/castOfAwait.symbols b/tests/baselines/reference/castOfAwait.symbols index b6ee77ac793f9..d0bca9f38c713 100644 --- a/tests/baselines/reference/castOfAwait.symbols +++ b/tests/baselines/reference/castOfAwait.symbols @@ -2,6 +2,9 @@ async function f() { >f : Symbol(f, Decl(castOfAwait.ts, 0, 0)) - return await 0; + await 0; + typeof await 0; + void await 0; + await void typeof void await 0; } diff --git a/tests/baselines/reference/castOfAwait.types b/tests/baselines/reference/castOfAwait.types index 7b6d8ef035bb7..ba1590fe03d95 100644 --- a/tests/baselines/reference/castOfAwait.types +++ b/tests/baselines/reference/castOfAwait.types @@ -1,10 +1,30 @@ === tests/cases/compiler/castOfAwait.ts === async function f() { ->f : () => Promise +>f : () => Promise - return await 0; + await 0; > await 0 : number >await 0 : number +>0 : number + + typeof await 0; +>typeof await 0 : string +>await 0 : number +>0 : number + + void await 0; +>void await 0 : undefined +>await 0 : number +>0 : number + + await void typeof void await 0; +>await void typeof void await 0 : any +>void typeof void await 0 : undefined +> typeof void await 0 : string +>typeof void await 0 : string +> void await 0 : number +>void await 0 : undefined +>await 0 : number >0 : number } diff --git a/tests/baselines/reference/castOfYield.errors.txt b/tests/baselines/reference/castOfYield.errors.txt new file mode 100644 index 0000000000000..abafe67878430 --- /dev/null +++ b/tests/baselines/reference/castOfYield.errors.txt @@ -0,0 +1,12 @@ +tests/cases/compiler/castOfYield.ts(4,14): error TS1109: Expression expected. + + +==== tests/cases/compiler/castOfYield.ts (1 errors) ==== + function* f() { + (yield 0); + // Unlike await, yield is not allowed to appear in a simple unary expression. + yield 0; + ~~~~~ +!!! error TS1109: Expression expected. + } + \ No newline at end of file diff --git a/tests/baselines/reference/castOfYield.js b/tests/baselines/reference/castOfYield.js new file mode 100644 index 0000000000000..652cf28e9e1f0 --- /dev/null +++ b/tests/baselines/reference/castOfYield.js @@ -0,0 +1,15 @@ +//// [castOfYield.ts] +function* f() { + (yield 0); + // Unlike await, yield is not allowed to appear in a simple unary expression. + yield 0; +} + + +//// [castOfYield.js] +function f() { + (yield 0); + // Unlike await, yield is not allowed to appear in a simple unary expression. + ; + yield 0; +} diff --git a/tests/cases/compiler/castOfAwait.ts b/tests/cases/compiler/castOfAwait.ts index 64e96be70f327..6a152226ea215 100644 --- a/tests/cases/compiler/castOfAwait.ts +++ b/tests/cases/compiler/castOfAwait.ts @@ -1,4 +1,7 @@ // @target: es6 async function f() { - return await 0; + await 0; + typeof await 0; + void await 0; + await void typeof void await 0; } diff --git a/tests/cases/compiler/castOfYield.ts b/tests/cases/compiler/castOfYield.ts new file mode 100644 index 0000000000000..9a85e6b21c27d --- /dev/null +++ b/tests/cases/compiler/castOfYield.ts @@ -0,0 +1,5 @@ +function* f() { + (yield 0); + // Unlike await, yield is not allowed to appear in a simple unary expression. + yield 0; +} From 275dbc7537accc869da2e47d7f62c519ee223ad4 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 22 Jul 2016 14:01:59 -0700 Subject: [PATCH 08/24] Forbid `await await` --- src/compiler/parser.ts | 5 ++++- .../baselines/reference/awaitAwait.errors.txt | 10 +++++++++ tests/baselines/reference/awaitAwait.js | 21 +++++++++++++++++++ tests/cases/compiler/awaitAwait.ts | 4 ++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/awaitAwait.errors.txt create mode 100644 tests/baselines/reference/awaitAwait.js create mode 100644 tests/cases/compiler/awaitAwait.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 61c7698c59d0d..9c54de6ba1384 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -3337,7 +3337,10 @@ namespace ts { function parseAwaitExpression() { const node = createNode(SyntaxKind.AwaitExpression); nextToken(); - node.expression = parseSimpleUnaryExpression(); + node.expression = token === SyntaxKind.AwaitKeyword + // Forbid `await await` + ? createMissingNode(SyntaxKind.Identifier, /*reportAtCurrentPosition*/ false, Diagnostics.Expression_expected) + : parseSimpleUnaryExpression(); return finishNode(node); } diff --git a/tests/baselines/reference/awaitAwait.errors.txt b/tests/baselines/reference/awaitAwait.errors.txt new file mode 100644 index 0000000000000..5a04051139c9f --- /dev/null +++ b/tests/baselines/reference/awaitAwait.errors.txt @@ -0,0 +1,10 @@ +tests/cases/compiler/awaitAwait.ts(2,11): error TS1109: Expression expected. + + +==== tests/cases/compiler/awaitAwait.ts (1 errors) ==== + async function f() { + await await 0; + ~~~~~ +!!! error TS1109: Expression expected. + } + \ No newline at end of file diff --git a/tests/baselines/reference/awaitAwait.js b/tests/baselines/reference/awaitAwait.js new file mode 100644 index 0000000000000..14fb3a11505da --- /dev/null +++ b/tests/baselines/reference/awaitAwait.js @@ -0,0 +1,21 @@ +//// [awaitAwait.ts] +async function f() { + await await 0; +} + + +//// [awaitAwait.js] +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments)).next()); + }); +}; +function f() { + return __awaiter(this, void 0, void 0, function* () { + yield ; + yield 0; + }); +} diff --git a/tests/cases/compiler/awaitAwait.ts b/tests/cases/compiler/awaitAwait.ts new file mode 100644 index 0000000000000..5a405804b0013 --- /dev/null +++ b/tests/cases/compiler/awaitAwait.ts @@ -0,0 +1,4 @@ +// @target: es6 +async function f() { + await await 0; +} From 52fd0334be9439d459757660b4b6cdf1e2778c6d Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 25 Jul 2016 11:08:02 -0700 Subject: [PATCH 09/24] Allow `await await` --- src/compiler/parser.ts | 7 ++----- .../baselines/reference/awaitAwait.errors.txt | 10 --------- tests/baselines/reference/awaitAwait.js | 21 ------------------- tests/baselines/reference/castOfAwait.js | 2 ++ tests/baselines/reference/castOfAwait.symbols | 1 + tests/baselines/reference/castOfAwait.types | 5 +++++ tests/cases/compiler/awaitAwait.ts | 4 ---- tests/cases/compiler/castOfAwait.ts | 1 + 8 files changed, 11 insertions(+), 40 deletions(-) delete mode 100644 tests/baselines/reference/awaitAwait.errors.txt delete mode 100644 tests/baselines/reference/awaitAwait.js delete mode 100644 tests/cases/compiler/awaitAwait.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 9c54de6ba1384..68753fdb63867 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -3337,10 +3337,7 @@ namespace ts { function parseAwaitExpression() { const node = createNode(SyntaxKind.AwaitExpression); nextToken(); - node.expression = token === SyntaxKind.AwaitKeyword - // Forbid `await await` - ? createMissingNode(SyntaxKind.Identifier, /*reportAtCurrentPosition*/ false, Diagnostics.Expression_expected) - : parseSimpleUnaryExpression(); + node.expression = parseSimpleUnaryExpression(); return finishNode(node); } @@ -3389,7 +3386,7 @@ namespace ts { * 6) - UnaryExpression[?yield] * 7) ~ UnaryExpression[?yield] * 8) ! UnaryExpression[?yield] - * 9) await AwaitExpression[?yield] + * 9) [+Await] await AwaitExpression[?yield] */ function parseSimpleUnaryExpression(): UnaryExpression { switch (token) { diff --git a/tests/baselines/reference/awaitAwait.errors.txt b/tests/baselines/reference/awaitAwait.errors.txt deleted file mode 100644 index 5a04051139c9f..0000000000000 --- a/tests/baselines/reference/awaitAwait.errors.txt +++ /dev/null @@ -1,10 +0,0 @@ -tests/cases/compiler/awaitAwait.ts(2,11): error TS1109: Expression expected. - - -==== tests/cases/compiler/awaitAwait.ts (1 errors) ==== - async function f() { - await await 0; - ~~~~~ -!!! error TS1109: Expression expected. - } - \ No newline at end of file diff --git a/tests/baselines/reference/awaitAwait.js b/tests/baselines/reference/awaitAwait.js deleted file mode 100644 index 14fb3a11505da..0000000000000 --- a/tests/baselines/reference/awaitAwait.js +++ /dev/null @@ -1,21 +0,0 @@ -//// [awaitAwait.ts] -async function f() { - await await 0; -} - - -//// [awaitAwait.js] -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments)).next()); - }); -}; -function f() { - return __awaiter(this, void 0, void 0, function* () { - yield ; - yield 0; - }); -} diff --git a/tests/baselines/reference/castOfAwait.js b/tests/baselines/reference/castOfAwait.js index 9d1399d9d65f3..95817b7f0e604 100644 --- a/tests/baselines/reference/castOfAwait.js +++ b/tests/baselines/reference/castOfAwait.js @@ -4,6 +4,7 @@ async function f() { typeof await 0; void await 0; await void typeof void await 0; + await await 0; } @@ -22,5 +23,6 @@ function f() { typeof yield 0; void yield 0; yield void typeof void yield 0; + yield yield 0; }); } diff --git a/tests/baselines/reference/castOfAwait.symbols b/tests/baselines/reference/castOfAwait.symbols index d0bca9f38c713..574ee21f7731b 100644 --- a/tests/baselines/reference/castOfAwait.symbols +++ b/tests/baselines/reference/castOfAwait.symbols @@ -6,5 +6,6 @@ async function f() { typeof await 0; void await 0; await void typeof void await 0; + await await 0; } diff --git a/tests/baselines/reference/castOfAwait.types b/tests/baselines/reference/castOfAwait.types index ba1590fe03d95..ad3597f6c018a 100644 --- a/tests/baselines/reference/castOfAwait.types +++ b/tests/baselines/reference/castOfAwait.types @@ -25,6 +25,11 @@ async function f() { > void await 0 : number >void await 0 : undefined >await 0 : number +>0 : number + + await await 0; +>await await 0 : number +>await 0 : number >0 : number } diff --git a/tests/cases/compiler/awaitAwait.ts b/tests/cases/compiler/awaitAwait.ts deleted file mode 100644 index 5a405804b0013..0000000000000 --- a/tests/cases/compiler/awaitAwait.ts +++ /dev/null @@ -1,4 +0,0 @@ -// @target: es6 -async function f() { - await await 0; -} diff --git a/tests/cases/compiler/castOfAwait.ts b/tests/cases/compiler/castOfAwait.ts index 6a152226ea215..fcd8f999e9fa9 100644 --- a/tests/cases/compiler/castOfAwait.ts +++ b/tests/cases/compiler/castOfAwait.ts @@ -4,4 +4,5 @@ async function f() { typeof await 0; void await 0; await void typeof void await 0; + await await 0; } From 0f134ed69efb8d59919bcec6a1e702f403665af6 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 2 Aug 2016 06:58:26 -0700 Subject: [PATCH 10/24] Improve error message --- src/compiler/checker.ts | 6 +++-- src/compiler/core.ts | 21 ++++++++++++++--- src/compiler/diagnosticMessages.json | 2 +- src/compiler/utilities.ts | 5 ++-- .../reference/moduleResolutionNoTs.errors.txt | 23 ++++++++++++++----- .../reference/moduleResolutionNoTs.js | 22 ++++++++++++++---- tests/cases/compiler/moduleResolutionNoTs.ts | 14 ++++++++--- 7 files changed, 71 insertions(+), 22 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7eab69f1df66c..290fa257daf9b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1366,8 +1366,10 @@ namespace ts { if (moduleNotFoundError) { // report errors only if it was requested - if (hasTypeScriptFileExtensionNonDts(moduleName)) { - error(moduleReferenceLiteral, Diagnostics.Module_name_should_not_include_a_ts_extension_Colon_0, moduleName); + const nonDtsExtension = tryExtractTypeScriptExtensionNonDts(moduleName); + if (nonDtsExtension) { + const diag = Diagnostics.An_import_path_should_not_end_with_a_0_extension_Consider_importing_1_instead; + error(moduleReferenceLiteral, diag, nonDtsExtension, removeExtension(moduleName, nonDtsExtension)); } else { error(moduleReferenceLiteral, moduleNotFoundError, moduleName); diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 7d6aea6246216..d77036660b0a7 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -93,6 +93,17 @@ namespace ts { return undefined; } + /** Works like Array.prototype.find. */ + export function find(array: T[], predicate: (element: T, index: number) => boolean): T | undefined { + for (let i = 0, len = array.length; i < len; i++) { + const value = array[i]; + if (predicate(value, i)) { + return value; + } + } + return undefined; + } + export function contains(array: T[], value: T): boolean { if (array) { for (const v of array) { @@ -941,7 +952,7 @@ namespace ts { * [^./] # matches everything up to the first . character (excluding directory seperators) * (\\.(?!min\\.js$))? # matches . characters but not if they are part of the .min.js file extension */ - const singleAsteriskRegexFragmentFiles = "([^./]|(\\.(?!min\\.js$))?)*"; + const singleAsteriskRegexFragmentFiles = "([^./]|(\\.(?!min\\.js$))?)*"; const singleAsteriskRegexFragmentOther = "[^/]*"; export function getRegularExpressionForWildcard(specs: string[], basePath: string, usage: "files" | "directories" | "exclude") { @@ -1271,8 +1282,12 @@ namespace ts { return path; } - export function tryRemoveExtension(path: string, extension: string): string { - return fileExtensionIs(path, extension) ? path.substring(0, path.length - extension.length) : undefined; + export function tryRemoveExtension(path: string, extension: string): string | undefined { + return fileExtensionIs(path, extension) ? removeExtension(path, extension) : undefined; + } + + export function removeExtension(path: string, extension: string): string { + return path.substring(0, path.length - extension.length); } export function isJsxOrTsxExtension(ext: string): boolean { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index ff534847cda44..93bd6c608f002 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1951,7 +1951,7 @@ "category": "Error", "code": 2690 }, - "Module name should not include a '.ts' extension: '{0}'.": { + "An import path should not end with a '{0}' extension. Consider importing '{1}' instead.": { "category": "Error", "code": 2691 }, diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index d6667d2b8b20e..b92037b7e5b53 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2726,8 +2726,9 @@ namespace ts { return forEach(supportedTypeScriptExtensions, extension => fileExtensionIs(fileName, extension)); } - export function hasTypeScriptFileExtensionNonDts(fileName: string) { - return forEach(supportedTypeScriptExtensionsNonDts, extension => fileExtensionIs(fileName, extension)); + /** Return ".ts" or ".tsx" if that is the extension. */ + export function tryExtractTypeScriptExtensionNonDts(fileName: string): string | undefined { + return find(supportedTypeScriptExtensionsNonDts, extension => fileExtensionIs(fileName, extension)); } /** diff --git a/tests/baselines/reference/moduleResolutionNoTs.errors.txt b/tests/baselines/reference/moduleResolutionNoTs.errors.txt index de105f7353965..2a9b74a1af674 100644 --- a/tests/baselines/reference/moduleResolutionNoTs.errors.txt +++ b/tests/baselines/reference/moduleResolutionNoTs.errors.txt @@ -1,14 +1,25 @@ -tests/cases/compiler/user.ts(4,15): error TS2690: Module name should not include a '.ts' extension: './m.ts'. +tests/cases/compiler/user.ts(4,15): error TS2691: An import path should not end with a '.ts' extension. Consider importing './x' instead. +tests/cases/compiler/user.ts(5,15): error TS2691: An import path should not end with a '.tsx' extension. Consider importing './y' instead. -==== tests/cases/compiler/user.ts (1 errors) ==== +==== tests/cases/compiler/user.ts (2 errors) ==== // '.ts' extension is OK in a reference - /// + /// - import x from "./m.ts"; + import x from "./x.ts"; ~~~~~~~~ -!!! error TS2690: Module name should not include a '.ts' extension: './m.ts'. +!!! error TS2691: An import path should not end with a '.ts' extension. Consider importing './x' instead. + import y from "./y.tsx"; + ~~~~~~~~~ +!!! error TS2691: An import path should not end with a '.tsx' extension. Consider importing './y' instead. -==== tests/cases/compiler/m.ts (0 errors) ==== + // Making sure the suggested fixes are valid: + import x2 from "./x"; + import y2 from "./y"; + +==== tests/cases/compiler/x.ts (0 errors) ==== + export default 0; + +==== tests/cases/compiler/y.tsx (0 errors) ==== export default 0; \ No newline at end of file diff --git a/tests/baselines/reference/moduleResolutionNoTs.js b/tests/baselines/reference/moduleResolutionNoTs.js index 8a912f33011f6..d9d918f5600df 100644 --- a/tests/baselines/reference/moduleResolutionNoTs.js +++ b/tests/baselines/reference/moduleResolutionNoTs.js @@ -1,20 +1,32 @@ //// [tests/cases/compiler/moduleResolutionNoTs.ts] //// -//// [m.ts] +//// [x.ts] +export default 0; + +//// [y.tsx] export default 0; //// [user.ts] // '.ts' extension is OK in a reference -/// +/// + +import x from "./x.ts"; +import y from "./y.tsx"; -import x from "./m.ts"; +// Making sure the suggested fixes are valid: +import x2 from "./x"; +import y2 from "./y"; -//// [m.js] +//// [x.js] +"use strict"; +exports.__esModule = true; +exports["default"] = 0; +//// [y.js] "use strict"; exports.__esModule = true; exports["default"] = 0; //// [user.js] // '.ts' extension is OK in a reference -/// +/// "use strict"; diff --git a/tests/cases/compiler/moduleResolutionNoTs.ts b/tests/cases/compiler/moduleResolutionNoTs.ts index 29d81589cd92c..683f6630d9f30 100644 --- a/tests/cases/compiler/moduleResolutionNoTs.ts +++ b/tests/cases/compiler/moduleResolutionNoTs.ts @@ -1,8 +1,16 @@ -// @filename: m.ts +// @filename: x.ts +export default 0; + +// @filename: y.tsx export default 0; // @filename: user.ts // '.ts' extension is OK in a reference -/// +/// + +import x from "./x.ts"; +import y from "./y.tsx"; -import x from "./m.ts"; +// Making sure the suggested fixes are valid: +import x2 from "./x"; +import y2 from "./y"; From 359c8b12ef3a8339940edd9203dcb2a10c08615b Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Wed, 3 Aug 2016 07:12:10 -0700 Subject: [PATCH 11/24] Don't allow ".d.ts" extension in an import either. --- src/compiler/checker.ts | 8 ++--- src/compiler/core.ts | 3 +- src/compiler/diagnosticMessages.json | 2 +- src/compiler/program.ts | 16 +++------- src/compiler/utilities.ts | 6 ++-- src/harness/compilerRunner.ts | 2 +- .../reference/moduleResolutionNoTs.errors.txt | 32 +++++++++++-------- .../reference/moduleResolutionNoTs.js | 11 ++++--- tests/cases/compiler/moduleResolutionNoTs.ts | 9 ++++-- 9 files changed, 46 insertions(+), 43 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 290fa257daf9b..6dd2747e33f96 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1366,10 +1366,10 @@ namespace ts { if (moduleNotFoundError) { // report errors only if it was requested - const nonDtsExtension = tryExtractTypeScriptExtensionNonDts(moduleName); - if (nonDtsExtension) { - const diag = Diagnostics.An_import_path_should_not_end_with_a_0_extension_Consider_importing_1_instead; - error(moduleReferenceLiteral, diag, nonDtsExtension, removeExtension(moduleName, nonDtsExtension)); + const tsExtension = tryExtractTypeScriptExtension(moduleName); + if (tsExtension) { + const diag = Diagnostics.An_import_path_cannot_end_with_a_0_extension_Consider_importing_1_instead; + error(moduleReferenceLiteral, diag, tsExtension, removeExtension(moduleName, tsExtension)); } else { error(moduleReferenceLiteral, moduleNotFoundError, moduleName); diff --git a/src/compiler/core.ts b/src/compiler/core.ts index d77036660b0a7..869cddb2d4d0b 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -1198,8 +1198,7 @@ namespace ts { /** * List of supported extensions in order of file resolution precedence. */ - export const supportedTypeScriptExtensionsNonDts = [".ts", ".tsx"]; - export const supportedTypeScriptExtensions = supportedTypeScriptExtensionsNonDts.concat([".d.ts"]); + export const supportedTypeScriptExtensions = [".ts", ".tsx", ".d.ts"]; export const supportedJavascriptExtensions = [".js", ".jsx"]; const allSupportedExtensions = supportedTypeScriptExtensions.concat(supportedJavascriptExtensions); diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 93bd6c608f002..8975e5daa04cf 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1951,7 +1951,7 @@ "category": "Error", "code": 2690 }, - "An import path should not end with a '{0}' extension. Consider importing '{1}' instead.": { + "An import path cannot end with a '{0}' extension. Consider importing '{1}' instead.": { "category": "Error", "code": 2691 }, diff --git a/src/compiler/program.ts b/src/compiler/program.ts index a2674d7978326..d35ce644120c4 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -669,17 +669,7 @@ namespace ts { * in cases when we know upfront that all load attempts will fail (because containing folder does not exists) however we still need to record all failed lookup locations. */ function loadModuleFromFile(candidate: string, extensions: string[], failedLookupLocation: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): string | undefined { - // If the candidate already has an extension load that or quit. - if (hasTypeScriptFileExtension(candidate)) { - // Don't allow `.ts` to appear at the end - if (!fileExtensionIs(candidate, ".d.ts")) { - return undefined; - } - return tryFile(candidate, failedLookupLocation, onlyRecordFailures, state); - } - - // Next, try adding an extension. - // We don't allow an import of "foo.ts" to be matched by "foo.ts.ts", but we do allow "foo.js" to be matched by "foo.js.ts". + // First, try adding an extension. An import of "foo" could be matched by a file "foo.ts", or "foo.js" by "foo.js.ts" const resolvedByAddingExtension = tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state); if (resolvedByAddingExtension) { return resolvedByAddingExtension; @@ -736,7 +726,9 @@ namespace ts { } const typesFile = tryReadTypesSection(packageJsonPath, candidate, state); if (typesFile) { - const result = loadModuleFromFile(typesFile, extensions, failedLookupLocation, !directoryProbablyExists(getDirectoryPath(typesFile), state.host), state); + const onlyRecordFailures = !directoryProbablyExists(getDirectoryPath(typesFile), state.host); + // The package.json "typings" property must specify the file with extension, so just try that exact filename. + const result = tryFile(typesFile, failedLookupLocation, onlyRecordFailures, state); if (result) { return result; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index b92037b7e5b53..22c9355072524 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2727,9 +2727,11 @@ namespace ts { } /** Return ".ts" or ".tsx" if that is the extension. */ - export function tryExtractTypeScriptExtensionNonDts(fileName: string): string | undefined { - return find(supportedTypeScriptExtensionsNonDts, extension => fileExtensionIs(fileName, extension)); + export function tryExtractTypeScriptExtension(fileName: string): string | undefined { + return find(supportedTypescriptExtensionsWithDtsFirst, extension => fileExtensionIs(fileName, extension)); } + // Must have '.d.ts' first because if '.ts' goes first, that will be detected as the extension instead of '.d.ts'. + const supportedTypescriptExtensionsWithDtsFirst = supportedTypeScriptExtensions.slice().reverse(); /** * Replace each instance of non-ascii characters by one, two, three, or four escape sequences diff --git a/src/harness/compilerRunner.ts b/src/harness/compilerRunner.ts index ecdc18394b079..b8467adda5ce4 100644 --- a/src/harness/compilerRunner.ts +++ b/src/harness/compilerRunner.ts @@ -52,7 +52,7 @@ class CompilerBaselineRunner extends RunnerBase { private makeUnitName(name: string, root: string) { const path = ts.toPath(name, root, (fileName) => Harness.Compiler.getCanonicalFileName(fileName)); const pathStart = ts.toPath(Harness.IO.getCurrentDirectory(), "", (fileName) => Harness.Compiler.getCanonicalFileName(fileName)); - return path.replace(pathStart, "/"); + return pathStart === "" ? path : path.replace(pathStart, "/"); }; public checkTestCodeOutput(fileName: string) { diff --git a/tests/baselines/reference/moduleResolutionNoTs.errors.txt b/tests/baselines/reference/moduleResolutionNoTs.errors.txt index 2a9b74a1af674..c054330731158 100644 --- a/tests/baselines/reference/moduleResolutionNoTs.errors.txt +++ b/tests/baselines/reference/moduleResolutionNoTs.errors.txt @@ -1,25 +1,31 @@ -tests/cases/compiler/user.ts(4,15): error TS2691: An import path should not end with a '.ts' extension. Consider importing './x' instead. -tests/cases/compiler/user.ts(5,15): error TS2691: An import path should not end with a '.tsx' extension. Consider importing './y' instead. +tests/cases/compiler/user.ts(1,15): error TS2691: An import path cannot end with a '.ts' extension. Consider importing './x' instead. +tests/cases/compiler/user.ts(2,15): error TS2691: An import path cannot end with a '.tsx' extension. Consider importing './y' instead. +tests/cases/compiler/user.ts(3,15): error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './z' instead. -==== tests/cases/compiler/user.ts (2 errors) ==== - // '.ts' extension is OK in a reference - /// +==== tests/cases/compiler/x.ts (0 errors) ==== + export default 0; + +==== tests/cases/compiler/y.tsx (0 errors) ==== + export default 0; + +==== tests/cases/compiler/z.d.ts (0 errors) ==== + declare const x: number; + export default x; +==== tests/cases/compiler/user.ts (3 errors) ==== import x from "./x.ts"; ~~~~~~~~ -!!! error TS2691: An import path should not end with a '.ts' extension. Consider importing './x' instead. +!!! error TS2691: An import path cannot end with a '.ts' extension. Consider importing './x' instead. import y from "./y.tsx"; ~~~~~~~~~ -!!! error TS2691: An import path should not end with a '.tsx' extension. Consider importing './y' instead. +!!! error TS2691: An import path cannot end with a '.tsx' extension. Consider importing './y' instead. + import z from "./z.d.ts"; + ~~~~~~~~~~ +!!! error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './z' instead. // Making sure the suggested fixes are valid: import x2 from "./x"; import y2 from "./y"; - -==== tests/cases/compiler/x.ts (0 errors) ==== - export default 0; - -==== tests/cases/compiler/y.tsx (0 errors) ==== - export default 0; + import z2 from "./z"; \ No newline at end of file diff --git a/tests/baselines/reference/moduleResolutionNoTs.js b/tests/baselines/reference/moduleResolutionNoTs.js index d9d918f5600df..9ff33d688ea24 100644 --- a/tests/baselines/reference/moduleResolutionNoTs.js +++ b/tests/baselines/reference/moduleResolutionNoTs.js @@ -6,16 +6,19 @@ export default 0; //// [y.tsx] export default 0; +//// [z.d.ts] +declare const x: number; +export default x; + //// [user.ts] -// '.ts' extension is OK in a reference -/// - import x from "./x.ts"; import y from "./y.tsx"; +import z from "./z.d.ts"; // Making sure the suggested fixes are valid: import x2 from "./x"; import y2 from "./y"; +import z2 from "./z"; //// [x.js] @@ -27,6 +30,4 @@ exports["default"] = 0; exports.__esModule = true; exports["default"] = 0; //// [user.js] -// '.ts' extension is OK in a reference -/// "use strict"; diff --git a/tests/cases/compiler/moduleResolutionNoTs.ts b/tests/cases/compiler/moduleResolutionNoTs.ts index 683f6630d9f30..2051bc259bf0e 100644 --- a/tests/cases/compiler/moduleResolutionNoTs.ts +++ b/tests/cases/compiler/moduleResolutionNoTs.ts @@ -4,13 +4,16 @@ export default 0; // @filename: y.tsx export default 0; -// @filename: user.ts -// '.ts' extension is OK in a reference -/// +// @filename: z.d.ts +declare const x: number; +export default x; +// @filename: user.ts import x from "./x.ts"; import y from "./y.tsx"; +import z from "./z.d.ts"; // Making sure the suggested fixes are valid: import x2 from "./x"; import y2 from "./y"; +import z2 from "./z"; From 2eb159e269f79f1c7d86723b2a0d9c44739627fd Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 15 Aug 2016 10:34:07 -0700 Subject: [PATCH 12/24] Rename 'find' functions --- src/compiler/checker.ts | 2 +- src/compiler/core.ts | 9 ++++++--- src/compiler/utilities.ts | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 583ae5a1f3528..37f13dbf807fe 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1032,7 +1032,7 @@ namespace ts { } function getDeclarationOfAliasSymbol(symbol: Symbol): Declaration { - return find(symbol.declarations, d => isAliasSymbolDeclaration(d) ? d : undefined); + return findMap(symbol.declarations, d => isAliasSymbolDeclaration(d) ? d : undefined); } function getTargetOfImportEqualsDeclaration(node: ImportEqualsDeclaration): Symbol { diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 36db85061b00b..d2ea69b304dd7 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -110,7 +110,7 @@ namespace ts { } /** Works like Array.prototype.find, returning `undefined` if no element satisfying the predicate is found. */ - export function tryFind(array: T[], predicate: (element: T, index: number) => boolean): T | undefined { + export function find(array: T[], predicate: (element: T, index: number) => boolean): T | undefined { for (let i = 0, len = array.length; i < len; i++) { const value = array[i]; if (predicate(value, i)) { @@ -120,8 +120,11 @@ namespace ts { return undefined; } - /** Like `forEach`, but assumes existence of array and fails if no truthy value is found. */ - export function find(array: T[], callback: (element: T, index: number) => U | undefined): U { + /** + * Returns the first truthy result of `callback`, or else fails. + * This is like `forEach`, but never returns undefined. + */ + export function findMap(array: T[], callback: (element: T, index: number) => U | undefined): U { for (let i = 0, len = array.length; i < len; i++) { const result = callback(array[i], i); if (result) { diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 0598c34bd7bff..8b32d5351eaf8 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2715,7 +2715,7 @@ namespace ts { /** Return ".ts" or ".tsx" if that is the extension. */ export function tryExtractTypeScriptExtension(fileName: string): string | undefined { - return tryFind(supportedTypescriptExtensionsWithDtsFirst, extension => fileExtensionIs(fileName, extension)); + return find(supportedTypescriptExtensionsWithDtsFirst, extension => fileExtensionIs(fileName, extension)); } // Must have '.d.ts' first because if '.ts' goes first, that will be detected as the extension instead of '.d.ts'. const supportedTypescriptExtensionsWithDtsFirst = supportedTypeScriptExtensions.slice().reverse(); From 8fc17afa003d53aa7baf914883f2b20606e8a879 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Thu, 18 Aug 2016 14:11:30 -0700 Subject: [PATCH 13/24] Move supportedTypescriptExtensionsWithDtsFirst next to supportedTypeScriptExtensions and rename --- src/compiler/core.ts | 2 ++ src/compiler/utilities.ts | 6 ++---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index d2ea69b304dd7..808f8a538df64 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -1243,6 +1243,8 @@ namespace ts { * List of supported extensions in order of file resolution precedence. */ export const supportedTypeScriptExtensions = [".ts", ".tsx", ".d.ts"]; + /** Must have ".d.ts" first because if ".ts" goes first, that will be detected as the extension instead of ".d.ts". */ + export const supportedTypescriptExtensionsForExtractExtension = [".d.ts", ".ts", ".tsx"]; export const supportedJavascriptExtensions = [".js", ".jsx"]; const allSupportedExtensions = supportedTypeScriptExtensions.concat(supportedJavascriptExtensions); diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 8b32d5351eaf8..faf210e461fd8 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2713,12 +2713,10 @@ namespace ts { return forEach(supportedTypeScriptExtensions, extension => fileExtensionIs(fileName, extension)); } - /** Return ".ts" or ".tsx" if that is the extension. */ + /** Return ".ts", ".d.ts", or ".tsx", if that is the extension. */ export function tryExtractTypeScriptExtension(fileName: string): string | undefined { - return find(supportedTypescriptExtensionsWithDtsFirst, extension => fileExtensionIs(fileName, extension)); + return find(supportedTypescriptExtensionsForExtractExtension, extension => fileExtensionIs(fileName, extension)); } - // Must have '.d.ts' first because if '.ts' goes first, that will be detected as the extension instead of '.d.ts'. - const supportedTypescriptExtensionsWithDtsFirst = supportedTypeScriptExtensions.slice().reverse(); /** * Replace each instance of non-ascii characters by one, two, three, or four escape sequences From 952d2fecc1817eb86d241a9aa6f93ac560be1d27 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Thu, 18 Aug 2016 14:19:17 -0700 Subject: [PATCH 14/24] Fix comment --- src/compiler/parser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 68753fdb63867..361e293011b54 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -3386,7 +3386,7 @@ namespace ts { * 6) - UnaryExpression[?yield] * 7) ~ UnaryExpression[?yield] * 8) ! UnaryExpression[?yield] - * 9) [+Await] await AwaitExpression[?yield] + * 9) [+Await] await UnaryExpression[?yield] */ function parseSimpleUnaryExpression(): UnaryExpression { switch (token) { From 03dcdda44342a07ea10701ae1725f174941d2d97 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Thu, 18 Aug 2016 17:12:40 -0700 Subject: [PATCH 15/24] Treat special property access symbol differently ... when retriving documentation --- src/services/services.ts | 18 +++++++++++++++++ .../completionEntryDetailAcrossFiles01.ts | 20 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tests/cases/fourslash/server/completionEntryDetailAcrossFiles01.ts diff --git a/src/services/services.ts b/src/services/services.ts index ea85caf1332ae..64b872d14a774 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -4913,6 +4913,24 @@ namespace ts { if (!documentation) { documentation = symbol.getDocumentationComment(); + if ((!documentation || documentation.length === 0) && symbol.flags & SymbolFlags.Property) { + // For some special property access expressions like `experts.foo = foo` or `module.exports.foo = foo` + // there documentation comments might be attached to the right hand side symbol of their declarations. + // The pattern of such special property access is that the parent symbol is the symbol of the file. + if (symbol.parent && forEach(symbol.parent.declarations, declaration => declaration.kind === SyntaxKind.SourceFile)) { + forEach(symbol.declarations, declaration => { + if (declaration.parent && declaration.parent.kind === SyntaxKind.BinaryExpression) { + const rhsSymbol = program.getTypeChecker().getSymbolAtLocation((declaration.parent).right); + if (rhsSymbol) { + documentation = rhsSymbol.getDocumentationComment(); + if (documentation && documentation.length > 0) { + return true; + } + } + } + }); + } + } } return { displayParts, documentation, symbolKind }; diff --git a/tests/cases/fourslash/server/completionEntryDetailAcrossFiles01.ts b/tests/cases/fourslash/server/completionEntryDetailAcrossFiles01.ts new file mode 100644 index 0000000000000..243975fde2d45 --- /dev/null +++ b/tests/cases/fourslash/server/completionEntryDetailAcrossFiles01.ts @@ -0,0 +1,20 @@ +/// + +// @allowNonTsExtensions: true +// @Filename: a.js +//// /** +//// * Modify the parameter +//// * @param {string} p1 +//// */ +//// var foo = function (p1) { } +//// exports.foo = foo; +//// fo/*1*/ + +// @Filename: b.ts +//// import a = require("./a"); +//// a.fo/*2*/ + +goTo.marker('1'); +verify.completionEntryDetailIs("foo", "var foo: (p1: string) => void", "Modify the parameter"); +goTo.marker('2'); +verify.completionEntryDetailIs("foo", "(property) a.foo: (p1: string) => void", "Modify the parameter"); From b452469419cfed00dc5b04f4d286c2c700ef2619 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 19 Aug 2016 06:14:28 -0700 Subject: [PATCH 16/24] Fix tests --- tests/baselines/reference/exportDefaultProperty2.js | 6 +++--- tests/baselines/reference/exportDefaultProperty2.symbols | 2 +- tests/baselines/reference/exportDefaultProperty2.types | 2 +- tests/baselines/reference/exportEqualsProperty2.js | 4 ++-- tests/baselines/reference/exportEqualsProperty2.symbols | 2 +- tests/baselines/reference/exportEqualsProperty2.types | 2 +- tests/cases/compiler/exportDefaultProperty2.ts | 2 +- tests/cases/compiler/exportEqualsProperty2.ts | 2 +- 8 files changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/baselines/reference/exportDefaultProperty2.js b/tests/baselines/reference/exportDefaultProperty2.js index 88f0fbb4e20f9..2b0a5dce526d7 100644 --- a/tests/baselines/reference/exportDefaultProperty2.js +++ b/tests/baselines/reference/exportDefaultProperty2.js @@ -13,7 +13,7 @@ namespace C { export default C.B; //// [b.ts] -import B from "./a.ts"; +import B from "./a"; const x: B = { c: B }; @@ -29,5 +29,5 @@ exports.__esModule = true; exports["default"] = C.B; //// [b.js] "use strict"; -var a_ts_1 = require("./a.ts"); -var x = { c: a_ts_1["default"] }; +var a_1 = require("./a"); +var x = { c: a_1["default"] }; diff --git a/tests/baselines/reference/exportDefaultProperty2.symbols b/tests/baselines/reference/exportDefaultProperty2.symbols index 7ac99d53a0898..d3d7519aa94ef 100644 --- a/tests/baselines/reference/exportDefaultProperty2.symbols +++ b/tests/baselines/reference/exportDefaultProperty2.symbols @@ -21,7 +21,7 @@ export default C.B; >B : Symbol(default, Decl(a.ts, 2, 9), Decl(a.ts, 5, 13)) === tests/cases/compiler/b.ts === -import B from "./a.ts"; +import B from "./a"; >B : Symbol(B, Decl(b.ts, 0, 6)) const x: B = { c: B }; diff --git a/tests/baselines/reference/exportDefaultProperty2.types b/tests/baselines/reference/exportDefaultProperty2.types index 8dff23f1c8916..2fc9d3a475057 100644 --- a/tests/baselines/reference/exportDefaultProperty2.types +++ b/tests/baselines/reference/exportDefaultProperty2.types @@ -21,7 +21,7 @@ export default C.B; >B : number === tests/cases/compiler/b.ts === -import B from "./a.ts"; +import B from "./a"; >B : number const x: B = { c: B }; diff --git a/tests/baselines/reference/exportEqualsProperty2.js b/tests/baselines/reference/exportEqualsProperty2.js index 283fa3996a75a..b5d91431722e3 100644 --- a/tests/baselines/reference/exportEqualsProperty2.js +++ b/tests/baselines/reference/exportEqualsProperty2.js @@ -13,7 +13,7 @@ namespace C { export = C.B; //// [b.ts] -import B = require("./a.ts"); +import B = require("./a"); const x: B = { c: B }; @@ -28,5 +28,5 @@ var C = (function () { module.exports = C.B; //// [b.js] "use strict"; -var B = require("./a.ts"); +var B = require("./a"); var x = { c: B }; diff --git a/tests/baselines/reference/exportEqualsProperty2.symbols b/tests/baselines/reference/exportEqualsProperty2.symbols index a765e95bdf39a..df7eda76661cf 100644 --- a/tests/baselines/reference/exportEqualsProperty2.symbols +++ b/tests/baselines/reference/exportEqualsProperty2.symbols @@ -1,5 +1,5 @@ === tests/cases/compiler/b.ts === -import B = require("./a.ts"); +import B = require("./a"); >B : Symbol(B, Decl(b.ts, 0, 0)) const x: B = { c: B }; diff --git a/tests/baselines/reference/exportEqualsProperty2.types b/tests/baselines/reference/exportEqualsProperty2.types index 90b239e7bb6ed..4594cbfd01541 100644 --- a/tests/baselines/reference/exportEqualsProperty2.types +++ b/tests/baselines/reference/exportEqualsProperty2.types @@ -1,5 +1,5 @@ === tests/cases/compiler/b.ts === -import B = require("./a.ts"); +import B = require("./a"); >B : number const x: B = { c: B }; diff --git a/tests/cases/compiler/exportDefaultProperty2.ts b/tests/cases/compiler/exportDefaultProperty2.ts index 180a58a22d49c..0db617c93e1ee 100644 --- a/tests/cases/compiler/exportDefaultProperty2.ts +++ b/tests/cases/compiler/exportDefaultProperty2.ts @@ -11,5 +11,5 @@ namespace C { export default C.B; // @Filename: b.ts -import B from "./a.ts"; +import B from "./a"; const x: B = { c: B }; diff --git a/tests/cases/compiler/exportEqualsProperty2.ts b/tests/cases/compiler/exportEqualsProperty2.ts index 6a07837334a21..61f117f162938 100644 --- a/tests/cases/compiler/exportEqualsProperty2.ts +++ b/tests/cases/compiler/exportEqualsProperty2.ts @@ -11,5 +11,5 @@ namespace C { export = C.B; // @Filename: b.ts -import B = require("./a.ts"); +import B = require("./a"); const x: B = { c: B }; From 7f6e36c7e1308bc4c87dacb9eb458059d4377b77 Mon Sep 17 00:00:00 2001 From: Yui Date: Fri, 19 Aug 2016 10:03:23 -0700 Subject: [PATCH 17/24] Update shim version to be 2.1 (#10424) --- src/services/shims.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/shims.ts b/src/services/shims.ts index ff57dd9cf7ab4..9a581ed6be18d 100644 --- a/src/services/shims.ts +++ b/src/services/shims.ts @@ -1203,6 +1203,6 @@ namespace TypeScript.Services { // TODO: it should be moved into a namespace though. /* @internal */ -const toolsVersion = "1.9"; +const toolsVersion = "2.1"; /* tslint:enable:no-unused-variable */ From 0168ab2051bc9a364f8f148296cf71848fc4bba9 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Fri, 19 Aug 2016 13:34:06 -0700 Subject: [PATCH 18/24] Check return code paths on getters (#10102) * Check return paths on getters * Remove TODO comment --- src/compiler/checker.ts | 12 ++--- .../getterControlFlowStrictNull.errors.txt | 27 +++++++++++ .../reference/getterControlFlowStrictNull.js | 47 +++++++++++++++++++ .../compiler/getterControlFlowStrictNull.ts | 20 ++++++++ 4 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 tests/baselines/reference/getterControlFlowStrictNull.errors.txt create mode 100644 tests/baselines/reference/getterControlFlowStrictNull.js create mode 100644 tests/cases/compiler/getterControlFlowStrictNull.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d954ea51170a2..8568cd5feed2f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14126,12 +14126,7 @@ namespace ts { checkSignatureDeclaration(node); if (node.kind === SyntaxKind.GetAccessor) { if (!isInAmbientContext(node) && nodeIsPresent(node.body) && (node.flags & NodeFlags.HasImplicitReturn)) { - if (node.flags & NodeFlags.HasExplicitReturn) { - if (compilerOptions.noImplicitReturns) { - error(node.name, Diagnostics.Not_all_code_paths_return_a_value); - } - } - else { + if (!(node.flags & NodeFlags.HasExplicitReturn)) { error(node.name, Diagnostics.A_get_accessor_must_return_a_value); } } @@ -14161,7 +14156,10 @@ namespace ts { checkAccessorDeclarationTypesIdentical(node, otherAccessor, getThisTypeOfDeclaration, Diagnostics.get_and_set_accessor_must_have_the_same_this_type); } } - getTypeOfAccessors(getSymbolOfNode(node)); + const returnType = getTypeOfAccessors(getSymbolOfNode(node)); + if (node.kind === SyntaxKind.GetAccessor) { + checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnType); + } } if (node.parent.kind !== SyntaxKind.ObjectLiteralExpression) { checkSourceElement(node.body); diff --git a/tests/baselines/reference/getterControlFlowStrictNull.errors.txt b/tests/baselines/reference/getterControlFlowStrictNull.errors.txt new file mode 100644 index 0000000000000..1c4f02d6a756f --- /dev/null +++ b/tests/baselines/reference/getterControlFlowStrictNull.errors.txt @@ -0,0 +1,27 @@ +tests/cases/compiler/getterControlFlowStrictNull.ts(2,9): error TS2366: Function lacks ending return statement and return type does not include 'undefined'. +tests/cases/compiler/getterControlFlowStrictNull.ts(11,14): error TS2366: Function lacks ending return statement and return type does not include 'undefined'. + + +==== tests/cases/compiler/getterControlFlowStrictNull.ts (2 errors) ==== + class A { + a(): string | null { + ~~~~~~~~~~~~~ +!!! error TS2366: Function lacks ending return statement and return type does not include 'undefined'. + if (Math.random() > 0.5) { + return ''; + } + + // it does error here as expected + } + } + class B { + get a(): string | null { + ~~~~~~~~~~~~~ +!!! error TS2366: Function lacks ending return statement and return type does not include 'undefined'. + if (Math.random() > 0.5) { + return ''; + } + + // it should error here because it returns undefined + } + } \ No newline at end of file diff --git a/tests/baselines/reference/getterControlFlowStrictNull.js b/tests/baselines/reference/getterControlFlowStrictNull.js new file mode 100644 index 0000000000000..c3f7e410d0a7c --- /dev/null +++ b/tests/baselines/reference/getterControlFlowStrictNull.js @@ -0,0 +1,47 @@ +//// [getterControlFlowStrictNull.ts] +class A { + a(): string | null { + if (Math.random() > 0.5) { + return ''; + } + + // it does error here as expected + } +} +class B { + get a(): string | null { + if (Math.random() > 0.5) { + return ''; + } + + // it should error here because it returns undefined + } +} + +//// [getterControlFlowStrictNull.js] +var A = (function () { + function A() { + } + A.prototype.a = function () { + if (Math.random() > 0.5) { + return ''; + } + // it does error here as expected + }; + return A; +}()); +var B = (function () { + function B() { + } + Object.defineProperty(B.prototype, "a", { + get: function () { + if (Math.random() > 0.5) { + return ''; + } + // it should error here because it returns undefined + }, + enumerable: true, + configurable: true + }); + return B; +}()); diff --git a/tests/cases/compiler/getterControlFlowStrictNull.ts b/tests/cases/compiler/getterControlFlowStrictNull.ts new file mode 100644 index 0000000000000..44fbe35978942 --- /dev/null +++ b/tests/cases/compiler/getterControlFlowStrictNull.ts @@ -0,0 +1,20 @@ +//@strictNullChecks: true +//@target: ES5 +class A { + a(): string | null { + if (Math.random() > 0.5) { + return ''; + } + + // it does error here as expected + } +} +class B { + get a(): string | null { + if (Math.random() > 0.5) { + return ''; + } + + // it should error here because it returns undefined + } +} \ No newline at end of file From da6d95101fd8adfacbdb5209782dc560cf09c62f Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Fri, 19 Aug 2016 13:56:27 -0700 Subject: [PATCH 19/24] Remove extraneous arguments from harness's runBaseline (#10419) * Remove extraneous arguments from runBaseline * Address comments from @yuit --- src/harness/compilerRunner.ts | 16 ++-- src/harness/fourslash.ts | 12 +-- src/harness/harness.ts | 84 +++++++++------------ src/harness/projectsRunner.ts | 10 +-- src/harness/rwcRunner.ts | 24 +++--- src/harness/test262Runner.ts | 12 +-- src/harness/unittests/initializeTSConfig.ts | 2 +- src/harness/unittests/transpile.ts | 8 +- 8 files changed, 73 insertions(+), 95 deletions(-) diff --git a/src/harness/compilerRunner.ts b/src/harness/compilerRunner.ts index 66396293dc284..88e81ffcf348d 100644 --- a/src/harness/compilerRunner.ts +++ b/src/harness/compilerRunner.ts @@ -147,7 +147,7 @@ class CompilerBaselineRunner extends RunnerBase { // check errors it("Correct errors for " + fileName, () => { if (this.errors) { - Harness.Baseline.runBaseline("Correct errors for " + fileName, justName.replace(/\.tsx?$/, ".errors.txt"), (): string => { + Harness.Baseline.runBaseline(justName.replace(/\.tsx?$/, ".errors.txt"), (): string => { if (result.errors.length === 0) return null; return getErrorBaseline(toBeCompiled, otherFiles, result); }); @@ -156,7 +156,7 @@ class CompilerBaselineRunner extends RunnerBase { it (`Correct module resolution tracing for ${fileName}`, () => { if (options.traceResolution) { - Harness.Baseline.runBaseline("Correct module resolution tracing for " + fileName, justName.replace(/\.tsx?$/, ".trace.json"), () => { + Harness.Baseline.runBaseline(justName.replace(/\.tsx?$/, ".trace.json"), () => { return JSON.stringify(result.traceResults || [], undefined, 4); }); } @@ -165,7 +165,7 @@ class CompilerBaselineRunner extends RunnerBase { // Source maps? it("Correct sourcemap content for " + fileName, () => { if (options.sourceMap || options.inlineSourceMap) { - Harness.Baseline.runBaseline("Correct sourcemap content for " + fileName, justName.replace(/\.tsx?$/, ".sourcemap.txt"), () => { + Harness.Baseline.runBaseline(justName.replace(/\.tsx?$/, ".sourcemap.txt"), () => { const record = result.getSourceMapRecord(); if (options.noEmitOnError && result.errors.length !== 0 && record === undefined) { // Because of the noEmitOnError option no files are created. We need to return null because baselining isn"t required. @@ -183,7 +183,7 @@ class CompilerBaselineRunner extends RunnerBase { } // check js output - Harness.Baseline.runBaseline("Correct JS output for " + fileName, justName.replace(/\.tsx?/, ".js"), () => { + Harness.Baseline.runBaseline(justName.replace(/\.tsx?/, ".js"), () => { let tsCode = ""; const tsSources = otherFiles.concat(toBeCompiled); if (tsSources.length > 1) { @@ -242,7 +242,7 @@ class CompilerBaselineRunner extends RunnerBase { throw new Error("Number of sourcemap files should be same as js files."); } - Harness.Baseline.runBaseline("Correct Sourcemap output for " + fileName, justName.replace(/\.tsx?/, ".js.map"), () => { + Harness.Baseline.runBaseline(justName.replace(/\.tsx?/, ".js.map"), () => { if (options.noEmitOnError && result.errors.length !== 0 && result.sourceMaps.length === 0) { // We need to return null here or the runBaseLine will actually create a empty file. // Baselining isn't required here because there is no output. @@ -330,11 +330,11 @@ class CompilerBaselineRunner extends RunnerBase { const pullExtension = isSymbolBaseLine ? ".symbols.pull" : ".types.pull"; if (fullBaseLine !== pullBaseLine) { - Harness.Baseline.runBaseline("Correct full information for " + fileName, justName.replace(/\.tsx?/, fullExtension), () => fullBaseLine); - Harness.Baseline.runBaseline("Correct pull information for " + fileName, justName.replace(/\.tsx?/, pullExtension), () => pullBaseLine); + Harness.Baseline.runBaseline(justName.replace(/\.tsx?/, fullExtension), () => fullBaseLine); + Harness.Baseline.runBaseline(justName.replace(/\.tsx?/, pullExtension), () => pullBaseLine); } else { - Harness.Baseline.runBaseline("Correct information for " + fileName, justName.replace(/\.tsx?/, fullExtension), () => fullBaseLine); + Harness.Baseline.runBaseline(justName.replace(/\.tsx?/, fullExtension), () => fullBaseLine); } } diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index b5fa53763cb9d..63731417f480a 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -1132,12 +1132,10 @@ namespace FourSlash { } Harness.Baseline.runBaseline( - "Breakpoint Locations for " + this.activeFile.fileName, baselineFile, () => { return this.baselineCurrentFileLocations(pos => this.getBreakpointStatementLocation(pos)); - }, - true /* run immediately */); + }); } public baselineGetEmitOutput() { @@ -1159,7 +1157,6 @@ namespace FourSlash { } Harness.Baseline.runBaseline( - "Generate getEmitOutput baseline : " + emitFiles.join(" "), this.testData.globalOptions[metadataOptionNames.baselineFile], () => { let resultString = ""; @@ -1185,8 +1182,7 @@ namespace FourSlash { }); return resultString; - }, - true /* run immediately */); + }); } public printBreakpointLocation(pos: number) { @@ -1730,13 +1726,11 @@ namespace FourSlash { public baselineCurrentFileNameOrDottedNameSpans() { Harness.Baseline.runBaseline( - "Name OrDottedNameSpans for " + this.activeFile.fileName, this.testData.globalOptions[metadataOptionNames.baselineFile], () => { return this.baselineCurrentFileLocations(pos => this.getNameOrDottedNameSpan(pos)); - }, - true /* run immediately */); + }); } public printNameOrDottedNameSpans(pos: number) { diff --git a/src/harness/harness.ts b/src/harness/harness.ts index db8c30ddcc938..3375b8e47e7dc 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -1604,31 +1604,7 @@ namespace Harness { } const fileCache: { [idx: string]: boolean } = {}; - function generateActual(actualFileName: string, generateContent: () => string): string { - // For now this is written using TypeScript, because sys is not available when running old test cases. - // But we need to move to sys once we have - // Creates the directory including its parent if not already present - function createDirectoryStructure(dirName: string) { - if (fileCache[dirName] || IO.directoryExists(dirName)) { - fileCache[dirName] = true; - return; - } - - const parentDirectory = IO.directoryName(dirName); - if (parentDirectory != "") { - createDirectoryStructure(parentDirectory); - } - IO.createDirectory(dirName); - fileCache[dirName] = true; - } - - // Create folders if needed - createDirectoryStructure(Harness.IO.directoryName(actualFileName)); - - // Delete the actual file in case it fails - if (IO.fileExists(actualFileName)) { - IO.deleteFile(actualFileName); - } + function generateActual(generateContent: () => string): string { const actual = generateContent(); @@ -1663,43 +1639,51 @@ namespace Harness { return { expected, actual }; } - function writeComparison(expected: string, actual: string, relativeFileName: string, actualFileName: string, descriptionForDescribe: string) { + function writeComparison(expected: string, actual: string, relativeFileName: string, actualFileName: string) { + // For now this is written using TypeScript, because sys is not available when running old test cases. + // But we need to move to sys once we have + // Creates the directory including its parent if not already present + function createDirectoryStructure(dirName: string) { + if (fileCache[dirName] || IO.directoryExists(dirName)) { + fileCache[dirName] = true; + return; + } + + const parentDirectory = IO.directoryName(dirName); + if (parentDirectory != "") { + createDirectoryStructure(parentDirectory); + } + IO.createDirectory(dirName); + fileCache[dirName] = true; + } + + // Create folders if needed + createDirectoryStructure(Harness.IO.directoryName(actualFileName)); + + // Delete the actual file in case it fails + if (IO.fileExists(actualFileName)) { + IO.deleteFile(actualFileName); + } + const encoded_actual = Utils.encodeString(actual); if (expected !== encoded_actual) { if (actual === NoContent) { - IO.writeFile(localPath(relativeFileName + ".delete"), ""); + IO.writeFile(actualFileName + ".delete", ""); } else { - IO.writeFile(localPath(relativeFileName), actual); + IO.writeFile(actualFileName, actual); } - // Overwrite & issue error - const errMsg = "The baseline file " + relativeFileName + " has changed."; - throw new Error(errMsg); + throw new Error(`The baseline file ${relativeFileName} has changed.`); } } + export function runBaseline(relativeFileName: string, generateContent: () => string, opts?: BaselineOptions): void { - export function runBaseline( - descriptionForDescribe: string, - relativeFileName: string, - generateContent: () => string, - runImmediately = false, - opts?: BaselineOptions): void { - - let actual = undefined; const actualFileName = localPath(relativeFileName, opts && opts.Baselinefolder, opts && opts.Subfolder); - if (runImmediately) { - actual = generateActual(actualFileName, generateContent); - const comparison = compareToBaseline(actual, relativeFileName, opts); - writeComparison(comparison.expected, comparison.actual, relativeFileName, actualFileName, descriptionForDescribe); - } - else { - actual = generateActual(actualFileName, generateContent); - - const comparison = compareToBaseline(actual, relativeFileName, opts); - writeComparison(comparison.expected, comparison.actual, relativeFileName, actualFileName, descriptionForDescribe); - } + const actual = generateActual(generateContent); + const comparison = compareToBaseline(actual, relativeFileName, opts); + writeComparison(comparison.expected, comparison.actual, relativeFileName, actualFileName); } } diff --git a/src/harness/projectsRunner.ts b/src/harness/projectsRunner.ts index 76f042834bb99..080c6edfa879f 100644 --- a/src/harness/projectsRunner.ts +++ b/src/harness/projectsRunner.ts @@ -459,7 +459,7 @@ class ProjectRunner extends RunnerBase { }); it("Resolution information of (" + moduleNameToString(moduleKind) + "): " + testCaseFileName, () => { - Harness.Baseline.runBaseline("Resolution information of (" + moduleNameToString(compilerResult.moduleKind) + "): " + testCaseFileName, getBaselineFolder(compilerResult.moduleKind) + testCaseJustName + ".json", () => { + Harness.Baseline.runBaseline(getBaselineFolder(compilerResult.moduleKind) + testCaseJustName + ".json", () => { return JSON.stringify(getCompilerResolutionInfo(), undefined, " "); }); }); @@ -467,7 +467,7 @@ class ProjectRunner extends RunnerBase { it("Errors for (" + moduleNameToString(moduleKind) + "): " + testCaseFileName, () => { if (compilerResult.errors.length) { - Harness.Baseline.runBaseline("Errors for (" + moduleNameToString(compilerResult.moduleKind) + "): " + testCaseFileName, getBaselineFolder(compilerResult.moduleKind) + testCaseJustName + ".errors.txt", () => { + Harness.Baseline.runBaseline(getBaselineFolder(compilerResult.moduleKind) + testCaseJustName + ".errors.txt", () => { return getErrorsBaseline(compilerResult); }); } @@ -481,7 +481,7 @@ class ProjectRunner extends RunnerBase { // There may be multiple files with different baselines. Run all and report at the end, else // it stops copying the remaining emitted files from 'local/projectOutput' to 'local/project'. try { - Harness.Baseline.runBaseline("Baseline of emitted result (" + moduleNameToString(compilerResult.moduleKind) + "): " + testCaseFileName, getBaselineFolder(compilerResult.moduleKind) + outputFile.fileName, () => { + Harness.Baseline.runBaseline(getBaselineFolder(compilerResult.moduleKind) + outputFile.fileName, () => { try { return Harness.IO.readFile(getProjectOutputFolder(outputFile.fileName, compilerResult.moduleKind)); } @@ -503,7 +503,7 @@ class ProjectRunner extends RunnerBase { it("SourceMapRecord for (" + moduleNameToString(moduleKind) + "): " + testCaseFileName, () => { if (compilerResult.sourceMapData) { - Harness.Baseline.runBaseline("SourceMapRecord for (" + moduleNameToString(compilerResult.moduleKind) + "): " + testCaseFileName, getBaselineFolder(compilerResult.moduleKind) + testCaseJustName + ".sourcemap.txt", () => { + Harness.Baseline.runBaseline(getBaselineFolder(compilerResult.moduleKind) + testCaseJustName + ".sourcemap.txt", () => { return Harness.SourceMapRecorder.getSourceMapRecord(compilerResult.sourceMapData, compilerResult.program, ts.filter(compilerResult.outputFiles, outputFile => Harness.Compiler.isJS(outputFile.emittedFileName))); }); @@ -516,7 +516,7 @@ class ProjectRunner extends RunnerBase { if (!compilerResult.errors.length && testCase.declaration) { const dTsCompileResult = compileCompileDTsFiles(compilerResult); if (dTsCompileResult && dTsCompileResult.errors.length) { - Harness.Baseline.runBaseline("Errors in generated Dts files for (" + moduleNameToString(compilerResult.moduleKind) + "): " + testCaseFileName, getBaselineFolder(compilerResult.moduleKind) + testCaseJustName + ".dts.errors.txt", () => { + Harness.Baseline.runBaseline(getBaselineFolder(compilerResult.moduleKind) + testCaseJustName + ".dts.errors.txt", () => { return getErrorsBaseline(dTsCompileResult); }); } diff --git a/src/harness/rwcRunner.ts b/src/harness/rwcRunner.ts index 7ea191e3c8340..17346016fbbd0 100644 --- a/src/harness/rwcRunner.ts +++ b/src/harness/rwcRunner.ts @@ -158,41 +158,41 @@ namespace RWC { it("has the expected emitted code", () => { - Harness.Baseline.runBaseline("has the expected emitted code", baseName + ".output.js", () => { + Harness.Baseline.runBaseline(baseName + ".output.js", () => { return Harness.Compiler.collateOutputs(compilerResult.files); - }, false, baselineOpts); + }, baselineOpts); }); it("has the expected declaration file content", () => { - Harness.Baseline.runBaseline("has the expected declaration file content", baseName + ".d.ts", () => { + Harness.Baseline.runBaseline(baseName + ".d.ts", () => { if (!compilerResult.declFilesCode.length) { return null; } return Harness.Compiler.collateOutputs(compilerResult.declFilesCode); - }, false, baselineOpts); + }, baselineOpts); }); it("has the expected source maps", () => { - Harness.Baseline.runBaseline("has the expected source maps", baseName + ".map", () => { + Harness.Baseline.runBaseline(baseName + ".map", () => { if (!compilerResult.sourceMaps.length) { return null; } return Harness.Compiler.collateOutputs(compilerResult.sourceMaps); - }, false, baselineOpts); + }, baselineOpts); }); /*it("has correct source map record", () => { if (compilerOptions.sourceMap) { - Harness.Baseline.runBaseline("has correct source map record", baseName + ".sourcemap.txt", () => { + Harness.Baseline.runBaseline(baseName + ".sourcemap.txt", () => { return compilerResult.getSourceMapRecord(); - }, false, baselineOpts); + }, baselineOpts); } });*/ it("has the expected errors", () => { - Harness.Baseline.runBaseline("has the expected errors", baseName + ".errors.txt", () => { + Harness.Baseline.runBaseline(baseName + ".errors.txt", () => { if (compilerResult.errors.length === 0) { return null; } @@ -200,14 +200,14 @@ namespace RWC { const baselineFiles = inputFiles.concat(otherFiles).filter(f => !Harness.isDefaultLibraryFile(f.unitName)); const errors = compilerResult.errors.filter(e => !Harness.isDefaultLibraryFile(e.file.fileName)); return Harness.Compiler.getErrorBaseline(baselineFiles, errors); - }, false, baselineOpts); + }, baselineOpts); }); // Ideally, a generated declaration file will have no errors. But we allow generated // declaration file errors as part of the baseline. it("has the expected errors in generated declaration files", () => { if (compilerOptions.declaration && !compilerResult.errors.length) { - Harness.Baseline.runBaseline("has the expected errors in generated declaration files", baseName + ".dts.errors.txt", () => { + Harness.Baseline.runBaseline(baseName + ".dts.errors.txt", () => { const declFileCompilationResult = Harness.Compiler.compileDeclarationFiles( inputFiles, otherFiles, compilerResult, /*harnessSettings*/ undefined, compilerOptions, currentDirectory); @@ -218,7 +218,7 @@ namespace RWC { return Harness.Compiler.minimalDiagnosticsToString(declFileCompilationResult.declResult.errors) + Harness.IO.newLine() + Harness.IO.newLine() + Harness.Compiler.getErrorBaseline(declFileCompilationResult.declInputFiles.concat(declFileCompilationResult.declOtherFiles), declFileCompilationResult.declResult.errors); - }, false, baselineOpts); + }, baselineOpts); } }); diff --git a/src/harness/test262Runner.ts b/src/harness/test262Runner.ts index c44b2286b832d..66cf474824b14 100644 --- a/src/harness/test262Runner.ts +++ b/src/harness/test262Runner.ts @@ -67,21 +67,21 @@ class Test262BaselineRunner extends RunnerBase { }); it("has the expected emitted code", () => { - Harness.Baseline.runBaseline("has the expected emitted code", testState.filename + ".output.js", () => { + Harness.Baseline.runBaseline(testState.filename + ".output.js", () => { const files = testState.compilerResult.files.filter(f => f.fileName !== Test262BaselineRunner.helpersFilePath); return Harness.Compiler.collateOutputs(files); - }, false, Test262BaselineRunner.baselineOptions); + }, Test262BaselineRunner.baselineOptions); }); it("has the expected errors", () => { - Harness.Baseline.runBaseline("has the expected errors", testState.filename + ".errors.txt", () => { + Harness.Baseline.runBaseline(testState.filename + ".errors.txt", () => { const errors = testState.compilerResult.errors; if (errors.length === 0) { return null; } return Harness.Compiler.getErrorBaseline(testState.inputFiles, errors); - }, false, Test262BaselineRunner.baselineOptions); + }, Test262BaselineRunner.baselineOptions); }); it("satisfies invariants", () => { @@ -90,10 +90,10 @@ class Test262BaselineRunner extends RunnerBase { }); it("has the expected AST", () => { - Harness.Baseline.runBaseline("has the expected AST", testState.filename + ".AST.txt", () => { + Harness.Baseline.runBaseline(testState.filename + ".AST.txt", () => { const sourceFile = testState.compilerResult.program.getSourceFile(Test262BaselineRunner.getTestFilePath(testState.filename)); return Utils.sourceFileToJSON(sourceFile); - }, false, Test262BaselineRunner.baselineOptions); + }, Test262BaselineRunner.baselineOptions); }); }); } diff --git a/src/harness/unittests/initializeTSConfig.ts b/src/harness/unittests/initializeTSConfig.ts index 059f07e01152b..cb995212a944a 100644 --- a/src/harness/unittests/initializeTSConfig.ts +++ b/src/harness/unittests/initializeTSConfig.ts @@ -10,7 +10,7 @@ namespace ts { const outputFileName = `tsConfig/${name.replace(/[^a-z0-9\-. ]/ig, "")}/tsconfig.json`; it(`Correct output for ${outputFileName}`, () => { - Harness.Baseline.runBaseline("Correct output", outputFileName, () => { + Harness.Baseline.runBaseline(outputFileName, () => { if (initResult) { return JSON.stringify(initResult, undefined, 4); } diff --git a/src/harness/unittests/transpile.ts b/src/harness/unittests/transpile.ts index 547d10b9fbc02..2dd9c89a1cb51 100644 --- a/src/harness/unittests/transpile.ts +++ b/src/harness/unittests/transpile.ts @@ -62,7 +62,7 @@ namespace ts { }); it("Correct errors for " + justName, () => { - Harness.Baseline.runBaseline("Correct errors", justName.replace(/\.tsx?$/, ".errors.txt"), () => { + Harness.Baseline.runBaseline(justName.replace(/\.tsx?$/, ".errors.txt"), () => { if (transpileResult.diagnostics.length === 0) { /* tslint:disable:no-null-keyword */ return null; @@ -75,7 +75,7 @@ namespace ts { if (canUseOldTranspile) { it("Correct errors (old transpile) for " + justName, () => { - Harness.Baseline.runBaseline("Correct errors", justName.replace(/\.tsx?$/, ".oldTranspile.errors.txt"), () => { + Harness.Baseline.runBaseline(justName.replace(/\.tsx?$/, ".oldTranspile.errors.txt"), () => { if (oldTranspileDiagnostics.length === 0) { /* tslint:disable:no-null-keyword */ return null; @@ -88,7 +88,7 @@ namespace ts { } it("Correct output for " + justName, () => { - Harness.Baseline.runBaseline("Correct output", justName.replace(/\.tsx?$/, ".js"), () => { + Harness.Baseline.runBaseline(justName.replace(/\.tsx?$/, ".js"), () => { if (transpileResult.outputText) { return transpileResult.outputText; } @@ -104,7 +104,7 @@ namespace ts { if (canUseOldTranspile) { it("Correct output (old transpile) for " + justName, () => { - Harness.Baseline.runBaseline("Correct output", justName.replace(/\.tsx?$/, ".oldTranspile.js"), () => { + Harness.Baseline.runBaseline(justName.replace(/\.tsx?$/, ".oldTranspile.js"), () => { return oldTranspileResult; }); }); From 6c60e5b1050c7c1fcd76c016aaac804368dd6d23 Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Fri, 19 Aug 2016 14:34:14 -0700 Subject: [PATCH 20/24] Remove needless call to basename --- Jakefile.js | 317 +++++++++++++++++++++++++++------------------------- 1 file changed, 164 insertions(+), 153 deletions(-) diff --git a/Jakefile.js b/Jakefile.js index 441f6aef4f935..4c751945c8766 100644 --- a/Jakefile.js +++ b/Jakefile.js @@ -27,9 +27,9 @@ var thirdParty = "ThirdPartyNoticeText.txt"; // add node_modules to path so we don't need global modules, prefer the modules by adding them first var nodeModulesPathPrefix = path.resolve("./node_modules/.bin/") + path.delimiter; if (process.env.path !== undefined) { - process.env.path = nodeModulesPathPrefix + process.env.path; + process.env.path = nodeModulesPathPrefix + process.env.path; } else if (process.env.PATH !== undefined) { - process.env.PATH = nodeModulesPathPrefix + process.env.PATH; + process.env.PATH = nodeModulesPathPrefix + process.env.PATH; } function toNs(diff) { @@ -205,11 +205,11 @@ var es2015LibrarySources = [ "es2015.symbol.wellknown.d.ts" ]; -var es2015LibrarySourceMap = es2015LibrarySources.map(function(source) { - return { target: "lib." + source, sources: ["header.d.ts", source] }; +var es2015LibrarySourceMap = es2015LibrarySources.map(function (source) { + return { target: "lib." + source, sources: ["header.d.ts", source] }; }); -var es2016LibrarySource = [ "es2016.array.include.d.ts" ]; +var es2016LibrarySource = ["es2016.array.include.d.ts"]; var es2016LibrarySourceMap = es2016LibrarySource.map(function (source) { return { target: "lib." + source, sources: ["header.d.ts", source] }; @@ -227,21 +227,21 @@ var es2017LibrarySourceMap = es2017LibrarySource.map(function (source) { var hostsLibrarySources = ["dom.generated.d.ts", "webworker.importscripts.d.ts", "scripthost.d.ts"]; var librarySourceMap = [ - // Host library - { target: "lib.dom.d.ts", sources: ["header.d.ts", "dom.generated.d.ts"] }, - { target: "lib.dom.iterable.d.ts", sources: ["header.d.ts", "dom.iterable.d.ts"] }, - { target: "lib.webworker.d.ts", sources: ["header.d.ts", "webworker.generated.d.ts"] }, - { target: "lib.scripthost.d.ts", sources: ["header.d.ts", "scripthost.d.ts"] }, - - // JavaScript library - { target: "lib.es5.d.ts", sources: ["header.d.ts", "es5.d.ts"] }, - { target: "lib.es2015.d.ts", sources: ["header.d.ts", "es2015.d.ts"] }, - { target: "lib.es2016.d.ts", sources: ["header.d.ts", "es2016.d.ts"] }, - { target: "lib.es2017.d.ts", sources: ["header.d.ts", "es2017.d.ts"] }, - - // JavaScript + all host library - { target: "lib.d.ts", sources: ["header.d.ts", "es5.d.ts"].concat(hostsLibrarySources) }, - { target: "lib.es6.d.ts", sources: ["header.d.ts", "es5.d.ts"].concat(es2015LibrarySources, hostsLibrarySources, "dom.iterable.d.ts") } + // Host library + { target: "lib.dom.d.ts", sources: ["header.d.ts", "dom.generated.d.ts"] }, + { target: "lib.dom.iterable.d.ts", sources: ["header.d.ts", "dom.iterable.d.ts"] }, + { target: "lib.webworker.d.ts", sources: ["header.d.ts", "webworker.generated.d.ts"] }, + { target: "lib.scripthost.d.ts", sources: ["header.d.ts", "scripthost.d.ts"] }, + + // JavaScript library + { target: "lib.es5.d.ts", sources: ["header.d.ts", "es5.d.ts"] }, + { target: "lib.es2015.d.ts", sources: ["header.d.ts", "es2015.d.ts"] }, + { target: "lib.es2016.d.ts", sources: ["header.d.ts", "es2016.d.ts"] }, + { target: "lib.es2017.d.ts", sources: ["header.d.ts", "es2017.d.ts"] }, + + // JavaScript + all host library + { target: "lib.d.ts", sources: ["header.d.ts", "es5.d.ts"].concat(hostsLibrarySources) }, + { target: "lib.es6.d.ts", sources: ["header.d.ts", "es5.d.ts"].concat(es2015LibrarySources, hostsLibrarySources, "dom.iterable.d.ts") } ].concat(es2015LibrarySourceMap, es2016LibrarySourceMap, es2017LibrarySourceMap); var libraryTargets = librarySourceMap.map(function (f) { @@ -257,7 +257,7 @@ function prependFile(prefixFile, destinationFile) { fail(destinationFile + " failed to be created!"); } var temp = "temptemp"; - jake.cpR(prefixFile, temp, {silent: true}); + jake.cpR(prefixFile, temp, { silent: true }); fs.appendFileSync(temp, fs.readFileSync(destinationFile)); fs.renameSync(temp, destinationFile); } @@ -269,11 +269,11 @@ function concatenateFiles(destinationFile, sourceFiles) { if (!fs.existsSync(sourceFiles[0])) { fail(sourceFiles[0] + " does not exist!"); } - jake.cpR(sourceFiles[0], temp, {silent: true}); + jake.cpR(sourceFiles[0], temp, { silent: true }); // append all files in sequence for (var i = 1; i < sourceFiles.length; i++) { if (!fs.existsSync(sourceFiles[i])) { - fail(sourceFiles[i] + " does not exist!"); + fail(sourceFiles[i] + " does not exist!"); } fs.appendFileSync(temp, fs.readFileSync(sourceFiles[i])); } @@ -307,11 +307,11 @@ var builtLocalCompiler = path.join(builtLocalDirectory, compilerFilename); * @param callback: a function to execute after the compilation process ends */ function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, opts, callback) { - file(outFile, prereqs, function() { + file(outFile, prereqs, function () { var startCompileTime = mark(); opts = opts || {}; var compilerPath = useBuiltCompiler ? builtLocalCompiler : LKGCompiler; - var options = "--noImplicitAny --noImplicitThis --noEmitOnError --types " + var options = "--noImplicitAny --noImplicitThis --noEmitOnError --types " if (opts.types) { options += opts.types.join(","); } @@ -341,7 +341,7 @@ function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, opts options += " --module commonjs"; } - if(opts.noResolve) { + if (opts.noResolve) { options += " --noResolve"; } @@ -368,13 +368,13 @@ function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, opts var ex = jake.createExec([cmd]); // Add listeners for output and error - ex.addListener("stdout", function(output) { + ex.addListener("stdout", function (output) { process.stdout.write(output); }); - ex.addListener("stderr", function(error) { + ex.addListener("stderr", function (error) { process.stderr.write(error); }); - ex.addListener("cmdEnd", function() { + ex.addListener("cmdEnd", function () { if (!useDebugMode && prefixes && fs.existsSync(outFile)) { for (var i in prefixes) { prependFile(prefixes[i], outFile); @@ -388,13 +388,13 @@ function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, opts measure(startCompileTime); complete(); }); - ex.addListener("error", function() { + ex.addListener("error", function () { fs.unlinkSync(outFile); fail("Compilation of " + outFile + " unsuccessful"); measure(startCompileTime); }); ex.run(); - }, {async: true}); + }, { async: true }); } // Prerequisite task for built directory and library typings @@ -407,7 +407,7 @@ for (var i in libraryTargets) { var sources = [copyright].concat(entry.sources.map(function (s) { return path.join(libraryDirectory, s); })); - file(target, [builtLocalDirectory].concat(sources), function() { + file(target, [builtLocalDirectory].concat(sources), function () { concatenateFiles(target, sources); }); })(i); @@ -430,30 +430,30 @@ file(processDiagnosticMessagesTs); // processDiagnosticMessages script compileFile(processDiagnosticMessagesJs, - [processDiagnosticMessagesTs], - [processDiagnosticMessagesTs], - [], + [processDiagnosticMessagesTs], + [processDiagnosticMessagesTs], + [], /*useBuiltCompiler*/ false); // The generated diagnostics map; built for the compiler and for the 'generate-diagnostics' task file(diagnosticInfoMapTs, [processDiagnosticMessagesJs, diagnosticMessagesJson], function () { - var cmd = host + " " + processDiagnosticMessagesJs + " " + diagnosticMessagesJson; + var cmd = host + " " + processDiagnosticMessagesJs + " " + diagnosticMessagesJson; console.log(cmd); var ex = jake.createExec([cmd]); // Add listeners for output and error - ex.addListener("stdout", function(output) { + ex.addListener("stdout", function (output) { process.stdout.write(output); }); - ex.addListener("stderr", function(error) { + ex.addListener("stderr", function (error) { process.stderr.write(error); }); - ex.addListener("cmdEnd", function() { + ex.addListener("cmdEnd", function () { complete(); }); ex.run(); -}, {async: true}); +}, { async: true }); -file(builtGeneratedDiagnosticMessagesJSON,[generatedDiagnosticMessagesJSON], function() { +file(builtGeneratedDiagnosticMessagesJSON, [generatedDiagnosticMessagesJSON], function () { if (fs.existsSync(builtLocalDirectory)) { jake.cpR(generatedDiagnosticMessagesJSON, builtGeneratedDiagnosticMessagesJSON); } @@ -471,17 +471,17 @@ var programTs = path.join(compilerDirectory, "program.ts"); file(configureNightlyTs); compileFile(/*outfile*/configureNightlyJs, - /*sources*/ [configureNightlyTs], - /*prereqs*/ [configureNightlyTs], - /*prefixes*/ [], + /*sources*/[configureNightlyTs], + /*prereqs*/[configureNightlyTs], + /*prefixes*/[], /*useBuiltCompiler*/ false, - { noOutFile: false, generateDeclarations: false, keepComments: false, noResolve: false, stripInternal: false }); + { noOutFile: false, generateDeclarations: false, keepComments: false, noResolve: false, stripInternal: false }); -task("setDebugMode", function() { +task("setDebugMode", function () { useDebugMode = true; }); -task("configure-nightly", [configureNightlyJs], function() { +task("configure-nightly", [configureNightlyJs], function () { var cmd = host + " " + configureNightlyJs + " " + packageJson + " " + programTs; console.log(cmd); exec(cmd); @@ -522,63 +522,65 @@ var nodePackageFile = path.join(builtLocalDirectory, "typescript.js"); var nodeDefinitionsFile = path.join(builtLocalDirectory, "typescript.d.ts"); var nodeStandaloneDefinitionsFile = path.join(builtLocalDirectory, "typescript_standalone.d.ts"); -compileFile(servicesFile, servicesSources,[builtLocalDirectory, copyright].concat(servicesSources), - /*prefixes*/ [copyright], +compileFile(servicesFile, servicesSources, [builtLocalDirectory, copyright].concat(servicesSources), + /*prefixes*/[copyright], /*useBuiltCompiler*/ true, - /*opts*/ { noOutFile: false, - generateDeclarations: true, - preserveConstEnums: true, - keepComments: true, - noResolve: false, - stripInternal: true - }, + /*opts*/ { + noOutFile: false, + generateDeclarations: true, + preserveConstEnums: true, + keepComments: true, + noResolve: false, + stripInternal: true + }, /*callback*/ function () { - jake.cpR(servicesFile, nodePackageFile, {silent: true}); + jake.cpR(servicesFile, nodePackageFile, { silent: true }); - prependFile(copyright, standaloneDefinitionsFile); + prependFile(copyright, standaloneDefinitionsFile); - // Stanalone/web definition file using global 'ts' namespace - jake.cpR(standaloneDefinitionsFile, nodeDefinitionsFile, {silent: true}); - var definitionFileContents = fs.readFileSync(nodeDefinitionsFile).toString(); - definitionFileContents = definitionFileContents.replace(/^(\s*)(export )?const enum (\S+) {(\s*)$/gm, '$1$2enum $3 {$4'); - fs.writeFileSync(standaloneDefinitionsFile, definitionFileContents); + // Stanalone/web definition file using global 'ts' namespace + jake.cpR(standaloneDefinitionsFile, nodeDefinitionsFile, { silent: true }); + var definitionFileContents = fs.readFileSync(nodeDefinitionsFile).toString(); + definitionFileContents = definitionFileContents.replace(/^(\s*)(export )?const enum (\S+) {(\s*)$/gm, '$1$2enum $3 {$4'); + fs.writeFileSync(standaloneDefinitionsFile, definitionFileContents); - // Official node package definition file, pointed to by 'typings' in package.json - // Created by appending 'export = ts;' at the end of the standalone file to turn it into an external module - var nodeDefinitionsFileContents = definitionFileContents + "\r\nexport = ts;"; - fs.writeFileSync(nodeDefinitionsFile, nodeDefinitionsFileContents); + // Official node package definition file, pointed to by 'typings' in package.json + // Created by appending 'export = ts;' at the end of the standalone file to turn it into an external module + var nodeDefinitionsFileContents = definitionFileContents + "\r\nexport = ts;"; + fs.writeFileSync(nodeDefinitionsFile, nodeDefinitionsFileContents); - // Node package definition file to be distributed without the package. Created by replacing - // 'ts' namespace with '"typescript"' as a module. - var nodeStandaloneDefinitionsFileContents = definitionFileContents.replace(/declare (namespace|module) ts/g, 'declare module "typescript"'); - fs.writeFileSync(nodeStandaloneDefinitionsFile, nodeStandaloneDefinitionsFileContents); - }); + // Node package definition file to be distributed without the package. Created by replacing + // 'ts' namespace with '"typescript"' as a module. + var nodeStandaloneDefinitionsFileContents = definitionFileContents.replace(/declare (namespace|module) ts/g, 'declare module "typescript"'); + fs.writeFileSync(nodeStandaloneDefinitionsFile, nodeStandaloneDefinitionsFileContents); + }); compileFile( servicesFileInBrowserTest, servicesSources, [builtLocalDirectory, copyright].concat(servicesSources), - /*prefixes*/ [copyright], + /*prefixes*/[copyright], /*useBuiltCompiler*/ true, - { noOutFile: false, - generateDeclarations: true, - preserveConstEnums: true, - keepComments: true, - noResolve: false, - stripInternal: true, - noMapRoot: true, - inlineSourceMap: true - }); + { + noOutFile: false, + generateDeclarations: true, + preserveConstEnums: true, + keepComments: true, + noResolve: false, + stripInternal: true, + noMapRoot: true, + inlineSourceMap: true + }); var serverFile = path.join(builtLocalDirectory, "tsserver.js"); -compileFile(serverFile, serverSources,[builtLocalDirectory, copyright].concat(serverSources), /*prefixes*/ [copyright], /*useBuiltCompiler*/ true, { types: ["node"] }); +compileFile(serverFile, serverSources, [builtLocalDirectory, copyright].concat(serverSources), /*prefixes*/[copyright], /*useBuiltCompiler*/ true, { types: ["node"] }); var tsserverLibraryFile = path.join(builtLocalDirectory, "tsserverlibrary.js"); var tsserverLibraryDefinitionFile = path.join(builtLocalDirectory, "tsserverlibrary.d.ts"); compileFile( tsserverLibraryFile, languageServiceLibrarySources, [builtLocalDirectory, copyright, builtLocalCompiler].concat(languageServiceLibrarySources).concat(libraryTargets), - /*prefixes*/ [copyright], + /*prefixes*/[copyright], /*useBuiltCompiler*/ true, { noOutFile: false, generateDeclarations: true }); @@ -587,12 +589,12 @@ desc("Builds language service server library"); task("lssl", [tsserverLibraryFile, tsserverLibraryDefinitionFile]); desc("Emit the start of the build fold"); -task("build-fold-start", [] , function() { +task("build-fold-start", [], function () { if (fold.isTravis()) console.log(fold.start("build")); }); desc("Emit the end of the build fold"); -task("build-fold-end", [] , function() { +task("build-fold-end", [], function () { if (fold.isTravis()) console.log(fold.end("build")); }); @@ -606,7 +608,7 @@ task("tsc", ["generate-diagnostics", "lib", tscFile]); // Local target to build the compiler and services desc("Sets release mode flag"); -task("release", function() { +task("release", function () { useDebugMode = false; }); @@ -616,7 +618,7 @@ task("default", ["local"]); // Cleans the built directory desc("Cleans the compiler output, declare files, and tests"); -task("clean", function() { +task("clean", function () { jake.rmRf(builtDirectory); }); @@ -630,9 +632,9 @@ file(word2mdTs); // word2md script compileFile(word2mdJs, - [word2mdTs], - [word2mdTs], - [], + [word2mdTs], + [word2mdTs], + [], /*useBuiltCompiler*/ false); // The generated spec.md; built for the 'generate-spec' task @@ -644,7 +646,7 @@ file(specMd, [word2mdJs, specWord], function () { child_process.exec(cmd, function () { complete(); }); -}, {async: true}); +}, { async: true }); desc("Generates a Markdown version of the Language Specification"); @@ -653,14 +655,14 @@ task("generate-spec", [specMd]); // Makes a new LKG. This target does not build anything, but errors if not all the outputs are present in the built/local directory desc("Makes a new LKG out of the built js files"); -task("LKG", ["clean", "release", "local"].concat(libraryTargets), function() { +task("LKG", ["clean", "release", "local"].concat(libraryTargets), function () { var expectedFiles = [tscFile, servicesFile, serverFile, nodePackageFile, nodeDefinitionsFile, standaloneDefinitionsFile, tsserverLibraryFile, tsserverLibraryDefinitionFile].concat(libraryTargets); var missingFiles = expectedFiles.filter(function (f) { return !fs.existsSync(f); }); if (missingFiles.length > 0) { fail("Cannot replace the LKG unless all built targets are present in directory " + builtLocalDirectory + - ". The following files are missing:\n" + missingFiles.join("\n")); + ". The following files are missing:\n" + missingFiles.join("\n")); } // Copy all the targets into the LKG directory jake.mkdirP(LKGDirectory); @@ -681,8 +683,8 @@ var run = path.join(builtLocalDirectory, "run.js"); compileFile( /*outFile*/ run, /*source*/ harnessSources, - /*prereqs*/ [builtLocalDirectory, tscFile].concat(libraryTargets).concat(servicesSources).concat(harnessSources), - /*prefixes*/ [], + /*prereqs*/[builtLocalDirectory, tscFile].concat(libraryTargets).concat(servicesSources).concat(harnessSources), + /*prefixes*/[], /*useBuiltCompiler:*/ true, /*opts*/ { inlineSourceMap: true, types: ["node", "mocha", "chai"] }); @@ -701,22 +703,22 @@ desc("Builds the test infrastructure using the built compiler"); task("tests", ["local", run].concat(libraryTargets)); function exec(cmd, completeHandler, errorHandler) { - var ex = jake.createExec([cmd], {windowsVerbatimArguments: true}); + var ex = jake.createExec([cmd], { windowsVerbatimArguments: true }); // Add listeners for output and error - ex.addListener("stdout", function(output) { + ex.addListener("stdout", function (output) { process.stdout.write(output); }); - ex.addListener("stderr", function(error) { + ex.addListener("stderr", function (error) { process.stderr.write(error); }); - ex.addListener("cmdEnd", function() { + ex.addListener("cmdEnd", function () { if (completeHandler) { completeHandler(); } complete(); }); - ex.addListener("error", function(e, status) { - if(errorHandler) { + ex.addListener("error", function (e, status) { + if (errorHandler) { errorHandler(e, status); } else { fail("Process exited with code " + status); @@ -760,7 +762,7 @@ function runConsoleTests(defaultReporter, runInParallel) { tests = process.env.test || process.env.tests || process.env.t; var light = process.env.light || false; var testConfigFile = 'test.config'; - if(fs.existsSync(testConfigFile)) { + if (fs.existsSync(testConfigFile)) { fs.unlinkSync(testConfigFile); } var workerCount, taskConfigsFolder; @@ -793,7 +795,7 @@ function runConsoleTests(defaultReporter, runInParallel) { // timeout normally isn't necessary but Travis-CI has been timing out on compiler baselines occasionally // default timeout is 2sec which really should be enough, but maybe we just need a small amount longer - if(!runInParallel) { + if (!runInParallel) { var startTime = mark(); tests = tests ? ' -g "' + tests + '"' : ''; var cmd = "mocha" + (debug ? " --debug-brk" : "") + " -R " + reporter + tests + colors + bail + ' -t ' + testTimeout + ' ' + run; @@ -806,7 +808,7 @@ function runConsoleTests(defaultReporter, runInParallel) { measure(startTime); runLinter(); finish(); - }, function(e, status) { + }, function (e, status) { process.env.NODE_ENV = savedNodeEnv; measure(startTime); finish(status); @@ -860,14 +862,14 @@ function runConsoleTests(defaultReporter, runInParallel) { var testTimeout = 20000; desc("Runs all the tests in parallel using the built run.js file. Optional arguments are: t[ests]=category1|category2|... d[ebug]=true."); -task("runtests-parallel", ["build-rules", "tests", builtLocalDirectory], function() { +task("runtests-parallel", ["build-rules", "tests", builtLocalDirectory], function () { runConsoleTests('min', /*runInParallel*/ true); -}, {async: true}); +}, { async: true }); desc("Runs the tests using the built run.js file. Optional arguments are: t[ests]=regex r[eporter]=[list|spec|json|] d[ebug]=true color[s]=false lint=true bail=false."); -task("runtests", ["build-rules", "tests", builtLocalDirectory], function() { +task("runtests", ["build-rules", "tests", builtLocalDirectory], function () { runConsoleTests('mocha-fivemat-progress-reporter', /*runInParallel*/ false); -}, {async: true}); +}, { async: true }); desc("Generates code coverage data via instanbul"); task("generate-code-coverage", ["tests", builtLocalDirectory], function () { @@ -882,23 +884,23 @@ var nodeServerInFile = "tests/webTestServer.ts"; compileFile(nodeServerOutFile, [nodeServerInFile], [builtLocalDirectory, tscFile], [], /*useBuiltCompiler:*/ true, { noOutFile: true }); desc("Runs browserify on run.js to produce a file suitable for running tests in the browser"); -task("browserify", ["tests", builtLocalDirectory, nodeServerOutFile], function() { +task("browserify", ["tests", builtLocalDirectory, nodeServerOutFile], function () { var cmd = 'browserify built/local/run.js -d -o built/local/bundle.js'; exec(cmd); -}, {async: true}); +}, { async: true }); desc("Runs the tests using the built run.js file like 'jake runtests'. Syntax is jake runtests-browser. Additional optional parameters tests=[regex], browser=[chrome|IE]"); -task("runtests-browser", ["tests", "browserify", builtLocalDirectory, servicesFileInBrowserTest], function() { +task("runtests-browser", ["tests", "browserify", builtLocalDirectory, servicesFileInBrowserTest], function () { cleanTestDirs(); host = "node"; browser = process.env.browser || process.env.b || "IE"; tests = process.env.test || process.env.tests || process.env.t; var light = process.env.light || false; var testConfigFile = 'test.config'; - if(fs.existsSync(testConfigFile)) { + if (fs.existsSync(testConfigFile)) { fs.unlinkSync(testConfigFile); } - if(tests || light) { + if (tests || light) { writeTestConfigFile(tests, light); } @@ -906,7 +908,7 @@ task("runtests-browser", ["tests", "browserify", builtLocalDirectory, servicesFi var cmd = host + " tests/webTestServer.js " + browser + " " + JSON.stringify(tests); console.log(cmd); exec(cmd); -}, {async: true}); +}, { async: true }); function getDiffTool() { var program = process.env['DIFF']; @@ -919,17 +921,17 @@ function getDiffTool() { // Baseline Diff desc("Diffs the compiler baselines using the diff tool specified by the 'DIFF' environment variable"); task('diff', function () { - var cmd = '"' + getDiffTool() + '" ' + refBaseline + ' ' + localBaseline; + var cmd = '"' + getDiffTool() + '" ' + refBaseline + ' ' + localBaseline; console.log(cmd); exec(cmd); -}, {async: true}); +}, { async: true }); desc("Diffs the RWC baselines using the diff tool specified by the 'DIFF' environment variable"); task('diff-rwc', function () { - var cmd = '"' + getDiffTool() + '" ' + refRwcBaseline + ' ' + localRwcBaseline; + var cmd = '"' + getDiffTool() + '" ' + refRwcBaseline + ' ' + localRwcBaseline; console.log(cmd); exec(cmd); -}, {async: true}); +}, { async: true }); desc("Builds the test sources and automation in debug mode"); task("tests-debug", ["setDebugMode", "tests"]); @@ -937,30 +939,39 @@ task("tests-debug", ["setDebugMode", "tests"]); // Makes the test results the new baseline desc("Makes the most recent test results the new baseline, overwriting the old baseline"); -task("baseline-accept", function(hardOrSoft) { - var files = jake.readdirR(localBaseline); +task("baseline-accept", function () { + acceptBaseline(""); +}); + +function acceptBaseline(containerFolder) { + var sourceFolder = path.join(localBaseline, containerFolder); + var targetFolder = path.join(refBaseline, containerFolder); + console.log('Accept baselines from ' + sourceFolder + ' to ' + targetFolder); + var files = fs.readdirSync(sourceFolder); var deleteEnding = '.delete'; for (var i in files) { - if (files[i].substr(files[i].length - deleteEnding.length) === deleteEnding) { - var filename = path.basename(files[i]); + var filename = files[i]; + if (filename.substr(filename.length - deleteEnding.length) === deleteEnding) { filename = filename.substr(0, filename.length - deleteEnding.length); - fs.unlink(path.join(refBaseline, filename)); + fs.unlinkSync(path.join(targetFolder, filename)); } else { - jake.cpR(files[i], refBaseline); + var target = path.join(targetFolder, filename); + if (fs.existsSync(target)) { + fs.unlinkSync(target); + } + fs.renameSync(path.join(sourceFolder, filename), target); } } -}); +} desc("Makes the most recent rwc test results the new baseline, overwriting the old baseline"); -task("baseline-accept-rwc", function() { - jake.rmRf(refRwcBaseline); - fs.renameSync(localRwcBaseline, refRwcBaseline); +task("baseline-accept-rwc", function () { + acceptBaseline("rwc"); }); desc("Makes the most recent test262 test results the new baseline, overwriting the old baseline"); -task("baseline-accept-test262", function() { - jake.rmRf(refTest262Baseline); - fs.renameSync(localTest262Baseline, refTest262Baseline); +task("baseline-accept-test262", function () { + acceptBaseline("test262"); }); @@ -970,8 +981,8 @@ var webhostJsPath = "tests/webhost/webtsc.js"; compileFile(webhostJsPath, [webhostPath], [tscFile, webhostPath].concat(libraryTargets), [], /*useBuiltCompiler*/true); desc("Builds the tsc web host"); -task("webhost", [webhostJsPath], function() { - jake.cpR(path.join(builtLocalDirectory, "lib.d.ts"), "tests/webhost/", {silent: true}); +task("webhost", [webhostJsPath], function () { + jake.cpR(path.join(builtLocalDirectory, "lib.d.ts"), "tests/webhost/", { silent: true }); }); // Perf compiler @@ -984,38 +995,38 @@ task("perftsc", [perftscJsPath]); // Instrumented compiler var loggedIOpath = harnessDirectory + 'loggedIO.ts'; var loggedIOJsPath = builtLocalDirectory + 'loggedIO.js'; -file(loggedIOJsPath, [builtLocalDirectory, loggedIOpath], function() { +file(loggedIOJsPath, [builtLocalDirectory, loggedIOpath], function () { var temp = builtLocalDirectory + 'temp'; jake.mkdirP(temp); var options = "--outdir " + temp + ' ' + loggedIOpath; var cmd = host + " " + LKGDirectory + compilerFilename + " " + options + " "; console.log(cmd + "\n"); var ex = jake.createExec([cmd]); - ex.addListener("cmdEnd", function() { + ex.addListener("cmdEnd", function () { fs.renameSync(temp + '/harness/loggedIO.js', loggedIOJsPath); jake.rmRf(temp); complete(); }); ex.run(); -}, {async: true}); +}, { async: true }); var instrumenterPath = harnessDirectory + 'instrumenter.ts'; var instrumenterJsPath = builtLocalDirectory + 'instrumenter.js'; compileFile(instrumenterJsPath, [instrumenterPath], [tscFile, instrumenterPath].concat(libraryTargets), [], /*useBuiltCompiler*/ true); desc("Builds an instrumented tsc.js"); -task('tsc-instrumented', [loggedIOJsPath, instrumenterJsPath, tscFile], function() { +task('tsc-instrumented', [loggedIOJsPath, instrumenterJsPath, tscFile], function () { var cmd = host + ' ' + instrumenterJsPath + ' record iocapture ' + builtLocalDirectory + compilerFilename; console.log(cmd); var ex = jake.createExec([cmd]); - ex.addListener("cmdEnd", function() { + ex.addListener("cmdEnd", function () { complete(); }); ex.run(); }, { async: true }); desc("Updates the sublime plugin's tsserver"); -task("update-sublime", ["local", serverFile], function() { +task("update-sublime", ["local", serverFile], function () { jake.cpR(serverFile, "../TypeScript-Sublime-Plugin/tsserver/"); jake.cpR(serverFile + ".map", "../TypeScript-Sublime-Plugin/tsserver/"); }); @@ -1031,33 +1042,33 @@ var tslintRules = [ "objectLiteralSurroundingSpaceRule", "noTypeAssertionWhitespaceRule" ]; -var tslintRulesFiles = tslintRules.map(function(p) { +var tslintRulesFiles = tslintRules.map(function (p) { return path.join(tslintRuleDir, p + ".ts"); }); -var tslintRulesOutFiles = tslintRules.map(function(p) { +var tslintRulesOutFiles = tslintRules.map(function (p) { return path.join(builtLocalDirectory, "tslint", p + ".js"); }); desc("Compiles tslint rules to js"); task("build-rules", ["build-rules-start"].concat(tslintRulesOutFiles).concat(["build-rules-end"])); -tslintRulesFiles.forEach(function(ruleFile, i) { +tslintRulesFiles.forEach(function (ruleFile, i) { compileFile(tslintRulesOutFiles[i], [ruleFile], [ruleFile], [], /*useBuiltCompiler*/ false, - { noOutFile: true, generateDeclarations: false, outDir: path.join(builtLocalDirectory, "tslint")}); + { noOutFile: true, generateDeclarations: false, outDir: path.join(builtLocalDirectory, "tslint") }); }); desc("Emit the start of the build-rules fold"); -task("build-rules-start", [] , function() { +task("build-rules-start", [], function () { if (fold.isTravis()) console.log(fold.start("build-rules")); }); desc("Emit the end of the build-rules fold"); -task("build-rules-end", [] , function() { +task("build-rules-end", [], function () { if (fold.isTravis()) console.log(fold.end("build-rules")); }); var lintTargets = compilerSources .concat(harnessSources) // Other harness sources - .concat(["instrumenter.ts"].map(function(f) { return path.join(harnessDirectory, f) })) + .concat(["instrumenter.ts"].map(function (f) { return path.join(harnessDirectory, f) })) .concat(serverCoreSources) .concat(tslintRulesFiles) .concat(servicesSources) @@ -1068,10 +1079,10 @@ function sendNextFile(files, child, callback, failures) { var file = files.pop(); if (file) { console.log("Linting '" + file + "'."); - child.send({kind: "file", name: file}); + child.send({ kind: "file", name: file }); } else { - child.send({kind: "close"}); + child.send({ kind: "close" }); callback(failures); } } @@ -1079,7 +1090,7 @@ function sendNextFile(files, child, callback, failures) { function spawnLintWorker(files, callback) { var child = child_process.fork("./scripts/parallel-lint"); var failures = 0; - child.on("message", function(data) { + child.on("message", function (data) { switch (data.kind) { case "result": if (data.failures > 0) { @@ -1099,7 +1110,7 @@ function spawnLintWorker(files, callback) { } desc("Runs tslint on the compiler sources. Optional arguments are: f[iles]=regex"); -task("lint", ["build-rules"], function() { +task("lint", ["build-rules"], function () { if (fold.isTravis()) console.log(fold.start("lint")); var startTime = mark(); var failed = 0; @@ -1114,7 +1125,7 @@ task("lint", ["build-rules"], function() { var workerCount = (process.env.workerCount && +process.env.workerCount) || os.cpus().length; - var names = Object.keys(done).sort(function(namea, nameb) { + var names = Object.keys(done).sort(function (namea, nameb) { return done[namea] - done[nameb]; }); @@ -1138,4 +1149,4 @@ task("lint", ["build-rules"], function() { } } } -}, {async: true}); +}, { async: true }); From 8ad2744e9a7d0d7eb30286f0b6718b7b5f6cc4f9 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Fri, 19 Aug 2016 15:44:14 -0700 Subject: [PATCH 21/24] Refactor baseliners out of compiler runner (#10440) --- src/harness/compilerRunner.ts | 222 ++-------------------------------- src/harness/harness.ts | 214 ++++++++++++++++++++++++++++++++ src/harness/rwcRunner.ts | 7 +- 3 files changed, 227 insertions(+), 216 deletions(-) diff --git a/src/harness/compilerRunner.ts b/src/harness/compilerRunner.ts index 88e81ffcf348d..a64435b7ddcbe 100644 --- a/src/harness/compilerRunner.ts +++ b/src/harness/compilerRunner.ts @@ -1,8 +1,6 @@ /// /// /// -// In harness baselines, null is different than undefined. See `generateActual` in `harness.ts`. -/* tslint:disable:no-null-keyword */ const enum CompilerTestType { Conformance, @@ -136,21 +134,10 @@ class CompilerBaselineRunner extends RunnerBase { otherFiles = undefined; }); - function getByteOrderMarkText(file: Harness.Compiler.GeneratedFile): string { - return file.writeByteOrderMark ? "\u00EF\u00BB\u00BF" : ""; - } - - function getErrorBaseline(toBeCompiled: Harness.Compiler.TestFile[], otherFiles: Harness.Compiler.TestFile[], result: Harness.Compiler.CompilerResult) { - return Harness.Compiler.getErrorBaseline(toBeCompiled.concat(otherFiles), result.errors); - } - // check errors it("Correct errors for " + fileName, () => { if (this.errors) { - Harness.Baseline.runBaseline(justName.replace(/\.tsx?$/, ".errors.txt"), (): string => { - if (result.errors.length === 0) return null; - return getErrorBaseline(toBeCompiled, otherFiles, result); - }); + Harness.Compiler.doErrorBaseline(justName, toBeCompiled.concat(otherFiles), result.errors); } }); @@ -168,8 +155,10 @@ class CompilerBaselineRunner extends RunnerBase { Harness.Baseline.runBaseline(justName.replace(/\.tsx?$/, ".sourcemap.txt"), () => { const record = result.getSourceMapRecord(); if (options.noEmitOnError && result.errors.length !== 0 && record === undefined) { - // Because of the noEmitOnError option no files are created. We need to return null because baselining isn"t required. + // Because of the noEmitOnError option no files are created. We need to return null because baselining isn't required. + /* tslint:disable:no-null-keyword */ return null; + /* tslint:enable:no-null-keyword */ } return record; }); @@ -178,87 +167,12 @@ class CompilerBaselineRunner extends RunnerBase { it("Correct JS output for " + fileName, () => { if (hasNonDtsFiles && this.emit) { - if (!options.noEmit && result.files.length === 0 && result.errors.length === 0) { - throw new Error("Expected at least one js file to be emitted or at least one error to be created."); - } - - // check js output - Harness.Baseline.runBaseline(justName.replace(/\.tsx?/, ".js"), () => { - let tsCode = ""; - const tsSources = otherFiles.concat(toBeCompiled); - if (tsSources.length > 1) { - tsCode += "//// [" + fileName + "] ////\r\n\r\n"; - } - for (let i = 0; i < tsSources.length; i++) { - tsCode += "//// [" + Harness.Path.getFileName(tsSources[i].unitName) + "]\r\n"; - tsCode += tsSources[i].content + (i < (tsSources.length - 1) ? "\r\n" : ""); - } - - let jsCode = ""; - for (let i = 0; i < result.files.length; i++) { - jsCode += "//// [" + Harness.Path.getFileName(result.files[i].fileName) + "]\r\n"; - jsCode += getByteOrderMarkText(result.files[i]); - jsCode += result.files[i].code; - } - - if (result.declFilesCode.length > 0) { - jsCode += "\r\n\r\n"; - for (let i = 0; i < result.declFilesCode.length; i++) { - jsCode += "//// [" + Harness.Path.getFileName(result.declFilesCode[i].fileName) + "]\r\n"; - jsCode += getByteOrderMarkText(result.declFilesCode[i]); - jsCode += result.declFilesCode[i].code; - } - } - - const declFileCompilationResult = - Harness.Compiler.compileDeclarationFiles( - toBeCompiled, otherFiles, result, harnessSettings, options, /*currentDirectory*/ undefined); - - if (declFileCompilationResult && declFileCompilationResult.declResult.errors.length) { - jsCode += "\r\n\r\n//// [DtsFileErrors]\r\n"; - jsCode += "\r\n\r\n"; - jsCode += getErrorBaseline(declFileCompilationResult.declInputFiles, declFileCompilationResult.declOtherFiles, declFileCompilationResult.declResult); - } - - if (jsCode.length > 0) { - return tsCode + "\r\n\r\n" + jsCode; - } - else { - return null; - } - }); + Harness.Compiler.doJsEmitBaseline(justName, fileName, options, result, toBeCompiled, otherFiles, harnessSettings); } }); it("Correct Sourcemap output for " + fileName, () => { - if (options.inlineSourceMap) { - if (result.sourceMaps.length > 0) { - throw new Error("No sourcemap files should be generated if inlineSourceMaps was set."); - } - return null; - } - else if (options.sourceMap) { - if (result.sourceMaps.length !== result.files.length) { - throw new Error("Number of sourcemap files should be same as js files."); - } - - Harness.Baseline.runBaseline(justName.replace(/\.tsx?/, ".js.map"), () => { - if (options.noEmitOnError && result.errors.length !== 0 && result.sourceMaps.length === 0) { - // We need to return null here or the runBaseLine will actually create a empty file. - // Baselining isn't required here because there is no output. - return null; - } - - let sourceMapCode = ""; - for (let i = 0; i < result.sourceMaps.length; i++) { - sourceMapCode += "//// [" + Harness.Path.getFileName(result.sourceMaps[i].fileName) + "]\r\n"; - sourceMapCode += getByteOrderMarkText(result.sourceMaps[i]); - sourceMapCode += result.sourceMaps[i].code; - } - - return sourceMapCode; - }); - } + Harness.Compiler.doSourcemapBaseline(justName, options, result); }); it("Correct type/symbol baselines for " + fileName, () => { @@ -266,129 +180,7 @@ class CompilerBaselineRunner extends RunnerBase { return; } - // NEWTODO: Type baselines - if (result.errors.length !== 0) { - return; - } - - // The full walker simulates the types that you would get from doing a full - // compile. The pull walker simulates the types you get when you just do - // a type query for a random node (like how the LS would do it). Most of the - // time, these will be the same. However, occasionally, they can be different. - // Specifically, when the compiler internally depends on symbol IDs to order - // things, then we may see different results because symbols can be created in a - // different order with 'pull' operations, and thus can produce slightly differing - // output. - // - // For example, with a full type check, we may see a type displayed as: number | string - // But with a pull type check, we may see it as: string | number - // - // These types are equivalent, but depend on what order the compiler observed - // certain parts of the program. - - const program = result.program; - const allFiles = toBeCompiled.concat(otherFiles).filter(file => !!program.getSourceFile(file.unitName)); - - const fullWalker = new TypeWriterWalker(program, /*fullTypeCheck*/ true); - - const fullResults = ts.createMap(); - const pullResults = ts.createMap(); - - for (const sourceFile of allFiles) { - fullResults[sourceFile.unitName] = fullWalker.getTypeAndSymbols(sourceFile.unitName); - pullResults[sourceFile.unitName] = fullWalker.getTypeAndSymbols(sourceFile.unitName); - } - - // Produce baselines. The first gives the types for all expressions. - // The second gives symbols for all identifiers. - let e1: Error, e2: Error; - try { - checkBaseLines(/*isSymbolBaseLine*/ false); - } - catch (e) { - e1 = e; - } - - try { - checkBaseLines(/*isSymbolBaseLine*/ true); - } - catch (e) { - e2 = e; - } - - if (e1 || e2) { - throw e1 || e2; - } - - return; - - function checkBaseLines(isSymbolBaseLine: boolean) { - const fullBaseLine = generateBaseLine(fullResults, isSymbolBaseLine); - const pullBaseLine = generateBaseLine(pullResults, isSymbolBaseLine); - - const fullExtension = isSymbolBaseLine ? ".symbols" : ".types"; - const pullExtension = isSymbolBaseLine ? ".symbols.pull" : ".types.pull"; - - if (fullBaseLine !== pullBaseLine) { - Harness.Baseline.runBaseline(justName.replace(/\.tsx?/, fullExtension), () => fullBaseLine); - Harness.Baseline.runBaseline(justName.replace(/\.tsx?/, pullExtension), () => pullBaseLine); - } - else { - Harness.Baseline.runBaseline(justName.replace(/\.tsx?/, fullExtension), () => fullBaseLine); - } - } - - function generateBaseLine(typeWriterResults: ts.Map, isSymbolBaseline: boolean): string { - const typeLines: string[] = []; - const typeMap: { [fileName: string]: { [lineNum: number]: string[]; } } = {}; - - allFiles.forEach(file => { - const codeLines = file.content.split("\n"); - typeWriterResults[file.unitName].forEach(result => { - if (isSymbolBaseline && !result.symbol) { - return; - } - - const typeOrSymbolString = isSymbolBaseline ? result.symbol : result.type; - const formattedLine = result.sourceText.replace(/\r?\n/g, "") + " : " + typeOrSymbolString; - if (!typeMap[file.unitName]) { - typeMap[file.unitName] = {}; - } - - let typeInfo = [formattedLine]; - const existingTypeInfo = typeMap[file.unitName][result.line]; - if (existingTypeInfo) { - typeInfo = existingTypeInfo.concat(typeInfo); - } - typeMap[file.unitName][result.line] = typeInfo; - }); - - typeLines.push("=== " + file.unitName + " ===\r\n"); - for (let i = 0; i < codeLines.length; i++) { - const currentCodeLine = codeLines[i]; - typeLines.push(currentCodeLine + "\r\n"); - if (typeMap[file.unitName]) { - const typeInfo = typeMap[file.unitName][i]; - if (typeInfo) { - typeInfo.forEach(ty => { - typeLines.push(">" + ty + "\r\n"); - }); - if (i + 1 < codeLines.length && (codeLines[i + 1].match(/^\s*[{|}]\s*$/) || codeLines[i + 1].trim() === "")) { - } - else { - typeLines.push("\r\n"); - } - } - } - else { - typeLines.push("No type information for this code."); - } - } - }); - - return typeLines.join(""); - } - + Harness.Compiler.doTypeAndSymbolBaseline(justName, result, toBeCompiled.concat(otherFiles).filter(file => !!result.program.getSourceFile(file.unitName))); }); }); } diff --git a/src/harness/harness.ts b/src/harness/harness.ts index 3375b8e47e7dc..14cb3147c323d 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -1328,6 +1328,220 @@ namespace Harness { Harness.IO.newLine() + Harness.IO.newLine() + outputLines.join("\r\n"); } + export function doErrorBaseline(baselinePath: string, inputFiles: TestFile[], errors: ts.Diagnostic[]) { + Harness.Baseline.runBaseline(baselinePath.replace(/\.tsx?$/, ".errors.txt"), (): string => { + if (errors.length === 0) { + /* tslint:disable:no-null-keyword */ + return null; + /* tslint:enable:no-null-keyword */ + } + return getErrorBaseline(inputFiles, errors); + }); + } + + export function doTypeAndSymbolBaseline(baselinePath: string, result: CompilerResult, allFiles: {unitName: string, content: string}[]) { + if (result.errors.length !== 0) { + return; + } + // The full walker simulates the types that you would get from doing a full + // compile. The pull walker simulates the types you get when you just do + // a type query for a random node (like how the LS would do it). Most of the + // time, these will be the same. However, occasionally, they can be different. + // Specifically, when the compiler internally depends on symbol IDs to order + // things, then we may see different results because symbols can be created in a + // different order with 'pull' operations, and thus can produce slightly differing + // output. + // + // For example, with a full type check, we may see a type displayed as: number | string + // But with a pull type check, we may see it as: string | number + // + // These types are equivalent, but depend on what order the compiler observed + // certain parts of the program. + + const program = result.program; + + const fullWalker = new TypeWriterWalker(program, /*fullTypeCheck*/ true); + + const fullResults = ts.createMap(); + + for (const sourceFile of allFiles) { + fullResults[sourceFile.unitName] = fullWalker.getTypeAndSymbols(sourceFile.unitName); + } + + // Produce baselines. The first gives the types for all expressions. + // The second gives symbols for all identifiers. + let e1: Error, e2: Error; + try { + checkBaseLines(/*isSymbolBaseLine*/ false); + } + catch (e) { + e1 = e; + } + + try { + checkBaseLines(/*isSymbolBaseLine*/ true); + } + catch (e) { + e2 = e; + } + + if (e1 || e2) { + throw e1 || e2; + } + + return; + + function checkBaseLines(isSymbolBaseLine: boolean) { + const fullBaseLine = generateBaseLine(fullResults, isSymbolBaseLine); + + const fullExtension = isSymbolBaseLine ? ".symbols" : ".types"; + + Harness.Baseline.runBaseline(baselinePath.replace(/\.tsx?/, fullExtension), () => fullBaseLine); + } + + function generateBaseLine(typeWriterResults: ts.Map, isSymbolBaseline: boolean): string { + const typeLines: string[] = []; + const typeMap: { [fileName: string]: { [lineNum: number]: string[]; } } = {}; + + allFiles.forEach(file => { + const codeLines = file.content.split("\n"); + typeWriterResults[file.unitName].forEach(result => { + if (isSymbolBaseline && !result.symbol) { + return; + } + + const typeOrSymbolString = isSymbolBaseline ? result.symbol : result.type; + const formattedLine = result.sourceText.replace(/\r?\n/g, "") + " : " + typeOrSymbolString; + if (!typeMap[file.unitName]) { + typeMap[file.unitName] = {}; + } + + let typeInfo = [formattedLine]; + const existingTypeInfo = typeMap[file.unitName][result.line]; + if (existingTypeInfo) { + typeInfo = existingTypeInfo.concat(typeInfo); + } + typeMap[file.unitName][result.line] = typeInfo; + }); + + typeLines.push("=== " + file.unitName + " ===\r\n"); + for (let i = 0; i < codeLines.length; i++) { + const currentCodeLine = codeLines[i]; + typeLines.push(currentCodeLine + "\r\n"); + if (typeMap[file.unitName]) { + const typeInfo = typeMap[file.unitName][i]; + if (typeInfo) { + typeInfo.forEach(ty => { + typeLines.push(">" + ty + "\r\n"); + }); + if (i + 1 < codeLines.length && (codeLines[i + 1].match(/^\s*[{|}]\s*$/) || codeLines[i + 1].trim() === "")) { + } + else { + typeLines.push("\r\n"); + } + } + } + else { + typeLines.push("No type information for this code."); + } + } + }); + + return typeLines.join(""); + } + } + + function getByteOrderMarkText(file: Harness.Compiler.GeneratedFile): string { + return file.writeByteOrderMark ? "\u00EF\u00BB\u00BF" : ""; + } + + export function doSourcemapBaseline(baselinePath: string, options: ts.CompilerOptions, result: CompilerResult) { + if (options.inlineSourceMap) { + if (result.sourceMaps.length > 0) { + throw new Error("No sourcemap files should be generated if inlineSourceMaps was set."); + } + return; + } + else if (options.sourceMap) { + if (result.sourceMaps.length !== result.files.length) { + throw new Error("Number of sourcemap files should be same as js files."); + } + + Harness.Baseline.runBaseline(baselinePath.replace(/\.tsx?/, ".js.map"), () => { + if (options.noEmitOnError && result.errors.length !== 0 && result.sourceMaps.length === 0) { + // We need to return null here or the runBaseLine will actually create a empty file. + // Baselining isn't required here because there is no output. + /* tslint:disable:no-null-keyword */ + return null; + /* tslint:enable:no-null-keyword */ + } + + let sourceMapCode = ""; + for (let i = 0; i < result.sourceMaps.length; i++) { + sourceMapCode += "//// [" + Harness.Path.getFileName(result.sourceMaps[i].fileName) + "]\r\n"; + sourceMapCode += getByteOrderMarkText(result.sourceMaps[i]); + sourceMapCode += result.sourceMaps[i].code; + } + + return sourceMapCode; + }); + } + } + + export function doJsEmitBaseline(baselinePath: string, header: string, options: ts.CompilerOptions, result: CompilerResult, toBeCompiled: Harness.Compiler.TestFile[], otherFiles: Harness.Compiler.TestFile[], harnessSettings: Harness.TestCaseParser.CompilerSettings) { + if (!options.noEmit && result.files.length === 0 && result.errors.length === 0) { + throw new Error("Expected at least one js file to be emitted or at least one error to be created."); + } + + // check js output + Harness.Baseline.runBaseline(baselinePath.replace(/\.tsx?/, ".js"), () => { + let tsCode = ""; + const tsSources = otherFiles.concat(toBeCompiled); + if (tsSources.length > 1) { + tsCode += "//// [" + header + "] ////\r\n\r\n"; + } + for (let i = 0; i < tsSources.length; i++) { + tsCode += "//// [" + Harness.Path.getFileName(tsSources[i].unitName) + "]\r\n"; + tsCode += tsSources[i].content + (i < (tsSources.length - 1) ? "\r\n" : ""); + } + + let jsCode = ""; + for (let i = 0; i < result.files.length; i++) { + jsCode += "//// [" + Harness.Path.getFileName(result.files[i].fileName) + "]\r\n"; + jsCode += getByteOrderMarkText(result.files[i]); + jsCode += result.files[i].code; + } + + if (result.declFilesCode.length > 0) { + jsCode += "\r\n\r\n"; + for (let i = 0; i < result.declFilesCode.length; i++) { + jsCode += "//// [" + Harness.Path.getFileName(result.declFilesCode[i].fileName) + "]\r\n"; + jsCode += getByteOrderMarkText(result.declFilesCode[i]); + jsCode += result.declFilesCode[i].code; + } + } + + const declFileCompilationResult = + Harness.Compiler.compileDeclarationFiles( + toBeCompiled, otherFiles, result, harnessSettings, options, /*currentDirectory*/ undefined); + + if (declFileCompilationResult && declFileCompilationResult.declResult.errors.length) { + jsCode += "\r\n\r\n//// [DtsFileErrors]\r\n"; + jsCode += "\r\n\r\n"; + jsCode += Harness.Compiler.getErrorBaseline(declFileCompilationResult.declInputFiles.concat(declFileCompilationResult.declOtherFiles), declFileCompilationResult.declResult.errors); + } + + if (jsCode.length > 0) { + return tsCode + "\r\n\r\n" + jsCode; + } + else { + /* tslint:disable:no-null-keyword */ + return null; + /* tslint:enable:no-null-keyword */ + } + }); + } + export function collateOutputs(outputFiles: Harness.Compiler.GeneratedFile[]): string { // Collect, test, and sort the fileNames outputFiles.sort((a, b) => cleanName(a.fileName).localeCompare(cleanName(b.fileName))); diff --git a/src/harness/rwcRunner.ts b/src/harness/rwcRunner.ts index 17346016fbbd0..5720f9b541c46 100644 --- a/src/harness/rwcRunner.ts +++ b/src/harness/rwcRunner.ts @@ -222,7 +222,12 @@ namespace RWC { } }); - // TODO: Type baselines (need to refactor out from compilerRunner) + it("has the expected types", () => { + Harness.Compiler.doTypeAndSymbolBaseline(baseName, compilerResult, inputFiles + .concat(otherFiles) + .filter(file => !!compilerResult.program.getSourceFile(file.unitName)) + .filter(e => !Harness.isDefaultLibraryFile(e.unitName))); + }); }); } } From 057357be88319368da2e1380da93915075ed05fc Mon Sep 17 00:00:00 2001 From: zhengbli Date: Fri, 19 Aug 2016 15:48:46 -0700 Subject: [PATCH 22/24] CR feedback --- src/services/services.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/services/services.ts b/src/services/services.ts index 64b872d14a774..94b69113da799 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -4913,22 +4913,24 @@ namespace ts { if (!documentation) { documentation = symbol.getDocumentationComment(); - if ((!documentation || documentation.length === 0) && symbol.flags & SymbolFlags.Property) { + if (documentation.length === 0 && symbol.flags & SymbolFlags.Property) { // For some special property access expressions like `experts.foo = foo` or `module.exports.foo = foo` // there documentation comments might be attached to the right hand side symbol of their declarations. // The pattern of such special property access is that the parent symbol is the symbol of the file. if (symbol.parent && forEach(symbol.parent.declarations, declaration => declaration.kind === SyntaxKind.SourceFile)) { - forEach(symbol.declarations, declaration => { - if (declaration.parent && declaration.parent.kind === SyntaxKind.BinaryExpression) { - const rhsSymbol = program.getTypeChecker().getSymbolAtLocation((declaration.parent).right); - if (rhsSymbol) { - documentation = rhsSymbol.getDocumentationComment(); - if (documentation && documentation.length > 0) { - return true; - } + for(const declaration of symbol.declarations) { + if (!declaration.parent || declaration.parent.kind !== SyntaxKind.BinaryExpression) { + continue; + } + + const rhsSymbol = program.getTypeChecker().getSymbolAtLocation((declaration.parent).right); + if (rhsSymbol) { + documentation = rhsSymbol.getDocumentationComment(); + if (documentation.length > 0) { + return; } } - }); + } } } } From a5bb13f6c1b5020c61d681432f33d5f3f939c941 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Fri, 19 Aug 2016 16:53:36 -0700 Subject: [PATCH 23/24] fix broken tests --- src/services/services.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/services/services.ts b/src/services/services.ts index 94b69113da799..c59551457ba8d 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -4918,17 +4918,19 @@ namespace ts { // there documentation comments might be attached to the right hand side symbol of their declarations. // The pattern of such special property access is that the parent symbol is the symbol of the file. if (symbol.parent && forEach(symbol.parent.declarations, declaration => declaration.kind === SyntaxKind.SourceFile)) { - for(const declaration of symbol.declarations) { + for (const declaration of symbol.declarations) { if (!declaration.parent || declaration.parent.kind !== SyntaxKind.BinaryExpression) { continue; } const rhsSymbol = program.getTypeChecker().getSymbolAtLocation((declaration.parent).right); - if (rhsSymbol) { - documentation = rhsSymbol.getDocumentationComment(); - if (documentation.length > 0) { - return; - } + if (!rhsSymbol) { + continue; + } + + documentation = rhsSymbol.getDocumentationComment(); + if (documentation.length > 0) { + break; } } } From a531b87b3c668f56a4140699f89b7fcc4160dfbc Mon Sep 17 00:00:00 2001 From: Yui Date: Fri, 19 Aug 2016 17:06:05 -0700 Subject: [PATCH 24/24] Pass in baselineOpts into types baselines so that RWC baselines can be written to internal folder (#10443) --- src/harness/harness.ts | 4 ++-- src/harness/rwcRunner.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/harness/harness.ts b/src/harness/harness.ts index 14cb3147c323d..9e27f500b80f9 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -1339,7 +1339,7 @@ namespace Harness { }); } - export function doTypeAndSymbolBaseline(baselinePath: string, result: CompilerResult, allFiles: {unitName: string, content: string}[]) { + export function doTypeAndSymbolBaseline(baselinePath: string, result: CompilerResult, allFiles: {unitName: string, content: string}[], opts?: Harness.Baseline.BaselineOptions) { if (result.errors.length !== 0) { return; } @@ -1396,7 +1396,7 @@ namespace Harness { const fullExtension = isSymbolBaseLine ? ".symbols" : ".types"; - Harness.Baseline.runBaseline(baselinePath.replace(/\.tsx?/, fullExtension), () => fullBaseLine); + Harness.Baseline.runBaseline(baselinePath.replace(/\.tsx?/, fullExtension), () => fullBaseLine, opts); } function generateBaseLine(typeWriterResults: ts.Map, isSymbolBaseline: boolean): string { diff --git a/src/harness/rwcRunner.ts b/src/harness/rwcRunner.ts index 5720f9b541c46..d56e8d6d35f63 100644 --- a/src/harness/rwcRunner.ts +++ b/src/harness/rwcRunner.ts @@ -226,7 +226,7 @@ namespace RWC { Harness.Compiler.doTypeAndSymbolBaseline(baseName, compilerResult, inputFiles .concat(otherFiles) .filter(file => !!compilerResult.program.getSourceFile(file.unitName)) - .filter(e => !Harness.isDefaultLibraryFile(e.unitName))); + .filter(e => !Harness.isDefaultLibraryFile(e.unitName)), baselineOpts); }); }); }