@@ -7,7 +7,13 @@ import path from "node:path";
77import type { ESTree } from "meriyah" ;
88
99// Import Internal Dependencies
10- import { JsSourceParser , type SourceParser } from "./parsers/JsSourceParser.ts" ;
10+ import {
11+ JsSourceParser ,
12+ type SourceParser
13+ } from "./parsers/JsSourceParser.ts" ;
14+ import {
15+ TsSourceParser
16+ } from "./parsers/TsSourceParser.ts" ;
1117import * as trojan from "./obfuscators/trojan-source.ts" ;
1218import {
1319 PipelineRunner ,
@@ -254,6 +260,14 @@ export class AstAnalyser {
254260 pathToFile : string | URL ,
255261 options : RuntimeOptions = { }
256262 ) : Promise < ReportOnFile > {
263+ const filePathString = pathToFile instanceof URL ?
264+ pathToFile . href :
265+ pathToFile ;
266+
267+ if ( filePathString . includes ( "d.ts" ) ) {
268+ throw new Error ( "Declaration files are not supported" ) ;
269+ }
270+
257271 try {
258272 const {
259273 packageName,
@@ -264,17 +278,20 @@ export class AstAnalyser {
264278 metadata
265279 } = options ;
266280
267- const str = await fs . readFile ( pathToFile , "utf-8" ) ;
268- const filePathString = pathToFile instanceof URL ? pathToFile . href : pathToFile ;
281+ let customParserToUse = customParser ;
282+ if ( ! customParser && path . extname ( filePathString ) === ".ts" ) {
283+ customParserToUse = new TsSourceParser ( ) ;
284+ }
269285
286+ const str = await fs . readFile ( pathToFile , "utf-8" ) ;
270287 const isMin = filePathString . includes ( ".min" ) || isMinifiedCode ( str ) ;
271288 const data = this . analyse ( str , {
272289 location : path . dirname ( filePathString ) ,
273290 isMinified : isMin ,
274291 removeHTMLComments,
275292 initialize,
276293 finalize,
277- customParser,
294+ customParser : customParserToUse ,
278295 metadata,
279296 packageName
280297 } ) ;
@@ -306,6 +323,14 @@ export class AstAnalyser {
306323 pathToFile : string | URL ,
307324 options : RuntimeOptions = { }
308325 ) : ReportOnFile {
326+ const filePathString = pathToFile instanceof URL ?
327+ pathToFile . href :
328+ pathToFile ;
329+
330+ if ( filePathString . includes ( "d.ts" ) ) {
331+ throw new Error ( "Declaration files are not supported" ) ;
332+ }
333+
309334 try {
310335 const {
311336 packageName,
@@ -316,17 +341,20 @@ export class AstAnalyser {
316341 metadata
317342 } = options ;
318343
319- const str = fsSync . readFileSync ( pathToFile , "utf-8" ) ;
320- const filePathString = pathToFile instanceof URL ? pathToFile . href : pathToFile ;
344+ let customParserToUse = customParser ;
345+ if ( ! customParser && path . extname ( filePathString ) === ".ts" ) {
346+ customParserToUse = new TsSourceParser ( ) ;
347+ }
321348
349+ const str = fsSync . readFileSync ( pathToFile , "utf-8" ) ;
322350 const isMin = filePathString . includes ( ".min" ) || isMinifiedCode ( str ) ;
323351 const data = this . analyse ( str , {
324352 location : path . dirname ( filePathString ) ,
325353 isMinified : isMin ,
326354 removeHTMLComments,
327355 initialize,
328356 finalize,
329- customParser,
357+ customParser : customParserToUse ,
330358 metadata,
331359 packageName
332360 } ) ;
0 commit comments