xshell-lint 1.0.5 → 1.0.7

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/index.js +109 -8
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -79,7 +79,7 @@ export const xlint_plugin = {
79
79
  context.report({
80
80
  // @ts-ignore
81
81
  node: body,
82
- message: 'Expected a linebreak before this statement.',
82
+ message: '语句体应换行书写',
83
83
  fix: fixer => fixer.replaceTextRange([token_before.range[1], body.range[0]], '\n' + ' '.repeat(indent + 4))
84
84
  });
85
85
  }
@@ -114,7 +114,7 @@ export const xlint_plugin = {
114
114
  context.report({
115
115
  node,
116
116
  // @ts-ignore
117
- message: 'Expected a space in this empty object.',
117
+ message: '空对象内应有空格',
118
118
  fix: fixer => fixer.replaceText(node, '{ }')
119
119
  });
120
120
  },
@@ -124,7 +124,7 @@ export const xlint_plugin = {
124
124
  context.report({
125
125
  node,
126
126
  // @ts-ignore
127
- message: 'Expected a space in this empty array.',
127
+ message: '空数组内应有空格',
128
128
  fix: fixer => fixer.replaceText(node, '[ ]')
129
129
  });
130
130
  },
@@ -133,20 +133,120 @@ export const xlint_plugin = {
133
133
  context.report({
134
134
  node,
135
135
  // @ts-ignore
136
- message: 'Expected a space in this empty block.',
136
+ message: '空代码块内应有空格',
137
137
  fix: fixer => fixer.replaceText(node, '{ }')
138
138
  });
139
139
  },
140
140
  };
141
141
  }
142
142
  },
