Skip to content
Merged
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
45 changes: 15 additions & 30 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3223,9 +3223,7 @@ func (c *Checker) checkImportType(node *ast.Node) {
func (c *Checker) getResolutionModeOverride(node *ast.ImportAttributes, reportErrors bool) core.ResolutionMode {
if len(node.Attributes.Nodes) != 1 {
if reportErrors {
c.grammarErrorOnNode(node.AsNode(), core.IfElse(node.Token == ast.KindWithKeyword,
diagnostics.Type_import_attributes_should_have_exactly_one_key_resolution_mode_with_value_import_or_require,
diagnostics.Type_import_assertions_should_have_exactly_one_key_resolution_mode_with_value_import_or_require))
c.grammarErrorOnNode(node.AsNode(), diagnostics.Type_import_attributes_should_have_exactly_one_key_resolution_mode_with_value_import_or_require)
}
return core.ResolutionModeNone
}
Expand All @@ -3235,9 +3233,7 @@ func (c *Checker) getResolutionModeOverride(node *ast.ImportAttributes, reportEr
}
if elem.Name().Text() != "resolution-mode" {
if reportErrors {
c.grammarErrorOnNode(elem.Name(), core.IfElse(node.Token == ast.KindWithKeyword,
diagnostics.X_resolution_mode_is_the_only_valid_key_for_type_import_attributes,
diagnostics.X_resolution_mode_is_the_only_valid_key_for_type_import_assertions))
c.grammarErrorOnNode(elem.Name(), diagnostics.X_resolution_mode_is_the_only_valid_key_for_type_import_attributes)
}
return core.ResolutionModeNone
}
Expand Down Expand Up @@ -5199,14 +5195,11 @@ func (c *Checker) checkExternalImportOrExportDeclaration(node *ast.Node) bool {
if !ast.IsImportEqualsDeclaration(node) {
attributes := ast.GetImportAttributes(node)
if attributes != nil {
diagnostic := core.IfElse(attributes.AsImportAttributes().Token == ast.KindWithKeyword,
diagnostics.Import_attribute_values_must_be_string_literal_expressions,
diagnostics.Import_assertion_values_must_be_string_literal_expressions)
hasError := false
for _, attr := range attributes.AsImportAttributes().Attributes.Nodes {
if !ast.IsStringLiteral(attr.AsImportAttribute().Value) {
hasError = true
c.error(attr.AsImportAttribute().Value, diagnostic)
c.error(attr.AsImportAttribute().Value, diagnostics.Import_attribute_values_must_be_string_literal_expressions)
}
}
return !hasError
Expand Down Expand Up @@ -5252,40 +5245,24 @@ func (c *Checker) checkImportAttributes(declaration *ast.Node) {
}
isTypeOnly := ast.IsExclusivelyTypeOnlyImportOrExport(declaration)
override := c.getResolutionModeOverride(node.AsImportAttributes(), isTypeOnly)
isImportAttributes := node.AsImportAttributes().Token == ast.KindWithKeyword
if isTypeOnly && override != core.ResolutionModeNone {
return // Other grammar checks do not apply to type-only imports with resolution mode assertions
return // Other grammar checks do not apply to type-only imports with resolution mode attributes
}

if !c.moduleKind.SupportsImportAttributes() {
if isImportAttributes {
c.grammarErrorOnNode(node, diagnostics.Import_attributes_are_only_supported_when_the_module_option_is_set_to_esnext_node18_node20_nodenext_or_preserve)
} else {
c.grammarErrorOnNode(node, diagnostics.Import_assertions_are_only_supported_when_the_module_option_is_set_to_esnext_node18_node20_nodenext_or_preserve)
}
return
}

if core.ModuleKindNode20 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext && !isImportAttributes {
c.grammarErrorOnNode(node, diagnostics.Import_assertions_have_been_replaced_by_import_attributes_Use_with_instead_of_assert)
c.grammarErrorOnNode(node, diagnostics.Import_attributes_are_only_supported_when_the_module_option_is_set_to_esnext_node18_node20_nodenext_or_preserve)
return
}

if moduleSpecifier := getModuleSpecifierFromNode(declaration); moduleSpecifier != nil {
if c.getEmitSyntaxForModuleSpecifierExpression(moduleSpecifier) == core.ModuleKindCommonJS {
if isImportAttributes {
c.grammarErrorOnNode(node, diagnostics.Import_attributes_are_not_allowed_on_statements_that_compile_to_CommonJS_require_calls)
} else {
c.grammarErrorOnNode(node, diagnostics.Import_assertions_are_not_allowed_on_statements_that_compile_to_CommonJS_require_calls)
}
c.grammarErrorOnNode(node, diagnostics.Import_attributes_are_not_allowed_on_statements_that_compile_to_CommonJS_require_calls)
return
}
}

if isTypeOnly {
c.grammarErrorOnNode(node, core.IfElse(isImportAttributes,
diagnostics.Import_attributes_cannot_be_used_with_type_only_imports_or_exports,
diagnostics.Import_assertions_cannot_be_used_with_type_only_imports_or_exports))
c.grammarErrorOnNode(node, diagnostics.Import_attributes_cannot_be_used_with_type_only_imports_or_exports)
return
}
if override != core.ResolutionModeNone {
Expand Down Expand Up @@ -8140,6 +8117,14 @@ func (c *Checker) checkImportCallExpression(node *ast.Node) *Type {
if importCallOptionsType != c.emptyObjectType {
c.checkTypeAssignableTo(optionsType, c.getNullableType(importCallOptionsType, TypeFlagsUndefined), args[1], nil)
}
if ast.IsObjectLiteralExpression(args[1]) {
for _, prop := range args[1].AsObjectLiteralExpression().Properties.Nodes {
if ast.IsPropertyAssignment(prop) && ast.IsIdentifier(prop.Name()) && prop.Name().Text() == "assert" {
c.error(prop.Name(), diagnostics.Import_assertions_have_been_replaced_by_import_attributes_Use_with_instead_of_assert)
break
}
}
}
}
// resolveExternalModuleName will return undefined if the moduleReferenceExpression is not a string literal
moduleSymbol := c.resolveExternalModuleName(node, specifier, false /*ignoreErrors*/)
Expand Down
9 changes: 9 additions & 0 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2433,6 +2433,9 @@ func (p *Parser) parseModuleExportName(disallowKeywords bool) (node *ast.Node, n

func (p *Parser) tryParseImportAttributes() *ast.Node {
if p.token == ast.KindWithKeyword || (p.token == ast.KindAssertKeyword && !p.hasPrecedingLineBreak()) {
if p.token == ast.KindAssertKeyword {
p.parseErrorAtCurrentToken(diagnostics.Import_assertions_have_been_replaced_by_import_attributes_Use_with_instead_of_assert)
}
return p.parseImportAttributes(p.token, false /*skipKeyword*/)
}
return nil
Expand Down Expand Up @@ -2497,6 +2500,9 @@ func (p *Parser) parseExportDeclaration(pos int, jsdoc jsdocScannerInfo, modifie
}
}
if moduleSpecifier != nil && (p.token == ast.KindWithKeyword || p.token == ast.KindAssertKeyword) && !p.hasPrecedingLineBreak() {
if p.token == ast.KindAssertKeyword {
p.parseErrorAtCurrentToken(diagnostics.Import_assertions_have_been_replaced_by_import_attributes_Use_with_instead_of_assert)
}
attributes = p.parseImportAttributes(p.token, false /*skipKeyword*/)
}
p.parseSemicolon()
Expand Down Expand Up @@ -2968,6 +2974,9 @@ func (p *Parser) parseImportType() *ast.Node {
p.parseExpected(ast.KindOpenBraceToken)
currentToken := p.token
if currentToken == ast.KindWithKeyword || currentToken == ast.KindAssertKeyword {
if currentToken == ast.KindAssertKeyword {
p.parseErrorAtCurrentToken(diagnostics.Import_assertions_have_been_replaced_by_import_attributes_Use_with_instead_of_assert)
}
p.nextToken()
} else {
p.parseErrorAtCurrentToken(diagnostics.X_0_expected, scanner.TokenToString(ast.KindWithKeyword))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/a.ts(1,35): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
/b.ts(1,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
/c.ts(1,51): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.


==== /a.ts (1 errors) ====
import json from "./package.json" assert { type: "json" };
~~~~~~
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.

==== /b.ts (1 errors) ====
import * as data from "./data.json" assert { type: "json" };
~~~~~~
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.

==== /c.ts (1 errors) ====
export { default as config } from "./config.json" assert { type: "json" };
~~~~~~
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.

==== /package.json (0 errors) ====
{}

==== /data.json (0 errors) ====
{}

==== /config.json (0 errors) ====
{}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/a.ts(2,35): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
/b.ts(1,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
/c.ts(1,51): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.


==== /a.ts (1 errors) ====
// With ignoreDeprecations: "6.0", import assertions should not produce a deprecation error.
import json from "./package.json" assert { type: "json" };
~~~~~~
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.

==== /b.ts (1 errors) ====
import * as data from "./data.json" assert { type: "json" };
~~~~~~
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.

==== /c.ts (1 errors) ====
export { default as config } from "./config.json" assert { type: "json" };
~~~~~~
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.

==== /package.json (0 errors) ====
{}

==== /data.json (0 errors) ====
{}

==== /config.json (0 errors) ====
{}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--- old.importAssertionsDeprecatedIgnored.errors.txt
+++ new.importAssertionsDeprecatedIgnored.errors.txt
@@= skipped -0, +0 lines =@@
-<no content>
+/a.ts(2,35): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
+/b.ts(1,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
+/c.ts(1,51): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
+
+
+==== /a.ts (1 errors) ====
+ // With ignoreDeprecations: "6.0", import assertions should not produce a deprecation error.
+ import json from "./package.json" assert { type: "json" };
+ ~~~~~~
+!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
+
+==== /b.ts (1 errors) ====
+ import * as data from "./data.json" assert { type: "json" };
+ ~~~~~~
+!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
+
+==== /c.ts (1 errors) ====
+ export { default as config } from "./config.json" assert { type: "json" };
+ ~~~~~~
+!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
+
+==== /package.json (0 errors) ====
+ {}
+
+==== /data.json (0 errors) ====
+ {}
+
+==== /config.json (0 errors) ====
+ {}
+
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/main.ts(1,30): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
/main.ts(2,30): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
/main.ts(4,31): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
/main.ts(5,31): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.


==== /types.d.ts (0 errors) ====
export interface MyType { x: string }

==== /main.ts (4 errors) ====
type A = import("./types", { assert: { "resolution-mode": "import" } }).MyType;
~~~~~~
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
type B = import("./types", { assert: { "resolution-mode": "require" } }).MyType;
~~~~~~
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.

const a = import("./types", { assert: { "resolution-mode": "import" } });
~~~~~~
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
const b = import("./types", { assert: { "resolution-mode": "require" } });
~~~~~~
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.

Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,21 @@
@@= skipped -0, +0 lines =@@
-/main.ts(1,38): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
-/main.ts(2,38): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
-/main.ts(4,31): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
-/main.ts(5,31): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
-
-
-==== /types.d.ts (0 errors) ====
- export interface MyType { x: string }
-
-==== /main.ts (4 errors) ====
- type A = import("./types", { assert: { "resolution-mode": "import" } }).MyType;
+/main.ts(1,30): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
+/main.ts(2,30): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
/main.ts(4,31): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
/main.ts(5,31): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.

@@= skipped -8, +8 lines =@@
==== /main.ts (4 errors) ====
type A = import("./types", { assert: { "resolution-mode": "import" } }).MyType;
- ~
-!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
- type B = import("./types", { assert: { "resolution-mode": "require" } }).MyType;
+ ~~~~~~
Comment on lines +14 to +16
Copy link
Member

Choose a reason for hiding this comment

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

Strada seems to issue this error in a dumb place. Funny

!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
type B = import("./types", { assert: { "resolution-mode": "require" } }).MyType;
- ~
-!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
-
- const a = import("./types", { assert: { "resolution-mode": "import" } });
- ~~~~~~
-!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
- const b = import("./types", { assert: { "resolution-mode": "require" } });
- ~~~~~~
-!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
-
+<no content>
+ ~~~~~~
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.

const a = import("./types", { assert: { "resolution-mode": "import" } });
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/main.ts(2,30): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
/main.ts(3,30): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
/main.ts(5,31): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
/main.ts(6,31): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.


==== /types.d.ts (0 errors) ====
export interface MyType { x: string }

==== /main.ts (4 errors) ====
// With ignoreDeprecations: "6.0", import type assertions should not produce a deprecation error.
type A = import("./types", { assert: { "resolution-mode": "import" } }).MyType;
~~~~~~
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
type B = import("./types", { assert: { "resolution-mode": "require" } }).MyType;
~~~~~~
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.

const a = import("./types", { assert: { "resolution-mode": "import" } });
~~~~~~
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
const b = import("./types", { assert: { "resolution-mode": "require" } });
~~~~~~
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.

Loading