js-slang icon indicating copy to clipboard operation
js-slang copied to clipboard

Source Typed Parser doesn't respect throwOnError

Open leeyi45 opened this issue 1 year ago • 0 comments

Regardless on whether throwOnError is specified as true or false, the typed parser doesn't throw any of the errors it finds.

I can provide some suggestions for fixes, but I'm not familiar with the internals of the checker itself.

// in the parse() method of the SourceTypedParser
try {
  TypeParser.parse(
    programStr,
   // throwOnError should be passed to createAcornParserOptions
    createAcornParserOptions(DEFAULT_ECMA_VERSION, context.errors, options, throwOnError)
  )
} catch (error) {
  if (error instanceof SyntaxError) {
    error = new FatalSyntaxError(
      positionToSourceLocation((error as any).loc, options?.sourceFile),
      error.toString()
    )
  }

  if (throwOnError) throw error
  context.errors.push(error)

  return null
}

// Parse again with babel parser to capture all type syntax
// and catch remaining syntax errors not caught by acorn type parser
const ast = babelParse(programStr, {
  ...defaultBabelOptions,
  sourceFilename: options?.sourceFile,
  errorRecovery: !throwOnError // was incorrectly written as throwOnError ?? false
})

if (ast.errors.length) {
  ast.errors
    .filter(error => error instanceof SyntaxError)
    .forEach(error => {
      context.errors.push(
        new FatalSyntaxError(
          positionToSourceLocation((error as any).loc, options?.sourceFile),
          error.toString()
        )
      )
    })

  return null
}

const typedProgram: TypedES.Program = ast.program as TypedES.Program
// checkForTypeErrors does not throw errors
const typedCheckedProgram: Program = checkForTypeErrors(typedProgram, context)

transformBabelASTToESTreeCompliantAST(typedCheckedProgram)

leeyi45 avatar May 03 '23 22:05 leeyi45