@1adybug/prettier-plugin-sort-imports 0.0.20 → 0.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +36 -22
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1,12 +1,13 @@
1
1
  import { createRequire } from "module";
2
2
  import { relative } from "path";
3
- import { parse } from "@babel/parser";
3
+ import { format } from "prettier";
4
+ import { parse as parser_parse } from "@babel/parser";
4
5
  import traverse from "@babel/traverse";
5
6
  const analyzer_traverse = "function" == typeof traverse ? traverse : traverse["default"];
6
7
  function analyzeUsedIdentifiers(code) {
7
8
  const usedIdentifiers = new Set();
8
9
  try {
9
- const ast = parse(code, {
10
+ const ast = parser_parse(code, {
10
11
  sourceType: "module",
11
12
  plugins: [
12
13
  "typescript",
@@ -174,7 +175,7 @@ function formatImportStatements(statements) {
174
175
  function parseImports(code, filepath) {
175
176
  const hasImportOrExport = /^\s*(import|export)\s/m.test(code);
176
177
  if (!hasImportOrExport) return [];
177
- const ast = parse(code, {
178
+ const ast = parser_parse(code, {
178
179
  sourceType: "module",
179
180
  plugins: [
180
181
  "typescript",
@@ -618,26 +619,25 @@ function preprocessImports(text, options, config = {}) {
618
619
  const { parsers: { babel } } = src_require("prettier/parser-babel");
619
620
  const { parsers: { typescript } } = src_require("prettier/parser-typescript");
620
621
  const { parsers: { "babel-ts": babelTs } } = src_require("prettier/parser-babel");
622
+ const PROCESSING_MARKER = Symbol("prettier-plugin-sort-imports-processing");
621
623
  function createCombinedPreprocess(parserName, config) {
622
- return function(text, options) {
623
- const otherPlugins = config.otherPlugins || [];
624
- if (0 === otherPlugins.length) return preprocessImports(text, options, config);
625
- const prettierOptions = config.prettierOptions || {};
626
- const mergedOptions = {
627
- ...options,
628
- ...prettierOptions
629
- };
630
- const preprocessFunctions = [];
631
- preprocessFunctions.push((text, options)=>preprocessImports(text, options, config));
632
- for (const plugin of otherPlugins){
633
- const parser = plugin?.parsers?.[parserName];
634
- if (parser?.preprocess && "function" == typeof parser.preprocess) preprocessFunctions.push(parser.preprocess);
635
- }
636
- let processedText = text;
637
- for (const preprocess of preprocessFunctions)try {
638
- processedText = preprocess(processedText, mergedOptions);
624
+ const otherPlugins = config.otherPlugins || [];
625
+ const pluginsWithPreprocess = otherPlugins.filter((plugin)=>{
626
+ const parser = plugin?.parsers?.[parserName];
627
+ return parser?.preprocess && "function" == typeof parser.preprocess;
628
+ });
629
+ return async function(text, options) {
630
+ if (options[PROCESSING_MARKER]) return text;
631
+ let processedText = preprocessImports(text, options, config);
632
+ if (0 === pluginsWithPreprocess.length) return processedText;
633
+ try {
634
+ processedText = await format(processedText, {
635
+ ...options,
636
+ plugins: pluginsWithPreprocess,
637
+ [PROCESSING_MARKER]: true
638
+ });
639
639
  } catch (error) {
640
- console.warn("Plugin preprocess failed:", error instanceof Error ? error.message : String(error));
640
+ console.warn("Failed to apply other plugins preprocess:", error instanceof Error ? error.message : String(error));
641
641
  }
642
642
  return processedText;
643
643
  };
@@ -685,16 +685,30 @@ function createPluginInstance(config = {}) {
685
685
  let merged = {
686
686
  ...baseParser
687
687
  };
688
+ const transformASTFunctions = [];
688
689
  for (const plugin of otherPlugins){
689
690
  const otherParser = plugin?.parsers?.[parserName];
690
691
  if (otherParser) {
691
- const { preprocess, ...otherAttrs } = otherParser;
692
+ if ("function" == typeof otherParser.__transformAST) transformASTFunctions.push(otherParser.__transformAST);
693
+ const { preprocess, parse, __transformAST, ...otherAttrs } = otherParser;
692
694
  merged = {
693
695
  ...merged,
694
696
  ...otherAttrs
695
697
  };
696
698
  }
697
699
  }
700
+ if (transformASTFunctions.length > 0) {
701
+ const originalParse = baseParser.parse;
702
+ merged.parse = function(text, options) {
703
+ let ast = originalParse(text, options);
704
+ for (const transformAST of transformASTFunctions)try {
705
+ ast = transformAST(ast, options);
706
+ } catch (error) {
707
+ console.warn("Plugin transformAST failed:", error instanceof Error ? error.message : String(error));
708
+ }
709
+ return ast;
710
+ };
711
+ }
698
712
  merged.preprocess = createCombinedPreprocess(parserName, config);
699
713
  mergedParsers[parserName] = merged;
700
714
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1adybug/prettier-plugin-sort-imports",
3
- "version": "0.0.20",
3
+ "version": "0.0.21",
4
4
  "description": "一个 Prettier 插件,用于对 JavaScript/TypeScript 文件的导入语句进行分组和排序",
5
5
  "keywords": [
6
6
  "prettier",
@@ -56,7 +56,7 @@
56
56
  "typescript": "^5.9.2"
57
57
  },
58
58
  "peerDependencies": {
59
- "prettier": "^3.0.0"
59
+ "prettier": "^3.7.3"
60
60
  },
61
61
  "scripts": {
62
62
  "build": "rslib build",