Skip to content

Commit 266ea5f

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 e81e6fb commit 266ea5f

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 = compilerOptions.strictNullChecks === undefined ? compilerOptions.strict : compilerOptions.strictNullChecks;
6969
const strictFunctionTypes = compilerOptions.strictFunctionTypes === undefined ? compilerOptions.strict : compilerOptions.strictFunctionTypes;
7070
const noImplicitAny = compilerOptions.noImplicitAny === undefined ? compilerOptions.strict : compilerOptions.noImplicitAny;

src/compiler/core.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,13 @@ namespace ts {
17101710
return moduleResolution;
17111711
}
17121712

1713+
export function getAllowSyntheticDefaultImports(compilerOptions: CompilerOptions) {
1714+
const moduleKind = getEmitModuleKind(compilerOptions);
1715+
return compilerOptions.allowSyntheticDefaultImports !== undefined
1716+
? compilerOptions.allowSyntheticDefaultImports
1717+
: moduleKind === ModuleKind.System;
1718+
}
1719+
17131720
export function hasZeroOrOneAsteriskCharacter(str: string): boolean {
17141721
let seenAsterisk = false;
17151722
for (let i = 0; i < str.length; i++) {

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)