Skip to content
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

Report every instance of TS1208 #50101

Merged
merged 3 commits into from Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/compiler/program.ts
Expand Up @@ -3509,11 +3509,12 @@ namespace ts {
createDiagnosticForOptionName(Diagnostics.Option_preserveConstEnums_cannot_be_disabled_when_isolatedModules_is_enabled, "preserveConstEnums", "isolatedModules");
}

const firstNonExternalModuleSourceFile = find(files, f => !isExternalModule(f) && !isSourceFileJS(f) && !f.isDeclarationFile && f.scriptKind !== ScriptKind.JSON);
if (firstNonExternalModuleSourceFile) {
const span = getErrorSpanForNode(firstNonExternalModuleSourceFile, firstNonExternalModuleSourceFile);
programDiagnostics.add(createFileDiagnostic(firstNonExternalModuleSourceFile, span.start, span.length,
Diagnostics._0_cannot_be_compiled_under_isolatedModules_because_it_is_considered_a_global_script_file_Add_an_import_export_or_an_empty_export_statement_to_make_it_a_module, getBaseFileName(firstNonExternalModuleSourceFile.fileName)));
for (const file of files) {
if (!isExternalModule(file) && !isSourceFileJS(file) && !file.isDeclarationFile && file.scriptKind !== ScriptKind.JSON) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!isExternalModule(file) && !isSourceFileJS(file) && !file.isDeclarationFile && file.scriptKind !== ScriptKind.JSON) {
const isIsolatedModules = !isExternalModule(file) && !isSourceFileJS(file) && !file.isDeclarationFile && file.scriptKind !== ScriptKind.JSON;
if (isIsolatedModules) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the predicate indicates that the file is a global (non-module) file, not that it's building under isolated modules.
There's enough context that I don't really think the predicate needs a name at all, really, but if you want one, it should be isNonModuleSourceFile. ('External' is obsolete)

const span = getErrorSpanForNode(file, file);
programDiagnostics.add(createFileDiagnostic(file, span.start, span.length,
Diagnostics._0_cannot_be_compiled_under_isolatedModules_because_it_is_considered_a_global_script_file_Add_an_import_export_or_an_empty_export_statement_to_make_it_a_module, getBaseFileName(file.fileName)));
}
}
}
else if (firstNonAmbientExternalModuleSourceFile && languageVersion < ScriptTarget.ES2015 && options.module === ModuleKind.None) {
Expand Down
@@ -0,0 +1,20 @@
tests/cases/compiler/file1.ts(1,1): error TS1208: 'file1.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.
tests/cases/compiler/file2.ts(1,1): error TS1208: 'file2.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.
tests/cases/compiler/file3.ts(1,1): error TS1208: 'file3.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.


==== tests/cases/compiler/file1.ts (1 errors) ====
var x;
~~~
!!! error TS1208: 'file1.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.

==== tests/cases/compiler/file2.ts (1 errors) ====
var y;
~~~
!!! error TS1208: 'file2.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.

==== tests/cases/compiler/file3.ts (1 errors) ====
var z;
~~~
!!! error TS1208: 'file3.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.

@@ -0,0 +1,18 @@
//// [tests/cases/compiler/isolatedModulesNoExternalModuleMultiple.ts] ////

//// [file1.ts]
var x;

//// [file2.ts]
var y;

//// [file3.ts]
var z;


//// [file1.js]
var x;
//// [file2.js]
var y;
//// [file3.js]
var z;
@@ -0,0 +1,12 @@
=== tests/cases/compiler/file1.ts ===
var x;
>x : Symbol(x, Decl(file1.ts, 0, 3))

=== tests/cases/compiler/file2.ts ===
var y;
>y : Symbol(y, Decl(file2.ts, 0, 3))

=== tests/cases/compiler/file3.ts ===
var z;
>z : Symbol(z, Decl(file3.ts, 0, 3))

@@ -0,0 +1,12 @@
=== tests/cases/compiler/file1.ts ===
var x;
>x : any

=== tests/cases/compiler/file2.ts ===
var y;
>y : any

=== tests/cases/compiler/file3.ts ===
var z;
>z : any

11 changes: 11 additions & 0 deletions tests/cases/compiler/isolatedModulesNoExternalModuleMultiple.ts
@@ -0,0 +1,11 @@
// @isolatedModules: true
// @target: es6
Comment on lines +1 to +2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a test with moduleDetection: force? Optionally you could also try to add a test moduleDetection: auto and with package.json containing {"type":"module"}. Note that I'm not super familiar with this new option and the latter proposed test might also require moduleResolution: node16 (but I'm not sure about it)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test case with isolatedModules + moduleDetection: force. I don't expect a regression in this area from the primary fix here, but it seems like a good test to have.

Did not add other test cases beyond that; I am not too familiar with the newer Node-related resolution behaviors, and I don't expect any regression around those.


// @filename: file1.ts
var x;

// @filename: file2.ts
var y;

// @filename: file3.ts
var z;