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

Add verbatimModuleSyntax, deprecate importsNotUsedAsValues and preserveValueImports #52203

Merged
merged 19 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Finish(?) syntax restrictions
  • Loading branch information
andrewbranch committed Dec 12, 2022
commit 5095a7efd6df67dfc3e2addbc9c753fc0c028d4d
14 changes: 14 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45553,6 +45553,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
break;

case SyntaxKind.ExportKeyword:
if (compilerOptions.verbatimModuleSyntax &&
!(node.flags & NodeFlags.Ambient) &&
node.kind !== SyntaxKind.TypeAliasDeclaration &&
node.kind !== SyntaxKind.InterfaceDeclaration &&
node.parent.kind === SyntaxKind.SourceFile &&
(moduleKind === ModuleKind.CommonJS || getSourceFileOfNode(node).impliedNodeFormat === ModuleKind.CommonJS)
) {
return grammarErrorOnNode(modifier, Diagnostics.A_top_level_export_modifier_can_only_be_used_on_type_aliases_and_interfaces_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled);
}

if (flags & ModifierFlags.Export) {
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_already_seen, "export");
}
Expand Down Expand Up @@ -47063,6 +47073,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function checkGrammarImportCallExpression(node: ImportCall): boolean {
if (compilerOptions.verbatimModuleSyntax && moduleKind === ModuleKind.CommonJS) {
return grammarErrorOnNode(node, Diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled);
}

if (moduleKind === ModuleKind.ES2015) {
return grammarErrorOnNode(node, Diagnostics.Dynamic_imports_are_only_supported_when_the_module_flag_is_set_to_es2020_es2022_esnext_commonjs_amd_system_umd_node16_or_nodenext);
}
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,10 @@
"category": "Error",
"code": 1281
},
"A top-level 'export' modifier can only be used on type aliases and interfaces in a CommonJS module when 'verbatimModuleSyntax' is enabled.": {
"category": "Error",
"code": 1282
},

