Skip to content

Commit af6b43a

Browse files
committed
Address PR feedback:
- extract test for synethetic default imports into getAllowSyntheticDefaultImports in core.ts - use getAllowSyntheticDefaultImports in checker.ts and importFixes.ts - move compilerOptions to top level destructuring
1 parent 0b13f5a commit af6b43a

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ namespace ts {
6464
const languageVersion = getEmitScriptTarget(compilerOptions);
6565
const modulekind = getEmitModuleKind(compilerOptions);
6666
const noUnusedIdentifiers = !!compilerOptions.noUnusedLocals || !!compilerOptions.noUnusedParameters;
67-
const allowSyntheticDefaultImports = typeof compilerOptions.allowSyntheticDefaultImports !== "undefined" ? compilerOptions.allowSyntheticDefaultImports : modulekind === ModuleKind.System;
67+
const allowSyntheticDefaultImports = getAllowSyntheticDefaultImports(compilerOptions);
6868
const strictNullChecks = getStrictOptionValue(compilerOptions, "strictNullChecks");
6969
const strictFunctionTypes = getStrictOptionValue(compilerOptions, "strictFunctionTypes");
7070
const noImplicitAny = getStrictOptionValue(compilerOptions, "noImplicitAny");

src/compiler/core.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1684,6 +1684,13 @@ namespace ts {
16841684
return moduleResolution;
16851685
}
16861686

1687+
export function getAllowSyntheticDefaultImports(compilerOptions: CompilerOptions) {
1688+
const moduleKind = getEmitModuleKind(compilerOptions);
1689+
return compilerOptions.allowSyntheticDefaultImports !== undefined
1690+
? compilerOptions.allowSyntheticDefaultImports
1691+
: moduleKind === ModuleKind.System;
1692+
}
1693+
16871694
export type StrictOptionName = "noImplicitAny" | "noImplicitThis" | "strictNullChecks" | "strictFunctionTypes" | "alwaysStrict";
16881695

16891696
export function getStrictOptionValue(compilerOptions: CompilerOptions, flag: StrictOptionName): boolean {

src/services/codefixes/importFixes.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ namespace ts.codefix {
648648
}
649649

650650
function getActionsForUMDImport(context: ImportCodeFixContext): ImportCodeAction[] {
651-
const { checker, symbolToken } = context;
651+
const { checker, symbolToken, compilerOptions } = context;
652652
const umdSymbol = checker.getSymbolAtLocation(symbolToken);
653653
let symbol: ts.Symbol;
654654
let symbolName: string;
@@ -665,15 +665,16 @@ namespace ts.codefix {
665665
Debug.fail("Either the symbol or the JSX namespace should be a UMD global if we got here");
666666
}
667667

668-
const { module, allowSyntheticDefaultImports } = context.compilerOptions;
668+
const allowSyntheticDefaultImports = getAllowSyntheticDefaultImports(compilerOptions);
669669

670670
// Prefer to import as a synthetic `default` if available.
671-
if (allowSyntheticDefaultImports || module === ModuleKind.System && allowSyntheticDefaultImports !== false) {
671+
if (allowSyntheticDefaultImports) {
672672
return getCodeActionForImport(symbol, { ...context, symbolName, kind: ImportKind.Default });
673673
}
674+
const moduleKind = getEmitModuleKind(compilerOptions);
674675

675676
// When a synthetic `default` is unavailable, use `import..require` if the module kind supports it.
676-
if (module === ModuleKind.AMD || module === ModuleKind.CommonJS || module === ModuleKind.UMD) {
677+
if (moduleKind === ModuleKind.AMD || moduleKind === ModuleKind.CommonJS || moduleKind === ModuleKind.UMD) {
677678
return getCodeActionForImport(symbol, { ...context, symbolName, kind: ImportKind.Equals });
678679
}
679680

0 commit comments

Comments
 (0)