From 7c18bf83cccfca3a6fddf6c52fd1678c04223b3e Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Sun, 16 Oct 2016 16:04:13 +0300 Subject: [PATCH] Increase test coverage (#175) * Increase test coverage * Test for error when binding `this` in destructuring pattern * Ignore coverage of inAsync check in parseAwait - already checked externally * Ignore coverage of default case in checkPropClash * Remove unused parameter isAsync from parseParenAndDistinguishExpression * Ignore coverage of an `else` branch in flowParseTypeParameterDeclaration * Flow: remove unused parameters to flowParseTypeAnnotatableIdentifier * Flow: ignore coverage of pass-through throw statement in parseConditional * Flow: Add test for error on property with type param * Flow: ignore coverage of pass-through throw statements in parseMaybeAssign, parseArrow * Add test for error on XML-style comment in module code * Update test for error on method in object pattern * Test for error: "Only '=' operator can be used for specifying default value" --- src/parser/expression.js | 12 +- src/parser/statement.js | 1 - src/plugins/flow.js | 24 +-- src/plugins/jsx/index.js | 1 + .../categorized/label-kind-switch/actual.js | 4 + .../label-kind-switch/expected.json | 117 ++++++++++++++ .../categorized/malformed-switch/actual.js | 3 + .../categorized/malformed-switch/options.json | 3 + .../actual.js | 3 + .../options.json | 3 + .../disallow-static-prototype/actual.js | 3 + .../disallow-static-prototype/options.json | 3 + .../class-methods/getter-signature/actual.js | 3 + .../getter-signature/options.json | 3 + .../malformed-super-expression/actual.js | 5 + .../malformed-super-expression/options.json | 3 + .../destructuring/binding-this/actual.js | 1 + .../destructuring/binding-this/options.json | 3 + .../error-operator-for-default/actual.js | 1 + .../error-operator-for-default/options.json | 3 + .../modules/xml-comment-in-module/actual.js | 1 + .../xml-comment-in-module/options.json | 3 + .../async-functions/no-constructor/actual.js | 3 + .../no-constructor/options.json | 3 + .../invalid-pattern-with-method/actual.js | 2 +- .../invalid-pattern-with-method/options.json | 4 +- .../error-without-plugin/actual.js | 1 + .../error-without-plugin/options.json | 4 + .../actual.js | 3 + .../options.json | 3 + test/fixtures/jsx/basic/entity/actual.js | 1 + test/fixtures/jsx/basic/entity/expected.json | 147 ++++++++++++++++++ .../jsx/basic/nonentity-decimal/actual.js | 1 + .../jsx/basic/nonentity-decimal/expected.json | 147 ++++++++++++++++++ test/fixtures/jsx/basic/nonentity/actual.js | 1 + .../jsx/basic/nonentity/expected.json | 147 ++++++++++++++++++ .../unicode-escape-in-identifier/actual.js | 1 + .../unicode-escape-in-identifier/options.json | 3 + 38 files changed, 646 insertions(+), 28 deletions(-) create mode 100644 test/fixtures/core/categorized/label-kind-switch/actual.js create mode 100644 test/fixtures/core/categorized/label-kind-switch/expected.json create mode 100644 test/fixtures/core/categorized/malformed-switch/actual.js create mode 100644 test/fixtures/core/categorized/malformed-switch/options.json create mode 100644 test/fixtures/es2015/class-methods/direct-super-outside-constructor/actual.js create mode 100644 test/fixtures/es2015/class-methods/direct-super-outside-constructor/options.json create mode 100644 test/fixtures/es2015/class-methods/disallow-static-prototype/actual.js create mode 100644 test/fixtures/es2015/class-methods/disallow-static-prototype/options.json create mode 100644 test/fixtures/es2015/class-methods/getter-signature/actual.js create mode 100644 test/fixtures/es2015/class-methods/getter-signature/options.json create mode 100644 test/fixtures/es2015/class-methods/malformed-super-expression/actual.js create mode 100644 test/fixtures/es2015/class-methods/malformed-super-expression/options.json create mode 100644 test/fixtures/es2015/destructuring/binding-this/actual.js create mode 100644 test/fixtures/es2015/destructuring/binding-this/options.json create mode 100644 test/fixtures/es2015/destructuring/error-operator-for-default/actual.js create mode 100644 test/fixtures/es2015/destructuring/error-operator-for-default/options.json create mode 100644 test/fixtures/es2015/modules/xml-comment-in-module/actual.js create mode 100644 test/fixtures/es2015/modules/xml-comment-in-module/options.json create mode 100644 test/fixtures/es2017/async-functions/no-constructor/actual.js create mode 100644 test/fixtures/es2017/async-functions/no-constructor/options.json create mode 100644 test/fixtures/experimental/no-async-generators/error-without-plugin/actual.js create mode 100644 test/fixtures/experimental/no-async-generators/error-without-plugin/options.json create mode 100644 test/fixtures/flow/type-parameter-declaration/error-object-property-type-param/actual.js create mode 100644 test/fixtures/flow/type-parameter-declaration/error-object-property-type-param/options.json create mode 100644 test/fixtures/jsx/basic/entity/actual.js create mode 100644 test/fixtures/jsx/basic/entity/expected.json create mode 100644 test/fixtures/jsx/basic/nonentity-decimal/actual.js create mode 100644 test/fixtures/jsx/basic/nonentity-decimal/expected.json create mode 100644 test/fixtures/jsx/basic/nonentity/actual.js create mode 100644 test/fixtures/jsx/basic/nonentity/expected.json create mode 100644 test/fixtures/jsx/errors/unicode-escape-in-identifier/actual.js create mode 100644 test/fixtures/jsx/errors/unicode-escape-in-identifier/options.json diff --git a/src/parser/expression.js b/src/parser/expression.js index 55fa1619f4..9335983b3e 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -45,6 +45,7 @@ pp.checkPropClash = function (prop, propHash) { name = String(key.value); break; + // istanbul ignore next: non-computed property keys are always one of the above default: return; } @@ -555,7 +556,7 @@ pp.parseParenExpression = function () { return val; }; -pp.parseParenAndDistinguishExpression = function (startPos, startLoc, canBeArrow, isAsync) { +pp.parseParenAndDistinguishExpression = function (startPos, startLoc, canBeArrow) { startPos = startPos || this.state.start; startLoc = startLoc || this.state.startLoc; @@ -597,15 +598,11 @@ pp.parseParenAndDistinguishExpression = function (startPos, startLoc, canBeArrow if (param.extra && param.extra.parenthesized) this.unexpected(param.extra.parenStart); } - return this.parseArrowExpression(arrowNode, exprList, isAsync); + return this.parseArrowExpression(arrowNode, exprList); } if (!exprList.length) { - if (isAsync) { - return; - } else { - this.unexpected(this.state.lastTokStart); - } + this.unexpected(this.state.lastTokStart); } if (optionalCommaStart) this.unexpected(optionalCommaStart); if (spreadStart) this.unexpected(spreadStart); @@ -1025,6 +1022,7 @@ pp.parseIdentifier = function (liberal) { // Parses await expression inside async function. pp.parseAwait = function (node) { + // istanbul ignore next: this condition is checked at the call site so won't be hit here if (!this.state.inAsync) { this.unexpected(); } diff --git a/src/parser/statement.js b/src/parser/statement.js index 0e89b5b557..26bc4e5525 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -670,7 +670,6 @@ pp.parseClassBody = function (node) { method.static = isMaybeStatic && !this.match(tt.parenL); if (method.static) { - if (isGenerator) this.unexpected(); isGenerator = this.eat(tt.star); this.parsePropertyName(method); } diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 50d48b5eb2..e248c0f90f 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -207,7 +207,7 @@ pp.flowParseTypeParameter = function () { let variance = this.flowParseVariance(); - let ident = this.flowParseTypeAnnotatableIdentifier(false, false); + let ident = this.flowParseTypeAnnotatableIdentifier(); node.name = ident.name; node.variance = variance; node.bound = ident.typeAnnotation; @@ -227,6 +227,7 @@ pp.flowParseTypeParameterDeclaration = function () { this.state.inType = true; + // istanbul ignore else: this condition is already checked at all call sites if (this.isRelational("<") || this.match(tt.jsxTagStart)) { this.next(); } else { @@ -705,26 +706,12 @@ pp.flowParseTypeAnnotation = function () { return this.finishNode(node, "TypeAnnotation"); }; -pp.flowParseTypeAnnotatableIdentifier = function (requireTypeAnnotation, canBeOptionalParam) { - +pp.flowParseTypeAnnotatableIdentifier = function () { let ident = this.parseIdentifier(); - let isOptionalParam = false; - - if (canBeOptionalParam && this.eat(tt.question)) { - this.expect(tt.question); - isOptionalParam = true; - } - - if (requireTypeAnnotation || this.match(tt.colon)) { + if (this.match(tt.colon)) { ident.typeAnnotation = this.flowParseTypeAnnotation(); this.finishNode(ident, ident.type); } - - if (isOptionalParam) { - ident.optional = true; - this.finishNode(ident, ident.type); - } - return ident; }; @@ -824,6 +811,7 @@ export default function (instance) { refNeedsArrowPos.start = err.pos || this.state.start; return expr; } else { + // istanbul ignore next: no such error is expected throw err; } } @@ -1208,6 +1196,7 @@ export default function (instance) { this.state = state; jsxError = err; } else { + // istanbul ignore next: no such error is expected throw err; } } @@ -1262,6 +1251,7 @@ export default function (instance) { if (err instanceof SyntaxError) { this.state = state; } else { + // istanbul ignore next: no such error is expected throw err; } } diff --git a/src/plugins/jsx/index.js b/src/plugins/jsx/index.js index 6bc98d396e..835314c4d8 100644 --- a/src/plugins/jsx/index.js +++ b/src/plugins/jsx/index.js @@ -365,6 +365,7 @@ pp.jsxParseElementAt = function(startPos, startLoc) { break; + // istanbul ignore next - should never happen default: this.unexpected(); } diff --git a/test/fixtures/core/categorized/label-kind-switch/actual.js b/test/fixtures/core/categorized/label-kind-switch/actual.js new file mode 100644 index 0000000000..43cf8d3377 --- /dev/null +++ b/test/fixtures/core/categorized/label-kind-switch/actual.js @@ -0,0 +1,4 @@ +{ + a: switch (i) { + } +} \ No newline at end of file diff --git a/test/fixtures/core/categorized/label-kind-switch/expected.json b/test/fixtures/core/categorized/label-kind-switch/expected.json new file mode 100644 index 0000000000..7eace83c32 --- /dev/null +++ b/test/fixtures/core/categorized/label-kind-switch/expected.json @@ -0,0 +1,117 @@ +{ + "type": "File", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "script", + "body": [ + { + "type": "BlockStatement", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "LabeledStatement", + "start": 4, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 3, + "column": 3 + } + }, + "body": { + "type": "SwitchStatement", + "start": 7, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 3, + "column": 3 + } + }, + "discriminant": { + "type": "Identifier", + "start": 15, + "end": 16, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 14 + }, + "identifierName": "i" + }, + "name": "i" + }, + "cases": [] + }, + "label": { + "type": "Identifier", + "start": 4, + "end": 5, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 3 + }, + "identifierName": "a" + }, + "name": "a" + } + } + ], + "directives": [] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/core/categorized/malformed-switch/actual.js b/test/fixtures/core/categorized/malformed-switch/actual.js new file mode 100644 index 0000000000..55cfd49552 --- /dev/null +++ b/test/fixtures/core/categorized/malformed-switch/actual.js @@ -0,0 +1,3 @@ +switch (x) { + var y = 5; +} \ No newline at end of file diff --git a/test/fixtures/core/categorized/malformed-switch/options.json b/test/fixtures/core/categorized/malformed-switch/options.json new file mode 100644 index 0000000000..0b22a698d6 --- /dev/null +++ b/test/fixtures/core/categorized/malformed-switch/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token (2:2)" +} \ No newline at end of file diff --git a/test/fixtures/es2015/class-methods/direct-super-outside-constructor/actual.js b/test/fixtures/es2015/class-methods/direct-super-outside-constructor/actual.js new file mode 100644 index 0000000000..4a9aab9c9d --- /dev/null +++ b/test/fixtures/es2015/class-methods/direct-super-outside-constructor/actual.js @@ -0,0 +1,3 @@ +class A { + x () {super()} +} \ No newline at end of file diff --git a/test/fixtures/es2015/class-methods/direct-super-outside-constructor/options.json b/test/fixtures/es2015/class-methods/direct-super-outside-constructor/options.json new file mode 100644 index 0000000000..476bc13a2b --- /dev/null +++ b/test/fixtures/es2015/class-methods/direct-super-outside-constructor/options.json @@ -0,0 +1,3 @@ +{ + "throws": "super() outside of class constructor (2:8)" +} \ No newline at end of file diff --git a/test/fixtures/es2015/class-methods/disallow-static-prototype/actual.js b/test/fixtures/es2015/class-methods/disallow-static-prototype/actual.js new file mode 100644 index 0000000000..5f87d23929 --- /dev/null +++ b/test/fixtures/es2015/class-methods/disallow-static-prototype/actual.js @@ -0,0 +1,3 @@ +class A { + static prototype() {} +} \ No newline at end of file diff --git a/test/fixtures/es2015/class-methods/disallow-static-prototype/options.json b/test/fixtures/es2015/class-methods/disallow-static-prototype/options.json new file mode 100644 index 0000000000..bc9a87203e --- /dev/null +++ b/test/fixtures/es2015/class-methods/disallow-static-prototype/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Classes may not have static property named prototype (2:9)" +} \ No newline at end of file diff --git a/test/fixtures/es2015/class-methods/getter-signature/actual.js b/test/fixtures/es2015/class-methods/getter-signature/actual.js new file mode 100644 index 0000000000..81a88e95c0 --- /dev/null +++ b/test/fixtures/es2015/class-methods/getter-signature/actual.js @@ -0,0 +1,3 @@ +class A { + get prop (arg) {} +} \ No newline at end of file diff --git a/test/fixtures/es2015/class-methods/getter-signature/options.json b/test/fixtures/es2015/class-methods/getter-signature/options.json new file mode 100644 index 0000000000..9699409e82 --- /dev/null +++ b/test/fixtures/es2015/class-methods/getter-signature/options.json @@ -0,0 +1,3 @@ +{ + "throws": "getter should have no params (2:2)" +} \ No newline at end of file diff --git a/test/fixtures/es2015/class-methods/malformed-super-expression/actual.js b/test/fixtures/es2015/class-methods/malformed-super-expression/actual.js new file mode 100644 index 0000000000..5c26b1ae52 --- /dev/null +++ b/test/fixtures/es2015/class-methods/malformed-super-expression/actual.js @@ -0,0 +1,5 @@ +class A { + x () { + super - 1; + } +} \ No newline at end of file diff --git a/test/fixtures/es2015/class-methods/malformed-super-expression/options.json b/test/fixtures/es2015/class-methods/malformed-super-expression/options.json new file mode 100644 index 0000000000..451cd47516 --- /dev/null +++ b/test/fixtures/es2015/class-methods/malformed-super-expression/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token (3:10)" +} \ No newline at end of file diff --git a/test/fixtures/es2015/destructuring/binding-this/actual.js b/test/fixtures/es2015/destructuring/binding-this/actual.js new file mode 100644 index 0000000000..11cebfa188 --- /dev/null +++ b/test/fixtures/es2015/destructuring/binding-this/actual.js @@ -0,0 +1 @@ +var { this }; diff --git a/test/fixtures/es2015/destructuring/binding-this/options.json b/test/fixtures/es2015/destructuring/binding-this/options.json new file mode 100644 index 0000000000..751e3a83c2 --- /dev/null +++ b/test/fixtures/es2015/destructuring/binding-this/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Binding this (1:6)" +} diff --git a/test/fixtures/es2015/destructuring/error-operator-for-default/actual.js b/test/fixtures/es2015/destructuring/error-operator-for-default/actual.js new file mode 100644 index 0000000000..82d61e00c7 --- /dev/null +++ b/test/fixtures/es2015/destructuring/error-operator-for-default/actual.js @@ -0,0 +1 @@ +([a += a] = a) diff --git a/test/fixtures/es2015/destructuring/error-operator-for-default/options.json b/test/fixtures/es2015/destructuring/error-operator-for-default/options.json new file mode 100644 index 0000000000..f52857170b --- /dev/null +++ b/test/fixtures/es2015/destructuring/error-operator-for-default/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Only '=' operator can be used for specifying default value. (1:3)" +} diff --git a/test/fixtures/es2015/modules/xml-comment-in-module/actual.js b/test/fixtures/es2015/modules/xml-comment-in-module/actual.js new file mode 100644 index 0000000000..81d1a714b9 --- /dev/null +++ b/test/fixtures/es2015/modules/xml-comment-in-module/actual.js @@ -0,0 +1 @@ +foo