xshell 1.3.44 → 1.3.45

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/package.json +1 -1
  2. package/xlint.js +38 -43
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xshell",
3
- "version": "1.3.44",
3
+ "version": "1.3.45",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "bin": {
package/xlint.js CHANGED
@@ -5,7 +5,7 @@ import TSParser from '@typescript-eslint/parser';
5
5
  import ts_plugin from '@typescript-eslint/eslint-plugin';
6
6
  import react_plugin from 'eslint-plugin-react';
7
7
  import stylistic_plugin from '@stylistic/eslint-plugin';
8
- import { check } from "./utils.js";
8
+ import { array_equals, check } from "./utils.js";
9
9
  import { path } from "./path.js";
10
10
  // 用 ast explorer 选 esprima 来看
11
11
  // 或者 https://explorer.eslint.org/
@@ -703,18 +703,6 @@ export const xlint_plugin = {
703
703
  'Program:exit'() {
704
704
  if (need_fix_source || imports.length <= 1)
705
705
  return;
706
- const { sourceCode: source } = context;
707
- const { body } = source.ast;
708
- const first = imports[0].node;
709
- const ifirst = body.indexOf(first);
710
- if (body.indexOf(imports.last.node, ifirst + 1) - ifirst !== imports.length - 1) {
711
- context.report({
712
- // @ts-ignore
713
- message: 'import 语句不连续,中间有其他语句,无法自动排序修复,需手动修复',
714
- node: first
715
- });
716
- return;
717
- }
718
706
  // 检查顺序并修复: 样式 xxx.sass < nodejs builtin < package < alias < subpath
719
707
  // 如果为样式,则放前面,其他的就按照数组来
720
708
  let imports_ = imports.toSorted((a, b) => {
@@ -724,40 +712,47 @@ export const xlint_plugin = {
724
712
  return a.ialias - b.ialias;
725
713
  return imtypes.indexOf(a.type) - imtypes.indexOf(b.type);
726
714
  });
727
- for (let i = 0; i < imports.length; i++) {
728
- let im = imports[i];
729
- let im_ = imports_[i];
730
- if (im.node === im_.node)
731
- continue;
715
+ if (array_equals(imports, imports_))
716
+ return;
717
+ const { sourceCode: source } = context;
718
+ const { body } = source.ast;
719
+ const first = imports[0].node;
720
+ const ifirst = body.indexOf(first);
721
+ if (body.indexOf(imports.last.node, ifirst + 1) - ifirst !== imports.length - 1) {
732
722
  context.report({
733
723
  // @ts-ignore
734
- message: `第 ${i} import 顺序不对`,
735
- node: im.node,
736
- fix(fixer) {
737
- let _start = Number.MAX_SAFE_INTEGER, _end = 0;
738
- let type;
739
- // 收集所有排序后的 import 文本
740
- const texts = imports_.map((im, i) => {
741
- const [start, end] = get_range_with_comments(source, im.node);
742
- if (start < _start)
743
- _start = start;
744
- if (end > _end)
745
- _end = end;
746
- let text = source.text.slice(start, end);
747
- // 分组之间空一行
748
- if (im.type !== type) {
749
- if (type)
750
- text = '\n' + text;
751
- type = im.type;
752
- }
753
- return text;
754
- });
755
- // 一次性替换所有 import
756
- return fixer.replaceTextRange([_start, _end], texts.join_lines(false));
757
- }
724
+ message: 'import 顺序不对,且 import 语句不连续,中间有其他语句,无法自动排序修复,需手动修复',
725
+ node: first
758
726
  });
759
- break;
727
+ return;
760
728
  }
729
+ context.report({
730
+ // @ts-ignore
731
+ message: 'import 顺序不对',
732
+ node: imports[0].node,
733
+ fix(fixer) {
734
+ let _start = Number.MAX_SAFE_INTEGER, _end = 0;
735
+ let type;
736
+ // 收集所有排序后的 import 文本
737
+ const texts = imports_.map((im, i) => {
738
+ const [start, end] = get_range_with_comments(source, im.node);
739
+ if (start < _start)
740
+ _start = start;
741
+ if (end > _end)
742
+ _end = end;
743
+ let text = source.text.slice(start, end);
744
+ // 分组之间空一行
745
+ if (im.type !== type) {
746
+ if (type)
747
+ text = '\n' + text;
748
+ type = im.type;
749
+ }
750
+ return text;
751
+ });
752
+ // 一次性替换所有 import
753
+ return fixer.replaceTextRange([_start, _end], texts.join_lines(false));
754
+ }
755
+ });
761
756
  }
762
757
  };
763
758
  }