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

feat(58453): Add a rule to the compiler options to disallow the assert keyword for import attributes #58498

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 14 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46517,6 +46517,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
function checkImportAttributes(declaration: ImportDeclaration | ExportDeclaration | JSDocImportTag) {
const node = declaration.attributes;
if (node) {
if (compilerOptions.disallowAssertKeywords && node.token === SyntaxKind.AssertKeyword) {
grammarErrorOnFirstToken(node, Diagnostics.The_assert_keyword_is_disallowed_Use_the_with_keyword_instead_for_import_attributes);
}

const importAttributesType = getGlobalImportAttributesType(/*reportErrors*/ true);
if (importAttributesType !== emptyObjectType) {
checkTypeAssignableTo(getTypeFromImportAttributes(node), getNullableType(importAttributesType, TypeFlags.Undefined), node);
Expand Down Expand Up @@ -51197,6 +51201,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}

if (compilerOptions.disallowAssertKeywords && nodeArguments.length > 1) {
const importAttributesArgument = nodeArguments[1];
if (isObjectLiteralExpression(importAttributesArgument) && importAttributesArgument.properties.length) {
const importAttributeName = find(importAttributesArgument.properties, p => !!p.name && isIdentifier(p.name) && idText(p.name) === tokenToString(SyntaxKind.AssertKeyword));
if (importAttributeName && importAttributeName.name) {
grammarErrorOnNode(importAttributeName.name, Diagnostics.The_assert_keyword_is_disallowed_Use_the_with_keyword_instead_for_import_attributes);
}
}
}

if (nodeArguments.length === 0 || nodeArguments.length > 2) {
return grammarErrorOnNode(node, Diagnostics.Dynamic_imports_can_only_accept_a_module_specifier_and_an_optional_set_of_attributes_as_arguments);
}
Expand Down
7 changes: 7 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,13 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
type: "string",
defaultValueDescription: undefined,
},
{
name: "disallowAssertKeywords",
type: "boolean",
category: Diagnostics.Backwards_Compatibility,
description: Diagnostics.Disallow_using_assert_keywords_in_import_attributes,
defaultValueDescription: false,
},
];

