-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support spell checking more document types
- Loading branch information
Showing
19 changed files
with
855 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/cspell-eslint-plugin/fixtures/yaml-support/eslint.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import eslint from '@eslint/js'; | ||
import cspellRecommended from '@cspell/eslint-plugin/recommended'; | ||
import parserYml from 'yaml-eslint-parser'; | ||
import pluginYml from 'eslint-plugin-yml'; | ||
|
||
/** | ||
* @type { import("eslint").Linter.FlatConfig[] } | ||
*/ | ||
const config = [ | ||
eslint.configs.recommended, | ||
cspellRecommended, | ||
...pluginYml.configs['flat/standard'], | ||
{ | ||
files: ['**/*.yaml', '**/*.yml'], | ||
languageOptions: { | ||
parser: parserYml, | ||
}, | ||
// plugins: { | ||
// yml: pluginYml, | ||
// }, | ||
rules: { | ||
'@cspell/spellchecker': 'warn', | ||
}, | ||
}, | ||
]; | ||
|
||
export default config; |
24 changes: 24 additions & 0 deletions
24
packages/cspell-eslint-plugin/fixtures/yaml-support/sample.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
# Starting comment | ||
list: | ||
- one | ||
- two | ||
- three | ||
- 4 | ||
- 5 | ||
- true | ||
- false | ||
obj: | ||
key: value | ||
key2: value2 # comment after value | ||
key3: value3 | ||
command: | | ||
echo "Hello, World!" | ||
echo "Goodbye, World!" | ||
command2: >- | ||
line 1 | ||
line 2 | ||
'command three': > | ||
line 3 | ||
line 4 | ||
# comment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import 'mocha'; | ||
|
||
export function testEach<T extends object>(cases: T[]): (title: string, fn: (arg: T) => void) => void { | ||
function fixTitle(title: string, testData: T) { | ||
const parts = title.split(/\b/g); | ||
for (let i = parts.length - 1; i >= 0; i--) { | ||
const prev = parts[i - 1]; | ||
if (prev && prev.endsWith('$')) { | ||
parts[i - 1] = prev.slice(0, -1); | ||
parts[i] = '$' + parts[i]; | ||
} | ||
} | ||
|
||
const map = new Map<string, string>(Object.entries(testData).map(([key, value]) => ['$' + key, `"${value}"`])); | ||
|
||
return parts.map((part) => map.get(part) ?? part).join(''); | ||
} | ||
|
||
return (title, fn) => { | ||
for (const c of cases) { | ||
it(fixTitle(title, c), () => fn(c)); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"tsBuildInfoFile": "../../dist/test-util/compile.tsbuildInfo", | ||
"rootDir": ".", | ||
"outDir": "../../dist/test-util" | ||
}, | ||
"include": ["."] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import * as fs from 'node:fs'; | ||
import * as path from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
|
||
import typeScriptParser from '@typescript-eslint/parser'; | ||
import { RuleTester } from 'eslint'; | ||
import react from 'eslint-plugin-react'; | ||
import globals from 'globals'; | ||
|
||
import type { Options as RuleOptions } from '../plugin/index.cjs'; | ||
import Rule from '../plugin/index.cjs'; | ||
|
||
const __dirname = fileURLToPath(new URL('.', import.meta.url)); | ||
const root = path.resolve(__dirname, '../..'); | ||
const fixturesDir = path.join(root, 'fixtures'); | ||
|
||
const parsers: Record<string, string | undefined | unknown> = { | ||
// Note: it is possible for @typescript-eslint/parser to break the path | ||
'.ts': typeScriptParser, | ||
}; | ||
|
||
type ValidTestCase = RuleTester.ValidTestCase; | ||
type Options = Partial<RuleOptions>; | ||
|
||
const KnownErrors: TestCaseError[] = [ce('Unknown word: "Summmer"', 8)]; | ||
|
||
const ruleTesterReact = new RuleTester({ | ||
files: ['**/*.{js,jsx,mjs,cjs,ts,tsx}'], | ||
plugins: { | ||
react, | ||
}, | ||
languageOptions: { | ||
parserOptions: { | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
globals: { | ||
...globals.browser, | ||
}, | ||
}, | ||
// ... others are omitted for brevity | ||
}); | ||
|
||
ruleTesterReact.run('cspell with React', Rule.rules.spellchecker, { | ||
valid: [readSample('react/sample.jsx'), readSample('react/sample.tsx')], | ||
invalid: [ | ||
// cspell:ignore Welcomeeeee Summmer | ||
readInvalid('with-errors/react/sample.jsx', ['Unknown word: "Welcomeeeee"', 'Unknown word: "Summmer"']), | ||
readInvalid('with-errors/react/sample.tsx', ['Unknown word: "Welcomeeeee"', 'Unknown word: "Summmer"']), | ||
readInvalid('with-errors/react/sample.tsx', ['Unknown word: "Summmer"'], { | ||
checkJSXText: false, | ||
}), | ||
readInvalid('with-errors/react/sample.jsx', ['Unknown word: "Summmer"'], { | ||
checkJSXText: false, | ||
}), | ||
], | ||
}); | ||
|
||
function resolveFix(filename: string): string { | ||
return path.resolve(fixturesDir, filename); | ||
} | ||
|
||
interface ValidTestCaseEsLint9 extends ValidTestCase { | ||
languageOptions?: { | ||
parser?: unknown; | ||
}; | ||
} | ||
|
||
function readFix(filename: string, options?: Options): ValidTestCase { | ||
const __filename = resolveFix(filename); | ||
const code = fs.readFileSync(__filename, 'utf8'); | ||
|
||
const sample: ValidTestCaseEsLint9 = { | ||
code, | ||
filename: __filename, | ||
}; | ||
if (options) { | ||
sample.options = [options]; | ||
} | ||
|
||
const parser = parsers[path.extname(__filename)]; | ||
if (parser) { | ||
sample.languageOptions ??= {}; | ||
sample.languageOptions.parser = parser; | ||
} | ||
|
||
return sample; | ||
} | ||
|
||
function readSample(sampleFile: string, options?: Options) { | ||
return readFix(path.join('samples', sampleFile), options); | ||
} | ||
|
||
interface TestCaseError { | ||
message?: string | RegExp | undefined; | ||
messageId?: string | undefined; | ||
type?: string | undefined; | ||
data?: unknown | undefined; | ||
line?: number | undefined; | ||
column?: number | undefined; | ||
endLine?: number | undefined; | ||
endColumn?: number | undefined; | ||
suggestions?: RuleTester.SuggestionOutput[] | undefined | number; | ||
} | ||
|
||
type InvalidTestCaseError = RuleTester.TestCaseError | TestCaseError | string; | ||
|
||
function readInvalid(filename: string, errors: (TestCaseError | string)[], options?: Options) { | ||
const sample = readFix(filename, options); | ||
return { | ||
...sample, | ||
errors: errors.map((err) => csError(err)), | ||
}; | ||
} | ||
|
||
function ce(message: string, suggestions?: number): RuleTester.TestCaseError { | ||
return { message, suggestions } as RuleTester.TestCaseError; | ||
} | ||
|
||
function csError(error: InvalidTestCaseError, suggestions?: number): RuleTester.TestCaseError { | ||
if (error && typeof error === 'object') return error as RuleTester.TestCaseError; | ||
const found = KnownErrors.find((e) => e.message === error) as RuleTester.TestCaseError | undefined; | ||
return found || ce(error, suggestions); | ||
} |
Oops, something went wrong.