Skip to content

Commit

Permalink
Remove esprima in favour of TypeScript parser (#7)
Browse files Browse the repository at this point in the history
The esprima parser is no longer actively maintained, meaning new
JavaScript features cause the script to fail to parse.

The TypeScript parser (already present) is able to parse newer syntax,
so we standardise on using it for validating all script types.

Signed-off-by: Thomas Chetwin <[email protected]>
  • Loading branch information
tchetwin authored Apr 2, 2019
1 parent bff9470 commit 04a399c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 49 deletions.
20 changes: 3 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bloomberg/pasta-sourcemaps",
"version": "1.0.4",
"version": "1.1.0",
"description": "Pretty (and) Accurate Stack Trace Analysis",
"main": "./dist/src/main.js",
"types": "./dist/src/main.d.ts",
Expand All @@ -23,7 +23,6 @@
"prepublishOnly": "npm run lint && npm run test"
},
"devDependencies": {
"@types/esprima": "^4.0.2",
"@types/tape": "^4.2.33",
"@typescript-eslint/eslint-plugin": "^1.0.0",
"cross-env": "^5.2.0",
Expand All @@ -37,7 +36,6 @@
"typedoc-plugin-sourcefile-url": "^1.0.3"
},
"dependencies": {
"esprima": "^4.0.0",
"typescript": "^3.2.2",
"vlq": "^1.0.0"
},
Expand Down
29 changes: 2 additions & 27 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { FunctionDesc } from "../src/functionDesc";
import { FileType } from "./types";

import * as ts from "typescript";
const esprima: typeof import("esprima") = require("esprima");

/**
* Parse a source file and return descriptions of all functions present in the
Expand Down Expand Up @@ -58,42 +57,18 @@ function parseTS(source: string, filetype: FileType): FunctionDesc[] {
true, // setParentNodes
scriptKind
);
switch (filetype) {
case "TypeScript":
case "TSX":
case "JSX":
validateTypeScript(tsSource);
break;
case "ECMAScript":
validateECMAScript(source);
break;
}
validateScript(tsSource);
const topLevelDesc = visitSourceNode(tsSource);
const otherDescs = traverseNode(tsSource);
return [topLevelDesc, ...otherDescs];
}

function validateECMAScript(source: string) {
// We don't know if the input will be a module or script.
// So we try parsing it as a module, and, failing that, as a script
try {
esprima.parseModule(source);
} catch (e1) {
try {
esprima.parseScript(source);
} catch (e2) {
e1.message = `Syntax error in source, ${e1.message}`;
throw e1;
}
}
}

let _program: ts.Program;
function getExpensiveProgram() {
return _program || (_program = ts.createProgram([""], {}));
}

function validateTypeScript(tsSource: ts.SourceFile) {
function validateScript(tsSource: ts.SourceFile) {
const program = getExpensiveProgram();
const diag = program.getSyntacticDiagnostics(tsSource);
if (diag.length > 0) {
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"newLine": "LF"
},
"include": [
"src/**/*.ts",
"src/**/*.ts",
"tests/**/*.ts"
],
],
"exclude": [
"tests/fixtures"
],
Expand Down

0 comments on commit 04a399c

Please sign in to comment.