/
cspell-eslint-plugin.ts
407 lines (355 loc) · 13.5 KB
/
cspell-eslint-plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// cspell:ignore TSESTree
import type { TSESTree } from '@typescript-eslint/types';
import assert from 'assert';
import { createTextDocument, CSpellSettings, DocumentValidator, ValidationIssue, TextDocument } from 'cspell-lib';
import type { Rule } from 'eslint';
// eslint-disable-next-line node/no-missing-import
import type { Comment, Identifier, Literal, Node, TemplateElement, ImportSpecifier } from 'estree';
import { format } from 'util';
import { normalizeOptions } from './options';
import optionsSchema from './_auto_generated_/options.schema.json';
const schema = optionsSchema as unknown as Rule.RuleMetaData['schema'];
interface PluginRules {
['spellchecker']: Rule.RuleModule;
}
const messages = {
wordUnknown: 'Unknown word: "{{word}}"',
wordForbidden: 'Forbidden word: "{{word}}"',
suggestWord: '{{word}}',
} as const;
type Messages = typeof messages;
type MessageIds = keyof Messages;
const meta: Rule.RuleMetaData = {
docs: {
description: 'CSpell spellchecker',
category: 'Possible Errors',
recommended: false,
},
messages,
hasSuggestions: true,
schema: [schema],
};
type ASTNode = (Node | Comment) & Partial<Rule.NodeParentExtension>;
const defaultSettings: CSpellSettings = {
patterns: [
// @todo: be able to use cooked / transformed strings.
// {
// // Do not block unicode escape sequences.
// name: 'js-unicode-escape',
// pattern: /$^/g,
// },
],
};
let isDebugMode = false;
function log(...args: Parameters<typeof console.log>) {
if (!isDebugMode) return;
console.log(...args);
}
function create(context: Rule.RuleContext): Rule.RuleListener {
const options = normalizeOptions(context.options[0]);
const toIgnore = new Set<string>();
const importedIdentifiers = new Set<string>();
isDebugMode = options.debugMode || false;
isDebugMode && logContext(context);
const validator = getDocValidator(context);
validator.prepareSync();
function checkLiteral(node: Literal & Rule.NodeParentExtension) {
if (!options.checkStrings) return;
if (typeof node.value === 'string') {
debugNode(node, node.value);
if (options.ignoreImports && isImportOrRequired(node)) return;
if (options.ignoreImportProperties && isImportedProperty(node)) return;
checkNodeText(node, node.value);
}
}
function checkTemplateElement(node: TemplateElement & Rule.NodeParentExtension) {
if (!options.checkStringTemplates) return;
debugNode(node, node.value);
// console.log('Template: %o', node.value);
checkNodeText(node, node.value.cooked || node.value.raw);
}
function checkIdentifier(node: Identifier & Rule.NodeParentExtension) {
debugNode(node, node.name);
if (options.ignoreImports) {
if (isRawImportIdentifier(node)) {
toIgnore.add(node.name);
return;
}
if (isImportIdentifier(node)) {
importedIdentifiers.add(node.name);
if (isLocalImportIdentifierUnique(node)) {
checkNodeText(node, node.name);
}
return;
} else if (options.ignoreImportProperties && isImportedProperty(node)) {
return;
}
}
if (!options.checkIdentifiers) return;
if (toIgnore.has(node.name) && !isObjectProperty(node)) return;
if (skipCheckForRawImportIdentifiers(node)) return;
checkNodeText(node, node.name);
}
function checkComment(node: Comment) {
if (!options.checkComments) return;
debugNode(node, node.value);
checkNodeText(node, node.value);
}
function checkNodeText(node: ASTNode, text: string) {
if (!node.range) return;
const adj = node.type === 'Literal' ? 1 : 0;
const range = [node.range[0] + adj, node.range[1] - adj] as const;
const scope: string[] = calcScope(node);
const result = validator.checkText(range, text, scope);
result.forEach((issue) => reportIssue(issue));
}
function calcScope(_node: ASTNode): string[] {
// inheritance(node);
return [];
}
function isImportIdentifier(node: ASTNode): boolean {
const parent = node.parent;
if (node.type !== 'Identifier' || !parent) return false;
return (
(parent.type === 'ImportSpecifier' ||
parent.type === 'ImportNamespaceSpecifier' ||
parent.type === 'ImportDefaultSpecifier') &&
parent.local === node
);
}
function isRawImportIdentifier(node: ASTNode): boolean {
const parent = node.parent;
if (node.type !== 'Identifier' || !parent) return false;
return (
(parent.type === 'ImportSpecifier' && parent.imported === node) ||
(parent.type === 'ExportSpecifier' && parent.local === node)
);
}
function isLocalImportIdentifierUnique(node: ASTNode): boolean {
const parent = getImportParent(node);
if (!parent) return true;
const { imported, local } = parent;
if (imported.name !== local.name) return true;
return imported.range?.[0] !== local.range?.[0] && imported.range?.[1] !== local.range?.[1];
}
function getImportParent(node: ASTNode): ImportSpecifier | undefined {
const parent = node.parent;
return parent?.type === 'ImportSpecifier' ? parent : undefined;
}
function skipCheckForRawImportIdentifiers(node: ASTNode): boolean {
if (options.ignoreImports) return false;
const parent = getImportParent(node);
return !!parent && parent.imported === node && !isLocalImportIdentifierUnique(node);
}
function isImportedProperty(node: ASTNode): boolean {
const obj = findOriginObject(node);
return !!obj && obj.type === 'Identifier' && importedIdentifiers.has(obj.name);
}
function isObjectProperty(node: ASTNode): boolean {
return node.parent?.type === 'MemberExpression';
}
function reportIssue(issue: ValidationIssue) {
const messageId: MessageIds = issue.isFlagged ? 'wordForbidden' : 'wordUnknown';
const data = {
word: issue.text,
};
const code = context.getSourceCode();
const start = issue.offset;
const end = issue.offset + (issue.length || issue.text.length);
const startPos = code.getLocFromIndex(start);
const endPos = code.getLocFromIndex(end);
const loc = { start: startPos, end: endPos };
function fixFactory(word: string): Rule.ReportFixer {
return (fixer) => fixer.replaceTextRange([start, end], word);
}
function createSug(word: string): Rule.SuggestionReportDescriptor {
const data = { word };
const messageId: MessageIds = 'suggestWord';
return {
messageId,
data,
fix: fixFactory(word),
};
}
log('Suggestions: %o', issue.suggestions);
const suggest: Rule.ReportDescriptorOptions['suggest'] = issue.suggestions?.map(createSug);
const des: Rule.ReportDescriptor = {
messageId,
data,
loc,
suggest,
};
context.report(des);
}
context
.getSourceCode()
.getAllComments()
.forEach(function (commentNode) {
checkComment(commentNode);
});
return {
Literal: checkLiteral,
TemplateElement: checkTemplateElement,
Identifier: checkIdentifier,
};
function mapNode(node: ASTNode | TSESTree.Node, index: number, nodes: ASTNode[]): string {
const child = nodes[index + 1];
if (node.type === 'ImportSpecifier') {
const extra = node.imported === child ? '.imported' : node.local === child ? '.local' : '';
return node.type + extra;
}
if (node.type === 'ImportDeclaration') {
const extra = node.source === child ? '.source' : '';
return node.type + extra;
}
if (node.type === 'ExportSpecifier') {
const extra = node.exported === child ? '.exported' : node.local === child ? '.local' : '';
return node.type + extra;
}
if (node.type === 'ExportNamedDeclaration') {
const extra = node.source === child ? '.source' : '';
return node.type + extra;
}
if (node.type === 'Property') {
const extra = node.key === child ? 'key' : node.value === child ? 'value' : '';
return [node.type, node.kind, extra].join('.');
}
if (node.type === 'MemberExpression') {
const extra = node.property === child ? 'property' : node.object === child ? 'object' : '';
return node.type + '.' + extra;
}
if (node.type === 'ArrowFunctionExpression') {
const extra = node.body === child ? 'body' : 'param';
return node.type + '.' + extra;
}
if (node.type === 'FunctionDeclaration') {
const extra = node.id === child ? 'id' : node.body === child ? 'body' : 'params';
return node.type + '.' + extra;
}
if (node.type === 'ClassDeclaration' || node.type === 'ClassExpression') {
const extra = node.id === child ? 'id' : node.body === child ? 'body' : 'superClass';
return node.type + '.' + extra;
}
if (node.type === 'CallExpression') {
const extra = node.callee === child ? 'callee' : 'arguments';
return node.type + '.' + extra;
}
if (node.type === 'Literal') {
return tagLiteral(node);
}
if (node.type === 'Block') {
return node.value[0] === '*' ? 'Comment.docBlock' : 'Comment.block';
}
if (node.type === 'Line') {
return 'Comment.line';
}
return node.type;
}
function inheritance(node: ASTNode) {
const a = [...context.getAncestors(), node];
return a.map(mapNode);
}
function inheritanceSummary(node: ASTNode) {
return inheritance(node).join(' ');
}
/**
* find the origin of a member expression
*/
function findOriginObject(node: ASTNode): ASTNode | undefined {
const parent = node.parent;
if (parent?.type !== 'MemberExpression' || parent.property !== node) return undefined;
let obj = parent.object;
while (obj.type === 'MemberExpression') {
obj = obj.object;
}
return obj;
}
function isFunctionCall(node: ASTNode | undefined, name: string): boolean {
return node?.type === 'CallExpression' && node.callee.type === 'Identifier' && node.callee.name === name;
}
function isRequireCall(node: ASTNode | undefined) {
return isFunctionCall(node, 'require');
}
function isImportOrRequired(node: ASTNode) {
return isRequireCall(node.parent) || (node.parent?.type === 'ImportDeclaration' && node.parent.source === node);
}
function debugNode(node: ASTNode, value: unknown) {
if (!isDebugMode) return;
const val = format('%o', value);
log(`${inheritanceSummary(node)}: ${val}`);
}
}
function tagLiteral(node: ASTNode | TSESTree.Node): string {
assert(node.type === 'Literal');
const kind = typeof node.value;
const extra =
kind === 'string'
? node.raw?.[0] === '"'
? 'string.double'
: 'string.single'
: node.value === null
? 'null'
: kind;
return node.type + '.' + extra;
}
export const rules: PluginRules = {
spellchecker: {
meta,
create,
},
};
function logContext(context: Rule.RuleContext) {
log('\n\n************************');
// log(context.getSourceCode().text);
log(`
id: ${context.id}
cwd: ${context.getCwd()}
filename: ${context.getFilename()}
physicalFilename: ${context.getPhysicalFilename()}
scope: ${context.getScope().type}
`);
}
export const configs = {
recommended: {
plugins: ['@cspell'],
rules: {
'@cspell/spellchecker': ['warn', {}],
},
},
debug: {
plugins: ['@cspell'],
rules: {
'@cspell/spellchecker': ['warn', { debugMode: true }],
},
},
};
interface CachedDoc {
filename: string;
doc: TextDocument;
}
const cache: { lastDoc: CachedDoc | undefined } = { lastDoc: undefined };
const docValCache = new WeakMap<TextDocument, DocumentValidator>();
function getDocValidator(context: Rule.RuleContext): DocumentValidator {
const text = context.getSourceCode().getText();
const doc = getTextDocument(context.getFilename(), text);
const cachedValidator = docValCache.get(doc);
if (cachedValidator) {
cachedValidator.updateDocumentText(text);
return cachedValidator;
}
const options = normalizeOptions(context.options[0]);
isDebugMode = options.debugMode || false;
isDebugMode && logContext(context);
const validator = new DocumentValidator(doc, options, defaultSettings);
docValCache.set(doc, validator);
return validator;
}
function getTextDocument(filename: string, content: string): TextDocument {
if (cache.lastDoc?.filename === filename) {
return cache.lastDoc.doc;
}
const doc = createTextDocument({ uri: filename, content });
// console.error(`CreateTextDocument: ${doc.uri}`);
cache.lastDoc = { filename, doc };
return doc;
}