diff options
Diffstat (limited to 'tools/eslint/node_modules/espree/espree.js')
-rw-r--r-- | tools/eslint/node_modules/espree/espree.js | 83 |
1 files changed, 42 insertions, 41 deletions
diff --git a/tools/eslint/node_modules/espree/espree.js b/tools/eslint/node_modules/espree/espree.js index d6e65c8d53..87074c8dd7 100644 --- a/tools/eslint/node_modules/espree/espree.js +++ b/tools/eslint/node_modules/espree/espree.js @@ -66,7 +66,7 @@ var astNodeTypes = require("./lib/ast-node-types"), var acorn = acornJSX(rawAcorn); - +var DEFAULT_ECMA_VERSION = 5; var lookahead, extra, lastToken; @@ -87,7 +87,7 @@ function resetExtra() { errors: [], strict: false, ecmaFeatures: {}, - ecmaVersion: 5, + ecmaVersion: DEFAULT_ECMA_VERSION, isModule: false }; } @@ -101,6 +101,37 @@ var tt = acorn.tokTypes, tt.jsxAttrValueToken = {}; /** + * Normalize ECMAScript version from the initial config + * @param {number} ecmaVersion ECMAScript version from the initial config + * @returns {number} normalized ECMAScript version + */ +function normalizeEcmaVersion(ecmaVersion) { + if (typeof ecmaVersion === "number") { + var version = ecmaVersion; + + // Calculate ECMAScript edition number from official year version starting with + // ES2015, which corresponds with ES6 (or a difference of 2009). + if (version >= 2015) { + version -= 2009; + } + + switch (version) { + case 3: + case 5: + case 6: + case 7: + case 8: + return version; + + default: + throw new Error("Invalid ecmaVersion."); + } + } else { + return DEFAULT_ECMA_VERSION; + } +} + +/** * Determines if a node is valid given the set of ecmaFeatures. * @param {ASTNode} node The node to check. * @returns {boolean} True if the node is allowed, false if not. @@ -163,13 +194,6 @@ function esprimaFinishNode(result) { } } - // Acorn currently uses expressions instead of declarations in default exports - if (result.type === "ExportDefaultDeclaration") { - if (/^(Class|Function)Expression$/.test(result.declaration.type)) { - result.declaration.type = result.declaration.type.replace("Expression", "Declaration"); - } - } - // Acorn uses undefined instead of null, which affects serialization if (result.type === "Literal" && result.value === undefined) { result.value = null; @@ -322,6 +346,7 @@ acorn.plugins.espree = function(instance) { instance.parseObj = function(isPattern, refShorthandDefaultPos) { var node = this.startNode(), first = true, + hasRestProperty = false, propHash = {}; node.properties = []; this.next(); @@ -331,6 +356,9 @@ acorn.plugins.espree = function(instance) { this.expect(tt.comma); if (this.afterTrailingComma(tt.braceR)) { + if (hasRestProperty) { + this.raise(node.properties[node.properties.length - 1].end, "Unexpected trailing comma after rest property"); + } break; } @@ -347,6 +375,7 @@ acorn.plugins.espree = function(instance) { if (extra.ecmaFeatures.experimentalObjectRestSpread && this.type === tt.ellipsis) { if (isPattern) { prop = this.parseObjectRest(); + hasRestProperty = true; } else { prop = this.parseSpread(); prop.type = "ExperimentalSpreadProperty"; @@ -489,7 +518,7 @@ function tokenize(code, options) { options = options || {}; var acornOptions = { - ecmaVersion: 5, + ecmaVersion: DEFAULT_ECMA_VERSION, plugins: { espree: true } @@ -518,21 +547,7 @@ function tokenize(code, options) { extra.tolerant = typeof options.tolerant === "boolean" && options.tolerant; - if (typeof options.ecmaVersion === "number") { - switch (options.ecmaVersion) { - case 3: - case 5: - case 6: - case 7: - case 8: - acornOptions.ecmaVersion = options.ecmaVersion; - extra.ecmaVersion = options.ecmaVersion; - break; - - default: - throw new Error("ecmaVersion must be 3, 5, 6, 7, or 8."); - } - } + acornOptions.ecmaVersion = extra.ecmaVersion = normalizeEcmaVersion(options.ecmaVersion); // apply parsing flags if (options.ecmaFeatures && typeof options.ecmaFeatures === "object") { @@ -615,7 +630,7 @@ function parse(code, options) { translator, impliedStrict, acornOptions = { - ecmaVersion: 5, + ecmaVersion: DEFAULT_ECMA_VERSION, plugins: { espree: true } @@ -656,21 +671,7 @@ function parse(code, options) { commentAttachment.reset(); } - if (typeof options.ecmaVersion === "number") { - switch (options.ecmaVersion) { - case 3: - case 5: - case 6: - case 7: - case 8: - acornOptions.ecmaVersion = options.ecmaVersion; - extra.ecmaVersion = options.ecmaVersion; - break; - - default: - throw new Error("ecmaVersion must be 3, 5, 6, 7, or 8."); - } - } + acornOptions.ecmaVersion = extra.ecmaVersion = normalizeEcmaVersion(options.ecmaVersion); if (options.sourceType === "module") { extra.isModule = true; |