diff --git a/README.md b/README.md index 76ed32f..a84fddf 100644 --- a/README.md +++ b/README.md @@ -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_. diff --git a/package.json b/package.json index 415f69f..789a590 100644 --- a/package.json +++ b/package.json @@ -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", @@ -60,5 +60,8 @@ "@vue/compiler-sfc": { "optional": true } + }, + "resolutions": { + "@types/babel__generator": "7.6.8" } } diff --git a/src/preprocessors/preprocessor.ts b/src/preprocessors/preprocessor.ts index bf5da66..cd1f89b 100644 --- a/src/preprocessors/preprocessor.ts +++ b/src/preprocessors/preprocessor.ts @@ -16,6 +16,7 @@ export function preprocessor(code: string, options: PrettierOptions) { importOrderSeparation, importOrderGroupNamespaceSpecifiers, importOrderSortSpecifiers, + importOrderImportAttributesKeyword, } = options; const parserOptions: ParserOptions = { @@ -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, + }); } diff --git a/src/utils/get-code-from-ast.ts b/src/utils/get-code-from-ast.ts index 2856266..a611da4 100644 --- a/src/utils/get-code-from-ast.ts +++ b/src/utils/get-code-from-ast.ts @@ -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. @@ -15,6 +16,7 @@ export const getCodeFromAst = ( directives: Directive[], originalCode: string, interpreter?: InterpreterDirective | null, + options?: Pick ) => { const allCommentsFromImports = getAllCommentsFromNodes(nodes); @@ -48,7 +50,7 @@ export const getCodeFromAst = ( }, }); - const { code } = generate(newAST); + const { code } = generate(newAST, { importAttributesKeyword: options?.importOrderImportAttributesKeyword }); return ( code.replace( diff --git a/types/index.d.ts b/types/index.d.ts index 4c5f02f..46bc56e 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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. + */ + importOrderImportAttributesKeyword?: 'assert' | 'with' | 'with-legacy'; } export type PrettierConfig = PluginConfig & Config; diff --git a/yarn.lock b/yarn.lock index cecb3f9..a54715f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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== @@ -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" @@ -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" @@ -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"