Skip to content

Commit

Permalink
Merge pull request #21 from nicolo-ribaudo/import-attributes
Browse files Browse the repository at this point in the history
Allow parsing the `with` keyword for import attributes
  • Loading branch information
xtuc authored May 12, 2023
2 parents fbde935 + 56615fd commit 91d68da
Show file tree
Hide file tree
Showing 7 changed files with 482 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function importAssertions(Parser) {
if (this.type !== tt.string) { this.unexpected(); }
node.source = this.parseExprAtom();

if (this.type === this.assertToken) {
if (this.type === this.assertToken || this.type === tt._with) {
this.next();
const assertions = this.parseImportAssertions();
if (assertions) {
Expand Down Expand Up @@ -140,7 +140,7 @@ export function importAssertions(Parser) {
if (this.type !== tt.string) { this.unexpected(); }
node.source = this.parseExprAtom();

if (this.type === this.assertToken) {
if (this.type === this.assertToken || this.type === tt._with) {
this.next();
const assertions = this.parseImportAssertions();
if (assertions) {
Expand Down Expand Up @@ -177,7 +177,7 @@ export function importAssertions(Parser) {
this.type === tt.string ? this.parseExprAtom() : this.unexpected();
}

if (this.type === this.assertToken) {
if (this.type === this.assertToken || this.type == tt._with) {
this.next();
const assertions = this.parseImportAssertions();
if (assertions) {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/with-keyword-export-all/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./foo.json" with { type: "json" };
125 changes: 125 additions & 0 deletions test/fixtures/with-keyword-export-all/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
{
"type": "Program",
"start": 0,
"end": 50,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 0
}
},
"range": [
0,
50
],
"body": [
{
"type": "ExportAllDeclaration",
"start": 0,
"end": 49,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 49
}
},
"range": [
0,
49
],
"exported": null,
"source": {
"type": "Literal",
"start": 14,
"end": 26,
"loc": {
"start": {
"line": 1,
"column": 14
},
"end": {
"line": 1,
"column": 26
}
},
"range": [
14,
26
],
"value": "./foo.json",
"raw": "\"./foo.json\""
},
"assertions": [
{
"type": "ImportAttribute",
"start": 34,
"end": 46,
"loc": {
"start": {
"line": 1,
"column": 34
},
"end": {
"line": 1,
"column": 46
}
},
"range": [
34,
46
],
"key": {
"type": "Identifier",
"start": 34,
"end": 38,
"loc": {
"start": {
"line": 1,
"column": 34
},
"end": {
"line": 1,
"column": 38
}
},
"range": [
34,
38
],
"name": "type"
},
"value": {
"type": "Literal",
"start": 40,
"end": 46,
"loc": {
"start": {
"line": 1,
"column": 40
},
"end": {
"line": 1,
"column": 46
}
},
"range": [
40,
46
],
"value": "json",
"raw": "\"json\""
}
}
]
}
],
"sourceType": "module"
}
1 change: 1 addition & 0 deletions test/fixtures/with-keyword-export-named/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { json } from "./foo.json" with { type: "json" };
186 changes: 186 additions & 0 deletions test/fixtures/with-keyword-export-named/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
{
"type": "Program",
"start": 0,
"end": 57,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 0
}
},
"range": [
0,
57
],
"body": [
{
"type": "ExportNamedDeclaration",
"start": 0,
"end": 56,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 56
}
},
"range": [
0,
56
],
"declaration": null,
"specifiers": [
{
"type": "ExportSpecifier",
"start": 9,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 13
}
},
"range": [
9,
13
],
"local": {
"type": "Identifier",
"start": 9,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 13
}
},
"range": [
9,
13
],
"name": "json"
},
"exported": {
"type": "Identifier",
"start": 9,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 13
}
},
"range": [
9,
13
],
"name": "json"
}
}
],
"source": {
"type": "Literal",
"start": 21,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 33
}
},
"range": [
21,
33
],
"value": "./foo.json",
"raw": "\"./foo.json\""
},
"assertions": [
{
"type": "ImportAttribute",
"start": 41,
"end": 53,
"loc": {
"start": {
"line": 1,
"column": 41
},
"end": {
"line": 1,
"column": 53
}
},
"range": [
41,
53
],
"key": {
"type": "Identifier",
"start": 41,
"end": 45,
"loc": {
"start": {
"line": 1,
"column": 41
},
"end": {
"line": 1,
"column": 45
}
},
"range": [
41,
45
],
"name": "type"
},
"value": {
"type": "Literal",
"start": 47,
"end": 53,
"loc": {
"start": {
"line": 1,
"column": 47
},
"end": {
"line": 1,
"column": 53
}
},
"range": [
47,
53
],
"value": "json",
"raw": "\"json\""
}
}
]
}
],
"sourceType": "module"
}
1 change: 1 addition & 0 deletions test/fixtures/with-keyword-import/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import json from "./foo.json" with { type: "json" };
Loading

0 comments on commit 91d68da

Please sign in to comment.