Skip to content

Commit bd895ad

Browse files
committed
feature: @putout/printer: ImportAttribute: improve
1 parent 87c822f commit bd895ad

5 files changed

Lines changed: 22 additions & 23 deletions

File tree

lib/tokenize/statements/import-declaration/fixture/import-assertions-fix.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import json from './foo.json' assert { type: 'json' };
1+
import json from './foo.json' assert {
2+
type: 'json',
3+
};
24

35
import('./foo.json', {
46
assert: {

lib/tokenize/statements/import-declaration/fixture/import-assertions.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import json from './foo.json' assert { type: 'json' };
1+
import json from './foo.json' assert {
2+
type: 'json',
3+
};
24

35
import('./foo.json', {
46
assert: {

lib/tokenize/statements/import-declaration/fixture/import-attributes.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import json from './foo.json' with { type: 'json' };
1+
import json from './foo.json' with {
2+
type: 'json',
3+
hello: 'world',
4+
};
25

36
import('./foo.json', {
47
with: {

lib/tokenize/statements/import-declaration/import-attribute.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,19 @@ module.exports.ImportAttribute = (path, {print}) => {
55
print(':');
66
print.space();
77
print('__value');
8+
print(',');
89
};
910

10-
module.exports.maybePrintAttributes = (path, {write, traverse}) => {
11+
module.exports.maybePrintAttributes = (path, printer) => {
1112
if (isAssertions(path))
12-
return printAttributes(path, {
13-
write,
14-
traverse,
15-
16-
keyword: 'assert',
17-
});
13+
return printAttributes(path, 'assert', printer);
1814

19-
printAttributes(path, {
20-
write,
21-
traverse,
22-
23-
keyword: 'with',
24-
});
15+
printAttributes(path, 'with', printer);
2516
};
2617

2718
const isAssertions = (path) => path.node.extra?.deprecatedAssertSyntax;
2819

29-
function printAttributes(path, {write, traverse, keyword}) {
20+
function printAttributes(path, keyword, {write, traverse, indent}) {
3021
const attributes = path.get('attributes');
3122

3223
if (!attributes.length)
@@ -36,12 +27,16 @@ function printAttributes(path, {write, traverse, keyword}) {
3627
write.space();
3728

3829
write('{');
39-
write.space();
30+
write.breakline();
31+
indent.inc();
4032

4133
for (const attr of attributes) {
34+
indent();
4235
traverse(attr);
36+
write.newline();
4337
}
4438

45-
write.space();
39+
indent.dec();
4640
write('}');
4741
}
42+

lib/tokenize/statements/import-declaration/import-declaration.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,7 @@ module.exports.ImportDeclaration = {
102102
print.space();
103103

104104
print('__source');
105-
maybePrintAttributes(path, {
106-
write,
107-
traverse,
108-
});
105+
maybePrintAttributes(path, printer);
109106
print(';');
110107

111108
if (isNext(path))

0 commit comments

Comments
 (0)