Skip to content
This repository was archived by the owner on May 19, 2018. It is now read-only.
Merged
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
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ build
coverage
lib
node_modules
test
test/expressions
test/fixtures
6 changes: 6 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"root": true,
"extends": "babel",
"plugins": [
"prettier"
],
"rules": {
"prettier/prettier": ["error", { "trailingComma": "all" }]
},
"env": {
"node": true
}
Expand Down
4 changes: 2 additions & 2 deletions bin/babylon.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint no-var: 0 */

var babylon = require("..");
var fs = require("fs");
var fs = require("fs");

var filename = process.argv[2];
if (!filename) {
Expand All @@ -11,6 +11,6 @@ if (!filename) {
}

var file = fs.readFileSync(filename, "utf8");
var ast = babylon.parse(file);
var ast = babylon.parse(file);

console.log(JSON.stringify(ast, null, " "));
35 changes: 22 additions & 13 deletions bin/generate-identifier-regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
// Which Unicode version should be used?
const version = "9.0.0";

const start = require("unicode-" + version + "/Binary_Property/ID_Start/code-points.js")
.filter(function(ch) { return ch > 0x7f; });
const start = require("unicode-" +
version +
"/Binary_Property/ID_Start/code-points.js").filter(function(ch) {
return ch > 0x7f;
});
let last = -1;
const cont = [0x200c, 0x200d].concat(
require("unicode-" + version + "/Binary_Property/ID_Continue/code-points.js")
.filter(function(ch) {
return ch > 0x7f && search(start, ch, last + 1) == -1;
})
);
require("unicode-" +
version +
"/Binary_Property/ID_Continue/code-points.js").filter(function(ch) {
return ch > 0x7f && search(start, ch, last + 1) == -1;
}),
);

function search(arr, ch, starting) {
for (let i = starting; arr[i] <= ch && i < arr.length; last = i++)
if (arr[i] === ch)
return i;
if (arr[i] === ch) return i;
return -1;
}

Expand Down Expand Up @@ -56,7 +59,13 @@ function generate(chars) {
const startData = generate(start);
const contData = generate(cont);

console.log("let nonASCIIidentifierStartChars = \"" + startData.nonASCII + "\";");
console.log("let nonASCIIidentifierChars = \"" + contData.nonASCII + "\";");
console.log("const astralIdentifierStartCodes = " + JSON.stringify(startData.astral) + ";");
console.log("const astralIdentifierCodes = " + JSON.stringify(contData.astral) + ";");
console.log('let nonASCIIidentifierStartChars = "' + startData.nonASCII + '";');
console.log('let nonASCIIidentifierChars = "' + contData.nonASCII + '";');
console.log(
"const astralIdentifierStartCodes = " +
JSON.stringify(startData.astral) +
";",
);
console.log(
"const astralIdentifierCodes = " + JSON.stringify(contData.astral) + ";",
);
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@
"chalk": "^1.1.3",
"cross-env": "^5.0.0",
"eslint": "^4.0.0",
"eslint-config-babel": "^7.0.0",
"eslint-config-babel": "^7.0.1",
"eslint-plugin-flowtype": "^2.34.0",
"eslint-plugin-prettier": "^2.1.2",
"flow-bin": "^0.47.0",
"husky": "^0.14.1",
"lint-staged": "^4.0.0",
"nyc": "^11.0.2",
"prettier": "^1.5.2",
"rimraf": "^2.5.4",
"rollup": "^0.42.0",
"rollup-plugin-babel": "3.0.0-alpha.12",
Expand All @@ -51,7 +55,7 @@
"changelog": "git log `git describe --tags --abbrev=0`..HEAD --pretty=format:' * %s (%an)' | grep -v 'Merge pull request'",
"clean": "rimraf lib",
"flow": "flow",
"lint": "eslint src bin",
"lint": "eslint src bin test",
"prepublish": "cross-env BABEL_ENV=production yarn run build",
"preversion": "yarn run test && npm run changelog",
"test": "yarn run lint && yarn run flow && yarn run build -- -m && yarn run test-only",
Expand Down Expand Up @@ -86,5 +90,11 @@
"Tag: New Feature": ":rocket: New Feature",
"Tag: Polish": ":nail_care: Polish"
}
},
"lint-staged": {
"*.js": [
"eslint --format=codeframe --fix",
"git add"
]
}
}
13 changes: 13 additions & 0 deletions scripts/yarn-install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";

const exec = require("child_process").exec;

const runIfYarn = fn => {
exec("yarn -V", error => {
if (error === null) fn();
});
};
runIfYarn(() => {
console.log("`package.json` was changed. Running yarn...🐈");
exec("yarn");
});
30 changes: 19 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,46 +37,54 @@ export function parseExpression(input: string, options?: Options): Expression {
return parser.getExpression();
}


export { tokTypes };

function getParser(options: ?Options, input: string): Parser {
const cls = options && options.plugins ? getParserClass(options.plugins) : Parser;
const cls =
options && options.plugins ? getParserClass(options.plugins) : Parser;
return new cls(options, input);
}

const parserClassCache: { [key: string]: Class<Parser> } = {};

