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 },