Skip to content

Commit 9bfaf9e

Browse files
authored
fix: resolve imports from ESLint plugin (#5790)
1 parent b98d94b commit 9bfaf9e

File tree

23 files changed

+404
-39
lines changed

23 files changed

+404
-39
lines changed

packages/cspell-eslint-plugin/README.md

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,22 @@ interface Options {
193193
dictionaryDefinitions?: DictionaryDefinition[];
194194
};
195195

196+
/**
197+
* Specify the root path of the cspell configuration.
198+
* It is used to resolve `imports` found in {@link cspell}.
199+
*
200+
* example:
201+
* ```js
202+
* cspellOptionsRoot: import.meta.url
203+
* // or
204+
* cspellOptionsRoot: __filename
205+
* ```
206+
*
207+
* @default cwd
208+
*
209+
*/
210+
cspellOptionsRoot?: string | URL;
211+
196212
/**
197213
* Specify a path to a custom word list file.
198214
*
@@ -282,22 +298,6 @@ export default [
282298
}
283299
```
284300

285-
## `configFile` - Using a CSpell Configuration File
286-
287-
**`eslint.config.mjs`**
288-
289-
```js
290-
rules: {
291-
'@cspell/spellchecker': [
292-
'error',
293-
{
294-
//
295-
configFile: new URL('./cspell.config.yaml', import.meta.url).toString(),
296-
},
297-
],
298-
},
299-
```
300-
301301
## `autoFix`
302302

303303
When enabled, `autoFix` corrects any spelling issues that have a single "preferred" suggestion. It attempts to match
@@ -324,6 +324,48 @@ When spell checking, if `colour` is not in one of the dictionaries, then `color`
324324

325325
CSpell will match case, but not word stems. `blacklist` and `Blacklist` will get replaced, but not `blacklists`.
326326

327+
## `configFile` - Using a CSpell Configuration File
328+
329+
**`eslint.config.mjs`**
330+
331+
```js
332+
rules: {
333+
'@cspell/spellchecker': [
334+
'error',
335+
{
336+
configFile: new URL('./cspell.config.yaml', import.meta.url).toString(),
337+
},
338+
],
339+
},
340+
```
341+
342+
## `cspell` and `cspellOptionsRoot` - CSpell Configuration
343+
344+
It is possible to send `cspell` configuration to the spell checker. Where possible, use a cspell configuration file and set `configFile`. But there are cases where this is not possible or desired (like fewer configuration files).
345+
346+
- Option `cspell` is used to pass along configuration to the spell checker.
347+
- Option `cspellOptionsRoot` is used to tell the spell checker how to find `cspell.import`s.
348+
349+
Example: **`eslint.config.mjs`**
350+
351+
```js
352+
rules: {
353+
'@cspell/spellchecker': [
354+
'warn',
355+
{
356+
cspell: {
357+
import: ['./cspell.config.yaml', '@cspell/dict-de-de']
358+
},
359+
cspellOptionsRoot: import.meta.url,
360+
},
361+
],
362+
},
363+
```
364+
365+
Assuming `import.meta.url` is `file:///Users/ci/project/app/eslint.config.mjs`, this tells the spell checker to import `cspell.config.yaml` from `file:///Users/ci/project/app/cspell.config.yaml` and to search for package `@cspell/dict-de-de` starting at `file:///Users/ci/project/app/`.
366+
367+
If `cspellOptionsRoot` is not specified, the current working directory is used.
368+
327369
## Checking Custom AST Nodes
328370

329371
The `checkScope` setting is used to enable / disable checking AST Nodes. ESLint uses parsers to generate the AST (Abstract Syntax Tree) to evaluate a document. Each PlugIn gets access to the AST. `checkScope` can be used to handle new AST nodes when a custom parser is added. Some knowledge of the AST output by the parser is necessary.

packages/cspell-eslint-plugin/assets/options.schema.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,19 @@
326326
},
327327
"type": "object"
328328
},
329+
"cspellOptionsRoot": {
330+
"anyOf": [
331+
{
332+
"type": "string"
333+
},
334+
{
335+
"format": "uri",
336+
"type": "string"
337+
}
338+
],
339+
"description": "Specify the root path of the cspell configuration. It is used to resolve `imports` found in {@link cspell } .\n\nexample: ```js cspellOptionsRoot: import.meta.url // or cspellOptionsRoot: __filename ```",
340+
"markdownDescription": "Specify the root path of the cspell configuration.\nIt is used to resolve `imports` found in {@link cspell } .\n\nexample:\n```js\ncspellOptionsRoot: import.meta.url\n// or\ncspellOptionsRoot: __filename\n```"
341+
},
329342
"customWordListFile": {
330343
"anyOf": [
331344
{

packages/cspell-eslint-plugin/cspell.config.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,4 @@ ignoreWords:
55
- todos
66
- bluelist
77
words:
8-
- estree
9-
- pnpm
108
- synckit
11-
- treeshake
12-
- tsbuildinfo
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import:
2+
- '@internal/fixture-test-dictionary'
3+
words: []
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"noConfigSearch": true
3+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* This is a sample file to test the import support.
3+
* Related to issue: [#5789](https://github.com/streetsidesoftware/cspell/issues/5789)
4+
*/
5+
6+
export function sample() {
7+
return 'sample';
8+
}
9+
10+
export const words = [
11+
`
12+
readstring
13+
resetmemorycategory
14+
retargeting
15+
rrotate
16+
rshift
17+
`,
18+
];

packages/cspell-eslint-plugin/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
"@eslint/eslintrc": "^3.1.0",
8181
"@eslint/js": "^9.5.0",
8282
"@internal/cspell-eslint-plugin-scripts": "workspace:*",
83+
"@internal/fixture-test-dictionary": "workspace:*",
84+
"@types/eslint": "^8.56.10",
8385
"@types/estree": "^1.0.5",
8486
"@types/mocha": "^10.0.7",
8587
"@typescript-eslint/parser": "^7.13.1",
@@ -101,6 +103,7 @@
101103
},
102104
"dependencies": {
103105
"@cspell/cspell-types": "workspace:*",
106+
"@cspell/url": "workspace:*",
104107
"cspell-lib": "workspace:*",
105108
"synckit": "^0.9.0"
106109
},

