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

Implement Export{Default,Namespace}Specifier parsing ourselves. #146

Merged
merged 6 commits into from
Apr 25, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 10 additions & 3 deletions lib/parsers/acorn-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
const tt = require("acorn").tokTypes;
const hasOwn = Object.prototype.hasOwnProperty;

exports.fix = function (parser) {
exports.enableAll = function (parser) {
exports.enableTolerance(parser);
exports.enableExportExtensions(parser);
};

exports.enableTolerance = function (parser) {
// It's not Reify's job to enforce strictness.
parser.strict = false;

// Tolerate recoverable parse errors.
parser.raiseRecoverable = noopRaiseRecoverable;
};

function noopRaiseRecoverable() {}

exports.enableExportExtensions = function (parser) {
// Lookahead method.
parser.lookAhead = lookAhead;

Expand All @@ -23,8 +32,6 @@ exports.fix = function (parser) {
parser.shouldParseExportDeclaration = shouldParseExportDeclaration;
};

function noopRaiseRecoverable() {}

function parseExport(node, exports) {
this.next();
if (this.type === tt.star) {
Expand Down
16 changes: 13 additions & 3 deletions lib/parsers/acorn.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

const acorn = require("acorn");
const fixParser = require("./acorn-extensions.js").fix;
let acorn = null;
let acornExtensions = null;

exports.options = {
ecmaVersion: 8,
Expand All @@ -12,8 +12,18 @@ exports.options = {
};

function acornParse(code) {
if (acorn === null) {
acorn = require("acorn");
}

if (acornExtensions === null) {
acornExtensions = require("./acorn-extensions.js");
}

const parser = new acorn.Parser(exports.options, code);
fixParser(parser);

acornExtensions.enableAll(parser);

return parser.parse();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the lazy loading of acorn and acorn-extensions do?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, probably not much if we always use the parser when we import this module (which I think we do). Are you concerned it might hurt?

}

Expand Down
17 changes: 14 additions & 3 deletions lib/parsers/top-level.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

const acorn = require("acorn");
const fixParser = require("./acorn-extensions.js").fix;
let acorn = null;
let acornExtensions = null;

exports.options = {
ecmaVersion: 8,
Expand All @@ -26,10 +26,21 @@ function quickParseBlock() {
}

function topLevelParse(code) {
if (acorn === null) {
acorn = require("acorn");
}

if (acornExtensions === null) {
acornExtensions = require("./acorn-extensions.js");
}

const parser = new acorn.Parser(exports.options, code);
fixParser(parser);

acornExtensions.enableAll(parser);

// Override the Parser's parseBlock method.
parser.parseBlock = quickParseBlock;

return parser.parse();
}

Expand Down