-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for Import Attributes and RegExp Modifiers (#639)
* feat: add support for import attributes and RegExp modifiers * test: fix test case * test: add test case to eslint-scope
- Loading branch information
Showing
32 changed files
with
4,896 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/** | ||
* @fileoverview Tests for ES2025 Import Attributes. | ||
* @author Yosuke Ota | ||
*/ | ||
|
||
import assert from "node:assert"; | ||
import * as espree from "espree"; | ||
import { KEYS } from "eslint-visitor-keys"; | ||
import { analyze } from "../lib/index.js"; | ||
|
||
describe("Import Attributes", () => { | ||
|
||
describe("const type = \"json\"; import pkg from \"./package.json\" with { type: \"json\" };", () => { | ||
let ast; | ||
let scopeManager; | ||
let globalScope; | ||
|
||
beforeEach(() => { | ||
ast = espree.parse("const type = \"json\"; import pkg from \"./package.json\" with { type: \"json\" };", { ecmaVersion: 16, sourceType: "module" }); | ||
scopeManager = analyze(ast, { ecmaVersion: 16, sourceType: "module", childVisitorKeys: KEYS }); | ||
({ globalScope } = scopeManager); | ||
}); | ||
|
||
it("the global scope should not have any variables", () => { | ||
assert.strictEqual(globalScope.variables.length, 0); | ||
}); | ||
|
||
it("the global scope should have one child scope, a module scope", () => { | ||
assert.strictEqual(globalScope.childScopes.length, 1); | ||
assert.strictEqual(globalScope.childScopes[0].type, "module"); | ||
}); | ||
|
||
it("the module scope should not have any child scopes", () => { | ||
const moduleScope = globalScope.childScopes[0]; | ||
|
||
assert.strictEqual(moduleScope.childScopes.length, 0); | ||
}); | ||
|
||
it("the module scope should have two variables", () => { | ||
const moduleScope = globalScope.childScopes[0]; | ||
|
||
assert.strictEqual(moduleScope.variables.length, 2); | ||
assert.strictEqual(moduleScope.variables[0].name, "type"); | ||
assert.strictEqual(moduleScope.variables[1].name, "pkg"); | ||
}); | ||
|
||
it("the type variable should have one reference, a variable declaration", () => { | ||
const moduleScope = globalScope.childScopes[0]; | ||
const typeVariable = moduleScope.variables[0]; | ||
|
||
assert.strictEqual(typeVariable.references.length, 1); | ||
assert.strictEqual(typeVariable.references[0].identifier, ast.body[0].declarations[0].id); | ||
}); | ||
}); | ||
|
||
describe("const type = \"json\"; export * from \"./package.json\" with { type: \"json\" };", () => { | ||
let ast; | ||
let scopeManager; | ||
let globalScope; | ||
|
||
beforeEach(() => { | ||
ast = espree.parse("const type = \"json\"; export * from \"./package.json\" with { type: \"json\" };", { ecmaVersion: 16, sourceType: "module" }); | ||
scopeManager = analyze(ast, { ecmaVersion: 16, sourceType: "module", childVisitorKeys: KEYS }); | ||
({ globalScope } = scopeManager); | ||
}); | ||
|
||
it("the global scope should not have any variables", () => { | ||
assert.strictEqual(globalScope.variables.length, 0); | ||
}); | ||
|
||
it("the global scope should have one child scope, a module scope", () => { | ||
assert.strictEqual(globalScope.childScopes.length, 1); | ||
assert.strictEqual(globalScope.childScopes[0].type, "module"); | ||
}); | ||
|
||
it("the module scope should not have any child scopes", () => { | ||
const moduleScope = globalScope.childScopes[0]; | ||
|
||
assert.strictEqual(moduleScope.childScopes.length, 0); | ||
}); | ||
|
||
it("the module scope should have one variable, a type variable", () => { | ||
const moduleScope = globalScope.childScopes[0]; | ||
|
||
assert.strictEqual(moduleScope.variables.length, 1); | ||
assert.strictEqual(moduleScope.variables[0].name, "type"); | ||
}); | ||
|
||
it("the type variable should have one reference, a variable declaration", () => { | ||
const moduleScope = globalScope.childScopes[0]; | ||
const typeVariable = moduleScope.variables[0]; | ||
|
||
assert.strictEqual(typeVariable.references.length, 1); | ||
assert.strictEqual(typeVariable.references[0].identifier, ast.body[0].declarations[0].id); | ||
}); | ||
}); | ||
|
||
|
||
describe("const type = \"json\"; export { default } from \"./package.json\" with { type: \"json\" };", () => { | ||
let ast; | ||
let scopeManager; | ||
let globalScope; | ||
|
||
beforeEach(() => { | ||
ast = espree.parse("const type = \"json\"; export { default } from \"./package.json\" with { type: \"json\" };", { ecmaVersion: 16, sourceType: "module" }); | ||
scopeManager = analyze(ast, { ecmaVersion: 16, sourceType: "module", childVisitorKeys: KEYS }); | ||
({ globalScope } = scopeManager); | ||
}); | ||
|
||
it("the global scope should not have any variables", () => { | ||
assert.strictEqual(globalScope.variables.length, 0); | ||
}); | ||
|
||
it("the global scope should have one child scope, a module scope", () => { | ||
assert.strictEqual(globalScope.childScopes.length, 1); | ||
assert.strictEqual(globalScope.childScopes[0].type, "module"); | ||
}); | ||
|
||
it("the module scope should not have any child scopes", () => { | ||
const moduleScope = globalScope.childScopes[0]; | ||
|
||
assert.strictEqual(moduleScope.childScopes.length, 0); | ||
}); | ||
|
||
it("the module scope should have one variable, a type variable", () => { | ||
const moduleScope = globalScope.childScopes[0]; | ||
|
||
assert.strictEqual(moduleScope.variables.length, 1); | ||
assert.strictEqual(moduleScope.variables[0].name, "type"); | ||
}); | ||
|
||
it("the type variable should have one reference, a variable declaration", () => { | ||
const moduleScope = globalScope.childScopes[0]; | ||
const typeVariable = moduleScope.variables[0]; | ||
|
||
assert.strictEqual(typeVariable.references.length, 1); | ||
assert.strictEqual(typeVariable.references[0].identifier, ast.body[0].declarations[0].id); | ||
}); | ||
}); | ||
|
||
|
||
describe("const type = \"json\"; import(\"./package.json\", { with: { type } });", () => { | ||
let ast; | ||
let scopeManager; | ||
let globalScope; | ||
|
||
beforeEach(() => { | ||
ast = espree.parse("const type = \"json\"; import(\"./package.json\", { with: { type } });", { ecmaVersion: 16, sourceType: "module" }); | ||
scopeManager = analyze(ast, { ecmaVersion: 16, sourceType: "module", childVisitorKeys: KEYS }); | ||
({ globalScope } = scopeManager); | ||
}); | ||
|
||
it("the global scope should not have any variables", () => { | ||
assert.strictEqual(globalScope.variables.length, 0); | ||
}); | ||
|
||
it("the global scope should have one child scope, a module scope", () => { | ||
assert.strictEqual(globalScope.childScopes.length, 1); | ||
assert.strictEqual(globalScope.childScopes[0].type, "module"); | ||
}); | ||
|
||
it("the module scope should not have any child scopes", () => { | ||
const moduleScope = globalScope.childScopes[0]; | ||
|
||
assert.strictEqual(moduleScope.childScopes.length, 0); | ||
}); | ||
|
||
it("the module scope should have one variable, a type variable", () => { | ||
const moduleScope = globalScope.childScopes[0]; | ||
|
||
assert.strictEqual(moduleScope.variables.length, 1); | ||
assert.strictEqual(moduleScope.variables[0].name, "type"); | ||
}); | ||
|
||
|
||
it("the type variable should have two references, a variable declaration and import options", () => { | ||
const moduleScope = globalScope.childScopes[0]; | ||
const typeVariable = moduleScope.variables[0]; | ||
|
||
assert.strictEqual(typeVariable.references.length, 2); | ||
assert.strictEqual(typeVariable.references[0].identifier, ast.body[0].declarations[0].id); | ||
assert.strictEqual(typeVariable.references[1].identifier, ast.body[1].expression.options.properties[0].value.properties[0].value); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...cma-version/16/import-attributes/invalid-import-attributes-with-dupe-key.module-result.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default { | ||
"index": 41, | ||
"lineNumber": 1, | ||
"column": 42, | ||
"message": "Duplicate attribute key 'type'" | ||
}; |
6 changes: 6 additions & 0 deletions
6
...tures/ecma-version/16/import-attributes/invalid-import-attributes-with-dupe-key.result.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default { | ||
"index": 0, | ||
"lineNumber": 1, | ||
"column": 1, | ||
"message": "'import' and 'export' may appear only with 'sourceType: module'" | ||
}; |
1 change: 1 addition & 0 deletions
1
...fixtures/ecma-version/16/import-attributes/invalid-import-attributes-with-dupe-key.src.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import "./foo.json" with { type: "json", type: "html" }; |
6 changes: 6 additions & 0 deletions
6
...-attributes/invalid-import-attributes-with-dynamic-with-multiple-trailing-comma.result.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default { | ||
"index": 19, | ||
"lineNumber": 1, | ||
"column": 20, | ||
"message": "Unexpected token ," | ||
}; |
1 change: 1 addition & 0 deletions
1
...ort-attributes/invalid-import-attributes-with-dynamic-with-multiple-trailing-comma.src.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import("foo.json", , ); |
6 changes: 6 additions & 0 deletions
6
...import-attributes/invalid-import-attributes-with-multiple-trailing-comma.module-result.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default { | ||
"index": 41, | ||
"lineNumber": 1, | ||
"column": 42, | ||
"message": "Unexpected token ," | ||
}; |
6 changes: 6 additions & 0 deletions
6
...ion/16/import-attributes/invalid-import-attributes-with-multiple-trailing-comma.result.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default { | ||
"index": 0, | ||
"lineNumber": 1, | ||
"column": 1, | ||
"message": "'import' and 'export' may appear only with 'sourceType: module'" | ||
}; |
1 change: 1 addition & 0 deletions
1
...ersion/16/import-attributes/invalid-import-attributes-with-multiple-trailing-comma.src.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import "./foo.json" with { type: "json", , }; |
6 changes: 6 additions & 0 deletions
6
...a-version/16/import-attributes/invalid-import-attributes-with-number-key.module-result.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default { | ||
"index": 27, | ||
"lineNumber": 1, | ||
"column": 28, | ||
"message": "Unexpected token 42" | ||
}; |
6 changes: 6 additions & 0 deletions
6
...res/ecma-version/16/import-attributes/invalid-import-attributes-with-number-key.result.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default { | ||
"index": 0, | ||
"lineNumber": 1, | ||
"column": 1, | ||
"message": "'import' and 'export' may appear only with 'sourceType: module'" | ||
}; |
1 change: 1 addition & 0 deletions
1
...xtures/ecma-version/16/import-attributes/invalid-import-attributes-with-number-key.src.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import "./foo.json" with { 42: "s" }; |
6 changes: 6 additions & 0 deletions
6
...version/16/import-attributes/invalid-import-attributes-with-number-value.module-result.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default { | ||
"index": 33, | ||
"lineNumber": 1, | ||
"column": 34, | ||
"message": "Unexpected token 42" | ||
}; |
6 changes: 6 additions & 0 deletions
6
...s/ecma-version/16/import-attributes/invalid-import-attributes-with-number-value.result.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default { | ||
"index": 0, | ||
"lineNumber": 1, | ||
"column": 1, | ||
"message": "'import' and 'export' may appear only with 'sourceType: module'" | ||
}; |
1 change: 1 addition & 0 deletions
1
...ures/ecma-version/16/import-attributes/invalid-import-attributes-with-number-value.src.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import "./foo.json" with { type: 42 }; |
Oops, something went wrong.