/** Get a Parser class with plugins applied. */
function getParserClass(pluginsFromOptions: $ReadOnlyArray<string>): Class<Parser> {

if (pluginsFromOptions.indexOf("decorators") >= 0 && pluginsFromOptions.indexOf("decorators2") >= 0) {
function getParserClass(
pluginsFromOptions: $ReadOnlyArray<string>,
): Class<Parser> {
if (
pluginsFromOptions.indexOf("decorators") >= 0 &&
pluginsFromOptions.indexOf("decorators2") >= 0
) {
throw new Error("Cannot use decorators and decorators2 plugin together");
}

// Filter out just the plugins that have an actual mixin associated with them.
let pluginList = pluginsFromOptions.filter((p) =>
p === "estree" || p === "flow" || p === "jsx" || p === "typescript");
let pluginList = pluginsFromOptions.filter(
p => p === "estree" || p === "flow" || p === "jsx" || p === "typescript",
);

if (pluginList.indexOf("flow") >= 0) {
// ensure flow plugin loads last
pluginList = pluginList.filter((plugin) => plugin !== "flow");
pluginList = pluginList.filter(plugin => plugin !== "flow");
pluginList.push("flow");
}

if (pluginList.indexOf("flow") >= 0 && pluginList.indexOf("typescript") >= 0) {
if (
pluginList.indexOf("flow") >= 0 &&
pluginList.indexOf("typescript") >= 0
) {
throw new Error("Cannot combine flow and typescript plugins.");
}

if (pluginList.indexOf("typescript") >= 0) {
// ensure typescript plugin loads last
pluginList = pluginList.filter((plugin) => plugin !== "typescript");
pluginList = pluginList.filter(plugin => plugin !== "typescript");
pluginList.push("typescript");
}

if (pluginList.indexOf("estree") >= 0) {
// ensure estree plugin loads first
pluginList = pluginList.filter((plugin) => plugin !== "estree");
pluginList = pluginList.filter(plugin => plugin !== "estree");
pluginList.unshift("estree");
}

Expand Down
20 changes: 10 additions & 10 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
// the parser process. These options are recognized:

export type Options = {
sourceType: "script" | "module";
sourceFilename?: string;
startLine: number;
allowReturnOutsideFunction: boolean;
allowImportExportEverywhere: boolean;
allowSuperOutsideMethod: boolean;
plugins: $ReadOnlyArray<string>;
strictMode: ?boolean;
ranges: boolean;
tokens: boolean;
sourceType: "script" | "module",
sourceFilename?: string,
startLine: number,
allowReturnOutsideFunction: boolean,
allowImportExportEverywhere: boolean,
allowSuperOutsideMethod: boolean,
plugins: $ReadOnlyArray<string>,
strictMode: ?boolean,
ranges: boolean,
tokens: boolean,
};

export const defaultOptions: Options = {
Expand Down
42 changes: 34 additions & 8 deletions src/parser/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ export default class CommentsParser extends BaseParser {
}
} else {
const lastInStack = last(stack);
if (stack.length > 0 && lastInStack.trailingComments && lastInStack.trailingComments[0].start >= node.end) {
if (
stack.length > 0 &&
lastInStack.trailingComments &&
lastInStack.trailingComments[0].start >= node.end
) {
trailingComments = lastInStack.trailingComments;
lastInStack.trailingComments = null;
}
Expand All @@ -93,7 +97,10 @@ export default class CommentsParser extends BaseParser {
if (lastComment.start >= node.start) {
if (this.state.commentPreviousNode) {
for (j = 0; j < this.state.leadingComments.length; j++) {
if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
if (
this.state.leadingComments[j].end <
this.state.commentPreviousNode.end
) {
this.state.leadingComments.splice(j, 1);
j--;
}
Expand All @@ -105,10 +112,18 @@ export default class CommentsParser extends BaseParser {
}
}
}
} else if (node.type === "CallExpression" && node.arguments && node.arguments.length) {
} else if (
node.type === "CallExpression" &&
node.arguments &&
node.arguments.length
) {
const lastArg = last(node.arguments);

if (lastArg && lastComment.start >= lastArg.start && lastComment.end <= node.end) {
if (
lastArg &&
lastComment.start >= lastArg.start &&
lastComment.end <= node.end
) {
if (this.state.commentPreviousNode) {
if (this.state.leadingComments.length > 0) {
lastArg.trailingComments = this.state.leadingComments;
Expand All @@ -121,7 +136,10 @@ export default class CommentsParser extends BaseParser {

if (lastChild) {
if (lastChild.leadingComments) {
if (lastChild !== node && last(lastChild.leadingComments).end <= node.start) {
if (
lastChild !== node &&
last(lastChild.leadingComments).end <= node.start
) {
node.leadingComments = lastChild.leadingComments;
lastChild.leadingComments = null;
} else {
Expand All @@ -140,7 +158,10 @@ export default class CommentsParser extends BaseParser {
if (last(this.state.leadingComments).end <= node.start) {
if (this.state.commentPreviousNode) {
for (j = 0; j < this.state.leadingComments.length; j++) {
if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
if (
this.state.leadingComments[j].end <
this.state.commentPreviousNode.end
) {
this.state.leadingComments.splice(j, 1);
j--;
}
Expand Down Expand Up @@ -173,7 +194,8 @@ export default class CommentsParser extends BaseParser {
// result in an empty array, and if so, the array must be
// deleted.
const leadingComments = this.state.leadingComments.slice(0, i);
node.leadingComments = leadingComments.length === 0 ? null : leadingComments;
node.leadingComments =
leadingComments.length === 0 ? null : leadingComments;

// Similarly, trailing comments are attached later. The variable
// must be reset to null if there are no trailing comments.
Expand All @@ -187,7 +209,11 @@ export default class CommentsParser extends BaseParser {
this.state.commentPreviousNode = node;

if (trailingComments) {
if (trailingComments.length && trailingComments[0].start >= node.start && last(trailingComments).end <= node.end) {
if (
trailingComments.length &&
trailingComments[0].start >= node.start &&
last(trailingComments).end <= node.end
) {
node.innerComments = trailingComments;
} else {
node.trailingComments = trailingComments;
Expand Down
Loading