1- import fs from "fs" ;
2- import path from "path" ;
31import type {
42 Comment ,
53 Locations ,
@@ -15,19 +13,13 @@ import type ESTree from "estree";
1513import type * as SvAST from "../parser/svelte-ast-types" ;
1614import { ScriptLetContext } from "./script-let" ;
1715import { LetDirectiveCollections } from "./let-directive-collection" ;
18- import { getParserForLang } from "../parser/resolve-parser" ;
1916import type { AttributeToken } from "../parser/html" ;
2017import { parseAttributes } from "../parser/html" ;
21- import {
22- isTSESLintParserObject ,
23- maybeTSESLintParserObject ,
24- } from "../parser/parser-object" ;
2518import { 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" ;
3123
3224export class ScriptsSourceCode {
3325 private raw : string ;
@@ -116,7 +108,7 @@ export type ContextSourceCode = {
116108export class Context {
117109 public readonly code : string ;
118110
119- public readonly parserOptions : any ;
111+ public readonly parserOptions : NormalizedParserOptions ;
120112
121113 // ----- Source Code ------
122114 public readonly sourceCode : ContextSourceCode ;
@@ -155,7 +147,7 @@ export class Context {
155147
156148 private readonly blocks : Block [ ] = [ ] ;
157149
158- public constructor ( code : string , parserOptions : any ) {
150+ public constructor ( code : string , parserOptions : NormalizedParserOptions ) {
159151 this . code = code ;
160152 this . parserOptions = parserOptions ;
161153 this . locs = new LinesAndColumns ( code ) ;
@@ -287,44 +279,7 @@ export class Context {
287279 return this . state . isTypeScript ;
288280 }
289281 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 ) ) ;
328283 }
329284
330285 public stripScriptCode ( start : number , end : number ) : void {
0 commit comments