diff --git a/src/index.js b/src/index.js index 758f69b..b39dfd0 100644 --- a/src/index.js +++ b/src/index.js @@ -143,10 +143,11 @@ function pluginImpl(options, Parser) { node.source = this.parseExprAtom(); if (this._matchKeywordToken()) { + const property = this._getProperty(); this.next(); const attributes = this.parseImportAttributes(); if (attributes) { - node[this._getProperty()] = attributes; + node[property] = attributes; } } @@ -187,10 +188,11 @@ function pluginImpl(options, Parser) { node.source = this.parseExprAtom(); if (this._matchKeywordToken()) { + const property = this._getProperty(); this.next(); const attributes = this.parseImportAttributes(); if (attributes) { - node[this._getProperty()] = attributes; + node[property] = attributes; } } } else { @@ -224,10 +226,11 @@ function pluginImpl(options, Parser) { } if (this._matchKeywordToken()) { + const property = this._getProperty(); this.next(); const attributes = this.parseImportAttributes(); if (attributes) { - node[this._getProperty()] = attributes; + node[property] = attributes; } } this.semicolon(); diff --git a/test/fixtures/assertions-type/actual.js b/test/fixtures/assertions-type/actual.js new file mode 100644 index 0000000..890e429 --- /dev/null +++ b/test/fixtures/assertions-type/actual.js @@ -0,0 +1 @@ +import json from "./foo.json" assert { type: "json" }; diff --git a/test/fixtures/assertions-type/expected.json b/test/fixtures/assertions-type/expected.json new file mode 100644 index 0000000..c95c751 --- /dev/null +++ b/test/fixtures/assertions-type/expected.json @@ -0,0 +1,165 @@ +{ + "type": "Program", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 0 + } + }, + "range": [ + 0, + 55 + ], + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 54 + } + }, + "range": [ + 0, + 54 + ], + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start": 7, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "range": [ + 7, + 11 + ], + "local": { + "type": "Identifier", + "start": 7, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "range": [ + 7, + 11 + ], + "name": "json" + } + } + ], + "source": { + "type": "Literal", + "start": 17, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "range": [ + 17, + 29 + ], + "value": "./foo.json", + "raw": "\"./foo.json\"" + }, + "assertions": [ + { + "type": "ImportAttribute", + "start": 39, + "end": 51, + "loc": { + "start": { + "line": 1, + "column": 39 + }, + "end": { + "line": 1, + "column": 51 + } + }, + "range": [ + 39, + 51 + ], + "key": { + "type": "Identifier", + "start": 39, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 39 + }, + "end": { + "line": 1, + "column": 43 + } + }, + "range": [ + 39, + 43 + ], + "name": "type" + }, + "value": { + "type": "Literal", + "start": 45, + "end": 51, + "loc": { + "start": { + "line": 1, + "column": 45 + }, + "end": { + "line": 1, + "column": 51 + } + }, + "range": [ + 45, + 51 + ], + "value": "json", + "raw": "\"json\"" + } + } + ] + } + ], + "sourceType": "module" +} \ No newline at end of file diff --git a/test/fixtures/attributes-assertions-dynamic-import/actual.js b/test/fixtures/attributes-assertions-dynamic-import/actual.js new file mode 100644 index 0000000..68c0de8 --- /dev/null +++ b/test/fixtures/attributes-assertions-dynamic-import/actual.js @@ -0,0 +1,2 @@ +import("./dynamic-package.json", { assert: { type: "json" } }); +import("./dynamic-package-with.json", { with: { type: "json" } }); diff --git a/test/fixtures/attributes-assertions-dynamic-import/expected.json b/test/fixtures/attributes-assertions-dynamic-import/expected.json new file mode 100644 index 0000000..e87e308 --- /dev/null +++ b/test/fixtures/attributes-assertions-dynamic-import/expected.json @@ -0,0 +1,442 @@ +{ + "type": "Program", + "start": 0, + "end": 131, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 0 + } + }, + "range": [ + 0, + 131 + ], + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 63, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 63 + } + }, + "range": [ + 0, + 63 + ], + "expression": { + "type": "ImportExpression", + "start": 0, + "end": 62, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 62 + } + }, + "range": [ + 0, + 62 + ], + "source": { + "type": "Literal", + "start": 7, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "range": [ + 7, + 31 + ], + "value": "./dynamic-package.json", + "raw": "\"./dynamic-package.json\"" + }, + "arguments": [ + { + "type": "ObjectExpression", + "start": 33, + "end": 61, + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 61 + } + }, + "range": [ + 33, + 61 + ], + "properties": [ + { + "type": "Property", + "start": 35, + "end": 59, + "loc": { + "start": { + "line": 1, + "column": 35 + }, + "end": { + "line": 1, + "column": 59 + } + }, + "range": [ + 35, + 59 + ], + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 35, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 35 + }, + "end": { + "line": 1, + "column": 41 + } + }, + "range": [ + 35, + 41 + ], + "name": "assert" + }, + "value": { + "type": "ObjectExpression", + "start": 43, + "end": 59, + "loc": { + "start": { + "line": 1, + "column": 43 + }, + "end": { + "line": 1, + "column": 59 + } + }, + "range": [ + 43, + 59 + ], + "properties": [ + { + "type": "Property", + "start": 45, + "end": 57, + "loc": { + "start": { + "line": 1, + "column": 45 + }, + "end": { + "line": 1, + "column": 57 + } + }, + "range": [ + 45, + 57 + ], + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 45, + "end": 49, + "loc": { + "start": { + "line": 1, + "column": 45 + }, + "end": { + "line": 1, + "column": 49 + } + }, + "range": [ + 45, + 49 + ], + "name": "type" + }, + "value": { + "type": "Literal", + "start": 51, + "end": 57, + "loc": { + "start": { + "line": 1, + "column": 51 + }, + "end": { + "line": 1, + "column": 57 + } + }, + "range": [ + 51, + 57 + ], + "value": "json", + "raw": "\"json\"" + }, + "kind": "init" + } + ] + }, + "kind": "init" + } + ] + } + ] + } + }, + { + "type": "ExpressionStatement", + "start": 64, + "end": 130, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 66 + } + }, + "range": [ + 64, + 130 + ], + "expression": { + "type": "ImportExpression", + "start": 64, + "end": 129, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 65 + } + }, + "range": [ + 64, + 129 + ], + "source": { + "type": "Literal", + "start": 71, + "end": 100, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 36 + } + }, + "range": [ + 71, + 100 + ], + "value": "./dynamic-package-with.json", + "raw": "\"./dynamic-package-with.json\"" + }, + "arguments": [ + { + "type": "ObjectExpression", + "start": 102, + "end": 128, + "loc": { + "start": { + "line": 2, + "column": 38 + }, + "end": { + "line": 2, + "column": 64 + } + }, + "range": [ + 102, + 128 + ], + "properties": [ + { + "type": "Property", + "start": 104, + "end": 126, + "loc": { + "start": { + "line": 2, + "column": 40 + }, + "end": { + "line": 2, + "column": 62 + } + }, + "range": [ + 104, + 126 + ], + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 104, + "end": 108, + "loc": { + "start": { + "line": 2, + "column": 40 + }, + "end": { + "line": 2, + "column": 44 + } + }, + "range": [ + 104, + 108 + ], + "name": "with" + }, + "value": { + "type": "ObjectExpression", + "start": 110, + "end": 126, + "loc": { + "start": { + "line": 2, + "column": 46 + }, + "end": { + "line": 2, + "column": 62 + } + }, + "range": [ + 110, + 126 + ], + "properties": [ + { + "type": "Property", + "start": 112, + "end": 124, + "loc": { + "start": { + "line": 2, + "column": 48 + }, + "end": { + "line": 2, + "column": 60 + } + }, + "range": [ + 112, + 124 + ], + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 112, + "end": 116, + "loc": { + "start": { + "line": 2, + "column": 48 + }, + "end": { + "line": 2, + "column": 52 + } + }, + "range": [ + 112, + 116 + ], + "name": "type" + }, + "value": { + "type": "Literal", + "start": 118, + "end": 124, + "loc": { + "start": { + "line": 2, + "column": 54 + }, + "end": { + "line": 2, + "column": 60 + } + }, + "range": [ + 118, + 124 + ], + "value": "json", + "raw": "\"json\"" + }, + "kind": "init" + } + ] + }, + "kind": "init" + } + ] + } + ] + } + } + ], + "sourceType": "module" +} \ No newline at end of file diff --git a/test/fixtures/attributes-assertions-export/actual.js b/test/fixtures/attributes-assertions-export/actual.js new file mode 100644 index 0000000..fc24322 --- /dev/null +++ b/test/fixtures/attributes-assertions-export/actual.js @@ -0,0 +1,2 @@ +export * from "./foo.json" assert { type: "json" } +export * from "./foo.json" with { type: "json" } diff --git a/test/fixtures/attributes-assertions-export/expected.json b/test/fixtures/attributes-assertions-export/expected.json new file mode 100644 index 0000000..335a80a --- /dev/null +++ b/test/fixtures/attributes-assertions-export/expected.json @@ -0,0 +1,228 @@ +{ + "type": "Program", + "start": 0, + "end": 100, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 0 + } + }, + "range": [ + 0, + 100 + ], + "body": [ + { + "type": "ExportAllDeclaration", + "start": 0, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 50 + } + }, + "range": [ + 0, + 50 + ], + "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": 36, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 36 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "range": [ + 36, + 48 + ], + "key": { + "type": "Identifier", + "start": 36, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 36 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "range": [ + 36, + 40 + ], + "name": "type" + }, + "value": { + "type": "Literal", + "start": 42, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 42 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "range": [ + 42, + 48 + ], + "value": "json", + "raw": "\"json\"" + } + } + ] + }, + { + "type": "ExportAllDeclaration", + "start": 51, + "end": 99, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 48 + } + }, + "range": [ + 51, + 99 + ], + "exported": null, + "source": { + "type": "Literal", + "start": 65, + "end": 77, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "range": [ + 65, + 77 + ], + "value": "./foo.json", + "raw": "\"./foo.json\"" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start": 85, + "end": 97, + "loc": { + "start": { + "line": 2, + "column": 34 + }, + "end": { + "line": 2, + "column": 46 + } + }, + "range": [ + 85, + 97 + ], + "key": { + "type": "Identifier", + "start": 85, + "end": 89, + "loc": { + "start": { + "line": 2, + "column": 34 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "range": [ + 85, + 89 + ], + "name": "type" + }, + "value": { + "type": "Literal", + "start": 91, + "end": 97, + "loc": { + "start": { + "line": 2, + "column": 40 + }, + "end": { + "line": 2, + "column": 46 + } + }, + "range": [ + 91, + 97 + ], + "value": "json", + "raw": "\"json\"" + } + } + ] + } + ], + "sourceType": "module" +} \ No newline at end of file diff --git a/test/fixtures/attributes-assertions-import/actual.js b/test/fixtures/attributes-assertions-import/actual.js new file mode 100644 index 0000000..19f7c35 --- /dev/null +++ b/test/fixtures/attributes-assertions-import/actual.js @@ -0,0 +1,2 @@ +import * as a from "./foo.json" assert { type: "json" }; +import b from "./foo.json" with { type: "json" }; diff --git a/test/fixtures/attributes-assertions-import/expected.json b/test/fixtures/attributes-assertions-import/expected.json new file mode 100644 index 0000000..0404456 --- /dev/null +++ b/test/fixtures/attributes-assertions-import/expected.json @@ -0,0 +1,308 @@ +{ + "type": "Program", + "start": 0, + "end": 107, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 0 + } + }, + "range": [ + 0, + 107 + ], + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 56, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 56 + } + }, + "range": [ + 0, + 56 + ], + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "start": 7, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "range": [ + 7, + 13 + ], + "local": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "range": [ + 12, + 13 + ], + "name": "a" + } + } + ], + "source": { + "type": "Literal", + "start": 19, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "range": [ + 19, + 31 + ], + "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\"" + } + } + ] + }, + { + "type": "ImportDeclaration", + "start": 57, + "end": 106, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 49 + } + }, + "range": [ + 57, + 106 + ], + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start": 64, + "end": 65, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "range": [ + 64, + 65 + ], + "local": { + "type": "Identifier", + "start": 64, + "end": 65, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "range": [ + 64, + 65 + ], + "name": "b" + } + } + ], + "source": { + "type": "Literal", + "start": 71, + "end": 83, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "range": [ + 71, + 83 + ], + "value": "./foo.json", + "raw": "\"./foo.json\"" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start": 91, + "end": 103, + "loc": { + "start": { + "line": 2, + "column": 34 + }, + "end": { + "line": 2, + "column": 46 + } + }, + "range": [ + 91, + 103 + ], + "key": { + "type": "Identifier", + "start": 91, + "end": 95, + "loc": { + "start": { + "line": 2, + "column": 34 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "range": [ + 91, + 95 + ], + "name": "type" + }, + "value": { + "type": "Literal", + "start": 97, + "end": 103, + "loc": { + "start": { + "line": 2, + "column": 40 + }, + "end": { + "line": 2, + "column": 46 + } + }, + "range": [ + 97, + 103 + ], + "value": "json", + "raw": "\"json\"" + } + } + ] + } + ], + "sourceType": "module" +} \ No newline at end of file diff --git a/test/index.js b/test/index.js index 841a7f3..ac64142 100644 --- a/test/index.js +++ b/test/index.js @@ -2,7 +2,8 @@ import { expect } from 'chai'; import { join } from 'path'; import { readdirSync, writeFileSync, statSync, readFileSync } from 'fs'; -import plugin from './test-plugin.js'; +import { importAttributes, importAssertions, importAttributesOrAssertions } from "../src/index.js"; +import testPlugin from './test-plugin.js'; const FIXTURE_PATH = join(__dirname, 'fixtures'); @@ -17,7 +18,12 @@ describe('acorn-import-attributes', () => { it(`should parse ${folderName}`, () => { const actual = readFileSync(join(FIXTURE_PATH, folderName, 'actual.js'), 'utf8'); const expectedFile = join(FIXTURE_PATH, folderName, 'expected.json'); - const result = plugin(actual); + const plugin = folderName.startsWith("assertions") + ? importAssertions + : folderName.startsWith("attributes-assertions") + ? importAttributesOrAssertions + : importAttributes; + const result = testPlugin(actual, plugin); let expected; // This lets us auto-generate test expected files easily: diff --git a/test/test-plugin.js b/test/test-plugin.js index 7dc3b32..928d57e 100644 --- a/test/test-plugin.js +++ b/test/test-plugin.js @@ -1,11 +1,9 @@ import * as acorn from 'acorn'; -import { importAttributes } from '../src/index'; -const Parser = acorn.Parser.extend(importAttributes); - -export default function testPlugin(code) { +export default function testPlugin(code, plugin) { let result; try { + const Parser = acorn.Parser.extend(plugin); result = Parser.parse(code, { ecmaVersion: 12, locations: true,