From 99e95a40ce4e9474f5aad0faa19bd838f6a9627a Mon Sep 17 00:00:00 2001 From: Roy Tinker Date: Thu, 19 Dec 2019 12:14:29 -0800 Subject: [PATCH 1/2] Implement #35656 --- src/compiler/commandLineParser.ts | 13 +------------ src/compiler/parser.ts | 13 +++++++++++++ src/compiler/types.ts | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 8bf71d7301d8a..c4437ca58dbaf 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -273,18 +273,7 @@ namespace ts { { name: "target", shortName: "t", - type: createMapFromTemplate({ - es3: ScriptTarget.ES3, - es5: ScriptTarget.ES5, - es6: ScriptTarget.ES2015, - es2015: ScriptTarget.ES2015, - es2016: ScriptTarget.ES2016, - es2017: ScriptTarget.ES2017, - es2018: ScriptTarget.ES2018, - es2019: ScriptTarget.ES2019, - es2020: ScriptTarget.ES2020, - esnext: ScriptTarget.ESNext, - }), + type: ScriptTargetMap, affectsSourceFile: true, affectsModuleResolution: true, affectsEmit: true, diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 6e4e168e2ca77..7a54b98a337f6 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -8186,6 +8186,19 @@ namespace ts { }); break; } + case "compilerOptions": { + forEach(toArray(entryOrList), entry => { + // _last_ compilerOptions target value in a file is the "winner" + const targetKey = (entry as PragmaPseudoMap["compilerOptions"]).arguments.target; + if (ScriptTargetMap.has(targetKey)) { + context.languageVersion = ScriptTargetMap.get(targetKey) as ScriptTarget; + } + else { + Debug.fail(`Unrecognized compilerOptions target in XML pragma: ${targetKey}`); + } + }); + break; + } case "jsx": return; // Accessed directly default: Debug.fail("Unhandled pragma kind"); // Can this be made into an assertNever in the future? } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 393e48695a2a6..5ea61a56228f9 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5163,6 +5163,20 @@ namespace ts { Latest = ESNext, } + export const ScriptTargetMap = createMapFromTemplate({ + es3: ScriptTarget.ES3, + es5: ScriptTarget.ES5, + es6: ScriptTarget.ES2015, + es2015: ScriptTarget.ES2015, + es2016: ScriptTarget.ES2016, + es2017: ScriptTarget.ES2017, + es2018: ScriptTarget.ES2018, + es2019: ScriptTarget.ES2019, + es2020: ScriptTarget.ES2020, + esnext: ScriptTarget.ESNext, + }); + Object.freeze(ScriptTargetMap); + export const enum LanguageVariant { Standard, JSX @@ -6454,6 +6468,10 @@ namespace ts { args: [{ name: "name" }], kind: PragmaKindFlags.TripleSlashXML }, + "compilerOptions": { + args: [{ name: "target" }], + kind: PragmaKindFlags.TripleSlashXML + }, "ts-check": { kind: PragmaKindFlags.SingleLine }, From 4481c5070f0fa9b6036d04f719701e14a6a4b9ba Mon Sep 17 00:00:00 2001 From: Roy Tinker Date: Thu, 19 Dec 2019 15:30:05 -0800 Subject: [PATCH 2/2] Fix issue with compiler-options pragma --- src/compiler/parser.ts | 9 +++++---- src/compiler/types.ts | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 7a54b98a337f6..1444239d40e2c 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -8186,15 +8186,16 @@ namespace ts { }); break; } - case "compilerOptions": { + case "compiler-options": { forEach(toArray(entryOrList), entry => { - // _last_ compilerOptions target value in a file is the "winner" - const targetKey = (entry as PragmaPseudoMap["compilerOptions"]).arguments.target; + // _last_ compiler-options target value in a file is the "winner" + const targetKey = (entry as PragmaPseudoMap["compiler-options"]).arguments.target; if (ScriptTargetMap.has(targetKey)) { + console.log("setting language version to " + targetKey); context.languageVersion = ScriptTargetMap.get(targetKey) as ScriptTarget; } else { - Debug.fail(`Unrecognized compilerOptions target in XML pragma: ${targetKey}`); + Debug.fail(`Unrecognized compiler-options target in XML pragma: ${targetKey}`); } }); break; diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 5ea61a56228f9..609f6cb953b6c 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -6468,7 +6468,7 @@ namespace ts { args: [{ name: "name" }], kind: PragmaKindFlags.TripleSlashXML }, - "compilerOptions": { + "compiler-options": { args: [{ name: "target" }], kind: PragmaKindFlags.TripleSlashXML },