From 676766533cc08687edc3bb514e1bf652cad2ae32 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 25 Jul 2015 19:54:19 +0100 Subject: [PATCH] improve babylon test coverage and remove dead code --- packages/babylon/src/parser/expression.js | 3 +- packages/babylon/src/parser/lval.js | 92 +++++----- packages/babylon/src/parser/node.js | 5 +- packages/babylon/src/plugins/jsx/index.js | 1 - packages/babylon/src/tokenizer/index.js | 25 +-- packages/babylon/src/util/identifier.js | 6 +- packages/babylon/src/util/location.js | 4 - .../flow/declare-statements/invalid/actual.js | 1 + .../declare-statements/invalid/options.json | 3 + .../call-expression/actual.js | 3 + .../call-expression/expected.json | 169 ++++++++++++++++++ .../attribute-arbitrary-expression/actual.js | 1 + .../options.json | 3 + .../attribute-empty-expression/actual.js | 1 + .../attribute-empty-expression/options.json | 3 + .../jsx/errors/unclosed-tag/actual.js | 1 + .../jsx/errors/unclosed-tag/options.json | 3 + .../jsx/errors/unterminated-string/actual.js | 1 + .../errors/unterminated-string/options.json | 3 + packages/babylon/test/index.js | 15 +- 20 files changed, 267 insertions(+), 76 deletions(-) create mode 100644 packages/babylon/test/fixtures/flow/declare-statements/invalid/actual.js create mode 100644 packages/babylon/test/fixtures/flow/declare-statements/invalid/options.json create mode 100644 packages/babylon/test/fixtures/harmony/computed-properties/call-expression/actual.js create mode 100644 packages/babylon/test/fixtures/harmony/computed-properties/call-expression/expected.json create mode 100644 packages/babylon/test/fixtures/jsx/errors/attribute-arbitrary-expression/actual.js create mode 100644 packages/babylon/test/fixtures/jsx/errors/attribute-arbitrary-expression/options.json create mode 100644 packages/babylon/test/fixtures/jsx/errors/attribute-empty-expression/actual.js create mode 100644 packages/babylon/test/fixtures/jsx/errors/attribute-empty-expression/options.json create mode 100644 packages/babylon/test/fixtures/jsx/errors/unclosed-tag/actual.js create mode 100644 packages/babylon/test/fixtures/jsx/errors/unclosed-tag/options.json create mode 100644 packages/babylon/test/fixtures/jsx/errors/unterminated-string/actual.js create mode 100644 packages/babylon/test/fixtures/jsx/errors/unterminated-string/options.json diff --git a/packages/babylon/src/parser/expression.js b/packages/babylon/src/parser/expression.js index e85f312f3e..38f52cde4a 100644 --- a/packages/babylon/src/parser/expression.js +++ b/packages/babylon/src/parser/expression.js @@ -236,8 +236,7 @@ pp.parseSubscripts = function(base, startPos, startLoc, noCalls) { this.expect(tt.bracketR); base = this.finishNode(node, "MemberExpression"); } else if (!noCalls && this.state.type === tt.parenL) { - let possibleAsync = false; - if (base.type === "Identifier" && base.name === "async" && !this.canInsertSemicolon()) possibleAsync = true; + let possibleAsync = base.type === "Identifier" && base.name === "async" && !this.canInsertSemicolon(); this.next(); let node = this.startNodeAt(startPos, startLoc); diff --git a/packages/babylon/src/parser/lval.js b/packages/babylon/src/parser/lval.js index 7804cb83bf..68fd6c1252 100644 --- a/packages/babylon/src/parser/lval.js +++ b/packages/babylon/src/parser/lval.js @@ -100,20 +100,20 @@ pp.parseRest = function () { pp.parseBindingAtom = function () { switch (this.state.type) { - case tt.name: - return this.parseIdent(); + case tt.name: + return this.parseIdent(); - case tt.bracketL: - let node = this.startNode(); - this.next(); - node.elements = this.parseBindingList(tt.bracketR, true, true); - return this.finishNode(node, "ArrayPattern"); + case tt.bracketL: + let node = this.startNode(); + this.next(); + node.elements = this.parseBindingList(tt.bracketR, true, true); + return this.finishNode(node, "ArrayPattern"); - case tt.braceL: - return this.parseObj(true); + case tt.braceL: + return this.parseObj(true); - default: - this.unexpected(); + default: + this.unexpected(); } }; @@ -163,49 +163,45 @@ pp.parseMaybeDefault = function (startPos, startLoc, left) { pp.checkLVal = function (expr, isBinding, checkClashes) { switch (expr.type) { - case "Identifier": - if (this.strict && (reservedWords.strictBind(expr.name) || reservedWords.strict(expr.name))) - this.raise(expr.start, (isBinding ? "Binding " : "Assigning to ") + expr.name + " in strict mode"); - if (checkClashes) { - if (checkClashes[expr.name]) { - this.raise(expr.start, "Argument name clash in strict mode"); - } else { - checkClashes[expr.name] = true; + case "Identifier": + if (this.strict && (reservedWords.strictBind(expr.name) || reservedWords.strict(expr.name))) + this.raise(expr.start, (isBinding ? "Binding " : "Assigning to ") + expr.name + " in strict mode"); + if (checkClashes) { + if (checkClashes[expr.name]) { + this.raise(expr.start, "Argument name clash in strict mode"); + } else { + checkClashes[expr.name] = true; + } } - } - break; + break; - case "MemberExpression": - if (isBinding) this.raise(expr.start, (isBinding ? "Binding" : "Assigning to") + " member expression"); - break; + case "MemberExpression": + if (isBinding) this.raise(expr.start, (isBinding ? "Binding" : "Assigning to") + " member expression"); + break; - case "ObjectPattern": - for (let prop of (expr.properties: Array)) { - if (prop.type === "Property") prop = prop.value; - this.checkLVal(prop, isBinding, checkClashes); - } - break; + case "ObjectPattern": + for (let prop of (expr.properties: Array)) { + if (prop.type === "Property") prop = prop.value; + this.checkLVal(prop, isBinding, checkClashes); + } + break; - case "ArrayPattern": - for (let elem of (expr.elements: Array)) { - if (elem) this.checkLVal(elem, isBinding, checkClashes); - } - break; + case "ArrayPattern": + for (let elem of (expr.elements: Array)) { + if (elem) this.checkLVal(elem, isBinding, checkClashes); + } + break; - case "AssignmentPattern": - this.checkLVal(expr.left, isBinding, checkClashes); - break; + case "AssignmentPattern": + this.checkLVal(expr.left, isBinding, checkClashes); + break; - case "SpreadProperty": - case "RestElement": - this.checkLVal(expr.argument, isBinding, checkClashes); - break; + case "SpreadProperty": + case "RestElement": + this.checkLVal(expr.argument, isBinding, checkClashes); + break; - case "ParenthesizedExpression": - this.checkLVal(expr.expression, isBinding, checkClashes); - break; - - default: - this.raise(expr.start, (isBinding ? "Binding" : "Assigning to") + " rvalue"); + default: + this.raise(expr.start, (isBinding ? "Binding" : "Assigning to") + " rvalue"); } }; diff --git a/packages/babylon/src/parser/node.js b/packages/babylon/src/parser/node.js index ef378d1711..7fb0cd2a62 100644 --- a/packages/babylon/src/parser/node.js +++ b/packages/babylon/src/parser/node.js @@ -10,10 +10,7 @@ export class Node { this.type = ""; this.start = pos; this.end = 0; - - if (parser) { - this.loc = new SourceLocation(loc); - } + this.loc = new SourceLocation(loc); } __clone() { diff --git a/packages/babylon/src/plugins/jsx/index.js b/packages/babylon/src/plugins/jsx/index.js index 81fcd6c9f6..b07e60fe3b 100644 --- a/packages/babylon/src/plugins/jsx/index.js +++ b/packages/babylon/src/plugins/jsx/index.js @@ -121,7 +121,6 @@ pp.jsxReadString = function(quote) { pp.jsxReadEntity = function() { var str = "", count = 0, entity; var ch = this.input[this.state.pos]; - if (ch !== "&") this.raise(this.state.pos, "Entity must start with an ampersand"); var startPos = ++this.state.pos; while (this.state.pos < this.input.length && count++ < 10) { diff --git a/packages/babylon/src/tokenizer/index.js b/packages/babylon/src/tokenizer/index.js index 5c33f02f43..5d2a210f4e 100644 --- a/packages/babylon/src/tokenizer/index.js +++ b/packages/babylon/src/tokenizer/index.js @@ -79,11 +79,6 @@ export default class Tokenizer { return curr; } - getToken() { - this.next(); - return new Token(this.state); - } - // Toggle strict mode. Re-reads the next number or string to please // pedantic tests (`"use strict"; 010;` should fail). @@ -455,13 +450,19 @@ export default class Tokenizer { for (;;) { if (this.state.pos >= this.input.length) this.raise(start, "Unterminated regular expression"); let ch = this.input.charAt(this.state.pos); - if (lineBreak.test(ch)) this.raise(start, "Unterminated regular expression"); + if (lineBreak.test(ch)) { + this.raise(start, "Unterminated regular expression"); + } if (escaped) { escaped = false; } else { - if (ch === "[") inClass = true; - else if (ch === "]" && inClass) inClass = false; - else if (ch === "/" && !inClass) break; + if (ch === "[") { + inClass = true; + } else if (ch === "]" && inClass) { + inClass = false; + } else if (ch === "/" && !inClass) { + break; + } escaped = ch === "\\"; } ++this.state.pos; @@ -502,7 +503,11 @@ export default class Tokenizer { // case the current environment doesn't support the flags it uses. value = tryCreateRegexp.call(this, content, mods); } - return this.finishToken(tt.regexp, {pattern: content, flags: mods, value: value}); + return this.finishToken(tt.regexp, { + pattern: content, + flags: mods, + value + }); } // Read an integer in the given radix. Return null if zero digits diff --git a/packages/babylon/src/util/identifier.js b/packages/babylon/src/util/identifier.js index 3bfe47cb36..48847c6277 100644 --- a/packages/babylon/src/util/identifier.js +++ b/packages/babylon/src/util/identifier.js @@ -83,19 +83,18 @@ function isInAstralSet(code, set) { // Test whether a given character code starts an identifier. -export function isIdentifierStart(code, astral) { +export function isIdentifierStart(code) { if (code < 65) return code === 36; if (code < 91) return true; if (code < 97) return code === 95; if (code < 123) return true; if (code <= 0xffff) return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code)); - if (astral === false) return false; return isInAstralSet(code, astralIdentifierStartCodes); } // Test whether a given character is part of an identifier. -export function isIdentifierChar(code, astral) { +export function isIdentifierChar(code) { if (code < 48) return code === 36; if (code < 58) return true; if (code < 65) return false; @@ -103,6 +102,5 @@ export function isIdentifierChar(code, astral) { if (code < 97) return code === 95; if (code < 123) return true; if (code <= 0xffff) return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code)); - if (astral === false) return false; return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes); } diff --git a/packages/babylon/src/util/location.js b/packages/babylon/src/util/location.js index 6130950b15..883d4b3daa 100644 --- a/packages/babylon/src/util/location.js +++ b/packages/babylon/src/util/location.js @@ -8,10 +8,6 @@ export class Position { this.line = line; this.column = col; } - - offset(n) { - return new Position(this.line, this.column + n); - } } export class SourceLocation { diff --git a/packages/babylon/test/fixtures/flow/declare-statements/invalid/actual.js b/packages/babylon/test/fixtures/flow/declare-statements/invalid/actual.js new file mode 100644 index 0000000000..c60ac0ee7d --- /dev/null +++ b/packages/babylon/test/fixtures/flow/declare-statements/invalid/actual.js @@ -0,0 +1 @@ +declare foobar diff --git a/packages/babylon/test/fixtures/flow/declare-statements/invalid/options.json b/packages/babylon/test/fixtures/flow/declare-statements/invalid/options.json new file mode 100644 index 0000000000..9ce9658f7d --- /dev/null +++ b/packages/babylon/test/fixtures/flow/declare-statements/invalid/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token (1:8)" +} diff --git a/packages/babylon/test/fixtures/harmony/computed-properties/call-expression/actual.js b/packages/babylon/test/fixtures/harmony/computed-properties/call-expression/actual.js new file mode 100644 index 0000000000..15b577cc19 --- /dev/null +++ b/packages/babylon/test/fixtures/harmony/computed-properties/call-expression/actual.js @@ -0,0 +1,3 @@ +var foo = { + [bar()]: "" +}; diff --git a/packages/babylon/test/fixtures/harmony/computed-properties/call-expression/expected.json b/packages/babylon/test/fixtures/harmony/computed-properties/call-expression/expected.json new file mode 100644 index 0000000000..df5fedc3b7 --- /dev/null +++ b/packages/babylon/test/fixtures/harmony/computed-properties/call-expression/expected.json @@ -0,0 +1,169 @@ +{ + "type": "File", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 2 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 2 + } + }, + "sourceType": "script", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 2 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "name": "foo" + }, + "init": { + "type": "ObjectExpression", + "start": 10, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "properties": [ + { + "type": "Property", + "start": 14, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "method": false, + "shorthand": false, + "computed": true, + "key": { + "type": "CallExpression", + "start": 15, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 3 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "callee": { + "type": "Identifier", + "start": 15, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 3 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "name": "bar" + }, + "arguments": [] + }, + "value": { + "type": "Literal", + "start": 23, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "value": "", + "rawValue": "", + "raw": "\"\"" + }, + "kind": "init" + } + ] + } + } + ], + "kind": "var" + } + ] + }, + "comments": [] +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/jsx/errors/attribute-arbitrary-expression/actual.js b/packages/babylon/test/fixtures/jsx/errors/attribute-arbitrary-expression/actual.js new file mode 100644 index 0000000000..d0fa2bd1b7 --- /dev/null +++ b/packages/babylon/test/fixtures/jsx/errors/attribute-arbitrary-expression/actual.js @@ -0,0 +1 @@ + diff --git a/packages/babylon/test/fixtures/jsx/errors/attribute-arbitrary-expression/options.json b/packages/babylon/test/fixtures/jsx/errors/attribute-arbitrary-expression/options.json new file mode 100644 index 0000000000..19a214000f --- /dev/null +++ b/packages/babylon/test/fixtures/jsx/errors/attribute-arbitrary-expression/options.json @@ -0,0 +1,3 @@ +{ + "throws": "JSX value should be either an expression or a quoted JSX text (1:9)" +} diff --git a/packages/babylon/test/fixtures/jsx/errors/attribute-empty-expression/actual.js b/packages/babylon/test/fixtures/jsx/errors/attribute-empty-expression/actual.js new file mode 100644 index 0000000000..a6aa94b0a8 --- /dev/null +++ b/packages/babylon/test/fixtures/jsx/errors/attribute-empty-expression/actual.js @@ -0,0 +1 @@ + diff --git a/packages/babylon/test/fixtures/jsx/errors/attribute-empty-expression/options.json b/packages/babylon/test/fixtures/jsx/errors/attribute-empty-expression/options.json new file mode 100644 index 0000000000..d5960e11a3 --- /dev/null +++ b/packages/babylon/test/fixtures/jsx/errors/attribute-empty-expression/options.json @@ -0,0 +1,3 @@ +{ + "throws": "JSX attributes must only be assigned a non-empty expression (1:9)" +} diff --git a/packages/babylon/test/fixtures/jsx/errors/unclosed-tag/actual.js b/packages/babylon/test/fixtures/jsx/errors/unclosed-tag/actual.js new file mode 100644 index 0000000000..1fee584ee3 --- /dev/null +++ b/packages/babylon/test/fixtures/jsx/errors/unclosed-tag/actual.js @@ -0,0 +1 @@ +yes diff --git a/packages/babylon/test/fixtures/jsx/errors/unclosed-tag/options.json b/packages/babylon/test/fixtures/jsx/errors/unclosed-tag/options.json new file mode 100644 index 0000000000..4b723200df --- /dev/null +++ b/packages/babylon/test/fixtures/jsx/errors/unclosed-tag/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unterminated JSX contents (1:5)" +} diff --git a/packages/babylon/test/fixtures/jsx/errors/unterminated-string/actual.js b/packages/babylon/test/fixtures/jsx/errors/unterminated-string/actual.js new file mode 100644 index 0000000000..32fb36baa5 --- /dev/null +++ b/packages/babylon/test/fixtures/jsx/errors/unterminated-string/actual.js @@ -0,0 +1 @@ +