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: support importOrderImportAttributesKeyword #273

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,20 @@ with options as a JSON string of the plugin array:
importOrderParserPlugins: []
```

#### `importOrderImportAttributesKeyword`

**type**: `'assert' | 'with' | 'with-legacy'`

The import attributes/assertions syntax to use. "with" for import "..." with { type: "json" },
"assert" for import "..." assert { type: "json" }, and "with-legacy" for import "..." with type: "json".

```
"importOrderImportAttributesKeyword": 'with'
```

_Default behavior:_ When not specified, @babel/generator will try to match the style in the input code based on the AST shape.


### How does import sort work ?

The plugin extracts the imports which are defined in `importOrder`. These imports are considered as _local imports_.
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"license": "Apache-2.0",
"dependencies": {
"@babel/generator": "7.17.7",
"@babel/generator": "7.23.6",
"@babel/parser": "^7.20.5",
"@babel/traverse": "7.23.2",
"@babel/types": "7.17.0",
Expand All @@ -60,5 +60,8 @@
"@vue/compiler-sfc": {
"optional": true
}
},
"resolutions": {
"@types/babel__generator": "7.6.8"
}
}
5 changes: 4 additions & 1 deletion src/preprocessors/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function preprocessor(code: string, options: PrettierOptions) {
importOrderSeparation,
importOrderGroupNamespaceSpecifiers,
importOrderSortSpecifiers,
importOrderImportAttributesKeyword,
} = options;

const parserOptions: ParserOptions = {
Expand Down Expand Up @@ -44,5 +45,7 @@ export function preprocessor(code: string, options: PrettierOptions) {
importOrderSortSpecifiers,
});

return getCodeFromAst(allImports, directives, code, interpreter);
return getCodeFromAst(allImports, directives, code, interpreter, {
importOrderImportAttributesKeyword,
});
}
4 changes: 3 additions & 1 deletion src/utils/get-code-from-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Directive, InterpreterDirective, Statement, file } from '@babel/types';
import { newLineCharacters } from '../constants';
import { getAllCommentsFromNodes } from './get-all-comments-from-nodes';
import { removeNodesFromOriginalCode } from './remove-nodes-from-original-code';
import { PrettierOptions } from '../types';

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

Expand Down Expand Up @@ -48,7 +50,7 @@ export const getCodeFromAst = (
},
});

const { code } = generate(newAST);
const { code } = generate(newAST, { importAttributesKeyword: options?.importOrderImportAttributesKeyword });

return (
code.replace(
Expand Down
13 changes: 13 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ used to order imports within each match group.
* @default ["typescript", "jsx"]
*/
importOrderParserPlugins?: ImportOrderParserPlugin[];


/**
* The import attributes/assertions syntax to use. "with" for import "..." with { type: "json" },
* "assert" for import "..." assert { type: "json" }, and "with-legacy" for import "..." with type: "json".
*
* ```
* "importOrderImportAttributesKeyword": 'with',
* ```
*
* _Default behavior:_ When not specified, @babel/generator will try to match the style in the input code based on the AST shape.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you find this default behavior not to be reliable enough?

*/
importOrderImportAttributesKeyword?: 'assert' | 'with' | 'with-legacy';
}

export type PrettierConfig = PluginConfig & Config;
34 changes: 29 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,17 @@
json5 "^2.2.1"
semver "^6.3.0"

"@babel/generator@7.17.7", "@babel/generator@^7.17.3":
"@babel/generator@7.23.6":
version "7.23.6"
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.23.6.tgz#9e1fca4811c77a10580d17d26b57b036133f3c2e"
integrity sha512-qrSfCYxYQB5owCmGLbl8XRpX1ytXlpueOb0N0UmQwA073KZxejgQTzAmJezxvpwQD9uGtK2shHdi55QT+MbjIw==
dependencies:
"@babel/types" "^7.23.6"
"@jridgewell/gen-mapping" "^0.3.2"
"@jridgewell/trace-mapping" "^0.3.17"
jsesc "^2.5.1"

"@babel/generator@^7.17.3":
version "7.17.7"
resolved "https://registry.npmjs.org/@babel/generator/-/generator-7.17.7.tgz"
integrity sha512-oLcVCTeIFadUoArDTwpluncplrYBmTCCZZgXCbgNGvOBBiSDDK3eWO4b/+eOTli5tKv1lg+a5/NAXg+nTcei1w==
Expand Down Expand Up @@ -186,6 +196,11 @@
resolved "https://registry.yarnpkg.com/@babel/helper-string-parser/-/helper-string-parser-7.22.5.tgz#533f36457a25814cf1df6488523ad547d784a99f"
integrity sha512-mM4COjgZox8U+JcXQwPijIZLElkgEpO5rsERVDJTc2qfCDfERyob6k5WegS14SX18IIjv+XD+GrqNumY5JRCDw==

"@babel/helper-string-parser@^7.23.4":
version "7.23.4"
resolved "https://registry.yarnpkg.com/@babel/helper-string-parser/-/helper-string-parser-7.23.4.tgz#9478c707febcbbe1ddb38a3d91a2e054ae622d83"
integrity sha512-803gmbQdqwdf4olxrX4AJyFBV/RTr3rSmOj0rKwesmzlfhYNDEs+/iOcznzpNWlJlIlTJC2QfPFcHB6DlzdVLQ==

"@babel/helper-validator-identifier@^7.16.7", "@babel/helper-validator-identifier@^7.18.6", "@babel/helper-validator-identifier@^7.19.1":
version "7.19.1"
resolved "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.19.1.tgz"
Expand Down Expand Up @@ -414,6 +429,15 @@
"@babel/helper-validator-identifier" "^7.22.20"
to-fast-properties "^2.0.0"

"@babel/types@^7.23.6":
version "7.23.6"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.23.6.tgz#be33fdb151e1f5a56877d704492c240fc71c7ccd"
integrity sha512-+uarb83brBzPKN38NX1MkB6vb6+mwvR6amUulqAE7ccQw1pEl+bCia9TbdG1lsnFP7lZySvUn37CHyXQdfTwzg==
dependencies:
"@babel/helper-string-parser" "^7.23.4"
"@babel/helper-validator-identifier" "^7.22.20"
to-fast-properties "^2.0.0"

"@bcoe/v8-coverage@^0.2.3":
version "0.2.3"
resolved "https://registry.npmjs.org/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz"
Expand Down Expand Up @@ -694,10 +718,10 @@
"@types/babel__template" "*"
"@types/babel__traverse" "*"

"@types/babel__generator@*":
version "7.6.4"
resolved "https://registry.npmjs.org/@types/babel__generator/-/babel__generator-7.6.4.tgz"
integrity sha512-tFkciB9j2K755yrTALxD44McOrk+gfpIpvC3sxHjRawj6PfnQxrse4Clq5y/Rq+G3mrBurMax/lG8Qn2t9mSsg==
"@types/babel__generator@*", "@types/babel__generator@7.6.8":
version "7.6.8"
resolved "https://registry.yarnpkg.com/@types/babel__generator/-/babel__generator-7.6.8.tgz#f836c61f48b1346e7d2b0d93c6dacc5b9535d3ab"
integrity sha512-ASsj+tpEDsEiFr1arWrlN6V3mdfjRMZt6LtK/Vp/kreFLnr5QH5+DhvD5nINYZXzwJvXeGq+05iUXcAzVrqWtw==
dependencies:
"@babel/types" "^7.0.0"

Expand Down
Loading