packages/cspell-eslint-plugin/src/common/options.cts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ export interface Check {
103103
* CSpell options to pass to the spell checker.
104104
*/
105105
cspell?: CSpellOptions;
106+
/**
107+
* Specify the root path of the cspell configuration.
108+
* It is used to resolve `imports` found in {@link cspell}.
109+
*
110+
* example:
111+
* ```js
112+
* cspellOptionsRoot: import.meta.url
113+
* // or
114+
* cspellOptionsRoot: __filename
115+
* ```
116+
*/
117+
cspellOptionsRoot?: string | URL;
106118
/**
107119
* Specify a path to a custom word list file.
108120
*

packages/cspell-eslint-plugin/src/plugin/cspell-eslint-plugin.cts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ function create(context: Rule.RuleContext): Rule.RuleListener {
131131
'errors: %o',
132132
errors.map((e) => e.message),
133133
);
134-
errors.forEach((error) => console.error('%s', error.message));
134+
errors.forEach((error) => context.report({ message: error.message, loc: { line: 1, column: 1 } }));
135135
}
136136
issues.forEach((issue) => reportIssue(issue));
137137
}

packages/cspell-eslint-plugin/src/plugin/defaultCheckOptions.cts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const defaultCheckOptions: Required<Check> = {
77
checkStrings: true,
88
checkStringTemplates: true,
99
configFile: '',
10+
cspellOptionsRoot: '',
1011
cspell: {
1112
words: [],
1213
flagWords: [],
@@ -26,6 +27,7 @@ export const defaultOptions: RequiredOptions = {
2627
};
2728

2829
export function normalizeOptions(opts: Options | undefined, cwd: string): WorkerOptions {
29-
const options: WorkerOptions = Object.assign({}, defaultOptions, opts || {}, { cwd });
30+
const cspellOptionsRoot = opts?.cspellOptionsRoot || 'eslint-configuration-file';
31+
const options: WorkerOptions = Object.assign({}, defaultOptions, opts || {}, { cspellOptionsRoot, cwd });
3032
return options;
3133
}

0 commit comments

Comments
 (0)