Skip to content

Replace all instances of compilerOptions.isolatedModules with function considering verbatimModuleSyntax #52746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/compiler/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
generateDjb2Hash,
getDirectoryPath,
getEmitDeclarations,
getIsolatedModules,
getNormalizedAbsolutePath,
getOptionsNameMap,
getOwnKeys,
Expand Down Expand Up @@ -763,7 +764,7 @@ function handleDtsMayChangeOfReferencingExportOfAffectedFile(

// Since isolated modules dont change js files, files affected by change in signature is itself
// But we need to cleanup semantic diagnostics and queue dts emit for affected files
if (state.compilerOptions.isolatedModules) {
if (getIsolatedModules(state.compilerOptions)) {
const seenFileNamesMap = new Map<Path, true>();
seenFileNamesMap.set(affectedFile.resolvedPath, true);
const queue = BuilderState.getReferencedByPaths(state, affectedFile.resolvedPath);
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/builderState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ExportedModulesFromDeclarationEmit,
GetCanonicalFileName,
getDirectoryPath,
getIsolatedModules,
getSourceFileOfNode,
HostForComputeHash,
isDeclarationFileName,
Expand Down Expand Up @@ -635,7 +636,7 @@ export namespace BuilderState {
}

const compilerOptions = programOfThisState.getCompilerOptions();
if (compilerOptions && (compilerOptions.isolatedModules || outFile(compilerOptions))) {
if (compilerOptions && (getIsolatedModules(compilerOptions) || outFile(compilerOptions))) {
return [sourceFileWithUpdatedShape];
}

Expand Down
9 changes: 5 additions & 4 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ import {
getEmitScriptTarget,
getErrorSpanForNode,
getExternalModuleName,
getIsolatedModules,
getJSXImplicitImportBase,
getJSXRuntimeImport,
getLineAndCharacterOfPosition,
Expand Down Expand Up @@ -3186,7 +3187,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg

// If we are importing helpers, we need to add a synthetic reference to resolve the
// helpers library.
if ((options.isolatedModules || isExternalModuleFile)
if ((getIsolatedModules(options) || isExternalModuleFile)
&& !file.isDeclarationFile) {
if (options.importHelpers) {
// synthesize 'import "tslib"' declaration
Expand Down Expand Up @@ -3992,13 +3993,13 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "exactOptionalPropertyTypes", "strictNullChecks");
}

if (options.isolatedModules) {
if (options.isolatedModules || options.verbatimModuleSyntax) {
if (options.out) {
createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_with_option_1, "out", "isolatedModules");
createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_with_option_1, "out", options.verbatimModuleSyntax ? "verbatimModuleSyntax" : "isolatedModules");
}

if (options.outFile) {
createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_with_option_1, "outFile", "isolatedModules");
createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_with_option_1, "outFile", options.verbatimModuleSyntax ? "verbatimModuleSyntax" : "isolatedModules");
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/compiler/transformers/module/esnextAnd2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
getEmitModuleKind,
getEmitScriptTarget,
getExternalModuleNameLiteral,
getIsolatedModules,
hasSyntacticModifier,
Identifier,
idText,
Expand Down Expand Up @@ -76,7 +77,7 @@ export function transformECMAScriptModule(context: TransformationContext): (x: S
return node;
}

if (isExternalModule(node) || compilerOptions.isolatedModules) {
if (isExternalModule(node) || getIsolatedModules(compilerOptions)) {
currentSourceFile = node;
importRequireStatements = undefined;
let result = updateExternalModule(node);
Expand Down Expand Up @@ -286,7 +287,7 @@ export function transformECMAScriptModule(context: TransformationContext): (x: S
*/
function onEmitNode(hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void): void {
if (isSourceFile(node)) {
if ((isExternalModule(node) || compilerOptions.isolatedModules) && compilerOptions.importHelpers) {
if ((isExternalModule(node) || getIsolatedModules(compilerOptions)) && compilerOptions.importHelpers) {
helperNameSubstitutions = new Map<string, Identifier>();
}
previousOnEmitNode(hint, node, emitCallback);
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
getEmitScriptTarget,
getFirstConstructorWithBody,
getInitializedVariables,
getIsolatedModules,
getOriginalNode,
getParseTreeNode,
getProperties,
Expand Down Expand Up @@ -2706,7 +2707,7 @@ export function transformTypeScript(context: TransformationContext) {
}

function tryGetConstEnumValue(node: Node): string | number | undefined {
if (compilerOptions.isolatedModules) {
if (getIsolatedModules(compilerOptions)) {
return undefined;
}

Expand Down
6 changes: 3 additions & 3 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1781,7 +1781,7 @@ function isCommonJSContainingModuleKind(kind: ModuleKind) {

/** @internal */
export function isEffectiveExternalModule(node: SourceFile, compilerOptions: CompilerOptions) {
return isExternalModule(node) || compilerOptions.isolatedModules || (isCommonJSContainingModuleKind(getEmitModuleKind(compilerOptions)) && !!node.commonJsModuleIndicator);
return isExternalModule(node) || getIsolatedModules(compilerOptions) || (isCommonJSContainingModuleKind(getEmitModuleKind(compilerOptions)) && !!node.commonJsModuleIndicator);
}

/**
Expand Down Expand Up @@ -1812,7 +1812,7 @@ export function isEffectiveStrictModeSourceFile(node: SourceFile, compilerOption
if (startsWithUseStrict(node.statements)) {
return true;
}
if (isExternalModule(node) || compilerOptions.isolatedModules) {
if (isExternalModule(node) || getIsolatedModules(compilerOptions)) {
// ECMAScript Modules are always strict.
if (getEmitModuleKind(compilerOptions) >= ModuleKind.ES2015) {
return true;
Expand Down Expand Up @@ -8443,7 +8443,7 @@ export function getEmitDeclarations(compilerOptions: CompilerOptions): boolean {

/** @internal */
export function shouldPreserveConstEnums(compilerOptions: CompilerOptions): boolean {
return !!(compilerOptions.preserveConstEnums || compilerOptions.isolatedModules);
return !!(compilerOptions.preserveConstEnums || getIsolatedModules(compilerOptions));
}

/** @internal */
Expand Down
5 changes: 3 additions & 2 deletions src/services/codefixes/fixImportNonExportedMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
findAncestor,
findLast,
firstOrUndefined,
getIsolatedModules,
getResolvedModule,
getTokenAtPosition,
Identifier,
Expand Down Expand Up @@ -173,7 +174,7 @@ function tryGetExportDeclaration(sourceFile: SourceFile, isTypeOnly: boolean) {

function updateExport(changes: textChanges.ChangeTracker, program: Program, sourceFile: SourceFile, node: ExportDeclaration, names: ExportName[]) {
const namedExports = node.exportClause && isNamedExports(node.exportClause) ? node.exportClause.elements : factory.createNodeArray([]);
const allowTypeModifier = !node.isTypeOnly && !!(program.getCompilerOptions().isolatedModules || find(namedExports, e => e.isTypeOnly));
const allowTypeModifier = !node.isTypeOnly && !!(getIsolatedModules(program.getCompilerOptions()) || find(namedExports, e => e.isTypeOnly));
changes.replaceNode(sourceFile, node,
factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly,
factory.createNamedExports(
Expand All @@ -183,7 +184,7 @@ function updateExport(changes: textChanges.ChangeTracker, program: Program, sour
function createExport(changes: textChanges.ChangeTracker, program: Program, sourceFile: SourceFile, names: ExportName[]) {
changes.insertNodeAtEndOfScope(sourceFile, sourceFile,
factory.createExportDeclaration(/*modifiers*/ undefined, /*isTypeOnly*/ false,
factory.createNamedExports(createExportSpecifiers(names, /*allowTypeModifier*/ !!program.getCompilerOptions().isolatedModules)), /*moduleSpecifier*/ undefined, /*assertClause*/ undefined));
factory.createNamedExports(createExportSpecifiers(names, /*allowTypeModifier*/ getIsolatedModules(program.getCompilerOptions()))), /*moduleSpecifier*/ undefined, /*assertClause*/ undefined));
}

function createExportSpecifiers(names: ExportName[], allowTypeModifier: boolean) {
Expand Down
8 changes: 4 additions & 4 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
ImportEqualsDeclaration,
importFromModuleSpecifier,
ImportKind,
importNameElisionDisabled,
ImportsNotUsedAsValues,
insertImports,
InternalSymbolName,
Expand Down Expand Up @@ -676,7 +677,7 @@ function getAddAsTypeOnly(
// Not writing a (top-level) type-only import here would create an error because the runtime dependency is unnecessary
return AddAsTypeOnly.Required;
}
if ((compilerOptions.isolatedModules && compilerOptions.preserveValueImports || compilerOptions.verbatimModuleSyntax) &&
if (importNameElisionDisabled(compilerOptions) &&
(!(targetFlags & SymbolFlags.Value) || !!checker.getTypeOnlyAliasDeclaration(symbol))
) {
// A type-only import is required for this symbol if under these settings if the symbol will
Expand Down Expand Up @@ -1276,7 +1277,7 @@ function getModuleSpecifierText(promotedDeclaration: ImportClause | ImportEquals

function promoteFromTypeOnly(changes: textChanges.ChangeTracker, aliasDeclaration: TypeOnlyAliasDeclaration, compilerOptions: CompilerOptions, sourceFile: SourceFile, preferences: UserPreferences) {
// See comment in `doAddExistingFix` on constant with the same name.
const convertExistingToTypeOnly = compilerOptions.preserveValueImports && compilerOptions.isolatedModules || compilerOptions.verbatimModuleSyntax;
const convertExistingToTypeOnly = importNameElisionDisabled(compilerOptions);
switch (aliasDeclaration.kind) {
case SyntaxKind.ImportSpecifier:
if (aliasDeclaration.isTypeOnly) {
Expand Down Expand Up @@ -1362,8 +1363,7 @@ function doAddExistingFix(
// never used in an emitting position). These are allowed to be imported without being type-only,
// but the user has clearly already signified that they don't need them to be present at runtime
// by placing them in a type-only import. So, just mark each specifier as type-only.
const convertExistingToTypeOnly = promoteFromTypeOnly
&& (compilerOptions.preserveValueImports && compilerOptions.isolatedModules || compilerOptions.verbatimModuleSyntax);
const convertExistingToTypeOnly = promoteFromTypeOnly && importNameElisionDisabled(compilerOptions);

if (defaultImport) {
Debug.assert(!clause.name, "Cannot add a default import to an import clause that already has one");
Expand Down
11 changes: 11 additions & 0 deletions tests/baselines/reference/verbatimModuleSyntaxConstEnum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//// [verbatimModuleSyntaxConstEnum.ts]
export const enum E {
A = 1,
}


//// [verbatimModuleSyntaxConstEnum.js]
export var E;
(function (E) {
E[E["A"] = 1] = "A";
})(E || (E = {}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== tests/cases/conformance/externalModules/verbatimModuleSyntaxConstEnum.ts ===
export const enum E {
>E : Symbol(E, Decl(verbatimModuleSyntaxConstEnum.ts, 0, 0))

A = 1,
>A : Symbol(E.A, Decl(verbatimModuleSyntaxConstEnum.ts, 0, 21))
}

9 changes: 9 additions & 0 deletions tests/baselines/reference/verbatimModuleSyntaxConstEnum.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=== tests/cases/conformance/externalModules/verbatimModuleSyntaxConstEnum.ts ===
export const enum E {
>E : E

A = 1,
>A : E.A
>1 : 1
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @verbatimModuleSyntax: true
// @module: esnext

export const enum E {
A = 1,
}