Skip to content

Commit 3341a50

Browse files
authored
feat: support importOrderImportAttributesKeyword (#273)
1 parent f545d2d commit 3341a50

File tree

10 files changed

+109
-4
lines changed

10 files changed

+109
-4
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,22 @@ import './polyfills';
240240
import foo from 'foo'
241241
```
242242

243+
#### `importOrderImportAttributesKeyword`
244+
245+
**type**: `'assert' | 'with' | 'with-legacy'`
246+
247+
The import attributes/assertions syntax:
248+
- `with`: `import "..." with { type: "json" }`
249+
- `assert`: `import "..." assert { type: "json" }`
250+
- `with-legacy`: `import "..." with type: "json"`.
251+
252+
```json
253+
"importOrderImportAttributesKeyword": 'with'
254+
```
255+
256+
_Default behavior:_ When not specified, @babel/generator will try to match the style in the input code based on the AST shape.
257+
258+
243259
### How does import sort work ?
244260

245261
The plugin extracts the imports which are defined in `importOrder`. These imports are considered as _local imports_.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,8 @@
7676
"svelte": {
7777
"optional": true
7878
}
79+
},
80+
"resolutions": {
81+
"@types/babel__generator": "7.6.8"
7982
}
8083
}

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ const options = {
5757
default: true,
5858
description: 'Should side effects be sorted?',
5959
},
60+
importOrderImportAttributesKeyword: {
61+
type: 'string',
62+
category: 'Global',
63+
default: 'with',
64+
description: 'Provide a keyword for import attributes',
65+
}
6066
};
6167

6268
module.exports = {

src/preprocessors/preprocessor.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export function preprocessor(code: string, options: PrettierOptions) {
1717
importOrderGroupNamespaceSpecifiers,
1818
importOrderSortSpecifiers,
1919
importOrderSideEffects,
20+
importOrderImportAttributesKeyword,
2021
} = options;
2122

2223
const parserOptions: ParserOptions = {
@@ -46,5 +47,7 @@ export function preprocessor(code: string, options: PrettierOptions) {
4647
importOrderSideEffects,
4748
});
4849

49-
return getCodeFromAst(allImports, directives, code, interpreter);
50+
return getCodeFromAst(allImports, directives, code, interpreter, {
51+
importOrderImportAttributesKeyword,
52+
});
5053
}

src/utils/get-code-from-ast.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Directive, InterpreterDirective, Statement, file } from '@babel/types';
44
import { newLineCharacters } from '../constants';
55
import { getAllCommentsFromNodes } from './get-all-comments-from-nodes';
66
import { removeNodesFromOriginalCode } from './remove-nodes-from-original-code';
7+
import { PrettierOptions } from '../types';
78

89
/**
910
* This function generate a code string from the passed nodes.
@@ -15,6 +16,7 @@ export const getCodeFromAst = (
1516
directives: Directive[],
1617
originalCode: string,
1718
interpreter?: InterpreterDirective | null,
19+
options?: Pick<PrettierOptions, 'importOrderImportAttributesKeyword'>
1820
) => {
1921
const allCommentsFromImports = getAllCommentsFromNodes(nodes);
2022

@@ -49,7 +51,7 @@ export const getCodeFromAst = (
4951
},
5052
});
5153

52-
const { code } = generate(newAST);
54+
const { code } = generate(newAST, { importAttributesKeyword: options?.importOrderImportAttributesKeyword });
5355

5456
return (
5557
code.replace(
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`imports-with-attributes-keyword.ts - typescript-verify: imports-with-attributes-keyword.ts 1`] = `
4+
// I am top level comment in this file.
5+
import thirdParty0 from "third-party0";
6+
import something3 from "@core/something3";
7+
import thirdDisco0 from "third-disco0";
8+
import otherthing3 from "@core/otherthing3";
9+
import { a } from "b" with { type: "json" };
10+
11+
import anotherSameLevelRelativePath3 from "./anotherSameLevelRelativePath3";
12+
import something0 from "@core/something0";
13+
14+
import { b } from "r" with { type: "json" };
15+
16+
17+
function add(a,b) {
18+
return a + b;
19+
}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20+
// I am top level comment in this file.
21+
import { a } from "b" with { type: "json" };
22+
import { b } from "r" with { type: "json" };
23+
import thirdDisco0 from "third-disco0";
24+
import thirdParty0 from "third-party0";
25+
import otherthing3 from "@core/otherthing3";
26+
import something0 from "@core/something0";
27+
import something3 from "@core/something3";
28+
import anotherSameLevelRelativePath3 from "./anotherSameLevelRelativePath3";
29+
30+
function add(a, b) {
31+
return a + b;
32+
}
33+
34+
`;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// I am top level comment in this file.
2+
import thirdParty0 from "third-party0";
3+
import something3 from "@core/something3";
4+
import thirdDisco0 from "third-disco0";
5+
import otherthing3 from "@core/otherthing3";
6+
import { a } from "b" with { type: "json" };
7+
8+
import anotherSameLevelRelativePath3 from "./anotherSameLevelRelativePath3";
9+
import something0 from "@core/something0";
10+
11+
import { b } from "r" with { type: "json" };
12+
13+
14+
function add(a,b) {
15+
return a + b;
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
run_spec(__dirname, ["typescript"], {
2+
importOrder: ['^@core/(.*)$', '^@server/(.*)', '^@ui/(.*)$', '^[./]'],
3+
importOrderSeparation: false,
4+
importOrderSideEffects: false,
5+
importOrderParserPlugins: ['typescript'],
6+
importOrderImportAttributesKeyword: 'with',
7+
});

types/index.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ used to order imports within each match group.
9292
* @default ["typescript", "jsx"]
9393
*/
9494
importOrderParserPlugins?: ImportOrderParserPlugin[];
95+
96+
97+
/**
98+
* The import attributes/assertions syntax to use. "with" for import "..." with { type: "json" },
99+
* "assert" for import "..." assert { type: "json" }, and "with-legacy" for import "..." with type: "json".
100+
*
101+
* ```
102+
* "importOrderImportAttributesKeyword": 'with',
103+
* ```
104+
*
105+
* _Default behavior:_ When not specified, @babel/generator will try to match the style in the input code based on the AST shape.
106+
*/
107+
importOrderImportAttributesKeyword?: 'assert' | 'with' | 'with-legacy';
95108
}
96109

97110
export type PrettierConfig = PluginConfig & Config;

yarn.lock

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@
9191
"@babel/helper-validator-identifier" "^7.25.9"
9292
"@babel/traverse" "^7.25.9"
9393

94-
"@babel/helper-plugin-utils@^7.0.0", "@babel/helper-plugin-utils@^7.10.4", "@babel/helper-plugin-utils@^7.12.13", "@babel/helper-plugin-utils@^7.14.5", "@babel/helper-plugin-utils@^7.25.9", "@babel/helper-plugin-utils@^7.8.0":
94+
"@babel/helper-plugin-utils@^7.0.0", "@babel/helper-plugin-utils@^7.10.4", "@babel/helper-plugin-utils@^7.12.13", "@babel/helper-plugin-utils@^7.14.5", "@babel/helper-plugin-utils@^7.8.0":
95+
version "7.16.7"
96+
resolved "https://registry.npmjs.org/@babel/helper-plugin-utils/-/helper-plugin-utils-7.16.7.tgz"
97+
integrity sha512-Qg3Nk7ZxpgMrsox6HreY1ZNKdBq7K72tDSliA6dCl5f007jR4ne8iD5UzuNnCJH2xBf2BEEVGr+/OL6Gdp7RxA==
98+
99+
"@babel/helper-plugin-utils@^7.25.9":
95100
version "7.25.9"
96101
resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.25.9.tgz#9cbdd63a9443a2c92a725cca7ebca12cc8dd9f46"
97102
integrity sha512-kSMlyUVdWe25rEsRGviIgOWnoT/nfABVWlqt9N19/dIPWViAOW2s9wznP5tURbs/IDuNk4gPy3YdYRgH3uxhBw==
@@ -570,7 +575,7 @@
570575
"@types/babel__template" "*"
571576
"@types/babel__traverse" "*"
572577

573-
"@types/babel__generator@*":
578+
"@types/babel__generator@*", "@types/[email protected]":
574579
version "7.6.8"
575580
resolved "https://registry.yarnpkg.com/@types/babel__generator/-/babel__generator-7.6.8.tgz#f836c61f48b1346e7d2b0d93c6dacc5b9535d3ab"
576581
integrity sha512-ASsj+tpEDsEiFr1arWrlN6V3mdfjRMZt6LtK/Vp/kreFLnr5QH5+DhvD5nINYZXzwJvXeGq+05iUXcAzVrqWtw==

0 commit comments

Comments
 (0)