143
+ // { a, b } 这样的大括号内要有空格(写死 always,空对象交给 empty-bracket-spacing 规则)
144
+ // 移植自 @stylistic/object-curly-spacing:stylistic 6.0 将其弃用并入 list-style,
145
+ // 但 list-style 额外强制换行一致、fixer 插裸换行不带缩进,与本项目风格冲突,故移植此纯空格规则
146
+ 'object-curly-spacing': {
147
+ meta: {
148
+ ...meta,
149
+ messages: {
150
+ requireSpaceAfter: "'{{token}}' 后应有空格",
151
+ requireSpaceBefore: "'{{token}}' 前应有空格"
152
+ }
153
+ },
154
+ // @ts-ignore
155
+ create(context) {
156
+ const source_code = context.sourceCode;
157
+ function validate_brace_spacing(node, opening_token, closing_token) {
158
+ const token_after_opening = source_code.getTokenAfter(opening_token, { includeComments: true });
159
+ if (ASTUtils.isTokenOnSameLine(opening_token, token_after_opening) && !source_code.isSpaceBetween(opening_token, token_after_opening))
160
+ context.report({
161
+ node,
162
+ loc: opening_token.loc,
163
+ messageId: 'requireSpaceAfter',
164
+ data: { token: opening_token.value },
165
+ fix: fixer => fixer.insertTextAfter(opening_token, ' ')
166
+ });
167
+ const token_before_closing = source_code.getTokenBefore(closing_token, { includeComments: true });
168
+ if (ASTUtils.isTokenOnSameLine(token_before_closing, closing_token) && !source_code.isSpaceBetween(token_before_closing, closing_token))
169
+ context.report({
170
+ node,
171
+ loc: closing_token.loc,
172
+ messageId: 'requireSpaceBefore',
173
+ data: { token: closing_token.value },
174
+ fix: fixer => fixer.insertTextBefore(closing_token, ' ')
175
+ });
176
+ }
177
+ function get_brace_token(node, node_type = node.type) {
178
+ switch (node_type) {
179
+ case 'ImportDeclaration':
180
+ case 'ExportNamedDeclaration':
181
+ case 'ExportAllDeclaration': {
182
+ const tokens = source_code.getTokens(node);
183
+ return [tokens.find(token => ASTUtils.isOpeningBraceToken(token)), tokens.find(token => ASTUtils.isClosingBraceToken(token))];
184
+ }
185
+ case 'ImportAttributes': {
186
+ const tokens = source_code.getTokens(node);
187
+ const opening = tokens.findLast(token => ASTUtils.isOpeningBraceToken(token));
188
+ const closing = tokens.findLast(token => ASTUtils.isClosingBraceToken(token));
189
+ if (!opening || !closing || !node.source || opening.range[0] < node.source.range[0])
190
+ return [null, null];
191
+ return [opening, closing];
192
+ }
193
+ default: {
194
+ const tokens = source_code.getTokens(node);
195
+ return [tokens.find(token => ASTUtils.isOpeningBraceToken(token)), node.type === 'ObjectPattern' && node.typeAnnotation ? source_code.getTokenBefore(node.typeAnnotation, ASTUtils.isClosingBraceToken) : tokens.findLast(token => ASTUtils.isClosingBraceToken(token))];
196
+ }
197
+ }
198
+ }
199
+ function check_for_object_like(node, properties, node_type = node.type) {
200
+ if (properties.length === 0)
201
+ return;
202
+ const [opening_token, closing_token] = get_brace_token(node, node_type);
203
+ if (opening_token && closing_token)
204
+ validate_brace_spacing(node, opening_token, closing_token);
205
+ }
206
+ return {
207
+ ObjectPattern(node) {
208
+ check_for_object_like(node, node.properties);
209
+ },
210
+ ObjectExpression(node) {
211
+ check_for_object_like(node, node.properties);
212
+ },
213
+ ImportDeclaration(node) {
214
+ if (node.attributes)
215
+ check_for_object_like(node, node.attributes, 'ImportAttributes');
216
+ const first_specifier_index = node.specifiers.findIndex(specifier => specifier.type === 'ImportSpecifier');
217
+ check_for_object_like(node, first_specifier_index === -1 ? [] : node.specifiers.slice(first_specifier_index));
218
+ },
219
+ ExportNamedDeclaration(node) {
220
+ check_for_object_like(node, node.specifiers);
221
+ if (node.attributes)
222
+ check_for_object_like(node, node.attributes, 'ImportAttributes');
223
+ },
224
+ ExportAllDeclaration(node) {
225
+ if (node.attributes)
226
+ check_for_object_like(node, node.attributes, 'ImportAttributes');
227
+ },
228
+ TSMappedType(node) {
229
+ validate_brace_spacing(node, source_code.getFirstToken(node), source_code.getLastToken(node));
230
+ },
231
+ TSTypeLiteral(node) {
232
+ check_for_object_like(node, node.members);
233
+ },
234
+ TSInterfaceBody(node) {
235
+ check_for_object_like(node, node.body);
236
+ },
237
+ TSEnumBody(node) {
238
+ check_for_object_like(node, node.members);
239
+ }
240
+ };
241
+ }
242
+ },
143
243
  // a + b**c
144
244
  'space-infix-ops-except-exponentiation': {
145
245
  meta: {
146
246
  ...meta,
147
247
  messages: {
148
- missingSpace: "Operator '{{operator}}' must be spaced.",
149
- redundantSpace: "Operator '{{operator}}' must not be spaced."
248
+ missingSpace: "运算符 '{{operator}}' 两侧应有空格",
249
+ redundantSpace: "运算符 '{{operator}}' 两侧不应有空格"
150
250
  }
151
251
  },
152
252
  // @ts-ignore
@@ -820,8 +920,9 @@ export const xlint_config = {
820
920
  // 简单属性不要冗余的大括号 <Component prop='simple-value' />
821
921
  '@stylistic/jsx-curly-brace-presence': ['error', 'never'],
822
922
  // ------------ 空格
823
- // { a, b } 这样的对象,大括号里面要有空格
824
- '@stylistic/object-curly-spacing': ['error', 'always'],
923
+ // { a, b } 这样的对象,大括号里面要有空格(移植的 xlint 规则,写死 always;
924
+ // 不用 @stylistic/object-curly-spacing 以避弃用警告,也不用 list-style —— 它会强制换行一致且 fixer 不带缩进)
925
+ 'xlint/object-curly-spacing': 'error',
825
926
  // [a, b, c]
826
927
  '@stylistic/comma-spacing': 'error',
827
928
  // foo()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xshell-lint",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "publisher": "ShenHongFei",