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: import-attributes support #712

Merged
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
5 changes: 4 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ node_modules/*
local/*
vendor/*
dist/*
src/tests/fixtures/deprecated/es5.js
src/tests/fixtures/deprecated/es5.js
src/tests/fixtures/syntax/import.js
src/tests/fixtures/syntax/export.js
src/tests/fixtures/tree/es6.js
12,732 changes: 7,706 additions & 5,026 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@babel/parser": "^7.16.4",
"@babel/preset-env": "^7.14.4",
"acorn": "^8.6.0",
"acorn-import-attributes": "^1.9.5",
"astravel": "^0.5.0",
"ava": "^3.15.0",
"babel-preset-minify": "^0.5.1",
Expand Down
39 changes: 38 additions & 1 deletion src/astring.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ export const GENERATOR = {
},
ImportDeclaration(node, state) {
state.write('import ')
const { specifiers } = node
const { specifiers, attributes } = node
const { length } = specifiers
// TODO: Once babili is fixed, put this after condition
// https://github.com/babel/babili/issues/430
Expand Down Expand Up @@ -583,8 +583,23 @@ export const GENERATOR = {
state.write(' from ')
}
this.Literal(node.source, state)

if (attributes && attributes.length > 0) {
state.write(' with { ')
for (let i = 0; i < attributes.length; i++) {
this.ImportAttribute(attributes[i], state)
if (i < attributes.length - 1) state.write(', ')
}

state.write(' }')
}
state.write(';')
},
ImportAttribute(node, state) {
this.Identifier(node.key, state)
state.write(': ')
this.Literal(node.value, state)
},
ImportExpression(node, state) {
state.write('import(')
this[node.source.type](node.source, state)
Expand Down Expand Up @@ -629,6 +644,17 @@ export const GENERATOR = {
state.write(' from ')
this.Literal(node.source, state)
}

if (node.attributes && node.attributes.length > 0) {
state.write(' with { ')
for (let i = 0; i < node.attributes.length; i++) {
this.ImportAttribute(node.attributes[i], state)
if (i < node.attributes.length - 1) state.write(', ')
}

state.write(' }')
}

state.write(';')
}
},
Expand All @@ -639,6 +665,17 @@ export const GENERATOR = {
state.write('export * from ')
}
this.Literal(node.source, state)

if (node.attributes && node.attributes.length > 0) {
state.write(' with { ')
for (let i = 0; i < node.attributes.length; i++) {
this.ImportAttribute(node.attributes[i], state)
if (i < node.attributes.length - 1) state.write(', ')
}

state.write(' }')
}

state.write(';')
},
MethodDefinition(node, state) {
Expand Down
38 changes: 29 additions & 9 deletions src/tests/astring.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import fs from 'fs'
import test from 'ava'
import path from 'path'
import { parse } from 'acorn'
import { Parser } from 'acorn'
import { importAttributesOrAssertions } from 'acorn-import-attributes'
import * as astravel from 'astravel'
import { pick } from 'lodash'

Expand All @@ -12,6 +13,8 @@ const FIXTURES_FOLDER = path.join(__dirname, 'fixtures')

const ecmaVersion = 13

const parser = Parser.extend(importAttributesOrAssertions)

const stripLocation = astravel.makeTraveler({
go(node, state) {
delete node.start
Expand All @@ -27,6 +30,23 @@ const stripLocation = astravel.makeTraveler({
// Always walk through value, regardless of `node.shorthand` flag
this.go(node.value, state)
},
// astravel does not support import attributes yet
ImportDeclaration(node, state) {
const { specifiers } = node,
{ length } = specifiers
for (let i = 0; i < length; i++) {
this.go(specifiers[i], state)
}
const { attributes } = node
if (attributes != null)
for (let i = 0; i < attributes.length; i++) this.go(attributes[i], state)

this.go(node.source, state)
},
ImportAttribute(node, state) {
this.go(node.key, state)
this.go(node.value, state)
},
})

test('Syntax check', (assert) => {
Expand All @@ -38,7 +58,7 @@ test('Syntax check', (assert) => {
}
files.forEach((filename) => {
const code = readFile(path.join(dirname, filename))
const ast = parse(code, options)
const ast = parser.parse(code, options)
assert.is(
generate(ast),
code,
Expand All @@ -57,9 +77,9 @@ test('Tree comparison', (assert) => {
}
files.forEach((filename) => {
const code = readFile(path.join(dirname, filename))
const ast = parse(code, options)
const ast = parser.parse(code, options)
stripLocation.go(ast)
const formattedAst = parse(generate(ast), options)
const formattedAst = parser.parse(generate(ast), options)
stripLocation.go(formattedAst)
assert.deepEqual(
formattedAst,
Expand All @@ -76,7 +96,7 @@ test('Deprecated syntax check', (assert) => {
files.forEach((filename) => {
const code = readFile(path.join(dirname, filename))
const version = parseInt(filename.substring(2, filename.length - 3))
const ast = parse(code, { ecmaVersion: version })
const ast = parser.parse(code, { ecmaVersion: version })
assert.is(generate(ast), code, 'es' + version)
})
})
Expand All @@ -89,7 +109,7 @@ test('Output stream', (assert) => {
this.buffer += code
},
}
const ast = parse(code, {
const ast = parser.parse(code, {
ecmaVersion,
})
const result = generate(ast, {
Expand All @@ -108,7 +128,7 @@ test('Comment generation', (assert) => {
files.forEach((filename) => {
const code = readFile(path.join(dirname, filename))
const comments = []
const ast = parse(code, {
const ast = parser.parse(code, {
ecmaVersion,
locations: true,
onComment: comments,
Expand Down Expand Up @@ -150,7 +170,7 @@ test('Source map generation', (assert) => {
})
},
}
const ast = parse(code, options)
const ast = parser.parse(code, options)
generate(ast, {
sourceMap,
})
Expand Down Expand Up @@ -185,7 +205,7 @@ test('Source map generation with comments', (assert) => {
},
}
const comments = []
const ast = parse(code, {
const ast = parser.parse(code, {
ecmaVersion,
comments: true,
locations: true,
Expand Down
3 changes: 3 additions & 0 deletions src/tests/fixtures/other/a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"content": "For testing import-attributes"
}
1 change: 1 addition & 0 deletions src/tests/fixtures/syntax/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export default i = 42;
export var j = 42;
export let k = 42;
export function l() {}
export {val} from '../other/a.json' with { type: "json" };
2 changes: 2 additions & 0 deletions src/tests/fixtures/syntax/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ import * as d from "module";
import e, {f as g, h as i, j} from "module";
import {k as l, m} from "module";
import {n, o as p} from "module";
import * as q from "../other/a.json" with { type: "json" };
import r from "../other/a.json" with { type: "json" };
import("module");
const x = import("module");
2 changes: 2 additions & 0 deletions src/tests/fixtures/tree/es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import i4, * as i5 from "module";
import i6, {} from "module";
import i7, { i8, var as i9 } from "module";
import "module";
import * as q from "./a.json" with { type: "json" };
import r from "./a.json" with { type: "json" };

export * from "module";
export {} from "module";
Expand Down
10 changes: 7 additions & 3 deletions src/tests/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import test from 'ava'
import fs from 'fs'
import path from 'path'
import normalizeNewline from 'normalize-newline'
import { parse } from 'acorn'
import { Parser } from 'acorn'
import { importAttributes } from 'acorn-import-attributes'
import * as astravel from 'astravel'
import glob from 'glob'

Expand Down Expand Up @@ -40,12 +41,15 @@ test('Script tests', (assert) => {
const code = normalizeNewline(fs.readFileSync(fileName, 'utf8'))
let ast
try {
ast = parse(code, options)
ast = Parser.extend(importAttributes).parse(code, options)
} catch (error) {
return
}
stripLocation.go(ast)
const formattedAst = parse(generate(ast), options)
const formattedAst = Parser.extend(importAttributes).parse(
generate(ast),
options,
)
stripLocation.go(formattedAst)
assert.deepEqual(formattedAst, ast, fileName)
} catch (error) {
Expand Down