@@ -12,14 +12,16 @@ import {
12
12
import semver from 'semver' ;
13
13
import ts from 'typescript' ;
14
14
import convert from './ast-converter' ;
15
+ import { convertError } from './convert' ;
16
+ import { Program } from './estree/spec' ;
17
+ import util from './node-utils' ;
15
18
import {
16
- Extra ,
17
- ParserOptions ,
19
+ ESTreeComment ,
18
20
ESTreeToken ,
19
- ESTreeComment
21
+ Extra ,
22
+ ParserOptions
20
23
} from './temp-types-based-on-js-source' ;
21
- import { Program } from './estree/spec' ;
22
- import util from './node-utils' ;
24
+ import { getFirstSemanticOrSyntacticError } from './semantic-errors' ;
23
25
24
26
const packageJSON = require ( '../package.json' ) ;
25
27
@@ -59,6 +61,7 @@ function resetExtra(): void {
59
61
log : console . log ,
60
62
projects : [ ] ,
61
63
errorOnUnknownASTType : false ,
64
+ errorOnTypeScriptSyntaticAndSemanticIssues : false ,
62
65
code : '' ,
63
66
tsconfigRootDir : process . cwd ( ) ,
64
67
extraFileExtensions : [ ]
@@ -246,6 +249,18 @@ function generateAST<T extends ParserOptions = ParserOptions>(
246
249
extra . errorOnUnknownASTType = true ;
247
250
}
248
251
252
+ /**
253
+ * Retrieve semantic and syntactic diagnostics from the underlying TypeScript Program
254
+ * and turn them into parse errors
255
+ */
256
+ if (
257
+ shouldGenerateServices &&
258
+ typeof options . errorOnTypeScriptSyntaticAndSemanticIssues === 'boolean' &&
259
+ options . errorOnTypeScriptSyntaticAndSemanticIssues
260
+ ) {
261
+ extra . errorOnTypeScriptSyntaticAndSemanticIssues = true ;
262
+ }
263
+
249
264
if ( typeof options . useJSXTextNode === 'boolean' && options . useJSXTextNode ) {
250
265
extra . useJSXTextNode = true ;
251
266
}
@@ -304,7 +319,23 @@ function generateAST<T extends ParserOptions = ParserOptions>(
304
319
) ;
305
320
306
321
extra . code = code ;
322
+
323
+ /**
324
+ * Convert the AST
325
+ */
307
326
const { estree, astMaps } = convert ( ast , extra , shouldProvideParserServices ) ;
327
+
328
+ /**
329
+ * Even if TypeScript parsed the source code ok, and we had no problems converting the AST,
330
+ * there may be other syntactic or semantic issues in the code that we can optionally report on.
331
+ */
332
+ if ( program && extra . errorOnTypeScriptSyntaticAndSemanticIssues ) {
333
+ const error = getFirstSemanticOrSyntacticError ( program , ast ) ;
334
+ if ( error ) {
335
+ throw convertError ( error ) ;
336
+ }
337
+ }
338
+
308
339
return {
309
340
estree,
310
341
program : shouldProvideParserServices ? program : undefined ,
@@ -327,6 +358,11 @@ export function parse<T extends ParserOptions = ParserOptions>(
327
358
code : string ,
328
359
options ?: T
329
360
) {
361
+ if ( options && options . errorOnTypeScriptSyntaticAndSemanticIssues ) {
362
+ throw new Error (
363
+ `"errorOnTypeScriptSyntaticAndSemanticIssues" is only supported for parseAndGenerateServices()`
364
+ ) ;
365
+ }
330
366
return generateAST < T > ( code , options ) . estree ;
331
367
}
332
368
0 commit comments