@@ -23,7 +23,7 @@ import {
2323 randomId ,
2424 SourceFile
2525} from "../core" ;
26- import { binarySearch , compareTextSpans , getNextSibling , getPreviousSibling , TextSpan } from "./parserUtils" ;
26+ import { binarySearch , compareTextSpans , getNextSibling , getPreviousSibling , TextSpan , hasFlowAnnotation , checkSyntaxErrors } from "./parserUtils" ;
2727import { JavaScriptTypeMapping } from "./typeMapping" ;
2828import path from "node:path" ;
2929import { ExpressionStatement , TypeTreeExpression } from "." ;
@@ -41,6 +41,8 @@ export class JavaScriptParser extends Parser {
4141 module : ts . ModuleKind . CommonJS ,
4242 allowJs : true ,
4343 esModuleInterop : true ,
44+ experimentalDecorators : true ,
45+ emitDecoratorMetadata : true
4446 } ;
4547 }
4648
@@ -147,19 +149,18 @@ export class JavaScriptParser extends Parser {
147149 continue ;
148150 }
149151
150- if ( this . hasFlowAnnotation ( sourceFile ) ) {
152+ if ( hasFlowAnnotation ( sourceFile ) ) {
151153 result . push ( ParseError . build ( this , input , relativeTo , ctx , new FlowSyntaxNotSupportedError ( `Flow syntax not supported: ${ input . path } ` ) , null ) ) ;
152154 continue ;
153155 }
154156
155- // ToDo: uncomment code after tests fixing
156- // const syntaxErrors = this.checkSyntaxErrors(program, sourceFile);
157- // if (syntaxErrors.length > 0) {
158- // syntaxErrors.forEach(
159- // e => result.push(ParseError.build(this, input, relativeTo, ctx, new SyntaxError(`Compiler error: ${e[0]} [${e[1]}]`), null))
160- // );
161- // continue;
162- // }
157+ const syntaxErrors = checkSyntaxErrors ( program , sourceFile ) ;
158+ if ( syntaxErrors . length > 0 ) {
159+ syntaxErrors . forEach (
160+ e => result . push ( ParseError . build ( this , input , relativeTo , ctx , new SyntaxError ( `Compiler error: ${ e [ 0 ] } [${ e [ 1 ] } ]` ) , null ) )
161+ ) ;
162+ continue ;
163+ }
163164
164165 try {
165166 const parsed = new JavaScriptParserVisitor ( this , sourceFile , typeChecker ) . visit ( sourceFile ) as SourceFile ;
@@ -171,39 +172,6 @@ export class JavaScriptParser extends Parser {
171172 return result ;
172173 }
173174
174- private checkSyntaxErrors ( program : ts . Program , sourceFile : ts . SourceFile ) {
175- const diagnostics = ts . getPreEmitDiagnostics ( program , sourceFile ) ;
176- // checking Parsing and Syntax Errors
177- let syntaxErrors : [ errorMsg : string , errorCode : number ] [ ] = [ ] ;
178- if ( diagnostics . length > 0 ) {
179- const errors = diagnostics . filter ( d => d . code >= 1000 && d . code < 2000 ) ;
180- if ( errors . length > 0 ) {
181- syntaxErrors = errors . map ( e => {
182- let errorMsg ;
183- if ( e . file ) {
184- let { line, character} = ts . getLineAndCharacterOfPosition ( e . file , e . start ! ) ;
185- let message = ts . flattenDiagnosticMessageText ( e . messageText , "\n" ) ;
186- errorMsg = `${ e . file . fileName } (${ line + 1 } ,${ character + 1 } ): ${ message } ` ;
187- } else {
188- errorMsg = ts . flattenDiagnosticMessageText ( e . messageText , "\n" ) ;
189- }
190- return [ errorMsg , e . code ] ;
191- } ) ;
192- }
193- }
194- return syntaxErrors ;
195- }
196-
197- private hasFlowAnnotation ( sourceFile : ts . SourceFile ) {
198- if ( sourceFile . fileName . endsWith ( '.js' ) || sourceFile . fileName . endsWith ( '.jsx' ) ) {
199- const comments = sourceFile . getFullText ( ) . match ( / \/ \* [ \s \S ] * ?\* \/ | \/ \/ .* (? = [ \r \n ] ) / g) ;
200- if ( comments ) {
201- return comments . some ( comment => comment . includes ( "@flow" ) ) ;
202- }
203- }
204- return false ;
205- }
206-
207175 accept ( path : string ) : boolean {
208176 return path . endsWith ( '.ts' ) || path . endsWith ( '.tsx' ) || path . endsWith ( '.js' ) || path . endsWith ( '.jsx' ) ;
209177 }
@@ -1866,7 +1834,7 @@ export class JavaScriptParserVisitor {
18661834 Markers . EMPTY
18671835 ) ,
18681836 node . qualifier ? this . leftPadded ( this . prefix ( this . findChildNode ( node , ts . SyntaxKind . DotToken ) ! ) , this . visit ( node . qualifier ) ) : null ,
1869- node . typeArguments ? this . mapTypeArguments ( this . suffix ( node . qualifier ! ) , node . typeArguments ) : null ,
1837+ node . typeArguments ? this . mapTypeArguments ( this . prefix ( this . findChildNode ( node , ts . SyntaxKind . LessThanToken ) ! ) , node . typeArguments ) : null ,
18701838 this . mapType ( node )
18711839 ) ;
18721840 }
0 commit comments