@@ -4,15 +4,12 @@ import assert from 'assert';
4
4
import type { CSpellSettings , TextDocument , ValidationIssue } from 'cspell-lib' ;
5
5
import { createTextDocument , DocumentValidator , refreshDictionaryCache } from 'cspell-lib' ;
6
6
import type { Comment , Identifier , ImportSpecifier , Literal , Node , TemplateElement } from 'estree' ;
7
- import { walk } from 'estree-walker' ;
8
7
import * as path from 'path' ;
9
8
import { format } from 'util' ;
10
9
10
+ import type { ASTNode , JSXText } from './ASTNode' ;
11
11
import type { CustomWordListFile , WorkerOptions } from './options' ;
12
-
13
- interface JSXText extends Omit < Literal , 'type' > {
14
- type : 'JSXText' ;
15
- }
12
+ import { walkTree } from './walkTree' ;
16
13
17
14
export interface Issue {
18
15
start : number ;
@@ -22,8 +19,6 @@ export interface Issue {
22
19
suggestions : string [ ] | undefined ;
23
20
}
24
21
25
- type ASTNode = ( Node | Comment | JSXText ) & { parent ?: Node } ;
26
-
27
22
const defaultSettings : CSpellSettings = {
28
23
patterns : [
29
24
// @todo : be able to use cooked / transformed strings.
@@ -41,12 +36,16 @@ function log(...args: Parameters<typeof console.log>) {
41
36
console . log ( ...args ) ;
42
37
}
43
38
44
- export function spellCheck ( filename : string , text : string , root : Node , options : WorkerOptions ) : Issue [ ] {
39
+ type SpellCheckFn = typeof spellCheck ;
40
+
41
+ export type SpellCheckSyncFn = ( ...p : Parameters < SpellCheckFn > ) => Awaited < ReturnType < SpellCheckFn > > ;
42
+
43
+ export async function spellCheck ( filename : string , text : string , root : Node , options : WorkerOptions ) : Promise < Issue [ ] > {
45
44
const toIgnore = new Set < string > ( ) ;
46
45
const importedIdentifiers = new Set < string > ( ) ;
47
46
isDebugMode = options . debugMode || false ;
48
47
const validator = getDocValidator ( filename , text , options ) ;
49
- validator . prepareSync ( ) ;
48
+ await validator . prepare ( ) ;
50
49
const issues : Issue [ ] = [ ] ;
51
50
52
51
function checkLiteral ( node : Literal | ASTNode ) {
@@ -373,18 +372,3 @@ function getTextDocument(filename: string, content: string): TextDocument {
373
372
function isCustomWordListFile ( value : string | CustomWordListFile | undefined ) : value is CustomWordListFile {
374
373
return ! ! value && typeof value === 'object' ;
375
374
}
376
-
377
- export function walkTree ( node : ASTNode , enter : ( node : ASTNode , parent : ASTNode | undefined , key : string ) => void ) {
378
- const visited = new Set < object > ( ) ;
379
-
380
- walk ( node , {
381
- enter : function ( node , parent , key ) {
382
- if ( visited . has ( node ) || key === 'tokens' ) {
383
- this . skip ( ) ;
384
- return ;
385
- }
386
- visited . add ( node ) ;
387
- enter ( node as ASTNode , parent as ASTNode , key ) ;
388
- } ,
389
- } ) ;
390
- }
0 commit comments