Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to babel 7 parser #73

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions lib/parser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var babylon = require('babylon');
var debug = require('./debug');
var babylon = require("@babel/parser");
var debug = require("./debug");

/**
* Parses the specified src string with babylon, returning the resulting AST
Expand All @@ -13,8 +13,8 @@ exports.parse = function(src, filePath) {
debug(`parsing ${filePath}`);
try {
return attempt(
() => _parse(src, filePath, 'script'),
() => _parse(src, filePath, 'module')
() => _parse(src, filePath, "script"),
() => _parse(src, filePath, "module")
);
} catch (err) {
let ctx = getErrorContext(err, src);
Expand All @@ -38,17 +38,30 @@ function _parse(src, filePath, sourceType) {
allowImportExportEverywhere: true,
sourceType: sourceType,
sourceFilename: filePath,
plugins: ['jsx', 'flow', 'doExpressions', 'objectRestSpread', 'decorators',
'classProperties', 'exportExtensions', 'asyncGenerators', 'functionBind',
'functionSent', 'dynamicImport']
plugins: [
"jsx",
"flow",
"doExpressions",
"objectRestSpread",
"decorators-legacy",
"classProperties",
"exportExtensions",
"asyncGenerators",
"functionBind",
"exportNamespaceFrom",
"functionSent",
"dynamicImport",
"nullishCoalescingOperator",
"optionalChaining"
]
}).program;
}

function getErrorContext(err, src) {
if (!err.loc || !err.loc.line || err.loc.column >= 100) return '';
if (!err.loc || !err.loc.line || err.loc.column >= 100) return "";

var line = src.split('\n')[err.loc.line - 1];
var caret = ' '.repeat(err.loc.column) + '^';
var line = src.split("\n")[err.loc.line - 1];
var caret = " ".repeat(err.loc.column) + "^";

return `\n${line}\n${caret}`;
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"url": "https://github.com/danielstjules/jsinspect.git"
},
"dependencies": {
"babylon": "6.16.1",
"@babel/parser": "^7.0.0-beta.49",
"chalk": "^2.1.0",
"commander": "^2.11.0",
"filepaths": "0.3.0",
Expand All @@ -31,7 +31,7 @@
"concat-stream": "^1.5.0",
"dirmap": "^0.0.2",
"expect.js": "^0.3.1",
"mocha": "^3.5.0"
"mocha": "^5.2.0"
},
"bin": {
"jsinspect": "./bin/jsinspect"
Expand Down