"'with' statements are not allowed in an async function block.": {
"category": "Error",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/b.ts(1,13): error TS1484: 'A' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
/b.ts(5,10): error TS1205: Re-exporting a type when 'verbatimModuleSyntax' is enabled requires using 'export type'.
/b.ts(6,10): error TS1205: Re-exporting a type when 'verbatimModuleSyntax' is enabled requires using 'export type'.
/c.ts(1,10): error TS1485: 'AClass' resolves to a type-only declaration and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.


==== /a.ts (0 errors) ====
export const a = 0;
export type A = typeof a;
export class AClass {}

==== /b.ts (3 errors) ====
import { a, A, AClass } from "./a";
~
!!! error TS1484: 'A' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
import type { a as aValue, A as AType } from "./a";
import { type A as AType2 } from "./a";

export { A };
~
!!! error TS1205: Re-exporting a type when 'verbatimModuleSyntax' is enabled requires using 'export type'.
export { A as A2 } from "./a";
~~~~~~~
!!! error TS1205: Re-exporting a type when 'verbatimModuleSyntax' is enabled requires using 'export type'.
export type { A as A3 } from "./a";
export { type A as A4 } from "./a";
export type { AClass } from "./a";

==== /c.ts (1 errors) ====
import { AClass } from "./b";
~~~~~~
!!! error TS1485: 'AClass' resolves to a type-only declaration and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
!!! related TS1377 /b.ts:9:15: 'AClass' was exported here.

38 changes: 38 additions & 0 deletions tests/baselines/reference/verbatimModuleSyntaxNoElisionESM.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//// [tests/cases/conformance/externalModules/verbatimModuleSyntaxNoElisionESM.ts] ////

//// [a.ts]
export const a = 0;
export type A = typeof a;
export class AClass {}

//// [b.ts]
import { a, A, AClass } from "./a";
import type { a as aValue, A as AType } from "./a";
import { type A as AType2 } from "./a";

export { A };
export { A as A2 } from "./a";
export type { A as A3 } from "./a";
export { type A as A4 } from "./a";
export type { AClass } from "./a";

//// [c.ts]
import { AClass } from "./b";


//// [a.js]
export var a = 0;
var AClass = /** @class */ (function () {
function AClass() {
}
return AClass;
}());
export { AClass };
//// [b.js]
import { a, A, AClass } from "./a";
import {} from "./a";
export { A };
export { A as A2 } from "./a";
export {} from "./a";
//// [c.js]
import { AClass } from "./b";
49 changes: 49 additions & 0 deletions tests/baselines/reference/verbatimModuleSyntaxNoElisionESM.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
=== /a.ts ===
export const a = 0;
>a : Symbol(a, Decl(a.ts, 0, 12))

export type A = typeof a;
>A : Symbol(A, Decl(a.ts, 0, 19))
>a : Symbol(a, Decl(a.ts, 0, 12))

export class AClass {}
>AClass : Symbol(AClass, Decl(a.ts, 1, 25))

=== /b.ts ===
import { a, A, AClass } from "./a";
>a : Symbol(a, Decl(b.ts, 0, 8))
>A : Symbol(A, Decl(b.ts, 0, 11))
>AClass : Symbol(AClass, Decl(b.ts, 0, 14))

import type { a as aValue, A as AType } from "./a";
>a : Symbol(a, Decl(a.ts, 0, 12))
>aValue : Symbol(aValue, Decl(b.ts, 1, 13))
>A : Symbol(A, Decl(a.ts, 0, 19))
>AType : Symbol(AType, Decl(b.ts, 1, 26))

import { type A as AType2 } from "./a";
>A : Symbol(A, Decl(a.ts, 0, 19))
>AType2 : Symbol(AType2, Decl(b.ts, 2, 8))

export { A };
>A : Symbol(A, Decl(b.ts, 4, 8))

export { A as A2 } from "./a";
>A : Symbol(A, Decl(a.ts, 0, 19))
>A2 : Symbol(A2, Decl(b.ts, 5, 8))

export type { A as A3 } from "./a";
>A : Symbol(A, Decl(a.ts, 0, 19))
>A3 : Symbol(A3, Decl(b.ts, 6, 13))

export { type A as A4 } from "./a";
>A : Symbol(A, Decl(a.ts, 0, 19))
>A4 : Symbol(A4, Decl(b.ts, 7, 8))

export type { AClass } from "./a";
>AClass : Symbol(AClass, Decl(b.ts, 8, 13))

=== /c.ts ===
import { AClass } from "./b";
>AClass : Symbol(AClass, Decl(c.ts, 0, 8))

50 changes: 50 additions & 0 deletions tests/baselines/reference/verbatimModuleSyntaxNoElisionESM.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
=== /a.ts ===
export const a = 0;
>a : 0
>0 : 0

export type A = typeof a;
>A : 0
>a : 0

export class AClass {}
>AClass : AClass

=== /b.ts ===
import { a, A, AClass } from "./a";
>a : 0
>A : any
>AClass : typeof AClass

import type { a as aValue, A as AType } from "./a";
>a : 0
>aValue : any
>A : any
>AType : 0

import { type A as AType2 } from "./a";
>A : any
>AType2 : any

export { A };
>A : any

export { A as A2 } from "./a";
>A : any
>A2 : any

export type { A as A3 } from "./a";
>A : any
>A3 : 0

export { type A as A4 } from "./a";
>A : any
>A4 : any

export type { AClass } from "./a";
>AClass : AClass

=== /c.ts ===
import { AClass } from "./b";
>AClass : typeof AClass

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/main.ts(1,8): error TS1281: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
/main.ts(2,13): error TS1281: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
/main.ts(3,10): error TS1281: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
/main.ts(5,1): error TS1281: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
/main.ts(8,1): error TS1282: A top-level 'export' modifier can only be used on type aliases and interfaces in a CommonJS module when 'verbatimModuleSyntax' is enabled.
/main2.ts(2,1): error TS2309: An export assignment cannot be used in a module with other exported elements.


==== /decl.d.ts (0 errors) ====
declare function esmy(): void;
export default esmy;
export declare function funciton(): void;

==== /main.ts (5 errors) ====
import esmy from "./decl"; // error
~~~~
!!! error TS1281: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
import * as esmy2 from "./decl"; // error
~~~~~
!!! error TS1281: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
import { funciton } from "./decl"; // error
~~~~~~~~
!!! error TS1281: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
import type { funciton as funciton2 } from "./decl"; // ok I guess?
import("./decl"); // error
~~~~~~~~~~~~~~~~
!!! error TS1281: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
type T = typeof import("./decl"); // ok
export {}; // error
export const x = 1; // error
~~~~~~
!!! error TS1282: A top-level 'export' modifier can only be used on type aliases and interfaces in a CommonJS module when 'verbatimModuleSyntax' is enabled.
export interface I {} // ok
export type { T }; // ok

==== /main2.ts (1 errors) ====
export interface I {}
export = { x: 1 };
~~~~~~~~~~~~~~~~~~
!!! error TS2309: An export assignment cannot be used in a module with other exported elements.

==== /main3.ts (0 errors) ====
namespace ns {
export const x = 1;
export interface I {}
}
export = ns;
75 changes: 75 additions & 0 deletions tests/baselines/reference/verbatimModuleSyntaxRestrictionsCJS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//// [tests/cases/conformance/externalModules/verbatimModuleSyntaxRestrictionsCJS.ts] ////

//// [decl.d.ts]
declare function esmy(): void;
export default esmy;
export declare function funciton(): void;

//// [main.ts]
import esmy from "./decl"; // error
import * as esmy2 from "./decl"; // error
import { funciton } from "./decl"; // error
import type { funciton as funciton2 } from "./decl"; // ok I guess?
import("./decl"); // error
type T = typeof import("./decl"); // ok
export {}; // error
export const x = 1; // error
export interface I {} // ok
export type { T }; // ok

//// [main2.ts]
export interface I {}
export = { x: 1 };

//// [main3.ts]
namespace ns {
export const x = 1;
export interface I {}
}
export = ns;

//// [main.js]
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.x = void 0;
const decl_1 = __importDefault(require("./decl")); // error
const esmy2 = __importStar(require("./decl")); // error
const decl_2 = require("./decl"); // error
Promise.resolve().then(() => __importStar(require("./decl"))); // error
exports.x = 1; // error
//// [main2.js]
"use strict";
module.exports = { x: 1 };
//// [main3.js]
"use strict";
var ns;
(function (ns) {
ns.x = 1;
})(ns || (ns = {}));
module.exports = ns;
Loading