Skip to content

Commit

Permalink
Require module/moduleResolution to match when either is node16/nodenext
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch committed Jun 7, 2023
1 parent fbd63e9 commit 9a775a4
Show file tree
Hide file tree
Showing 36 changed files with 648 additions and 750 deletions.
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4329,6 +4329,14 @@
"category": "Error",
"code": 5108
},
"Option 'moduleResolution' must be set to '{0}' (or left unspecified) when option 'module' is set to '{1}'.": {
"category": "Error",
"code": 5109
},
"Option 'module' must be set to '{0}' when option 'moduleResolution' is set to '{1}'.": {
"category": "Error",
"code": 5110
},

"Generates a sourcemap for each corresponding '.d.ts' file.": {
"category": "Message",
Expand Down
16 changes: 16 additions & 0 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4402,6 +4402,22 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
createOptionValueDiagnostic("moduleResolution", Diagnostics.Option_0_can_only_be_used_when_module_is_set_to_es2015_or_later, "bundler");
}

if (
(ModuleKind.Node16 <= moduleKind && moduleKind <= ModuleKind.NodeNext) &&
!(ModuleResolutionKind.Node16 <= moduleResolution && moduleResolution <= ModuleResolutionKind.NodeNext)
) {
const moduleKindName = ModuleKind[moduleKind];
createOptionValueDiagnostic("moduleResolution", Diagnostics.Option_moduleResolution_must_be_set_to_0_or_left_unspecified_when_option_module_is_set_to_1, moduleKindName, moduleKindName);
}
else if (
(ModuleResolutionKind.Node16 <= moduleResolution && moduleResolution <= ModuleResolutionKind.NodeNext) &&
!(ModuleKind.Node16 <= moduleKind && moduleKind <= ModuleKind.NodeNext)
) {
const moduleResolutionName = ModuleResolutionKind[moduleResolution];
createOptionValueDiagnostic("module", Diagnostics.Option_module_must_be_set_to_0_when_option_moduleResolution_is_set_to_1, moduleResolutionName, moduleResolutionName);
}


