1
- import fs from "fs" ;
2
- import path from "path" ;
3
1
import type {
4
2
Comment ,
5
3
Locations ,
@@ -15,19 +13,13 @@ import type ESTree from "estree";
15
13
import type * as SvAST from "../parser/svelte-ast-types" ;
16
14
import { ScriptLetContext } from "./script-let" ;
17
15
import { LetDirectiveCollections } from "./let-directive-collection" ;
18
- import { getParserForLang } from "../parser/resolve-parser" ;
19
16
import type { AttributeToken } from "../parser/html" ;
20
17
import { parseAttributes } from "../parser/html" ;
21
- import {
22
- isTSESLintParserObject ,
23
- maybeTSESLintParserObject ,
24
- } from "../parser/parser-object" ;
25
18
import { sortedLastIndex } from "../utils" ;
26
-
27
- const TS_PARSER_NAMES = [
28
- "@typescript-eslint/parser" ,
29
- "typescript-eslint-parser-for-extra-files" ,
30
- ] ;
19
+ import {
20
+ isTypeScript ,
21
+ type NormalizedParserOptions ,
22
+ } from "../parser/parser-options" ;
31
23
32
24
export class ScriptsSourceCode {
33
25
private raw : string ;
@@ -116,7 +108,7 @@ export type ContextSourceCode = {
116
108
export class Context {
117
109
public readonly code : string ;
118
110
119
- public readonly parserOptions : any ;
111
+ public readonly parserOptions : NormalizedParserOptions ;
120
112
121
113
// ----- Source Code ------
122
114
public readonly sourceCode : ContextSourceCode ;
@@ -155,7 +147,7 @@ export class Context {
155
147
156
148
private readonly blocks : Block [ ] = [ ] ;
157
149
158
- public constructor ( code : string , parserOptions : any ) {
150
+ public constructor ( code : string , parserOptions : NormalizedParserOptions ) {
159
151
this . code = code ;
160
152
this . parserOptions = parserOptions ;
161
153
this . locs = new LinesAndColumns ( code ) ;
@@ -287,44 +279,7 @@ export class Context {
287
279
return this . state . isTypeScript ;
288
280
}
289
281
const lang = this . sourceCode . scripts . attrs . lang ;
290
- if ( ! lang ) {
291
- return ( this . state . isTypeScript = false ) ;
292
- }
293
- const parserValue = getParserForLang (
294
- this . sourceCode . scripts . attrs ,
295
- this . parserOptions ?. parser ,
296
- ) ;
297
- if ( typeof parserValue !== "string" ) {
298
- return ( this . state . isTypeScript =
299
- maybeTSESLintParserObject ( parserValue ) ||
300
- isTSESLintParserObject ( parserValue ) ) ;
301
- }
302
- const parserName = parserValue ;
303
- if ( TS_PARSER_NAMES . includes ( parserName ) ) {
304
- return ( this . state . isTypeScript = true ) ;
305
- }
306
- if ( TS_PARSER_NAMES . some ( ( nm ) => parserName . includes ( nm ) ) ) {
307
- let targetPath = parserName ;
308
- while ( targetPath ) {
309
- const pkgPath = path . join ( targetPath , "package.json" ) ;
310
- if ( fs . existsSync ( pkgPath ) ) {
311
- try {
312
- return ( this . state . isTypeScript = TS_PARSER_NAMES . includes (
313
- JSON . parse ( fs . readFileSync ( pkgPath , "utf-8" ) ) ?. name ,
314
- ) ) ;
315
- } catch {
316
- return ( this . state . isTypeScript = false ) ;
317
- }
318
- }
319
- const parent = path . dirname ( targetPath ) ;
320
- if ( targetPath === parent ) {
321
- break ;
322
- }
323
- targetPath = parent ;
324
- }
325
- }
326
-
327
- return ( this . state . isTypeScript = false ) ;
282
+ return ( this . state . isTypeScript = isTypeScript ( this . parserOptions , lang ) ) ;
328
283
}
329
284
330
285
public stripScriptCode ( start : number , end : number ) : void {
0 commit comments