update to latest acorn, better array shortcuts, don't add code frame to error message

This commit is contained in:
Sebastian McKenzie 2015-03-20 20:05:29 +11:00
parent c8b88182f6
commit a602873281
46 changed files with 322 additions and 202 deletions

View File

@ -5,7 +5,7 @@ transform.version = require("../../../package").version;
transform.transform = transform;
transform.run = function (code, opts = {}) {
opts.sourceMap = "inline";
opts.sourceMaps = "inline";
return new Function(transform(code, opts).code)();
};

View File

@ -6,7 +6,7 @@ export default class SourceMap {
this.position = position;
this.opts = opts;
if (opts.sourceMap) {
if (opts.sourceMaps) {
this.map = new sourceMap.SourceMapGenerator({
file: opts.sourceMapName,
sourceRoot: opts.sourceRoot

View File

@ -3,15 +3,13 @@ import repeating from "repeating";
import jsTokens from "js-tokens";
import esutils from "esutils";
import chalk from "chalk";
import ary from "lodash/function/ary";
var defs = {
string: chalk.red,
punctuator: chalk.white.bold,
punctuator: chalk.bold,
curly: chalk.green,
parens: chalk.blue.bold,
square: chalk.yellow,
name: chalk.white,
keyword: chalk.cyan,
number: chalk.magenta,
regex: chalk.magenta,
@ -19,50 +17,51 @@ var defs = {
invalid: chalk.inverse
};
var newline = /\r\n|[\n\r\u2028\u2029]/;
const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
var highlight = function (text) {
var tokenType = function (match) {
var token = jsTokens.matchToToken(match);
if (token.type === "name" && esutils.keyword.isReservedWordES6(token.value)) {
return "keyword";
function getTokenType(match) {
var token = jsTokens.matchToToken(match);
if (token.type === "name" && esutils.keyword.isReservedWordES6(token.value)) {
return "keyword";
}
if (token.type === "punctuator") {
switch (token.value) {
case "{":
case "}":
return "curly";
case "(":
case ")":
return "parens";
case "[":
case "]":
return "square";
}
}
if (token.type === "punctuator") {
switch (token.value) {
case "{":
case "}":
return "curly";
case "(":
case ")":
return "parens";
case "[":
case "]":
return "square";
}
return token.type;
}
function highlight(text) {
return text.replace(jsTokens, function (...args) {
var type = getTokenType(args);
var colorize = defs[type];
if (colorize) {
return args[0].split(NEWLINE).map(str => colorize(str)).join("\n");
} else {
return args[0];
}
return token.type;
};
return text.replace(jsTokens, function (match) {
var type = tokenType(arguments);
if (type in defs) {
var colorize = ary(defs[type], 1);
return match.split(newline).map(colorize).join("\n");
}
return match;
});
};
}
export default function (lines: number, lineNumber: number, colNumber: number, color?): string {
export default function (lines: number, lineNumber: number, colNumber: number, opts?): string {
colNumber = Math.max(colNumber, 0);
if (color && chalk.supportsColor) {
if (opts.highlightCode && chalk.supportsColor) {
lines = highlight(lines);
}
lines = lines.split(newline);
lines = lines.split(NEWLINE);
var start = Math.max(lineNumber - 3, 0);
var end = Math.min(lines.length, lineNumber + 3);
@ -72,7 +71,7 @@ export default function (lines: number, lineNumber: number, colNumber: number, c
end = lines.length;
}
return "\n" + lineNumbers(lines.slice(start, end), {
return lineNumbers(lines.slice(start, end), {
start: start + 1,
before: " ",
after: " | ",

View File

@ -40,11 +40,12 @@ export default function (opts, code, callback) {
if (!err._babel) {
err._babel = true;
var message = `${opts.filename}: ${err.message}`;
var message = err.message = `${opts.filename}: ${err.message}`;
var loc = err.loc;
if (loc) {
message += codeFrame(code, loc.line, loc.column + 1, opts.highlightErrors);
err.codeFrame = codeFrame(code, loc.line, loc.column + 1, opts);
message += "\n" + err.codeFrame;
}
if (err.stack) {
@ -55,8 +56,6 @@ export default function (opts, code, callback) {
// `err.stack` may be a readonly property in some environments
}
}
err.message = message;
}
throw err;

View File

@ -65,6 +65,7 @@ export default class File {
"to-array",
"to-consumable-array",
"sliced-to-array",
"sliced-to-array-loose",
"object-without-properties",
"has-own",
"slice",
@ -111,11 +112,15 @@ export default class File {
var optionParser = optionParsers[option.type];
if (optionParser) val = optionParser(key, val);
opts[key] = val;
if (option.alias) {
opts[option.alias] ||= val;
} else {
opts[key] = val;
}
}
if (opts.inputSourceMap) {
opts.sourceMap = true;
opts.sourceMaps = true;
}
// normalize windows path separators to unix
@ -382,9 +387,9 @@ export default class File {
//
var parseOpts = {
highlightErrors: opts.highlightErrors,
filename: opts.filename,
plugins: {}
highlightCode: opts.highlightCode,
filename: opts.filename,
plugins: {}
};
var features = parseOpts.features = {};
@ -448,9 +453,8 @@ export default class File {
var stack = this.transformerStack;
for (var i = 0; i < stack.length; i++) {
var transformer = stack[i].transformer;
if (transformer[key]) {
transformer[key](this);
}
var fn = transformer[key];
if (fn) fn(this);
}
}
@ -527,11 +531,11 @@ export default class File {
result.map = this.mergeSourceMap(result.map);
if (opts.sourceMap === "inline" || opts.sourceMap === "both") {
if (opts.sourceMaps === "inline" || opts.sourceMaps === "both") {
result.code += "\n" + convertSourceMap.fromObject(result.map).toComment();
}
if (opts.sourceMap === "inline") {
if (opts.sourceMaps === "inline") {
result.map = null;
}

View File

@ -12,6 +12,10 @@ export function transformerList(key, val) {
}
export function boolean(key, val) {
return !!val;
}
export function booleanString(key, val) {
return util.booleanify(val);
}

View File

@ -19,8 +19,12 @@
},
"highlightErrors": {
"description": "ANSI syntax highlight error messages",
"highlightTheme": {
"hidden": true
},
"highlightCode": {
"description": "ANSI syntax highlight code frames",
"type": "boolean",
"default": true
},
@ -135,7 +139,12 @@
},
"sourceMap": {
"type": "string",
"alias": "sourceMaps",
"hidden": true
},
"sourceMaps": {
"type": "booleanString",
"default": false,
"shorthand": "s"
},

View File

@ -1,5 +1,5 @@
for (var LOOP_OBJECT = OBJECT,
IS_ARRAY = Array.isArray(LOOP_OBJECT),
IS_ARRAY = LOOP_OBJECT && LOOP_OBJECT.constructor === Array,
INDEX = 0,
LOOP_OBJECT = IS_ARRAY ? LOOP_OBJECT : LOOP_OBJECT[Symbol.iterator]();;) {
var ID;

View File

@ -0,0 +1,14 @@
(function (arr, i) {
if (arr && arr.constructor === Array) {
return arr;
} else if (Symbol.iterator in Object(arr)) {
var _arr = [];
for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
_arr.push(_step.value);
if (i && _arr.length === i) break;
}
return _arr;
} else {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
});

View File

@ -1,5 +1,5 @@
(function (arr, i) {
if (Array.isArray(arr)) {
if (arr && arr.constructor === Array) {
return arr;
} else if (Symbol.iterator in Object(arr)) {
// this is an expanded form of `for...of` that properly supports abrupt completions of
@ -17,9 +17,8 @@
var _d = false;
var _e = undefined;
try {
for (var _i = arr[Symbol.iterator](), _s; !(_n (_s = _i.next()).done); _n = true) {
var val = _s.value;
_arr.push(_step.value);
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {

View File

@ -1,3 +1,3 @@
(function (arr) {
return Array.isArray(arr) ? arr : Array.from(arr);
return arr && arr.constructor === Array ? arr : Array.from(arr);
});

View File

@ -1,5 +1,5 @@
(function (arr) {
if (Array.isArray(arr)) {
if (arr && arr.constructor === Array) {
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
return arr2;
} else {

View File

@ -1,6 +1,10 @@
import * as messages from "../../../messages";
import * as t from "../../../types";
export var metadata = {
readOnly: true
};
export function ForOfStatement(node, parent, scope, file) {
var left = node.left;
if (t.isVariableDeclaration(left)) {

View File

@ -38,8 +38,9 @@ traverse.node = function (node, opts, scope, state, parentPath) {
};
const CLEAR_KEYS = [
"trailingComments", "leadingComments", "_declarations", "extendedRange",
"_paths", "tokens", "range", "start", "end", "loc", "raw"
"trailingComments", "leadingComments", "extendedRange",
"_declarations", "_scopeInfo" ,"_paths",
"tokens", "range", "start", "end", "loc", "raw"
];
function clearNode(node) {

View File

@ -5,6 +5,7 @@ import isString from "lodash/lang/isString";
import traverse from "../index";
import includes from "lodash/collection/includes";
import assign from "lodash/object/assign";
import extend from "lodash/object/extend";
import Scope from "../scope";
import * as t from "../../types";
@ -158,7 +159,6 @@ export default class TraversalPath {
}
this.flatten();
// TODO: duplicate internal path metadata across the new node paths
}
}
@ -210,15 +210,24 @@ export default class TraversalPath {
return this.shouldStop;
}
get(key) {
var node = this.node;
var container = node[key];
if (Array.isArray(container)) {
return container.map((_, i) => {
return TraversalPath.get(this, this.context, node, container, i);
});
} else {
return TraversalPath.get(this, this.context, node, node, key);
get(key: string): TraversalPath {
var parts = key.split(".");
if (parts.length === 1) { // "foo.bar"
var node = this.node;
var container = node[key];
if (Array.isArray(container)) {
return container.map((_, i) => {
return TraversalPath.get(this, this.context, node, container, i);
});
} else {
return TraversalPath.get(this, this.context, node, node, key);
}
} else { // "foo"
var path = this;
for (var i = 0; i > parts.length; i++) {
path = path.get(parts[i]);
}
return path;
}
}

View File

@ -294,6 +294,7 @@ export default class Scope {
} else if (i) {
args.push(t.literal(i));
helperName = "sliced-to-array";
if (this.file.isLoose("es6.forOf")) helperName += "-loose";
}
return t.callExpression(file.addHelper(helperName), args);
}
@ -400,14 +401,14 @@ export default class Scope {
//
var info = path.getData("scopeInfo");
var info = this.block._scopeInfo;
if (info) return extend(this, info);
info = path.setData("scopeInfo", {
info = this.block._scopeInfo = {
bindings: object(),
globals: object(),
uids: object()
});
};
extend(this, info);

View File

@ -282,6 +282,7 @@ export function inheritsComments(child: Object, parent: Object): Object {
export function inherits(child: Object, parent: Object): Object {
child._declarations = parent._declarations;
child._scopeInfo = parent._scopeInfo;
child.range = parent.range;
child.start = parent.start;
child.loc = parent.loc;

View File

@ -1,5 +1,6 @@
import "./patch";
import escapeRegExp from "lodash/string/escapeRegExp";
import buildDebug from "debug/node";
import cloneDeep from "lodash/lang/cloneDeep";
import isBoolean from "lodash/lang/isBoolean";
@ -43,7 +44,7 @@ export function list(val: string): Array<string> {
export function regexify(val: any): RegExp {
if (!val) return new RegExp(/.^/);
if (Array.isArray(val)) val = val.join("|");
if (Array.isArray(val)) val = new RegExp(val.map(escapeRegExp).join("|"), "i");
if (isString(val)) return minimatch.makeRe(val, { nocase: true });
if (isRegExp(val)) return val;
throw new TypeError("illegal type for regexify");

View File

@ -1,5 +0,0 @@
var dynamicThisGetter = () => function () { return this; };
assert.equal(
'(' + dynamicThisGetter.toString() + ')',
'(function () {\n return function () {\n return this;\n };\n})'
);

View File

@ -1,6 +1,6 @@
"use strict";
for (var _iterator = arr, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
for (var _iterator = arr, _isArray = _iterator && _iterator.constructor === Array, _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
if (_isArray) {
if (_i >= _iterator.length) break;
i = _iterator[_i++];

View File

@ -1,6 +1,6 @@
"use strict";
for (var _iterator = foo, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
for (var _iterator = foo, _isArray = _iterator && _iterator.constructor === Array, _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {

View File

@ -1,6 +1,6 @@
"use strict";
for (var _iterator = arr, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
for (var _iterator = arr, _isArray = _iterator && _iterator.constructor === Array, _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {

View File

@ -1,6 +1,6 @@
"use strict";
for (var _iterator = arr, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
for (var _iterator = arr, _isArray = _iterator && _iterator.constructor === Array, _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
if (_isArray) {
if (_i >= _iterator.length) break;
obj.prop = _iterator[_i++];

View File

@ -1,6 +1,6 @@
"use strict";
for (var _iterator = arr, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
for (var _iterator = arr, _isArray = _iterator && _iterator.constructor === Array, _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
@ -15,7 +15,7 @@ for (var _iterator = arr, _isArray = Array.isArray(_iterator), _i = 0, _iterator
var i = _ref;
}
for (var _iterator2 = numbers, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
for (var _iterator2 = numbers, _isArray2 = _iterator2 && _iterator2.constructor === Array, _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
var _ref2;
if (_isArray2) {

View File

@ -1,6 +1,6 @@
"use strict";
for (var _iterator = arr, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
for (var _iterator = arr, _isArray = _iterator && _iterator.constructor === Array, _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {

View File

@ -1,8 +1,7 @@
"use strict";
var _slice = Array.prototype.slice;
function foo() {
return bar([].concat(_slice.call(arguments)));
return bar([].concat(babelHelpers.slice.call(arguments)));
}
function bar(one, two, three) {

View File

@ -1,8 +1,7 @@
"use strict";
var _slice = Array.prototype.slice;
function foo() {
return bar.apply(undefined, ["test"].concat(_slice.call(arguments)));
return bar.apply(undefined, ["test"].concat(babelHelpers.slice.call(arguments)));
}
function bar(one, two, three) {

View File

@ -1,5 +1,3 @@
"use strict";
var _toConsumableArray = function (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } };
var lyrics = [].concat(_toConsumableArray(parts), ["head", "and", "toes"]);
var lyrics = [].concat(babelHelpers.toConsumableArray(parts), ["head", "and", "toes"]);

View File

@ -1,5 +1,3 @@
"use strict";
var _toConsumableArray = function (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } };
var a = [b].concat(_toConsumableArray(c), [d]);
var a = [b].concat(babelHelpers.toConsumableArray(c), [d]);

View File

@ -1,5 +1,3 @@
"use strict";
var _toConsumableArray = function (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } };
var a = [b].concat(_toConsumableArray(c), [d, e], _toConsumableArray(f));
var a = [b].concat(babelHelpers.toConsumableArray(c), [d, e], babelHelpers.toConsumableArray(f));

View File

@ -1,5 +1,3 @@
"use strict";
var _toConsumableArray = function (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } };
var lyrics = ["head", "and", "toes"].concat(_toConsumableArray(parts));
var lyrics = ["head", "and", "toes"].concat(babelHelpers.toConsumableArray(parts));

View File

@ -2,6 +2,4 @@
var _obj;
var _toConsumableArray = function (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } };
(_obj = obj)[method].apply(_obj, [foo, bar].concat(_toConsumableArray(args)));
(_obj = obj)[method].apply(_obj, [foo, bar].concat(babelHelpers.toConsumableArray(args)));

View File

@ -2,6 +2,4 @@
var _obj;
var _toConsumableArray = function (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } };
(_obj = obj)[method].apply(_obj, _toConsumableArray(args));
(_obj = obj)[method].apply(_obj, babelHelpers.toConsumableArray(args));

View File

@ -2,7 +2,5 @@
var _foob, _foob$test;
var _toConsumableArray = function (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } };
(_foob = foob).add.apply(_foob, [foo, bar].concat(_toConsumableArray(numbers)));
(_foob$test = foob.test).add.apply(_foob$test, [foo, bar].concat(_toConsumableArray(numbers)));
(_foob = foob).add.apply(_foob, [foo, bar].concat(babelHelpers.toConsumableArray(numbers)));
(_foob$test = foob.test).add.apply(_foob$test, [foo, bar].concat(babelHelpers.toConsumableArray(numbers)));

View File

@ -2,7 +2,5 @@
var _foob, _foob$test;
var _toConsumableArray = function (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } };
(_foob = foob).add.apply(_foob, _toConsumableArray(numbers));
(_foob$test = foob.test).add.apply(_foob$test, _toConsumableArray(numbers));
(_foob = foob).add.apply(_foob, babelHelpers.toConsumableArray(numbers));
(_foob$test = foob.test).add.apply(_foob$test, babelHelpers.toConsumableArray(numbers));

View File

@ -1,5 +1,3 @@
"use strict";
var _toConsumableArray = function (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } };
add.apply(undefined, _toConsumableArray(numbers).concat([foo, bar]));
add.apply(undefined, babelHelpers.toConsumableArray(numbers).concat([foo, bar]));

View File

@ -1,5 +1,3 @@
"use strict";
var _toConsumableArray = function (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } };
add.apply(undefined, [foo].concat(_toConsumableArray(numbers), [bar]));
add.apply(undefined, [foo].concat(babelHelpers.toConsumableArray(numbers), [bar]));

View File

@ -1,5 +1,3 @@
"use strict";
var _toConsumableArray = function (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } };
add.apply(undefined, [foo, bar].concat(_toConsumableArray(numbers)));
add.apply(undefined, [foo, bar].concat(babelHelpers.toConsumableArray(numbers)));

View File

@ -1,5 +1,3 @@
"use strict";
var _toConsumableArray = function (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } };
add.apply(undefined, [foo].concat(_toConsumableArray(numbers), [bar, what], _toConsumableArray(test)));
add.apply(undefined, [foo].concat(babelHelpers.toConsumableArray(numbers), [bar, what], babelHelpers.toConsumableArray(test)));

View File

@ -1,5 +1,3 @@
"use strict";
var _toConsumableArray = function (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } };
add.apply(undefined, _toConsumableArray(numbers));
add.apply(undefined, babelHelpers.toConsumableArray(numbers));

View File

@ -1,8 +1,4 @@
"use strict";
var _applyConstructor = function (Constructor, args) { var instance = Object.create(Constructor.prototype); var result = Constructor.apply(instance, args); return result != null && (typeof result == "object" || typeof result == "function") ? result : instance; };
var _toConsumableArray = function (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } };
_applyConstructor(Numbers, _toConsumableArray(nums));
_applyConstructor(Numbers, [1].concat(_toConsumableArray(nums)));
babelHelpers.applyConstructor(Numbers, babelHelpers.toConsumableArray(nums));
babelHelpers.applyConstructor(Numbers, [1].concat(babelHelpers.toConsumableArray(nums)));

View File

@ -1,3 +1,4 @@
{
"blacklist": ["es6.tailCall"]
}
{
"externalHelpers": true,
"blacklist": ["es6.tailCall"]
}

View File

@ -1,5 +1,3 @@
"use strict";
var _toConsumableArray = function (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } };
[].concat(_toConsumableArray(foo));
[].concat(babelHelpers.toConsumableArray(foo));

View File

@ -78,8 +78,8 @@ suite("util", function () {
assert.deepEqual(util.regexify(false), /.^/);
assert.deepEqual(util.regexify(null), /.^/);
assert.deepEqual(util.regexify(""), /.^/);
assert.deepEqual(util.regexify(["foo", "bar"]), /foo|bar/);
assert.deepEqual(util.regexify("foobar"), /foobar/);
assert.deepEqual(util.regexify(["foo", "bar"]), /foo|bar/i);
assert.deepEqual(util.regexify("foobar"), /^(?:(?=.)foobar)$/i);
assert.deepEqual(util.regexify(/foobar/), /foobar/);
assert.throws(function () {

110
vendor/acorn/acorn.js vendored
View File

@ -243,6 +243,7 @@
this.label = label;
this.keyword = conf.keyword;
this.beforeExpr = !!conf.beforeExpr;
this.startsExpr = !!conf.startsExpr;
this.rightAssociative = !!conf.rightAssociative;
this.isLoop = !!conf.isLoop;
this.isAssign = !!conf.isAssign;
@ -255,21 +256,21 @@
function binop(name, prec) {
return new TokenType(name, {beforeExpr: true, binop: prec});
}
var beforeExpr = {beforeExpr: true};
var beforeExpr = {beforeExpr: true}, startsExpr = {startsExpr: true};;
var tt = exports.tokTypes = {
num: new TokenType("num"),
regexp: new TokenType("regexp"),
string: new TokenType("string"),
name: new TokenType("name"),
num: new TokenType("num", startsExpr),
regexp: new TokenType("regexp", startsExpr),
string: new TokenType("string", startsExpr),
name: new TokenType("name", startsExpr),
eof: new TokenType("eof"),
// Punctuation token types.
bracketL: new TokenType("[", beforeExpr),
bracketL: new TokenType("[", {beforeExpr: true, startsExpr: true}),
bracketR: new TokenType("]"),
braceL: new TokenType("{", beforeExpr),
braceL: new TokenType("{", {beforeExpr: true, startsExpr: true}),
braceR: new TokenType("}"),
parenL: new TokenType("(", beforeExpr),
parenL: new TokenType("(", {beforeExpr: true, startsExpr: true}),
parenR: new TokenType(")"),
comma: new TokenType(",", beforeExpr),
semi: new TokenType(";", beforeExpr),
@ -279,8 +280,8 @@
arrow: new TokenType("=>", beforeExpr),
template: new TokenType("template"),
ellipsis: new TokenType("...", beforeExpr),
backQuote: new TokenType("`"),
dollarBraceL: new TokenType("${", beforeExpr),
backQuote: new TokenType("`", startsExpr),
dollarBraceL: new TokenType("${", {beforeExpr: true, startsExpr: true}),
// Operators. These carry several kinds of properties to help the
// parser use them properly (the presence of these properties is
@ -298,8 +299,8 @@
eq: new TokenType("=", {beforeExpr: true, isAssign: true}),
assign: new TokenType("_=", {beforeExpr: true, isAssign: true}),
incDec: new TokenType("++/--", {prefix: true, postfix: true}),
prefix: new TokenType("prefix", {beforeExpr: true, prefix: true}),
incDec: new TokenType("++/--", {prefix: true, postfix: true, startsExpr: true}),
prefix: new TokenType("prefix", {beforeExpr: true, prefix: true, startsExpr: true}),
logicalOR: binop("||", 1),
logicalAND: binop("&&", 2),
bitwiseOR: binop("|", 3),
@ -308,7 +309,7 @@
equality: binop("==/!=", 6),
relational: binop("</>", 7),
bitShift: binop("<</>>", 8),
plusMin: new TokenType("+/-", {beforeExpr: true, binop: 9, prefix: true}),
plusMin: new TokenType("+/-", {beforeExpr: true, binop: 9, prefix: true, startsExpr: true}),
modulo: binop("%", 10),
star: binop("*", 10),
slash: binop("/", 10),
@ -347,22 +348,22 @@
kw("const");
kw("while", {isLoop: true});
kw("with");
kw("new", beforeExpr);
kw("this");
kw("super");
kw("new", {beforeExpr: true, startsExpr: true});
kw("this", startsExpr);
kw("super", startsExpr);
kw("class");
kw("extends", beforeExpr);
kw("export");
kw("import");
kw("yield", beforeExpr);
kw("null");
kw("true");
kw("false");
kw("yield", {beforeExpr: true, startsExpr: true});
kw("null", startsExpr);
kw("true", startsExpr);
kw("false", startsExpr);
kw("in", {beforeExpr: true, binop: 7});
kw("instanceof", {beforeExpr: true, binop: 7});
kw("typeof", {beforeExpr: true, prefix: true});
kw("void", {beforeExpr: true, prefix: true});
kw("delete", {beforeExpr: true, prefix: true});
kw("typeof", {beforeExpr: true, prefix: true, startsExpr: true});
kw("void", {beforeExpr: true, prefix: true, startsExpr: true});
kw("delete", {beforeExpr: true, prefix: true, startsExpr: true});
// This is a trick taken from Esprima. It turns out that, on
// non-Chrome browsers, to check whether a string is in a set, a
@ -1125,13 +1126,15 @@
if (this.options.ecmaVersion >= 6) validFlags = /^[gmsiyu]*$/;
if (!validFlags.test(mods)) this.raise(start, "Invalid regular expression flag");
if (mods.indexOf('u') >= 0 && !regexpUnicodeSupport) {
// Replace each astral symbol and every Unicode code point
// escape sequence that represents such a symbol with a single
// ASCII symbol to avoid throwing on regular expressions that
// Replace each astral symbol and every Unicode escape sequence that
// possibly represents an astral symbol or a paired surrogate with a
// single ASCII symbol to avoid throwing on regular expressions that
// are only valid in combination with the `/u` flag.
tmp = tmp
.replace(/\\u\{([0-9a-fA-F]+)\}/g, "x")
.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "x");
// Note: replacing with the ASCII symbol `x` might cause false
// negatives in unlikely scenarios. For example, `[\u{61}-b]` is a
// perfectly valid pattern that is equivalent to `[a-b]`, but it would
// be replaced by `[x-b]` which throws an error.
tmp = tmp.replace(/\\u([a-fA-F0-9]{4})|\\u\{([0-9a-fA-F]+)\}|[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "x")
}
}
// Detect invalid regular expressions.
@ -1604,24 +1607,23 @@
// Convert list of expression atoms to binding list.
pp.toAssignableList = function(exprList, isBinding) {
if (exprList.length) {
for (var i = 0; i < exprList.length - 1; i++) {
this.toAssignable(exprList[i], isBinding);
}
var last = exprList[exprList.length - 1];
switch (last.type) {
case "RestElement":
break;
case "SpreadElement":
last.type = "RestElement";
var arg = last.argument;
this.toAssignable(arg, isBinding);
if (arg.type !== "Identifier" && arg.type !== "MemberExpression" && arg.type !== "ArrayPattern")
this.unexpected(arg.start);
break;
default:
this.toAssignable(last, isBinding);
}
var end = exprList.length;
if (end) {
var last = exprList[end - 1];
if (last && last.type == "RestElement") {
--end;
} else if (last && last.type == "SpreadElement") {
last.type = "RestElement";
var arg = last.argument;
this.toAssignable(arg, isBinding);
if (arg.type !== "Identifier" && arg.type !== "MemberExpression" && arg.type !== "ArrayPattern")
this.unexpected(arg.start);
--end;
}
}
for (var i = 0; i < end; i++) {
var elt = exprList[i];
if (elt) this.toAssignable(elt, isBinding);
}
return exprList;
};
@ -2193,6 +2195,8 @@
decl.init = this.parseMaybeAssign(noIn);
} else if (kind === tt._const && !(this.type === tt._in || (this.options.ecmaVersion >= 6 && this.isContextual("of")))) {
this.unexpected();
} else if (decl.id.type != "Identifier") {
this.raise(this.lastTokEnd, "Complex binding patterns require an initialization value");
} else {
decl.init = null;
}
@ -2634,12 +2638,10 @@
if (this.options.ecmaVersion >= 6) {
prop.method = false;
prop.shorthand = false;
if (isPattern || refShorthandDefaultPos) {
if (isPattern || refShorthandDefaultPos)
start = this.currentPos();
}
if (!isPattern) {
if (!isPattern)
isGenerator = this.eat(tt.star);
}
}
if (this.options.features["es7.asyncFunctions"] && this.isContextual("async")) {
if (isGenerator || isPattern) this.unexpected();
@ -2680,6 +2682,10 @@
} else if (this.options.ecmaVersion >= 6 && !prop.computed && prop.key.type === "Identifier") {
prop.kind = "init";
if (isPattern) {
if (this.isKeyword(prop.key.name) ||
(this.strict && (isStrictBadIdWord(prop.key.name) || isStrictReservedWord(prop.key.name))) ||
(!this.options.allowReserved && this.isReservedWord(prop.key.name)))
this.raise(prop.key.start, "Binding " + prop.key.name);
prop.value = this.parseMaybeDefault(start, prop.key);
} else if (this.type === tt.eq && refShorthandDefaultPos) {
if (!refShorthandDefaultPos.start)
@ -3040,7 +3046,7 @@
pp.parseYield = function() {
var node = this.startNode();
this.next();
if (this.type == tt.semi || this.canInsertSemicolon()) {
if (this.type == tt.semi || this.canInsertSemicolon() || (this.type != tt.star && !this.type.startsExpr)) {
node.delegate = false;
node.argument = null;
} else {

View File

@ -5989,6 +5989,53 @@ test("var x = { *test () { yield *v } };", {
locations: true
});
test("function* foo() { console.log(yield); }", {
body: [
{
id: {
name: "foo",
type: "Identifier",
},
generator: true,
expression: false,
params: [],
body: {
body: [
{
expression: {
callee: {
object: {
name: "console",
type: "Identifier",
},
property: {
name: "log",
type: "Identifier",
},
computed: false,
type: "MemberExpression",
},
arguments: [
{
delegate: false,
argument: null,
type: "YieldExpression",
}
],
type: "CallExpression",
},
type: "ExpressionStatement",
}
],
type: "BlockStatement",
},
type: "FunctionDeclaration",
}
],
sourceType: "script",
type: "Program"
}, {ecmaVersion: 6})
test("function* t() {}", {
type: "Program",
body: [{
@ -13814,6 +13861,34 @@ test("/[a-z]/u", {
ecmaVersion: 6
});
test("/[\\uD834\\uDF06-\\uD834\\uDF08a-z]/u", {
type: "Program",
body: [
{
type: "ExpressionStatement",
expression: {
type: "Literal",
regex: {
pattern: "[\\uD834\\uDF06-\\uD834\\uDF08a-z]",
flags: "u"
},
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 1,
column: 33
}
}
}
}
]
}, {
locations: true,
ecmaVersion: 6
});
test("do {} while (false) foo();", {
type: "Program",
@ -15419,6 +15494,36 @@ test("let {x} = y", {
"end": 11
}, {ecmaVersion: 6})
test("[x,,] = 1", {
type: "Program",
body: [
{
type: "ExpressionStatement",
expression: {
type: "AssignmentExpression",
operator: "=",
left: {
type: "ArrayPattern",
elements: [
{
type: "Identifier",
name: "x"
},
null
]
},
right: {
type: "Literal",
value: 1,
raw: "1"
}
}
}
]
}, {ecmaVersion: 6});
testFail("let [x]", "Complex binding patterns require an initialization value (1:7)", {ecmaVersion: 6})
testFail("var [x]", "Complex binding patterns require an initialization value (1:7)", {ecmaVersion: 6})
testFail("var _𖫵 = 11;", "Unexpected character '𖫵' (1:5)", {ecmaVersion: 6});
testFail("var 𫠞_ = 12;", "Unexpected character '𫠞' (1:4)", {ecmaVersion: 6});
testFail("var 𫠝_ = 10;", "Unexpected character '𫠝' (1:4)", {ecmaVersion: 5});
@ -15431,3 +15536,5 @@ testFail("'use strict'; [...eval] = arr", "Assigning to eval in strict mode (1:1
testFail("'use strict'; ({eval = defValue} = obj)", "Assigning to eval in strict mode (1:16)", {ecmaVersion: 6});
testFail("[...eval] = arr", "Assigning to eval in strict mode (1:4)", {ecmaVersion: 6, sourceType: "module"});
testFail("function* y({yield}) {}", "Binding yield (1:13)", {ecmaVersion: 6});