/** @internal */
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3903,6 +3903,10 @@
"category": "Error",
"code": 2868
},
"The `assert` keyword is disallowed. Use the `with` keyword instead for import attributes.": {
"category": "Error",
"code": 2869
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down Expand Up @@ -5959,6 +5963,10 @@
"category": "Message",
"code": 6609
},
"Disallow using 'assert' keywords in import attributes.": {
"category": "Message",
"code": 6610
},
"Enable constraints that allow a TypeScript project to be used with project references.": {
"category": "Message",
"code": 6611
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7377,6 +7377,7 @@ export interface CompilerOptions {
esModuleInterop?: boolean;
/** @internal */ showConfig?: boolean;
useDefineForClassFields?: boolean;
disallowAssertKeywords?: boolean;

[option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined;
}
Expand Down
1 change: 1 addition & 0 deletions src/testRunner/compilerRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class CompilerTest {
"allowImportingTsExtensions",
"allowSyntheticDefaultImports",
"alwaysStrict",
"disallowAssertKeywords",
"downlevelIteration",
"experimentalDecorators",
"emitDecoratorMetadata",
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7020,6 +7020,7 @@ declare namespace ts {
verbatimModuleSyntax?: boolean;
esModuleInterop?: boolean;
useDefineForClassFields?: boolean;
disallowAssertKeywords?: boolean;
[option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined;
}
interface WatchOptions {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"disallowAssertKeywords": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [tests/cases/conformance/importAssertion/importAssertion6.ts] ////

//// [a.ts]
export const a = 1;
export const b = 2;

//// [b.ts]
import './a' assert { type: "json" }

//// [c.ts]
const b = import('./a', { assert: { type: "json" } });


//// [a.js]
export const a = 1;
export const b = 2;
//// [b.js]
import './a' assert { type: "json" };
//// [c.js]
const b = import('./a', { assert: { type: "json" } });
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [tests/cases/conformance/importAssertion/importAssertion6.ts] ////

=== a.ts ===
export const a = 1;
>a : Symbol(a, Decl(a.ts, 0, 12))

export const b = 2;
>b : Symbol(b, Decl(a.ts, 1, 12))

=== b.ts ===

import './a' assert { type: "json" }

=== c.ts ===
const b = import('./a', { assert: { type: "json" } });
>b : Symbol(b, Decl(c.ts, 0, 5))
>'./a' : Symbol("a", Decl(a.ts, 0, 0))
>assert : Symbol(assert, Decl(c.ts, 0, 25))
>type : Symbol(type, Decl(c.ts, 0, 35))

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//// [tests/cases/conformance/importAssertion/importAssertion6.ts] ////

=== a.ts ===
export const a = 1;
>a : 1
> : ^
>1 : 1
> : ^

export const b = 2;
>b : 2
> : ^
>2 : 2
> : ^

=== b.ts ===
import './a' assert { type: "json" }
>type : error

=== c.ts ===
const b = import('./a', { assert: { type: "json" } });
>b : Promise<typeof import("a")>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>import('./a', { assert: { type: "json" } }) : Promise<typeof import("a")>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>'./a' : "./a"
> : ^^^^^
>{ assert: { type: "json" } } : { assert: { type: string; }; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>assert : { type: string; }
> : ^^^^^^^^^^^^^^^^^
>{ type: "json" } : { type: string; }
> : ^^^^^^^^^^^^^^^^^
>type : string
> : ^^^^^^
>"json" : "json"
> : ^^^^^^

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
b.ts(1,14): error TS2869: The `assert` keyword is disallowed. Use the `with` keyword instead for import attributes.
c.ts(1,27): error TS2869: The `assert` keyword is disallowed. Use the `with` keyword instead for import attributes.


==== a.ts (0 errors) ====
export const a = 1;
export const b = 2;

==== b.ts (1 errors) ====
import './a' assert { type: "json" }
~~~~~~
!!! error TS2869: The `assert` keyword is disallowed. Use the `with` keyword instead for import attributes.

==== c.ts (1 errors) ====
const b = import('./a', { assert: { type: "json" } });
~~~~~~
!!! error TS2869: The `assert` keyword is disallowed. Use the `with` keyword instead for import attributes.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [tests/cases/conformance/importAssertion/importAssertion6.ts] ////

//// [a.ts]
export const a = 1;
export const b = 2;

//// [b.ts]
import './a' assert { type: "json" }

//// [c.ts]
const b = import('./a', { assert: { type: "json" } });


//// [a.js]
export const a = 1;
export const b = 2;
//// [b.js]
import './a' assert { type: "json" };
//// [c.js]
const b = import('./a', { assert: { type: "json" } });
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [tests/cases/conformance/importAssertion/importAssertion6.ts] ////

=== a.ts ===
export const a = 1;
>a : Symbol(a, Decl(a.ts, 0, 12))

export const b = 2;
>b : Symbol(b, Decl(a.ts, 1, 12))

=== b.ts ===

import './a' assert { type: "json" }

=== c.ts ===
const b = import('./a', { assert: { type: "json" } });
>b : Symbol(b, Decl(c.ts, 0, 5))
>'./a' : Symbol("a", Decl(a.ts, 0, 0))
>assert : Symbol(assert, Decl(c.ts, 0, 25))
>type : Symbol(type, Decl(c.ts, 0, 35))

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//// [tests/cases/conformance/importAssertion/importAssertion6.ts] ////

=== a.ts ===
export const a = 1;
>a : 1
> : ^
>1 : 1
> : ^

export const b = 2;
>b : 2
> : ^
>2 : 2
> : ^

=== b.ts ===
import './a' assert { type: "json" }
>type : any
> : ^^^

=== c.ts ===
const b = import('./a', { assert: { type: "json" } });
>b : Promise<typeof import("a")>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>import('./a', { assert: { type: "json" } }) : Promise<typeof import("a")>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>'./a' : "./a"
> : ^^^^^
>{ assert: { type: "json" } } : { assert: { type: string; }; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>assert : { type: string; }
> : ^^^^^^^^^^^^^^^^^
>{ type: "json" } : { type: string; }
> : ^^^^^^^^^^^^^^^^^
>type : string
> : ^^^^^^
>"json" : "json"
> : ^^^^^^

13 changes: 13 additions & 0 deletions tests/cases/conformance/importAssertion/importAssertion6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @module: esnext
// @target: esnext
// @disallowAssertKeywords: true,false

// @filename: a.ts
export const a = 1;
export const b = 2;

// @filename: b.ts
import './a' assert { type: "json" }

// @filename: c.ts
const b = import('./a', { assert: { type: "json" } });