// If the emit is enabled make sure that every output file is unique and not overwriting any of the input files
if (!options.noEmit && !options.suppressOutputPathCheck) {
const emitHost = getEmitHost();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
/c.ts(1,16): error TS2307: Cannot find module './thisfiledoesnotexist.ts' or its corresponding type declarations.


!!! error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
==== /ts.ts (0 errors) ====
export {};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
error TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResolution' is set to 'NodeNext'.
/c.ts(1,16): error TS2307: Cannot find module './thisfiledoesnotexist.ts' or its corresponding type declarations.


!!! error TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResolution' is set to 'NodeNext'.
==== /ts.ts (0 errors) ====
export {};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.


!!! error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
==== /types.d.ts (0 errors) ====
export declare type User = {
name: string;
}

==== /a.ts (0 errors) ====
import type { User } from "./types.d.ts";
export type { User } from "./types.d.ts";

export const user: User = { name: "John" };

export function getUser(): import("./types.d.ts").User {
return user;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResolution' is set to 'NodeNext'.


!!! error TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResolution' is set to 'NodeNext'.
==== /types.d.ts (0 errors) ====
export declare type User = {
name: string;
}

==== /a.ts (0 errors) ====
import type { User } from "./types.d.ts";
export type { User } from "./types.d.ts";

export const user: User = { name: "John" };

export function getUser(): import("./types.d.ts").User {
return user;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.


!!! error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
==== /types.d.ts (0 errors) ====
export declare type User = {
name: string;
}

==== /a.ts (0 errors) ====
import type { User } from "./types.d.ts";
export type { User } from "./types.d.ts";

export const user: User = { name: "John" };

export function getUser(): import("./types.d.ts").User {
return user;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResolution' is set to 'NodeNext'.


!!! error TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResolution' is set to 'NodeNext'.
==== /types.d.ts (0 errors) ====
export declare type User = {
name: string;
}

==== /a.ts (0 errors) ====
import type { User } from "./types.d.ts";
export type { User } from "./types.d.ts";

export const user: User = { name: "John" };

export function getUser(): import("./types.d.ts").User {
return user;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.


!!! error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
==== /node_modules/dep/package.json (0 errors) ====
// This documents bug https://github.com/microsoft/TypeScript/issues/50762.

{
"name": "dep",
"version": "1.0.0",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts",
}
}
}

==== /node_modules/dep/dist/index.d.ts (0 errors) ====
export {};

==== /node_modules/dep/dist/index.mjs (0 errors) ====
export {};

==== /index.mts (0 errors) ====
import {} from "dep";
// Should be an untyped resolution to dep/dist/index.mjs,
// but the first search is only for TS files, and when
// there's no dist/index.d.mts, it continues looking for
// matching conditions and resolves via `types`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
error TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResolution' is set to 'NodeNext'.


!!! error TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResolution' is set to 'NodeNext'.
==== /node_modules/dep/package.json (0 errors) ====
// This documents bug https://github.com/microsoft/TypeScript/issues/50762.

{
"name": "dep",
"version": "1.0.0",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts",
}
}
}

==== /node_modules/dep/dist/index.d.ts (0 errors) ====
export {};

==== /node_modules/dep/dist/index.mjs (0 errors) ====
export {};

==== /index.mts (0 errors) ====
import {} from "dep";
// Should be an untyped resolution to dep/dist/index.mjs,
// but the first search is only for TS files, and when
// there's no dist/index.d.mts, it continues looking for
// matching conditions and resolves via `types`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.


!!! error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
==== tests/cases/compiler/index.js (0 errors) ====
import { Foo } from "./other.js";
import * as other from "./other.js";
import defaultFoo from "./other.js";

const x = new Foo();
const y = other.Foo();
const z = new defaultFoo();

==== tests/cases/compiler/other.d.ts (0 errors) ====
export interface Foo {
bar: number;
}

export default interface Bar {
foo: number;
}

==== tests/cases/compiler/other.js (0 errors) ====
export class Foo {
bar = 2.4;
}

export default class Bar {
foo = 1.2;
}

18 changes: 9 additions & 9 deletions tests/baselines/reference/elidedJSImport2(module=commonjs).types
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import defaultFoo from "./other.js";
>defaultFoo : any

const x = new Foo();
>x : error
>new Foo() : error
>Foo : error
>x : any
>new Foo() : any
>Foo : any

const y = other.Foo();
>y : error
>other.Foo() : error
>other.Foo : error
>y : any
>other.Foo() : any
>other.Foo : any
>other : typeof other
>Foo : any

const z = new defaultFoo();
>z : error
>new defaultFoo() : error
>defaultFoo : error
>z : any
>new defaultFoo() : any
>defaultFoo : any

=== tests/cases/compiler/other.d.ts ===
export interface Foo {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.


!!! error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
==== tests/cases/compiler/index.js (0 errors) ====
import { Foo } from "./other.js";
import * as other from "./other.js";
import defaultFoo from "./other.js";

const x = new Foo();
const y = other.Foo();
const z = new defaultFoo();

==== tests/cases/compiler/other.d.ts (0 errors) ====
export interface Foo {
bar: number;
}

export default interface Bar {
foo: number;
}

==== tests/cases/compiler/other.js (0 errors) ====
export class Foo {
bar = 2.4;
}

export default class Bar {
foo = 1.2;
}

18 changes: 9 additions & 9 deletions tests/baselines/reference/elidedJSImport2(module=es2022).types
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import defaultFoo from "./other.js";
>defaultFoo : any

const x = new Foo();
>x : error
>new Foo() : error
>Foo : error
>x : any
>new Foo() : any
>Foo : any

const y = other.Foo();
>y : error
>other.Foo() : error
>other.Foo : error
>y : any
>other.Foo() : any
>other.Foo : any
>other : typeof other
>Foo : any

const z = new defaultFoo();
>z : error
>new defaultFoo() : error
>defaultFoo : error
>z : any
>new defaultFoo() : any
>defaultFoo : any

=== tests/cases/compiler/other.d.ts ===
export interface Foo {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error TS5109: Option 'moduleResolution' must be set to 'Node16' (or left unspecified) when option 'module' is set to 'Node16'.


!!! error TS5109: Option 'moduleResolution' must be set to 'Node16' (or left unspecified) when option 'module' is set to 'Node16'.
==== tests/cases/compiler/a.ts (0 errors) ====
declare var dec: any, __decorate: any;
@dec export class A {
}

const o = { a: 1 };
const y = { ...o };

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error TS5109: Option 'moduleResolution' must be set to 'NodeNext' (or left unspecified) when option 'module' is set to 'NodeNext'.


!!! error TS5109: Option 'moduleResolution' must be set to 'NodeNext' (or left unspecified) when option 'module' is set to 'NodeNext'.
==== tests/cases/compiler/a.ts (0 errors) ====
declare var dec: any, __decorate: any;
@dec export class A {
}

const o = { a: 1 };
const y = { ...o };

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.


!!! error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
==== /project/a.js (0 errors) ====
export default "a.js";

==== /project/a.js.js (0 errors) ====
export default "a.js.js";

==== /project/dir/index.ts (0 errors) ====
export default "dir/index.ts";

==== /project/dir.js (0 errors) ====
export default "dir.js";

==== /project/b.ts (0 errors) ====
import a from "./a.js";
import dir from "./dir";

Loading

0 comments on commit 9a775a4

Please sign in to comment.