Skip to content

Commit 2c865e4

Browse files
--erasableSyntaxOnly (#61011)
Co-authored-by: Jake Bailey <[email protected]>
1 parent 329387d commit 2c865e4

File tree

26 files changed

+832
-3
lines changed

26 files changed

+832
-3
lines changed

src/compiler/checker.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -41295,6 +41295,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4129541295
checkVariableLikeDeclaration(node);
4129641296
const func = getContainingFunction(node)!;
4129741297
if (hasSyntacticModifier(node, ModifierFlags.ParameterPropertyModifier)) {
41298+
if (compilerOptions.erasableSyntaxOnly) {
41299+
error(node, Diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled);
41300+
}
4129841301
if (!(func.kind === SyntaxKind.Constructor && nodeIsPresent(func.body))) {
4129941302
error(node, Diagnostics.A_parameter_property_is_only_allowed_in_a_constructor_implementation);
4130041303
}
@@ -47509,6 +47512,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4750947512
checkExportsOnMergedDeclarations(node);
4751047513
node.members.forEach(checkEnumMember);
4751147514

47515+
if (compilerOptions.erasableSyntaxOnly && !(node.flags & NodeFlags.Ambient)) {
47516+
error(node, Diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled);
47517+
}
47518+
4751247519
computeEnumMemberValues(node);
4751347520

4751447521
// Spec 2014 - Section 9.3:
@@ -47648,6 +47655,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4764847655
&& !inAmbientContext
4764947656
&& isInstantiatedModule(node, shouldPreserveConstEnums(compilerOptions))
4765047657
) {
47658+
if (compilerOptions.erasableSyntaxOnly) {
47659+
error(node.name, Diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled);
47660+
}
47661+
4765147662
if (getIsolatedModules(compilerOptions) && !getSourceFileOfNode(node).externalModuleIndicator) {
4765247663
// This could be loosened a little if needed. The only problem we are trying to avoid is unqualified
4765347664
// references to namespace members declared in other files. But use of namespaces is discouraged anyway,
@@ -48164,7 +48175,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4816448175
}
4816548176

4816648177
checkGrammarModifiers(node);
48167-
if (isInternalModuleImportEqualsDeclaration(node) || checkExternalImportOrExportDeclaration(node)) {
48178+
const isImportEquals = isInternalModuleImportEqualsDeclaration(node);
48179+
if (compilerOptions.erasableSyntaxOnly && isImportEquals && !(node.flags & NodeFlags.Ambient)) {
48180+
error(node, Diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled);
48181+
}
48182+
if (isImportEquals || checkExternalImportOrExportDeclaration(node)) {
4816848183
checkImportBinding(node);
4816948184
markLinkedReferences(node, ReferenceHint.ExportImportEquals);
4817048185
if (node.moduleReference.kind !== SyntaxKind.ExternalModuleReference) {

src/compiler/commandLineParser.ts

+9
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,15 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
856856
affectsBuildInfo: true,
857857
affectsSemanticDiagnostics: true,
858858
},
859+
{
860+
name: "erasableSyntaxOnly",
861+
type: "boolean",
862+
category: Diagnostics.Interop_Constraints,
863+
description: Diagnostics.Do_not_allow_runtime_constructs_that_are_not_part_of_ECMAScript,
864+
defaultValueDescription: false,
865+
affectsBuildInfo: true,
866+
affectsSemanticDiagnostics: true,
867+
},
859868

860869
// Strict Type Checks
861870
{

src/compiler/diagnosticMessages.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,10 @@
971971
"category": "Error",
972972
"code": 1293
973973
},
974-
974+
"This syntax is not allowed when 'erasableSyntaxOnly' is enabled.": {
975+
"category": "Error",
976+
"code": 1294
977+
},
975978
"'with' statements are not allowed in an async function block.": {
976979
"category": "Error",
977980
"code": 1300
@@ -6471,11 +6474,14 @@
64716474
"category": "Message",
64726475
"code": 6719
64736476
},
6474-
64756477
"Built-in iterators are instantiated with a 'TReturn' type of 'undefined' instead of 'any'.": {
64766478
"category": "Message",
64776479
"code": 6720
64786480
},
6481+
"Do not allow runtime constructs that are not part of ECMAScript.": {
6482+
"category": "Message",
6483+
"code": 6721
6484+
},
64796485
"Default catch clause variables as 'unknown' instead of 'any'.": {
64806486
"category": "Message",
64816487
"code": 6803

src/compiler/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7502,6 +7502,7 @@ export interface CompilerOptions {
75027502
/** Paths used to compute primary types search locations */
75037503
typeRoots?: string[];
75047504
verbatimModuleSyntax?: boolean;
7505+
erasableSyntaxOnly?: boolean;
75057506
/** @internal */ version?: boolean;
75067507
/** @internal */ watch?: boolean;
75077508
esModuleInterop?: boolean;

tests/baselines/reference/api/typescript.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7102,6 +7102,7 @@ declare namespace ts {
71027102
/** Paths used to compute primary types search locations */
71037103
typeRoots?: string[];
71047104
verbatimModuleSyntax?: boolean;
7105+
erasableSyntaxOnly?: boolean;
71057106
esModuleInterop?: boolean;
71067107
useDefineForClassFields?: boolean;
71077108
[option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined;

tests/baselines/reference/config/initTSConfig/Default initialized TSConfig/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
7878
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
7979
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
80+
// "erasableSyntaxOnly": true, /* Do not allow runtime constructs that are not part of ECMAScript. */
8081
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
8182
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
8283
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */

tests/baselines/reference/config/initTSConfig/Initialized TSConfig with --help/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
7878
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
7979
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
80+
// "erasableSyntaxOnly": true, /* Do not allow runtime constructs that are not part of ECMAScript. */
8081
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
8182
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
8283
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */

tests/baselines/reference/config/initTSConfig/Initialized TSConfig with --watch/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
7878
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
7979
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
80+
// "erasableSyntaxOnly": true, /* Do not allow runtime constructs that are not part of ECMAScript. */
8081
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
8182
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
8283
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */

tests/baselines/reference/config/initTSConfig/Initialized TSConfig with advanced options/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
7878
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
7979
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
80+
// "erasableSyntaxOnly": true, /* Do not allow runtime constructs that are not part of ECMAScript. */
8081
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
8182
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
8283
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */

tests/baselines/reference/config/initTSConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
7878
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
7979
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
80+
// "erasableSyntaxOnly": true, /* Do not allow runtime constructs that are not part of ECMAScript. */
8081
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
8182
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
8283
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */

tests/baselines/reference/config/initTSConfig/Initialized TSConfig with enum value compiler options/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
7878
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
7979
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
80+
// "erasableSyntaxOnly": true, /* Do not allow runtime constructs that are not part of ECMAScript. */
8081
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
8182
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
8283
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */

tests/baselines/reference/config/initTSConfig/Initialized TSConfig with files options/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
7878
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
7979
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
80+
// "erasableSyntaxOnly": true, /* Do not allow runtime constructs that are not part of ECMAScript. */
8081
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
8182
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
8283
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */

tests/baselines/reference/config/initTSConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
7878
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
7979
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
80+
// "erasableSyntaxOnly": true, /* Do not allow runtime constructs that are not part of ECMAScript. */
8081
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
8182
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
8283
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */

tests/baselines/reference/config/initTSConfig/Initialized TSConfig with incorrect compiler option/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
7878
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
7979
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
80+
// "erasableSyntaxOnly": true, /* Do not allow runtime constructs that are not part of ECMAScript. */
8081
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
8182
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
8283
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */

tests/baselines/reference/config/initTSConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
7878
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
7979
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
80+
// "erasableSyntaxOnly": true, /* Do not allow runtime constructs that are not part of ECMAScript. */
8081
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
8182
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
8283
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */

tests/baselines/reference/config/initTSConfig/Initialized TSConfig with list compiler options/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
7878
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
7979
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
80+
// "erasableSyntaxOnly": true, /* Do not allow runtime constructs that are not part of ECMAScript. */
8081
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
8182
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
8283
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"erasableSyntaxOnly": true
4+
}
5+
}

0 commit comments

Comments
 (0)