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

Allow JS with isolated modules #31483

Merged
merged 4 commits into from May 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Expand Up @@ -655,7 +655,7 @@
"category": "Error",
"code": 1207
},
"Cannot compile namespaces when the '--isolatedModules' flag is provided.": {
"All files must be modules when the '--isolatedModules' flag is provided.": {
"category": "Error",
"code": 1208
},
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/program.ts
Expand Up @@ -2855,10 +2855,10 @@ namespace ts {
createDiagnosticForOptionName(Diagnostics.Option_isolatedModules_can_only_be_used_when_either_option_module_is_provided_or_option_target_is_ES2015_or_higher, "isolatedModules", "target");
}

const firstNonExternalModuleSourceFile = find(files, f => !isExternalModule(f) && !f.isDeclarationFile && f.scriptKind !== ScriptKind.JSON);
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.Cannot_compile_namespaces_when_the_isolatedModules_flag_is_provided));
programDiagnostics.add(createFileDiagnostic(firstNonExternalModuleSourceFile, span.start, span.length, Diagnostics.All_files_must_be_modules_when_the_isolatedModules_flag_is_provided));
}
}
else if (firstNonAmbientExternalModuleSourceFile && languageVersion < ScriptTarget.ES2015 && options.module === ModuleKind.None) {
Expand Down
9 changes: 9 additions & 0 deletions tests/baselines/reference/commonJsIsolatedModules.symbols
@@ -0,0 +1,9 @@
=== tests/cases/compiler/index.js ===
module.exports = {}
>module.exports : Symbol("tests/cases/compiler/index", Decl(index.js, 0, 0))
>module : Symbol(module, Decl(index.js, 0, 0))
>exports : Symbol("tests/cases/compiler/index", Decl(index.js, 0, 0))

var x = 1
>x : Symbol(x, Decl(index.js, 1, 3))

12 changes: 12 additions & 0 deletions tests/baselines/reference/commonJsIsolatedModules.types
@@ -0,0 +1,12 @@
=== tests/cases/compiler/index.js ===
module.exports = {}
>module.exports = {} : typeof import("tests/cases/compiler/index")
>module.exports : typeof import("tests/cases/compiler/index")
>module : { "tests/cases/compiler/index": typeof import("tests/cases/compiler/index"); }
>exports : typeof import("tests/cases/compiler/index")
>{} : {}

var x = 1
>x : number
>1 : 1

12 changes: 12 additions & 0 deletions tests/cases/compiler/commonJsIsolatedModules.ts
@@ -0,0 +1,12 @@
// @Filename: tsconfig.json
{
"compilerOptions": {
"allowJs": true,
"noEmit": true,
Copy link
Member

Choose a reason for hiding this comment

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

What happens when noEmit is false?

Copy link
Member Author

Choose a reason for hiding this comment

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

error saying that you can't overwrite index.js. The real example of this is from babel pipelines, where we recommended people use isolatedModules, so they use babel for emit, not typescript.

Copy link
Member

Choose a reason for hiding this comment

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

But you could change it to emit into the outDir right? Would it affect in some way the emit that could happen?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll try it.

Copy link
Member Author

Choose a reason for hiding this comment

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

It shouldn't affect emit because we're not transforming js as far as I know. I pushed a commit with outDir replacing noEmit in the test.

"isolatedModules": true,
}
}

// @Filename: index.js
module.exports = {}
var x = 1