From 5aa91bc0cc2b48270ae30974f99c3ee6901c7559 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Thu, 4 Apr 2019 11:57:08 -0700 Subject: [PATCH] Merge pull request #30734 from andrewbranch/bug/30664 Fall back from `globalReadonlyArrayType` to `globalArrayType` when transpiling --- src/compiler/checker.ts | 2 +- src/testRunner/unittests/services/transpile.ts | 8 ++++++++ .../reference/transpile/Supports as const arrays.js | 2 ++ .../transpile/Supports as const arrays.oldTranspile.js | 2 ++ .../transpile/Supports readonly keyword for arrays.js | 2 ++ .../Supports readonly keyword for arrays.oldTranspile.js | 2 ++ 6 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/transpile/Supports as const arrays.js create mode 100644 tests/baselines/reference/transpile/Supports as const arrays.oldTranspile.js create mode 100644 tests/baselines/reference/transpile/Supports readonly keyword for arrays.js create mode 100644 tests/baselines/reference/transpile/Supports readonly keyword for arrays.oldTranspile.js diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 98a459b6ccd3d..a6d21b2b010ad 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -30293,7 +30293,7 @@ namespace ts { autoArrayType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); } - globalReadonlyArrayType = getGlobalTypeOrUndefined("ReadonlyArray" as __String, /*arity*/ 1); + globalReadonlyArrayType = getGlobalTypeOrUndefined("ReadonlyArray" as __String, /*arity*/ 1) || globalArrayType; anyReadonlyArrayType = globalReadonlyArrayType ? createTypeFromGenericGlobalType(globalReadonlyArrayType, [anyType]) : anyArrayType; globalThisType = getGlobalTypeOrUndefined("ThisType" as __String, /*arity*/ 1); diff --git a/src/testRunner/unittests/services/transpile.ts b/src/testRunner/unittests/services/transpile.ts index d197d6db62104..8429179ffa345 100644 --- a/src/testRunner/unittests/services/transpile.ts +++ b/src/testRunner/unittests/services/transpile.ts @@ -445,5 +445,13 @@ var x = 0;`, { } } }); + + transpilesCorrectly("Supports readonly keyword for arrays", "let x: readonly string[];", { + options: { compilerOptions: { module: ModuleKind.CommonJS } } + }); + + transpilesCorrectly("Supports 'as const' arrays", `([] as const).forEach(k => console.log(k));`, { + options: { compilerOptions: { module: ModuleKind.CommonJS } } + }); }); } diff --git a/tests/baselines/reference/transpile/Supports as const arrays.js b/tests/baselines/reference/transpile/Supports as const arrays.js new file mode 100644 index 0000000000000..aec83eb5ae525 --- /dev/null +++ b/tests/baselines/reference/transpile/Supports as const arrays.js @@ -0,0 +1,2 @@ +[].forEach(function (k) { return console.log(k); }); +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Supports as const arrays.oldTranspile.js b/tests/baselines/reference/transpile/Supports as const arrays.oldTranspile.js new file mode 100644 index 0000000000000..aec83eb5ae525 --- /dev/null +++ b/tests/baselines/reference/transpile/Supports as const arrays.oldTranspile.js @@ -0,0 +1,2 @@ +[].forEach(function (k) { return console.log(k); }); +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Supports readonly keyword for arrays.js b/tests/baselines/reference/transpile/Supports readonly keyword for arrays.js new file mode 100644 index 0000000000000..50fa840c62e8f --- /dev/null +++ b/tests/baselines/reference/transpile/Supports readonly keyword for arrays.js @@ -0,0 +1,2 @@ +var x; +//# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Supports readonly keyword for arrays.oldTranspile.js b/tests/baselines/reference/transpile/Supports readonly keyword for arrays.oldTranspile.js new file mode 100644 index 0000000000000..50fa840c62e8f --- /dev/null +++ b/tests/baselines/reference/transpile/Supports readonly keyword for arrays.oldTranspile.js @@ -0,0 +1,2 @@ +var x; +//# sourceMappingURL=file.js.map \ No newline at end of file