Skip to content

Commit

Permalink
It is very helpful to show the JS file that failed parse.
Browse files Browse the repository at this point in the history
The TypeScript compiler will accept even a broken program,
so for now, only going to support this for Esprima/JS.
  • Loading branch information
brentlintner committed Jun 7, 2017
1 parent 50be6b4 commit 1260b2e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/similar/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ const find_similar_methods_and_classes = (
) : synt.ParseResult[] =>
_.flatMap(filepaths, (filepath) => {
const code = fs.readFileSync(filepath).toString()
const node = astify(code, opts)
let node : es.Node

try {
node = astify(code, opts)
} catch (err) {
throw new Error(`in ${filepath}\n\n${err.stack}`)
}

return parse_methods_and_classes(node, filepath)
})

Expand Down
18 changes: 18 additions & 0 deletions test/spec/system.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ describe "system :: cli", ->
expect(stdout).to.eql(read CLI_OUTPUT_TEST_ES_MODULES_FAIL)
done()

describe "when a file fails to parse", ->
it "also shows the filename", (done) ->
cmd = CLI + " analyze -d --estype script #{FILE_JS_ES}"

child_process.exec cmd,
(error, stdout, stderr) ->
expect(error.code).to.eql 1
console.log(stdout)
expect(stderr)
.to.match new RegExp("Error: in test/fixtures/system/test-es.js")
expect(stderr)
.to.match new RegExp(path.relative(process.cwd(), FILE_JS_ES))

expect(stderr).to.match /line 1: unexpected token/i
expect(stderr).match /esprima/i
expect(stdout).to.eql(read CLI_OUTPUT_TEST_ES_MODULES_FAIL)
done()

describe "typescript", ->
it "can compare similar functions and classes", (done) ->
cmd = CLI + " analyze -d #{FILE_TS}"
Expand Down

0 comments on commit 1260b2e

Please sign in to comment.