From a7a5b04030d2d37fa091f2a7145e4ce4a7a73429 Mon Sep 17 00:00:00 2001 From: Henry Zhu Date: Thu, 17 Nov 2016 15:41:33 -0500 Subject: [PATCH 1/3] Deprecate built-in rules --- README.md | 17 +- rules/array-bracket-spacing.js | 205 +--- rules/arrow-parens.js | 70 +- rules/func-params-comma-dangle.js | 139 +-- rules/generator-star-spacing.js | 97 +- rules/object-shorthand.js | 82 +- tests/rules/array-bracket-spacing.js | 733 -------------- tests/rules/arrow-parens.js | 187 ---- tests/rules/func-params-comma-dangle.js | 278 ------ tests/rules/generator-star-spacing.js | 1176 ----------------------- tests/rules/object-shorthand.js | 108 --- 11 files changed, 71 insertions(+), 3021 deletions(-) delete mode 100644 tests/rules/array-bracket-spacing.js delete mode 100644 tests/rules/arrow-parens.js delete mode 100644 tests/rules/func-params-comma-dangle.js delete mode 100644 tests/rules/generator-star-spacing.js delete mode 100644 tests/rules/object-shorthand.js diff --git a/README.md b/README.md index 171a408..e837566 100644 --- a/README.md +++ b/README.md @@ -26,15 +26,10 @@ original ones as well!). ```json { "rules": { - "babel/generator-star-spacing": 1, "babel/new-cap": 1, - "babel/array-bracket-spacing": 1, "babel/object-curly-spacing": 1, - "babel/object-shorthand": 1, - "babel/arrow-parens": 1, "babel/no-await-in-loop": 1, "babel/flow-object-type": 1, - "babel/func-params-comma-dangle": 1, "babel/no-invalid-this": 1 } } @@ -45,12 +40,8 @@ Each rule corresponds to a core `eslint` rule, and has the same options. 🛠 : means it's autofixable with `--fix`. -- `babel/generator-star-spacing`: Handles async/await functions correctly - `babel/new-cap`: Ignores capitalized decorators (`@Decorator`) -- `babel/array-bracket-spacing`: Handles destructuring arrays with flow type in function parameters (🛠 ) - `babel/object-curly-spacing`: doesn't complain about `export x from "mod";` or `export * as x from "mod";` (🛠 ) -- `babel/object-shorthand`: doesn't fail when using object spread (`...obj`) -- `babel/arrow-parens`: Handles async functions correctly (🛠 ) - `babel/no-invalid-this`: doesn't fail when inside class properties (`class A { a = this.b; }`) The following rules are not in `eslint`, but are relevant only to syntax that is not specified by @@ -60,4 +51,12 @@ the current JavaScript standard or supported by `eslint`. - `babel/flow-object-type`: Require a particular separator between properties in Flow object types. (🛠 ) - Use the option `semicolon` to require semicolons (e.g. `type Foo = { bar: number; baz: string }`). - Use the option `comma` to require commas (e.g. `type Foo = { bar: number, baz: string }`). + + +#### Deprecated + +- `babel/generator-star-spacing`: Handles async/await functions correctly +- `babel/object-shorthand`: doesn't fail when using object spread (`...obj`) +- `babel/arrow-parens`: Handles async functions correctly (🛠 ) - `babel/func-params-comma-dangle`: Require or forbid trailing commas for function paramater lists. Behaves like, and takes the same options as, `eslint`'s [`comma-dangle`](http://eslint.org/docs/rules/comma-dangle). (🛠 ) +- `babel/array-bracket-spacing`: Handles destructuring arrays with flow type in function parameters (🛠 ) diff --git a/rules/array-bracket-spacing.js b/rules/array-bracket-spacing.js index b67fbe9..a7da50b 100644 --- a/rules/array-bracket-spacing.js +++ b/rules/array-bracket-spacing.js @@ -1,203 +1,20 @@ -/** - * @fileoverview Disallows or enforces spaces inside of array brackets. - * @author Jamund Ferguson - * @copyright 2015 Jamund Ferguson. All rights reserved. - * @copyright 2014 Brandyn Bennett. All rights reserved. - * @copyright 2014 Michael Ficarra. No rights reserved. - * @copyright 2014 Vignesh Anand. All rights reserved. - */ "use strict"; -//------------------------------------------------------------------------------ -// Rule Definition -//------------------------------------------------------------------------------ - -module.exports = function(context) { - var spaced = context.options[0] === "always", - sourceCode = context.getSourceCode(); - - /** - * Determines whether an option is set, relative to the spacing option. - * If spaced is "always", then check whether option is set to false. - * If spaced is "never", then check whether option is set to true. - * @param {Object} option - The option to exclude. - * @returns {boolean} Whether or not the property is excluded. - */ - function isOptionSet(option) { - return context.options[1] ? context.options[1][option] === !spaced : false; - } - - var options = { - spaced: spaced, - singleElementException: isOptionSet("singleValue"), - objectsInArraysException: isOptionSet("objectsInArrays"), - arraysInArraysException: isOptionSet("arraysInArrays") - }; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Determines whether two adjacent tokens are on the same line. - * @param {Object} left - The left token object. - * @param {Object} right - The right token object. - * @returns {boolean} Whether or not the tokens are on the same line. - */ - function isSameLine(left, right) { - return left.loc.start.line === right.loc.start.line; - } - - /** - * Reports that there shouldn't be a space after the first token - * @param {ASTNode} node - The node to report in the event of an error. - * @param {Token} token - The token to use for the report. - * @returns {void} - */ - function reportNoBeginningSpace(node, token) { - context.report({ - node: node, - loc: token.loc.start, - message: "There should be no space after '" + token.value + "'", - fix: function(fixer) { - var nextToken = sourceCode.getTokenAfter(token); - return fixer.removeRange([token.range[1], nextToken.range[0]]); - } - }); - } - - /** - * Reports that there shouldn't be a space before the last token - * @param {ASTNode} node - The node to report in the event of an error. - * @param {Token} token - The token to use for the report. - * @returns {void} - */ - function reportNoEndingSpace(node, token) { - context.report({ - node: node, - loc: token.loc.start, - message: "There should be no space before '" + token.value + "'", - fix: function(fixer) { - var previousToken = sourceCode.getTokenBefore(token); - return fixer.removeRange([previousToken.range[1], token.range[0]]); - } - }); - } - - /** - * Reports that there should be a space after the first token - * @param {ASTNode} node - The node to report in the event of an error. - * @param {Token} token - The token to use for the report. - * @returns {void} - */ - function reportRequiredBeginningSpace(node, token) { - context.report({ - node: node, - loc: token.loc.start, - message: "A space is required after '" + token.value + "'", - fix: function(fixer) { - return fixer.insertTextAfter(token, " "); - } - }); - } - - /** - * Reports that there should be a space before the last token - * @param {ASTNode} node - The node to report in the event of an error. - * @param {Token} token - The token to use for the report. - * @returns {void} - */ - function reportRequiredEndingSpace(node, token) { - context.report({ - node: node, - loc: token.loc.start, - message: "A space is required before '" + token.value + "'", - fix: function(fixer) { - return fixer.insertTextBefore(token, " "); - } - }); - } - - /** - * Determines if a node is an object type - * @param {ASTNode} node - The node to check. - * @returns {boolean} Whether or not the node is an object type. - */ - function isObjectType(node) { - return node && (node.type === "ObjectExpression" || node.type === "ObjectPattern"); - } - - /** - * Determines if a node is an array type - * @param {ASTNode} node - The node to check. - * @returns {boolean} Whether or not the node is an array type. - */ - function isArrayType(node) { - return node && (node.type === "ArrayExpression" || node.type === "ArrayPattern"); - } - - /** - * Validates the spacing around array brackets - * @param {ASTNode} node - The node we're checking for spacing - * @returns {void} - */ - function validateArraySpacing(node) { - if (options.spaced && node.elements.length === 0) { - return; - } - - var first = sourceCode.getFirstToken(node), - second = sourceCode.getFirstToken(node, 1), - last = sourceCode.getLastToken(node), - firstElement = node.elements[0], - lastElement = node.elements[node.elements.length - 1]; - - while (last.type !== "Punctuation" && last.value !== "]") { - last = sourceCode.getTokenBefore(last); - } - - var penultimate = sourceCode.getTokenBefore(last); - - var openingBracketMustBeSpaced = - options.objectsInArraysException && isObjectType(firstElement) || - options.arraysInArraysException && isArrayType(firstElement) || - options.singleElementException && node.elements.length === 1 - ? !options.spaced : options.spaced; - - var closingBracketMustBeSpaced = - options.objectsInArraysException && isObjectType(lastElement) || - options.arraysInArraysException && isArrayType(lastElement) || - options.singleElementException && node.elements.length === 1 - ? !options.spaced : options.spaced; - - if (isSameLine(first, second)) { - if (openingBracketMustBeSpaced && !sourceCode.isSpaceBetweenTokens(first, second)) { - reportRequiredBeginningSpace(node, first); - } - if (!openingBracketMustBeSpaced && sourceCode.isSpaceBetweenTokens(first, second)) { - reportNoBeginningSpace(node, first); +var isWarnedForDeprecation = false; +module.exports = function() { + return { + Program() { + if (isWarnedForDeprecation || /\=-(f|-format)=/.test(process.argv.join('='))) { + return; } - } - if (first !== penultimate && isSameLine(penultimate, last)) { - if (closingBracketMustBeSpaced && !sourceCode.isSpaceBetweenTokens(penultimate, last)) { - reportRequiredEndingSpace(node, last); - } - if (!closingBracketMustBeSpaced && sourceCode.isSpaceBetweenTokens(penultimate, last)) { - reportNoEndingSpace(node, last); - } + /* eslint-disable no-console */ + console.log('The babel/array-bracket-spacing rule is deprecated. Please ' + + 'use the built in array-bracket-spacing rule instead.'); + /* eslint-enable no-console */ + isWarnedForDeprecation = true; } - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - ArrayPattern: validateArraySpacing, - ArrayExpression: validateArraySpacing }; - }; module.exports.schema = [ diff --git a/rules/arrow-parens.js b/rules/arrow-parens.js index 6ca3ffc..03f50dc 100644 --- a/rules/arrow-parens.js +++ b/rules/arrow-parens.js @@ -1,67 +1,19 @@ -/** - * @fileoverview Rule to require parens in arrow function arguments. - * @author Jxck - * @copyright 2015 Jxck. All rights reserved. - */ "use strict"; -//------------------------------------------------------------------------------ -// Rule Definition -//------------------------------------------------------------------------------ - -module.exports = function(context) { - var message = "Expected parentheses around arrow function argument."; - var asNeededMessage = "Unexpected parentheses around single function argument"; - var asNeeded = context.options[0] === "as-needed"; - - /** - * Determines whether a arrow function argument end with `)` - * @param {ASTNode} node The arrow function node. - * @returns {void} - */ - function parens(node) { - var token = context.getFirstToken(node); - if (node.async) token = context.getTokenAfter(token); - - // as-needed: x => x - if (asNeeded && node.params.length === 1 - && node.params[0].type === "Identifier" - && node.params[0].typeAnnotation === undefined) { - if (token.type === "Punctuator" && token.value === "(") { - context.report({ - node: node, - message: asNeededMessage, - fix: function(fixer) { - var paramToken = context.getTokenAfter(token); - var closingParenToken = context.getTokenAfter(paramToken); - return fixer.replaceTextRange([ - token.range[0], - closingParenToken.range[1] - ], paramToken.value); - } - }); +var isWarnedForDeprecation = false; +module.exports = function() { + return { + Program() { + if (isWarnedForDeprecation || /\=-(f|-format)=/.test(process.argv.join('='))) { + return; } - return; - } - if (token.type === "Identifier") { - var after = context.getTokenAfter(token); - - // (x) => x - if (after.value !== ")") { - context.report({ - node: node, - message: message, - fix: function(fixer) { - return fixer.replaceText(token, '(' + token.value + ')'); - } - }); - } + /* eslint-disable no-console */ + console.log('The babel/arrow-parens rule is deprecated. Please ' + + 'use the built in arrow-parens rule instead.'); + /* eslint-enable no-console */ + isWarnedForDeprecation = true; } - } - - return { - "ArrowFunctionExpression": parens }; }; diff --git a/rules/func-params-comma-dangle.js b/rules/func-params-comma-dangle.js index d750323..bf5c1ee 100644 --- a/rules/func-params-comma-dangle.js +++ b/rules/func-params-comma-dangle.js @@ -1,135 +1,22 @@ 'use strict'; -// Based on https://github.com/eslint/eslint/blob/v2.11.1/lib/rules/comma-dangle.js - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -function last(arr) { - return arr.length !== 0 ? arr[arr.length - 1] : undefined; -} - -//------------------------------------------------------------------------------ -// Rule Definition -//------------------------------------------------------------------------------ - -module.exports = function(context) { - var mode = context.options[0]; - var UNEXPECTED_MESSAGE = 'Unexpected trailing comma.'; - var MISSING_MESSAGE = 'Missing trailing comma.'; - - function isMultiline(node) { - var lastItem = last(node.params || node.arguments); - - if (!lastItem) { - return false; - } - - var sourceCode = context.getSourceCode(); - var penultimateToken = sourceCode.getLastToken(lastItem); - var lastToken = sourceCode.getTokenAfter(penultimateToken); - - if (lastToken.value === ',') { - penultimateToken = lastToken; - lastToken = sourceCode.getTokenAfter(lastToken); - } - - return lastToken.loc.end.line !== penultimateToken.loc.end.line; - } - - function forbidTrailingComma(node) { - var lastItem = last(node.params || node.arguments); - - if (!lastItem) { - return; - } - - var sourceCode = context.getSourceCode(); - var trailingToken = sourceCode.getTokenAfter(lastItem); - - if (trailingToken.value === ',') { - context.report({ - node: lastItem, - loc: trailingToken.loc.start, - message: UNEXPECTED_MESSAGE, - fix: function(fixer) { - return fixer.remove(trailingToken); - } - }); - } - } - - function forceTrailingComma(node) { - var lastItem = last(node.params || node.arguments); - - if (!lastItem) { - return; - } - - // `f(...a,)` is ok, but `function f(...a,) {}` is invalid syntax. - if (lastItem.type === 'RestElement') { - return; - } - - var sourceCode = context.getSourceCode(); - var penultimateToken = lastItem; - var trailingToken = sourceCode.getTokenAfter(lastItem); - - // `f = a, => {}` is invalid syntax. - if (node.type === 'ArrowFunctionExpression' && - node.params.length === 1 && - sourceCode.getTokenBefore(lastItem).value !== '(') { - return; - } - - if (trailingToken.value !== ',') { - context.report({ - node: lastItem, - loc: lastItem.loc.end, - message: MISSING_MESSAGE, - fix: function(fixer) { - return fixer.insertTextAfter(penultimateToken, ','); - } - }); - } - } - - function forceTrailingCommaIfMultiline(node) { - if (isMultiline(node)) { - forceTrailingComma(node); - } else { - forbidTrailingComma(node); - } - } - - function allowTrailingCommaIfMultiline(node) { - if (!isMultiline(node)) { - forbidTrailingComma(node); - } - } - - var checkForTrailingComma; - if (mode === 'always') { - checkForTrailingComma = forceTrailingComma; - } else if (mode === 'always-multiline') { - checkForTrailingComma = forceTrailingCommaIfMultiline; - } else if (mode === 'only-multiline') { - checkForTrailingComma = allowTrailingCommaIfMultiline; - } else { - checkForTrailingComma = forbidTrailingComma; - } - +var isWarnedForDeprecation = false; +module.exports = function() { return { - ArrowFunctionExpression: checkForTrailingComma, - FunctionDeclaration: checkForTrailingComma, - FunctionExpression: checkForTrailingComma, - CallExpression: checkForTrailingComma, - NewExpression: checkForTrailingComma, + Program() { + if (isWarnedForDeprecation || /\=-(f|-format)=/.test(process.argv.join('='))) { + return; + } + + /* eslint-disable no-console */ + console.log('The babel/func-params-comma-dangle rule is deprecated. Please ' + + 'use the built in comma-dangle rule instead.'); + /* eslint-enable no-console */ + isWarnedForDeprecation = true; + } }; }; -module.exports.fixable = 'code'; module.exports.schema = [ { enum: ['always', 'always-multiline', 'only-multiline', 'never'] diff --git a/rules/generator-star-spacing.js b/rules/generator-star-spacing.js index 416efff..38d586c 100644 --- a/rules/generator-star-spacing.js +++ b/rules/generator-star-spacing.js @@ -1,93 +1,20 @@ -/** - * @fileoverview Rule to check the spacing around the * in generator functions. - * @author Jamund Ferguson - * @copyright 2015 Brandon Mills. All rights reserved. - * @copyright 2014 Jamund Ferguson. All rights reserved. - */ - "use strict"; -module.exports = function(context) { - - var mode = (function(option) { - if (option == null || typeof option === "string") { - return { - before: { before: true, after: false }, - after: { before: false, after: true }, - both: { before: true, after: true }, - neither: { before: false, after: false } - }[option || "before"]; - } - return option; - }(context.options[0])); - - function isAsyncGenerator(node){ - return context.getFirstToken(node, 2).value === '*' - } - - /** - * Checks the spacing between two tokens before or after the star token. - * @param {string} side Either "before" or "after". - * @param {Token} leftToken `function` keyword token if side is "before", or - * star token if side is "after". - * @param {Token} rightToken Star token if side is "before", or identifier - * token if side is "after". - * @returns {void} - */ - function checkSpacing(side, leftToken, rightToken) { - if (!!(rightToken.range[0] - leftToken.range[1]) !== mode[side]) { - context.report( - leftToken.value === "*" ? leftToken : rightToken, - "{{type}} space {{side}} *.", - { - type: mode[side] ? "Missing" : "Unexpected", - side: side - } - ); - } - } - - /** - * Enforces the spacing around the star if node is a generator function. - * @param {ASTNode} node A function expression or declaration node. - * @returns {void} - */ - function checkFunction(node) { - var first = context.getFirstToken(node) - , isMethod = node.parent.method || node.parent.type === "MethodDefinition" - , isAsync = first.value === 'async'; - - var prevToken, starToken, nextToken; - - - if ( !node.generator || (isAsync && !isAsyncGenerator(node))) { - return; - } - - if (isMethod) { - starToken = context.getTokenBefore(node, 1); - } else { - starToken = context.getFirstToken(node, isAsync ? 2 : 1); - } - - // Only check before when preceded by `function` keyword - prevToken = context.getTokenBefore(starToken); - if (prevToken.value === "function" || prevToken.value === "static") { - checkSpacing("before", prevToken, starToken); - } +var isWarnedForDeprecation = false; +module.exports = function() { + return { + Program() { + if (isWarnedForDeprecation || /\=-(f|-format)=/.test(process.argv.join('='))) { + return; + } - // Only check after when followed by an identifier - nextToken = context.getTokenAfter(starToken); - if (nextToken.type === "Identifier") { - checkSpacing("after", starToken, nextToken); + /* eslint-disable no-console */ + console.log('The babel/generator-star-spacing rule is deprecated. Please ' + + 'use the built in generator-star-spacing rule instead.'); + /* eslint-enable no-console */ + isWarnedForDeprecation = true; } - } - - return { - "FunctionDeclaration": checkFunction, - "FunctionExpression": checkFunction }; - }; module.exports.schema = [ diff --git a/rules/object-shorthand.js b/rules/object-shorthand.js index 4bed9e8..eaf24a1 100644 --- a/rules/object-shorthand.js +++ b/rules/object-shorthand.js @@ -1,70 +1,20 @@ - -/** - * @fileoverview Rule to enforce concise object methods and properties. - * @author Jamund Ferguson - * @copyright 2015 Jamund Ferguson. All rights reserved. - */ - -'use strict'; - -var OPTIONS = { - always: 'always', - never: 'never', - methods: 'methods', - properties: 'properties' +"use strict"; + +var isWarnedForDeprecation = false; +module.exports = function() { + return { + Program() { + if (isWarnedForDeprecation || /\=-(f|-format)=/.test(process.argv.join('='))) { + return; + } + + /* eslint-disable no-console */ + console.log('The babel/object-shorthand rule is deprecated. Please ' + + 'use the built in object-shorthand rule instead.'); + /* eslint-enable no-console */ + isWarnedForDeprecation = true; + } }; - -//------------------------------------------------------------------------------ -// Rule Definition -//------------------------------------------------------------------------------ - -module.exports = function(context) { - - var APPLY = context.options[0] || OPTIONS.always; - var APPLY_TO_METHODS = APPLY === OPTIONS.methods || APPLY === OPTIONS.always; - var APPLY_TO_PROPS = APPLY === OPTIONS.properties || APPLY === OPTIONS.always; - var APPLY_NEVER = APPLY === OPTIONS.never; - - return { - Property: function(node) { - var isConciseProperty = node.method || node.shorthand - , isSpreadProperty = !!this.getSource(node).match(/^\.\.\./) - , type; - - // if we're 'never' and concise we should warn now - if (APPLY_NEVER && isConciseProperty) { - type = node.method ? 'method' : 'property'; - context.report(node, 'Expected longform ' + type + ' syntax.'); - } - - // at this point if we're concise or if we're 'never' we can leave - if (APPLY_NEVER || isConciseProperty) { - return; - } - - // getters, setters and computed properties are ignored - if (node.kind === "get" || node.kind === "set" || node.computed) { - return; - } - - if (isSpreadProperty) { - return; - } - - if (node.value.type === 'FunctionExpression' && node.value.id == null && APPLY_TO_METHODS) { - // {x: function(){}} should be written as {x() {}} - context.report(node, 'Expected method shorthand.'); - } - else if (node.value.type === 'Identifier' && node.key.name === node.value.name && APPLY_TO_PROPS) { - // {x: x} should be written as {x} - context.report(node, 'Expected property shorthand.'); - } - else if (node.value.type === 'Identifier' && node.key.type === 'Literal' && node.key.value === node.value.name && APPLY_TO_PROPS) { - // {'x': x} should be written as {x} - context.report(node, 'Expected property shorthand.'); - } - } - }; }; module.exports.schema = [ diff --git a/tests/rules/array-bracket-spacing.js b/tests/rules/array-bracket-spacing.js deleted file mode 100644 index ea5aa2f..0000000 --- a/tests/rules/array-bracket-spacing.js +++ /dev/null @@ -1,733 +0,0 @@ -/** - * @fileoverview Disallows or enforces spaces inside of brackets. - * @author Ian Christian Myers - * @copyright 2014 Vignesh Anand. All rights reserved. - */ -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -var rule = require('../../rules/array-bracket-spacing'), - RuleTester = require('../RuleTester'); - -//------------------------------------------------------------------------------ -// Tests -//------------------------------------------------------------------------------ - -var ruleTester = new RuleTester(); -ruleTester.run("array-bracket-spacing", rule, { - - valid: [ - { code: "var foo = obj[ 1 ]", options: ["always"] }, - { code: "var foo = obj[ 'foo' ];", options: ["always"] }, - { code: "var foo = obj[ [ 1, 1 ] ];", options: ["always"] }, - - // always - singleValue - { code: "var foo = ['foo']", options: ["always", {singleValue: false}] }, - { code: "var foo = [2]", options: ["always", {singleValue: false}] }, - { code: "var foo = [[ 1, 1 ]]", options: ["always", {singleValue: false}] }, - { code: "var foo = [{ 'foo': 'bar' }]", options: ["always", {singleValue: false}] }, - { code: "var foo = [bar]", options: ["always", {singleValue: false}] }, - - // always - objectsInArrays - { code: "var foo = [{ 'bar': 'baz' }, 1, 5 ];", options: ["always", {objectsInArrays: false}] }, - { code: "var foo = [ 1, 5, { 'bar': 'baz' }];", options: ["always", {objectsInArrays: false}] }, - { code: "var foo = [{\n'bar': 'baz', \n'qux': [{ 'bar': 'baz' }], \n'quxx': 1 \n}]", options: ["always", {objectsInArrays: false}] }, - { code: "var foo = [{ 'bar': 'baz' }]", options: ["always", {objectsInArrays: false}] }, - { code: "var foo = [{ 'bar': 'baz' }, 1, { 'bar': 'baz' }];", options: ["always", {objectsInArrays: false}] }, - { code: "var foo = [ 1, { 'bar': 'baz' }, 5 ];", options: ["always", {objectsInArrays: false}] }, - { code: "var foo = [ 1, { 'bar': 'baz' }, [{ 'bar': 'baz' }] ];", options: ["always", {objectsInArrays: false}] }, - { code: "var foo = [ function(){} ];", options: ["always", {objectsInArrays: false}] }, - - // always - arraysInArrays - { code: "var arr = [[ 1, 2 ], 2, 3, 4 ];", options: ["always", {"arraysInArrays": false}] }, - { code: "var arr = [[ 1, 2 ], [[[ 1 ]]], 3, 4 ];", options: ["always", {"arraysInArrays": false}] }, - { code: "var foo = [ arr[i], arr[j] ];", options: ["always", {"arraysInArrays": false}] }, - - // always - arraysInArrays, objectsInArrays - { code: "var arr = [[ 1, 2 ], 2, 3, { 'foo': 'bar' }];", options: ["always", {"arraysInArrays": false, objectsInArrays: false}] }, - - // always - arraysInArrays, objectsInArrays, singleValue - { code: "var arr = [[ 1, 2 ], [2], 3, { 'foo': 'bar' }];", options: ["always", {"arraysInArrays": false, objectsInArrays: false, singleValue: false}] }, - - // always - { code: "obj[ foo ]", options: ["always"] }, - { code: "obj[\nfoo\n]", options: ["always"] }, - { code: "obj[ 'foo' ]", options: ["always"] }, - { code: "obj[ 'foo' + 'bar' ]", options: ["always"] }, - { code: "obj[ obj2[ foo ] ]", options: ["always"] }, - { code: "obj.map(function(item) { return [\n1,\n2,\n3,\n4\n]; })", options: ["always"] }, - { code: "obj[ 'map' ](function(item) { return [\n1,\n2,\n3,\n4\n]; })", options: ["always"] }, - { code: "obj[ 'for' + 'Each' ](function(item) { return [\n1,\n2,\n3,\n4\n]; })", options: ["always"] }, - - { code: "var arr = [ 1, 2, 3, 4 ];", options: ["always"] }, - { code: "var arr = [ [ 1, 2 ], 2, 3, 4 ];", options: ["always"] }, - { code: "var arr = [\n1, 2, 3, 4\n];", options: ["always"] }, - { code: "var foo = [];", options: ["always"] }, - - // singleValue: false, objectsInArrays: true, arraysInArrays - { code: "this.db.mappings.insert([\n { alias: 'a', url: 'http://www.amazon.de' },\n { alias: 'g', url: 'http://www.google.de' }\n], function() {});", options: ["always", {singleValue: false, objectsInArrays: true, arraysInArrays: true}] }, - - // always - destructuring assignment - { code: "var [ x, y ] = z", ecmaFeatures: { destructuring: true }, options: ["always"] }, - { code: "var [ x,y ] = z", ecmaFeatures: { destructuring: true }, options: ["always"] }, - { code: "var [ x, y\n] = z", ecmaFeatures: { destructuring: true }, options: ["always"] }, - { code: "var [\nx, y ] = z", ecmaFeatures: { destructuring: true }, options: ["always"] }, - { code: "var [\nx, y\n] = z", ecmaFeatures: { destructuring: true }, options: ["always"] }, - { code: "var [\nx, y\n] = z", ecmaFeatures: { destructuring: true }, options: ["always"] }, - { code: "var [\nx,,,\n] = z", ecmaFeatures: { destructuring: true }, options: ["always"] }, - { code: "var [ ,x, ] = z", ecmaFeatures: { destructuring: true }, options: ["always"] }, - { code: "var [\nx, ...y\n] = z", ecmaFeatures: { destructuring: true, spread:true }, options: ["always"] }, - { code: "var [\nx, ...y ] = z", ecmaFeatures: { destructuring: true, spread:true }, options: ["always"] }, - { code: "var [[ x, y ], z ] = arr;", ecmaFeatures: { destructuring: true }, options: ["always", {"arraysInArrays": false}] }, - { code: "var [ x, [ y, z ]] = arr;", ecmaFeatures: { destructuring: true }, options: ["always", {"arraysInArrays": false}] }, - { code: "[{ x, y }, z ] = arr;", ecmaFeatures: { destructuring: true }, options: ["always", {objectsInArrays: false}] }, - { code: "[ x, { y, z }] = arr;", ecmaFeatures: { destructuring: true }, options: ["always", {objectsInArrays: false}] }, - - // never - { code: "obj[foo]", options: ["never"] }, - { code: "obj['foo']", options: ["never"] }, - { code: "obj['foo' + 'bar']", options: ["never"] }, - { code: "obj['foo'+'bar']", options: ["never"] }, - { code: "obj[obj2[foo]]", options: ["never"] }, - { code: "obj.map(function(item) { return [\n1,\n2,\n3,\n4\n]; })", options: ["never"] }, - { code: "obj['map'](function(item) { return [\n1,\n2,\n3,\n4\n]; })", options: ["never"] }, - { code: "obj['for' + 'Each'](function(item) { return [\n1,\n2,\n3,\n4\n]; })", options: ["never"] }, - { code: "obj['for' + 'Each'](function(item) { return [\n1,\n2,\n3,\n4\n]; })", options: ["never"] }, - { code: "var arr = [1, 2, 3, 4];", options: ["never"] }, - { code: "var arr = [[1, 2], 2, 3, 4];", options: ["never"] }, - { code: "var arr = [\n1, 2, 3, 4\n];", options: ["never"] }, - { code: "obj[\nfoo]", options: ["never"] }, - { code: "obj[foo\n]", options: ["never"] }, - { code: "var arr = [1,\n2,\n3,\n4\n];", options: ["never"] }, - { code: "var arr = [\n1,\n2,\n3,\n4];", options: ["never"] }, - - // never - destructuring assignment - { code: "var [x, y] = z", ecmaFeatures: { destructuring: true }, options: ["never"] }, - { code: "var [x,y] = z", ecmaFeatures: { destructuring: true }, options: ["never"] }, - { code: "var [x, y\n] = z", ecmaFeatures: { destructuring: true }, options: ["never"] }, - { code: "var [\nx, y] = z", ecmaFeatures: { destructuring: true }, options: ["never"] }, - { code: "var [\nx, y\n] = z", ecmaFeatures: { destructuring: true }, options: ["never"] }, - { code: "var [\nx, y\n] = z", ecmaFeatures: { destructuring: true }, options: ["never"] }, - { code: "var [\nx,,,\n] = z", ecmaFeatures: { destructuring: true }, options: ["never"] }, - { code: "var [,x,] = z", ecmaFeatures: { destructuring: true }, options: ["never"] }, - { code: "var [\nx, ...y\n] = z", ecmaFeatures: { destructuring: true, spread:true }, options: ["never"] }, - { code: "var [\nx, ...y] = z", ecmaFeatures: { destructuring: true, spread:true }, options: ["never"] }, - { code: "var [ [x, y], z] = arr;", ecmaFeatures: { destructuring: true }, options: ["never", {"arraysInArrays": true}] }, - { code: "var [x, [y, z] ] = arr;", ecmaFeatures: { destructuring: true }, options: ["never", {"arraysInArrays": true}] }, - { code: "[ { x, y }, z] = arr;", ecmaFeatures: { destructuring: true }, options: ["never", {objectsInArrays: true}] }, - { code: "[x, { y, z } ] = arr;", ecmaFeatures: { destructuring: true }, options: ["never", {objectsInArrays: true}] }, - - // never - singleValue - { code: "var foo = [ 'foo' ]", options: ["never", {singleValue: true}] }, - { code: "var foo = [ 2 ]", options: ["never", {singleValue: true}] }, - { code: "var foo = [ [1, 1] ]", options: ["never", {singleValue: true}] }, - { code: "var foo = [ {'foo': 'bar'} ]", options: ["never", {singleValue: true}] }, - { code: "var foo = [ bar ]", options: ["never", {singleValue: true}] }, - - // never - objectsInArrays - { code: "var foo = [ {'bar': 'baz'}, 1, 5];", options: ["never", {objectsInArrays: true}] }, - { code: "var foo = [1, 5, {'bar': 'baz'} ];", options: ["never", {objectsInArrays: true}] }, - { code: "var foo = [ {\n'bar': 'baz', \n'qux': [ {'bar': 'baz'} ], \n'quxx': 1 \n} ]", options: ["never", {objectsInArrays: true}] }, - { code: "var foo = [ {'bar': 'baz'} ]", options: ["never", {objectsInArrays: true}] }, - { code: "var foo = [ {'bar': 'baz'}, 1, {'bar': 'baz'} ];", options: ["never", {objectsInArrays: true}] }, - { code: "var foo = [1, {'bar': 'baz'} , 5];", options: ["never", {objectsInArrays: true}] }, - { code: "var foo = [1, {'bar': 'baz'}, [ {'bar': 'baz'} ]];", options: ["never", {objectsInArrays: true}] }, - { code: "var foo = [function(){}];", options: ["never", {objectsInArrays: true}] }, - { code: "var foo = [];", options: ["never", {objectsInArrays: true}] }, - - // never - arraysInArrays - { code: "var arr = [ [1, 2], 2, 3, 4];", options: ["never", {"arraysInArrays": true}] }, - { code: "var foo = [arr[i], arr[j]];", options: ["never", {"arraysInArrays": true}] }, - { code: "var foo = [];", options: ["never", {"arraysInArrays": true}] }, - - // never - arraysInArrays, singleValue - { code: "var arr = [ [1, 2], [ [ [ 1 ] ] ], 3, 4];", options: ["never", {"arraysInArrays": true, singleValue: true}] }, - - // never - arraysInArrays, objectsInArrays - { code: "var arr = [ [1, 2], 2, 3, {'foo': 'bar'} ];", options: ["never", {"arraysInArrays": true, objectsInArrays: true}] }, - - // should not warn - { code: "var foo = {};", options: ["never"] }, - { code: "var foo = [];", options: ["never"] }, - - { code: "var foo = [{'bar':'baz'}, 1, {'bar': 'baz'}];", options: ["never"] }, - { code: "var foo = [{'bar': 'baz'}];", options: ["never"] }, - { code: "var foo = [{\n'bar': 'baz', \n'qux': [{'bar': 'baz'}], \n'quxx': 1 \n}]", options: ["never"] }, - { code: "var foo = [1, {'bar': 'baz'}, 5];", options: ["never"] }, - { code: "var foo = [{'bar': 'baz'}, 1, 5];", options: ["never"] }, - { code: "var foo = [1, 5, {'bar': 'baz'}];", options: ["never"] }, - { code: "var obj = {'foo': [1, 2]}", options: ["never"] }, - - // Babel test cases. - // always - destructuring typed array param - { code: "function fn([ a,b ]: Array){}", options: ["always"], parser: "babel-eslint", ecmaFeatures: { destructuring: true } }, - - // never - destructuring typed array param - { code: "function fn([a,b]: Array){}", options: ["never"], parser: "babel-eslint", ecmaFeatures: { destructuring: true } }, - - ], - - invalid: [ - { - code: "var foo = [ ]", - output: "var foo = []", - options: ["never"], - errors: [ - { - message: "There should be no space after '['", - type: "ArrayExpression", - line: 1, - column: 11 - } - ] - }, - // objectsInArrays - { - code: "var foo = [ { 'bar': 'baz' }, 1, 5];", - output: "var foo = [{ 'bar': 'baz' }, 1, 5 ];", - options: ["always", {objectsInArrays: false}], - errors: [ - { - message: "There should be no space after '['", - type: "ArrayExpression", - line: 1, - column: 11 - }, - { - message: "A space is required before ']'", - type: "ArrayExpression", - line: 1, - column: 36 - } - ] - }, - { - code: "var foo = [1, 5, { 'bar': 'baz' } ];", - output: "var foo = [ 1, 5, { 'bar': 'baz' }];", - options: ["always", {objectsInArrays: false}], - errors: [ - { - message: "A space is required after '['", - type: "ArrayExpression", - line: 1, - column: 11 - }, - { - message: "There should be no space before ']'", - type: "ArrayExpression", - line: 1, - column: 35 - } - ] - }, - { - code: "var foo = [ { 'bar':'baz' }, 1, { 'bar': 'baz' } ];", - output: "var foo = [{ 'bar':'baz' }, 1, { 'bar': 'baz' }];", - options: ["always", {objectsInArrays: false}], - errors: [ - { - message: "There should be no space after '['", - type: "ArrayExpression", - line: 1, - column: 11 - }, - { - message: "There should be no space before ']'", - type: "ArrayExpression", - line: 1, - column: 50 - } - ] - }, - - // singleValue - { - code: "var obj = [ 'foo' ];", - output: "var obj = ['foo'];", - options: ["always", {singleValue: false}], - errors: [ - { - message: "There should be no space after '['", - type: "ArrayExpression", - line: 1, - column: 11 - }, - { - message: "There should be no space before ']'", - type: "ArrayExpression", - line: 1, - column: 19 - } - ] - }, - { - code: "var obj = ['foo' ];", - output: "var obj = ['foo'];", - options: ["always", {singleValue: false}], - errors: [ - { - message: "There should be no space before ']'", - type: "ArrayExpression", - line: 1, - column: 18 - } - ] - }, - { - code: "var obj = ['foo'];", - output: "var obj = [ 'foo' ];", - options: ["never", {singleValue: true}], - errors: [ - { - message: "A space is required after '['", - type: "ArrayExpression", - line: 1, - column: 11 - }, - { - message: "A space is required before ']'", - type: "ArrayExpression", - line: 1, - column: 17 - } - ] - }, - - // always - arraysInArrays - { - code: "var arr = [ [ 1, 2 ], 2, 3, 4 ];", - output: "var arr = [[ 1, 2 ], 2, 3, 4 ];", - options: ["always", {"arraysInArrays": false}], - errors: [ - { - message: "There should be no space after '['", - type: "ArrayExpression", - line: 1, - column: 11 - } - ] - }, - { - code: "var arr = [ 1, 2, 2, [ 3, 4 ] ];", - output: "var arr = [ 1, 2, 2, [ 3, 4 ]];", - options: ["always", {"arraysInArrays": false}], - errors: [ - { - message: "There should be no space before ']'", - type: "ArrayExpression", - line: 1, - column: 31 - } - ] - }, - { - code: "var arr = [[ 1, 2 ], 2, [ 3, 4 ] ];", - output: "var arr = [[ 1, 2 ], 2, [ 3, 4 ]];", - options: ["always", {"arraysInArrays": false}], - errors: [ - { - message: "There should be no space before ']'", - type: "ArrayExpression", - line: 1, - column: 34 - } - ] - }, - { - code: "var arr = [ [ 1, 2 ], 2, [ 3, 4 ]];", - output: "var arr = [[ 1, 2 ], 2, [ 3, 4 ]];", - options: ["always", {"arraysInArrays": false}], - errors: [ - { - message: "There should be no space after '['", - type: "ArrayExpression", - line: 1, - column: 11 - } - ] - }, - { - code: "var arr = [ [ 1, 2 ], 2, [ 3, 4 ] ];", - output: "var arr = [[ 1, 2 ], 2, [ 3, 4 ]];", - options: ["always", {"arraysInArrays": false}], - errors: [ - { - message: "There should be no space after '['", - type: "ArrayExpression", - line: 1, - column: 11 - }, - { - message: "There should be no space before ']'", - type: "ArrayExpression", - line: 1, - column: 35 - } - ] - }, - - // always - destructuring - { - code: "var [x,y] = y", - output: "var [ x,y ] = y", - options: ["always"], - ecmaFeatures: { destructuring: true }, - errors: [{ - message: "A space is required after '['", - type: "ArrayPattern", - line: 1, - column: 5 - }, - { - message: "A space is required before ']'", - type: "ArrayPattern", - line: 1, - column: 9 - }] - }, - { - code: "var [x,y ] = y", - output: "var [ x,y ] = y", - options: ["always"], - ecmaFeatures: { destructuring: true }, - errors: [{ - message: "A space is required after '['", - type: "ArrayPattern", - line: 1, - column: 5 - }] - }, - { - code: "var [,,,x,,] = y", - output: "var [ ,,,x,, ] = y", - options: ["always"], - ecmaFeatures: { destructuring: true }, - errors: [{ - message: "A space is required after '['", - type: "ArrayPattern", - line: 1, - column: 5 - }, - { - message: "A space is required before ']'", - type: "ArrayPattern", - line: 1, - column: 12 - }] - }, - { - code: "var [ ,,,x,,] = y", - output: "var [ ,,,x,, ] = y", - options: ["always"], - ecmaFeatures: { destructuring: true }, - errors: [{ - message: "A space is required before ']'", - type: "ArrayPattern", - line: 1, - column: 13 - }] - }, - { - code: "var [...horse] = y", - output: "var [ ...horse ] = y", - options: ["always"], - ecmaFeatures: { destructuring: true, spread:true }, - errors: [{ - message: "A space is required after '['", - type: "ArrayPattern", - line: 1, - column: 5 - }, - { - message: "A space is required before ']'", - type: "ArrayPattern", - line: 1, - column: 14 - }] - }, - { - code: "var [...horse ] = y", - output: "var [ ...horse ] = y", - options: ["always"], - ecmaFeatures: { destructuring: true, spread:true }, - errors: [{ - message: "A space is required after '['", - type: "ArrayPattern", - line: 1, - column: 5 - }] - }, - { - code: "var [ [ x, y ], z ] = arr;", - output: "var [[ x, y ], z ] = arr;", - options: ["always", {"arraysInArrays": false}], - ecmaFeatures: { destructuring: true }, - errors: [{ - message: "There should be no space after '['", - type: "ArrayPattern", - line: 1, - column: 5 - }] - }, - { - code: "[ { x, y }, z ] = arr;", - output: "[{ x, y }, z ] = arr;", - options: ["always", {"objectsInArrays": false}], - ecmaFeatures: { destructuring: true }, - errors: [{ - message: "There should be no space after '['", - type: "ArrayPattern", - line: 1, - column: 1 - }] - }, - { - code: "[ x, { y, z } ] = arr;", - output: "[ x, { y, z }] = arr;", - options: ["always", {"objectsInArrays": false}], - ecmaFeatures: { destructuring: true }, - errors: [{ - message: "There should be no space before ']'", - type: "ArrayPattern", - line: 1, - column: 15 - }] - }, - - // never - arraysInArrays - { - code: "var arr = [[1, 2], 2, [3, 4]];", - output: "var arr = [ [1, 2], 2, [3, 4] ];", - options: ["never", {"arraysInArrays": true}], - errors: [ - { - message: "A space is required after '['", - type: "ArrayExpression", - line: 1, - column: 11 - }, - { - message: "A space is required before ']'", - type: "ArrayExpression", - line: 1, - column: 29 - } - ] - }, - { - code: "var arr = [ ];", - output: "var arr = [];", - options: ["never", {"arraysInArrays": true}], - errors: [ - { - message: "There should be no space after '['", - type: "ArrayExpression", - line: 1, - column: 11 - } - ] - }, - - // never - objectsInArrays - { - code: "var arr = [ ];", - output: "var arr = [];", - options: ["never", {"objectsInArrays": true}], - errors: [ - { - message: "There should be no space after '['", - type: "ArrayExpression", - line: 1, - column: 11 - } - ] - }, - - // always - { - code: "var arr = [1, 2, 3, 4];", - output: "var arr = [ 1, 2, 3, 4 ];", - options: ["always"], - errors: [ - { - message: "A space is required after '['", - type: "ArrayExpression", - line: 1, - column: 11 - }, - { - message: "A space is required before ']'", - type: "ArrayExpression", - line: 1, - column: 22 - } - ] - }, - { - code: "var arr = [1, 2, 3, 4 ];", - output: "var arr = [ 1, 2, 3, 4 ];", - options: ["always"], - errors: [ - { - message: "A space is required after '['", - type: "ArrayExpression", - line: 1, - column: 11 - } - ] - }, - { - code: "var arr = [ 1, 2, 3, 4];", - output: "var arr = [ 1, 2, 3, 4 ];", - options: ["always"], - errors: [ - { - message: "A space is required before ']'", - type: "ArrayExpression", - line: 1, - column: 23 - } - ] - }, - - // never - { - code: "var arr = [ 1, 2, 3, 4 ];", - output: "var arr = [1, 2, 3, 4];", - options: ["never"], - errors: [ - { - message: "There should be no space after '['", - type: "ArrayExpression", - line: 1, - column: 11 - }, - { - message: "There should be no space before ']'", - type: "ArrayExpression", - line: 1, - column: 24 - } - ] - }, - { - code: "var arr = [1, 2, 3, 4 ];", - output: "var arr = [1, 2, 3, 4];", - options: ["never"], - errors: [ - { - message: "There should be no space before ']'", - type: "ArrayExpression", - line: 1, - column: 23 - } - ] - }, - { - code: "var arr = [ 1, 2, 3, 4];", - output: "var arr = [1, 2, 3, 4];", - options: ["never"], - errors: [ - { - message: "There should be no space after '['", - type: "ArrayExpression", - line: 1, - column: 11 - } - ] - }, - { - code: "var arr = [ [ 1], 2, 3, 4];", - output: "var arr = [[1], 2, 3, 4];", - options: ["never"], - errors: [ - { - message: "There should be no space after '['", - type: "ArrayExpression", - line: 1, - column: 11 - }, - { - message: "There should be no space after '['", - type: "ArrayExpression", - line: 1, - column: 13 - } - ] - }, - { - code: "var arr = [[1 ], 2, 3, 4 ];", - output: "var arr = [[1], 2, 3, 4];", - options: ["never"], - errors: [ - { - message: "There should be no space before ']'", - type: "ArrayExpression", - line: 1, - column: 15 - }, - { - message: "There should be no space before ']'", - type: "ArrayExpression", - line: 1, - column: 26 - } - ] - }, - - // Babel test cases. - - // always - destructuring typed array param - { - code: "function fn([a,b]: Array){}", - output: "function fn([ a,b ]: Array){}", - options: ["always"], - parser: "babel-eslint", - ecmaFeatures: { - destructuring: true - }, - errors: [ - { - message: "A space is required after '['", - type: "ArrayPattern", - line: 1, - column: 13 - }, - { - message: "A space is required before ']'", - type: "ArrayPattern", - line: 1, - column: 17 - } - ] - }, - - // never - destructuring typed array param - { - code: "function fn([ a,b ]: Array){}", - output: "function fn([a,b]: Array){}", - options: ["never"], - parser: "babel-eslint", - ecmaFeatures: { - destructuring: true - }, - errors: [ - { - message: "There should be no space after '['", - type: "ArrayPattern", - line: 1, - column: 13 - }, - { - message: "There should be no space before ']'", - type: "ArrayPattern", - line: 1, - column: 19 - } - ] - } - ] -}); diff --git a/tests/rules/arrow-parens.js b/tests/rules/arrow-parens.js deleted file mode 100644 index 1962be7..0000000 --- a/tests/rules/arrow-parens.js +++ /dev/null @@ -1,187 +0,0 @@ -/* eslint-disable */ - -/** - * @fileoverview Tests for arrow-parens - * @author Jxck - */ - -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -var rule = require("../../rules/arrow-parens"), - RuleTester = require('../RuleTester'); - -function ok(code, args){ - return { code: code, options: args, parser: 'babel-eslint' } -} - -function err(code, output, errors, args){ - var e = ok(code, args) - e.errors = errors - e.output = output - return e -} - -//------------------------------------------------------------------------------ -// Tests -//------------------------------------------------------------------------------ -var ruleTester = new RuleTester(); - -var valid = [ - { code: "() => {}", ecmaFeatures: { arrowFunctions: true } }, - { code: "(a) => {}", ecmaFeatures: { arrowFunctions: true } }, - { code: "(a) => a", ecmaFeatures: { arrowFunctions: true } }, - { code: "(a) => {\n}", ecmaFeatures: { arrowFunctions: true } }, - { code: "a.then((foo) => {});", ecmaFeatures: { arrowFunctions: true } }, - { code: "a.then((foo) => { if (true) {}; });", ecmaFeatures: { arrowFunctions: true } }, - - // // as-needed - { code: "() => {}", options: ["as-needed"], ecmaFeatures: { arrowFunctions: true } }, - { code: "a => {}", options: ["as-needed"], ecmaFeatures: { arrowFunctions: true } }, - { code: "a => a", options: ["as-needed"], ecmaFeatures: { arrowFunctions: true } }, - { code: "([a, b]) => {}", options: ["as-needed"], ecmaFeatures: { arrowFunctions: true, destructuring: true } }, - { code: "({ a, b }) => {}", options: ["as-needed"], ecmaFeatures: { arrowFunctions: true, destructuring: true } }, - { code: "(a = 10) => {}", options: ["as-needed"], ecmaFeatures: { arrowFunctions: true, destructuring: true, defaultParams: true } }, - { code: "(...a) => a[0]", options: ["as-needed"], ecmaFeatures: { arrowFunctions: true, restParams: true } }, - { code: "(a, b) => {}", options: ["as-needed"], ecmaFeatures: { arrowFunctions: true } }, - ok("(a: string) => a", ["as-needed"]), - - // async - ok("async () => {}"), - ok("async (a) => {}"), - ok("async (a) => a"), - ok("async (a) => {\n}"), - ok("a.then(async (foo) => {});"), - ok("a.then((foo) => { if (true) {}; })"), - - ok("async () => {}", ["as-needed"]), - ok("async a => {}", ["as-needed"]), - ok("async a => a", ["as-needed"]), - ok("async ([a, b]) => {}", ["as-needed"]), - ok("async ({ a, b }) => {}", ["as-needed"]), - ok("async (a = 10) => {}", ["as-needed"]), - ok("async (...a) => a[0]", ["as-needed"]), - ok("async (a, b) => {}", ["as-needed"]), - -]; - -var message = message; -var asNeededMessage = asNeededMessage; -var type = type; - -var invalid = [ - { - code: "a => {}", - output: "(a) => {}", - ecmaFeatures: { arrowFunctions: true }, - errors: [{ - line: 1, - column: 1, - message: message, - type: type - }] - }, - { - code: "a => a", - output: "(a) => a", - ecmaFeatures: { arrowFunctions: true }, - errors: [{ - line: 1, - column: 1, - message: message, - type: type - }] - }, - { - code: "a => {\n}", - output: "(a) => {\n}", - ecmaFeatures: { arrowFunctions: true }, - errors: [{ - line: 1, - column: 1, - message: message, - type: type - }] - }, - { - code: "a.then(foo => {});", - output: "a.then((foo) => {});", - ecmaFeatures: { arrowFunctions: true }, - errors: [{ - line: 1, - column: 8, - message: message, - type: type - }] - }, - { - code: "a.then(foo => a);", - output: "a.then((foo) => a);", - ecmaFeatures: { arrowFunctions: true }, - errors: [{ - line: 1, - column: 8, - message: message, - type: type - }] - }, - { - code: "a(foo => { if (true) {}; });", - output: "a((foo) => { if (true) {}; });", - ecmaFeatures: { arrowFunctions: true }, - errors: [{ - line: 1, - column: 3, - message: message, - type: type - }] - }, - - // as-needed - { - code: "(a) => a", - output: "a => a", - options: ["as-needed"], - ecmaFeatures: { arrowFunctions: true }, - errors: [{ - line: 1, - column: 1, - message: asNeededMessage, - type: type - }] - }, - { - code: "(b) => b", - output: "b => b", - options: ["as-needed"], - ecmaFeatures: { arrowFunctions: true }, - errors: [{ - line: 1, - column: 1, - message: asNeededMessage, - type: type - }] - }, - - // async - err('async a => {}', 'async (a) => {}', [ - { message: 'Expected parentheses around arrow function argument.' }, - ]), - - err('async a => a', 'async (a) => a', [ - { message: 'Expected parentheses around arrow function argument.' }, - ]), - - err('async (a) => a', 'async a => a', [ - { message: 'Unexpected parentheses around single function argument' }, - ], - ["as-needed"]) -]; - -ruleTester.run("arrow-parens", rule, { - valid: valid, - invalid: invalid -}); diff --git a/tests/rules/func-params-comma-dangle.js b/tests/rules/func-params-comma-dangle.js deleted file mode 100644 index a1676e4..0000000 --- a/tests/rules/func-params-comma-dangle.js +++ /dev/null @@ -1,278 +0,0 @@ -'use strict'; - -var rule = require('../../rules/func-params-comma-dangle'); -var RuleTester = require('../RuleTester'); - -var MISSING_I = [{message: 'Missing trailing comma.', type: 'Identifier'}]; -var MISSING_AP = [{message: 'Missing trailing comma.', type: 'AssignmentPattern'}]; -var MISSING_SE = [{message: 'Missing trailing comma.', type: 'SpreadElement'}]; -var UNEXPECTED_I = [{message: 'Unexpected trailing comma.', type: 'Identifier'}]; -var UNEXPECTED_AP = [{message: 'Unexpected trailing comma.', type: 'AssignmentPattern'}]; -var UNEXPECTED_SE = [{message: 'Unexpected trailing comma.', type: 'SpreadElement'}]; - -var ruleTester = new RuleTester({parser: 'babel-eslint'}); -ruleTester.run('func-params-comma-dangle', rule, { - valid: [ - {code: 'function f() {}'}, - {code: 'function f(\n) {}'}, - {code: 'function f(...a) {}'}, - {code: 'function f(...a\n) {}'}, - - {code: 'f()'}, - {code: 'f(\n)'}, - - {code: 'new F()'}, - {code: 'new F(\n)'}, - - // FunctionDeclaration - {code: 'function f(a) {}', options: ['always-multiline']}, - {code: 'function f(a) {}', options: ['never']}, - {code: 'function f(a) {}', options: ['only-multiline']}, - {code: 'function f(a,) {}', options: ['always']}, - {code: 'function f(a,\n) {}', options: ['always']}, - {code: 'function f(a,\n) {}', options: ['always-multiline']}, - {code: 'function f(a,\n) {}', options: ['only-multiline']}, - {code: 'function f(a\n) {}', options: ['never']}, - {code: 'function f(a\n) {}', options: ['only-multiline']}, - - {code: 'function f(a=1) {}', options: ['always-multiline']}, - {code: 'function f(a=1) {}', options: ['never']}, - {code: 'function f(a=1) {}', options: ['only-multiline']}, - {code: 'function f(a=1,) {}', options: ['always']}, - {code: 'function f(a=1,\n) {}', options: ['always']}, - {code: 'function f(a=1,\n) {}', options: ['always-multiline']}, - {code: 'function f(a=1,\n) {}', options: ['only-multiline']}, - {code: 'function f(a=1\n) {}', options: ['never']}, - {code: 'function f(a=1\n) {}', options: ['only-multiline']}, - - {code: 'function f(a:T) {}', options: ['always-multiline']}, - {code: 'function f(a:T) {}', options: ['never']}, - {code: 'function f(a:T) {}', options: ['only-multiline']}, - {code: 'function f(a:T,) {}', options: ['always']}, - {code: 'function f(a:T,\n) {}', options: ['always']}, - {code: 'function f(a:T,\n) {}', options: ['always-multiline']}, - {code: 'function f(a:T,\n) {}', options: ['only-multiline']}, - {code: 'function f(a:T\n) {}', options: ['never']}, - {code: 'function f(a:T\n) {}', options: ['only-multiline']}, - - // FunctionExpression - {code: 'f = function(a) {}', options: ['always-multiline']}, - {code: 'f = function(a) {}', options: ['never']}, - {code: 'f = function(a) {}', options: ['only-multiline']}, - {code: 'f = function(a,) {}', options: ['always']}, - {code: 'f = function(a,\n) {}', options: ['always']}, - {code: 'f = function(a,\n) {}', options: ['always-multiline']}, - {code: 'f = function(a,\n) {}', options: ['only-multiline']}, - {code: 'f = function(a\n) {}', options: ['never']}, - {code: 'f = function(a\n) {}', options: ['only-multiline']}, - - {code: 'f = function(a=1) {}', options: ['always-multiline']}, - {code: 'f = function(a=1) {}', options: ['never']}, - {code: 'f = function(a=1) {}', options: ['only-multiline']}, - {code: 'f = function(a=1,) {}', options: ['always']}, - {code: 'f = function(a=1,\n) {}', options: ['always']}, - {code: 'f = function(a=1,\n) {}', options: ['always-multiline']}, - {code: 'f = function(a=1,\n) {}', options: ['only-multiline']}, - {code: 'f = function(a=1\n) {}', options: ['never']}, - {code: 'f = function(a=1\n) {}', options: ['only-multiline']}, - - {code: 'f = function(a:T) {}', options: ['always-multiline']}, - {code: 'f = function(a:T) {}', options: ['never']}, - {code: 'f = function(a:T) {}', options: ['only-multiline']}, - {code: 'f = function(a:T,) {}', options: ['always']}, - {code: 'f = function(a:T,\n) {}', options: ['always']}, - {code: 'f = function(a:T,\n) {}', options: ['always-multiline']}, - {code: 'f = function(a:T,\n) {}', options: ['only-multiline']}, - {code: 'f = function(a:T\n) {}', options: ['never']}, - {code: 'f = function(a:T\n) {}', options: ['only-multiline']}, - - // ArrowFunctionExpression - {code: 'f = (a) => {}', options: ['always-multiline']}, - {code: 'f = (a) => {}', options: ['never']}, - {code: 'f = (a) => {}', options: ['only-multiline']}, - {code: 'f = (a,) => {}', options: ['always']}, - {code: 'f = (a,\n) => {}', options: ['always']}, - {code: 'f = (a,\n) => {}', options: ['always-multiline']}, - {code: 'f = (a,\n) => {}', options: ['only-multiline']}, - {code: 'f = (a\n) => {}', options: ['never']}, - {code: 'f = (a\n) => {}', options: ['only-multiline']}, - - {code: 'f = a => {}', options: ['always-multiline']}, - {code: 'f = a => {}', options: ['never']}, - {code: 'f = a => {}', options: ['only-multiline']}, - {code: 'f = a => {}', options: ['always']}, - - {code: 'f = (a=1) => {}', options: ['always-multiline']}, - {code: 'f = (a=1) => {}', options: ['never']}, - {code: 'f = (a=1) => {}', options: ['only-multiline']}, - {code: 'f = (a=1,) => {}', options: ['always']}, - {code: 'f = (a=1,\n) => {}', options: ['always']}, - {code: 'f = (a=1,\n) => {}', options: ['always-multiline']}, - {code: 'f = (a=1,\n) => {}', options: ['only-multiline']}, - {code: 'f = (a=1\n) => {}', options: ['never']}, - {code: 'f = (a=1\n) => {}', options: ['only-multiline']}, - - {code: 'f = (a:T) => {}', options: ['always-multiline']}, - {code: 'f = (a:T) => {}', options: ['never']}, - {code: 'f = (a:T) => {}', options: ['only-multiline']}, - // Arrow functions with flow types aren't getting the correct loc. - // {code: 'f = (a:T,) => {}', options: ['always']}, - // {code: 'f = (a:T,\n) => {}', options: ['always']}, - {code: 'f = (a:T,\n) => {}', options: ['always-multiline']}, - {code: 'f = (a:T,\n) => {}', options: ['only-multiline']}, - {code: 'f = (a:T\n) => {}', options: ['never']}, - {code: 'f = (a:T\n) => {}', options: ['only-multiline']}, - - // CallExpression - {code: 'f(a)', options: ['always-multiline']}, - {code: 'f(a)', options: ['never']}, - {code: 'f(a)', options: ['only-multiline']}, - {code: 'f(a,)', options: ['always']}, - {code: 'f(a,\n)', options: ['always']}, - {code: 'f(a,\n)', options: ['always-multiline']}, - {code: 'f(a,\n)', options: ['only-multiline']}, - {code: 'f(a\n)', options: ['never']}, - {code: 'f(a\n)', options: ['only-multiline']}, - {code: 'f(...a)', options: ['always-multiline']}, - {code: 'f(...a)', options: ['never']}, - {code: 'f(...a)', options: ['only-multiline']}, - {code: 'f(...a,)', options: ['always']}, - {code: 'f(...a,\n)', options: ['always']}, - {code: 'f(...a,\n)', options: ['always-multiline']}, - {code: 'f(...a,\n)', options: ['only-multiline']}, - {code: 'f(...a\n)', options: ['never']}, - {code: 'f(...a\n)', options: ['only-multiline']}, - - // NewExpression - {code: 'new F(a)', options: ['always-multiline']}, - {code: 'new F(a)', options: ['never']}, - {code: 'new F(a)', options: ['only-multiline']}, - {code: 'new F(a,)', options: ['always']}, - {code: 'new F(a,\n)', options: ['always']}, - {code: 'new F(a,\n)', options: ['always-multiline']}, - {code: 'new F(a,\n)', options: ['only-multiline']}, - {code: 'new F(a\n)', options: ['never']}, - {code: 'new F(a\n)', options: ['only-multiline']}, - {code: 'new F(...a)', options: ['always-multiline']}, - {code: 'new F(...a)', options: ['never']}, - {code: 'new F(...a)', options: ['only-multiline']}, - {code: 'new F(...a,)', options: ['always']}, - {code: 'new F(...a,\n)', options: ['always']}, - {code: 'new F(...a,\n)', options: ['always-multiline']}, - {code: 'new F(...a,\n)', options: ['only-multiline']}, - {code: 'new F(...a\n)', options: ['never']}, - {code: 'new F(...a\n)', options: ['only-multiline']}, - ], - invalid: [ - // FunctionDeclaration - {code: 'function f(a) {}', output: 'function f(a,) {}', options: ['always'], errors: MISSING_I}, - {code: 'function f(a,) {}', output: 'function f(a) {}', options: ['always-multiline'], errors: UNEXPECTED_I}, - {code: 'function f(a,) {}', output: 'function f(a) {}', options: ['only-multiline'], errors: UNEXPECTED_I}, - {code: 'function f(a,) {}', output: 'function f(a) {}', options: ['never'], errors: UNEXPECTED_I}, - {code: 'function f(a,\n) {}', output: 'function f(a\n) {}', options: ['never'], errors: UNEXPECTED_I}, - {code: 'function f(a\n) {}', output: 'function f(a,\n) {}', options: ['always'], errors: MISSING_I}, - {code: 'function f(a\n) {}', output: 'function f(a,\n) {}', options: ['always-multiline'], errors: MISSING_I}, - - {code: 'function f(a=1) {}', output: 'function f(a=1,) {}', options: ['always'], errors: MISSING_AP}, - {code: 'function f(a=1,) {}', output: 'function f(a=1) {}', options: ['always-multiline'], errors: UNEXPECTED_AP}, - {code: 'function f(a=1,) {}', output: 'function f(a=1) {}', options: ['always-multiline'], errors: UNEXPECTED_AP}, - {code: 'function f(a=1,) {}', output: 'function f(a=1) {}', options: ['only-multiline'], errors: UNEXPECTED_AP}, - {code: 'function f(a=1,) {}', output: 'function f(a=1) {}', options: ['never'], errors: UNEXPECTED_AP}, - {code: 'function f(a=1,\n) {}', output: 'function f(a=1\n) {}', options: ['never'], errors: UNEXPECTED_AP}, - {code: 'function f(a=1\n) {}', output: 'function f(a=1,\n) {}', options: ['always'], errors: MISSING_AP}, - {code: 'function f(a=1\n) {}', output: 'function f(a=1,\n) {}', options: ['always-multiline'], errors: MISSING_AP}, - - {code: 'function f(a:T) {}', output: 'function f(a:T,) {}', options: ['always'], errors: MISSING_I}, - {code: 'function f(a:T,) {}', output: 'function f(a:T) {}', options: ['always-multiline'], errors: UNEXPECTED_I}, - {code: 'function f(a:T,) {}', output: 'function f(a:T) {}', options: ['only-multiline'], errors: UNEXPECTED_I}, - {code: 'function f(a:T,) {}', output: 'function f(a:T) {}', options: ['never'], errors: UNEXPECTED_I}, - {code: 'function f(a:T,\n) {}', output: 'function f(a:T\n) {}', options: ['never'], errors: UNEXPECTED_I}, - {code: 'function f(a:T\n) {}', output: 'function f(a:T,\n) {}', options: ['always'], errors: MISSING_I}, - {code: 'function f(a:T\n) {}', output: 'function f(a:T,\n) {}', options: ['always-multiline'], errors: MISSING_I}, - - // FunctionExpression - {code: 'f = function f(a) {}', output: 'f = function f(a,) {}', options: ['always'], errors: MISSING_I}, - {code: 'f = function f(a,) {}', output: 'f = function f(a) {}', options: ['always-multiline'], errors: UNEXPECTED_I}, - {code: 'f = function f(a,) {}', output: 'f = function f(a) {}', options: ['only-multiline'], errors: UNEXPECTED_I}, - {code: 'f = function f(a,) {}', output: 'f = function f(a) {}', options: ['never'], errors: UNEXPECTED_I}, - {code: 'f = function f(a,\n) {}', output: 'f = function f(a\n) {}', options: ['never'], errors: UNEXPECTED_I}, - {code: 'f = function f(a\n) {}', output: 'f = function f(a,\n) {}', options: ['always'], errors: MISSING_I}, - {code: 'f = function f(a\n) {}', output: 'f = function f(a,\n) {}', options: ['always-multiline'], errors: MISSING_I}, - - {code: 'f = function f(a=1) {}', output: 'f = function f(a=1,) {}', options: ['always'], errors: MISSING_AP}, - {code: 'f = function f(a=1,) {}', output: 'f = function f(a=1) {}', options: ['always-multiline'], errors: UNEXPECTED_AP}, - {code: 'f = function f(a=1,) {}', output: 'f = function f(a=1) {}', options: ['only-multiline'], errors: UNEXPECTED_AP}, - {code: 'f = function f(a=1,) {}', output: 'f = function f(a=1) {}', options: ['never'], errors: UNEXPECTED_AP}, - {code: 'f = function f(a=1,\n) {}', output: 'f = function f(a=1\n) {}', options: ['never'], errors: UNEXPECTED_AP}, - {code: 'f = function f(a=1\n) {}', output: 'f = function f(a=1,\n) {}', options: ['always'], errors: MISSING_AP}, - {code: 'f = function f(a=1\n) {}', output: 'f = function f(a=1,\n) {}', options: ['always-multiline'], errors: MISSING_AP}, - - {code: 'f = function f(a:T) {}', output: 'f = function f(a:T,) {}', options: ['always'], errors: MISSING_I}, - {code: 'f = function f(a:T,) {}', output: 'f = function f(a:T) {}', options: ['always-multiline'], errors: UNEXPECTED_I}, - {code: 'f = function f(a:T,) {}', output: 'f = function f(a:T) {}', options: ['only-multiline'], errors: UNEXPECTED_I}, - {code: 'f = function f(a:T,) {}', output: 'f = function f(a:T) {}', options: ['never'], errors: UNEXPECTED_I}, - {code: 'f = function f(a:T,\n) {}', output: 'f = function f(a:T\n) {}', options: ['never'], errors: UNEXPECTED_I}, - {code: 'f = function f(a:T\n) {}', output: 'f = function f(a:T,\n) {}', options: ['always'], errors: MISSING_I}, - {code: 'f = function f(a:T\n) {}', output: 'f = function f(a:T,\n) {}', options: ['always-multiline'], errors: MISSING_I}, - - // ArrowFunctionExpression - {code: 'f = (a) => {}', output: 'f = (a,) => {}', options: ['always'], errors: MISSING_I}, - {code: 'f = (a,) => {}', output: 'f = (a) => {}', options: ['always-multiline'], errors: UNEXPECTED_I}, - {code: 'f = (a,) => {}', output: 'f = (a) => {}', options: ['only-multiline'], errors: UNEXPECTED_I}, - {code: 'f = (a,) => {}', output: 'f = (a) => {}', options: ['never'], errors: UNEXPECTED_I}, - {code: 'f = (a,\n) => {}', output: 'f = (a\n) => {}', options: ['never'], errors: UNEXPECTED_I}, - {code: 'f = (a\n) => {}', output: 'f = (a,\n) => {}', options: ['always'], errors: MISSING_I}, - {code: 'f = (a\n) => {}', output: 'f = (a,\n) => {}', options: ['always-multiline'], errors: MISSING_I}, - - {code: 'f = (a=1) => {}', output: 'f = (a=1,) => {}', options: ['always'], errors: MISSING_AP}, - {code: 'f = (a=1,) => {}', output: 'f = (a=1) => {}', options: ['always-multiline'], errors: UNEXPECTED_AP}, - {code: 'f = (a=1,) => {}', output: 'f = (a=1) => {}', options: ['only-multiline'], errors: UNEXPECTED_AP}, - {code: 'f = (a=1,) => {}', output: 'f = (a=1) => {}', options: ['never'], errors: UNEXPECTED_AP}, - {code: 'f = (a=1,\n) => {}', output: 'f = (a=1\n) => {}', options: ['never'], errors: UNEXPECTED_AP}, - {code: 'f = (a=1\n) => {}', output: 'f = (a=1,\n) => {}', options: ['always'], errors: MISSING_AP}, - {code: 'f = (a=1\n) => {}', output: 'f = (a=1,\n) => {}', options: ['always-multiline'], errors: MISSING_AP}, - - // Arrow functions with flow types aren't getting the correct loc. - // {code: 'f = (a:T) => {}', output: 'f = (a:T,) => {}', options: ['always'], errors: MISSING_I}, - // {code: 'f = (a:T,) => {}', output: 'f = (a:T) => {}', options: ['always-multiline'], errors: UNEXPECTED_I}, - // {code: 'f = (a:T,) => {}', output: 'f = (a:T) => {}', options: ['only-multiline'], errors: UNEXPECTED_I}, - // {code: 'f = (a:T,) => {}', output: 'f = (a:T) => {}', options: ['never'], errors: UNEXPECTED_I}, - // {code: 'f = (a:T,\n) => {}', output: 'f = (a:T\n) => {}', options: ['never'], errors: UNEXPECTED_I}, - // {code: 'f = (a:T\n) => {}', output: 'f = (a:T,\n) => {}', options: ['always'], errors: MISSING_I}, - // {code: 'f = (a:T\n) => {}', output: 'f = (a:T,\n) => {}', options: ['always-multiline'], errors: MISSING_I}, - - // CallExpression - {code: 'f(a)', output: 'f(a,)', options: ['always'], errors: MISSING_I}, - {code: 'f(a,)', output: 'f(a)', options: ['always-multiline'], errors: UNEXPECTED_I}, - {code: 'f(a,)', output: 'f(a)', options: ['only-multiline'], errors: UNEXPECTED_I}, - {code: 'f(a,)', output: 'f(a)', options: ['never'], errors: UNEXPECTED_I}, - {code: 'f(a,\n)', output: 'f(a\n)', options: ['never'], errors: UNEXPECTED_I}, - {code: 'f(a\n)', output: 'f(a,\n)', options: ['always'], errors: MISSING_I}, - {code: 'f(a\n)', output: 'f(a,\n)', options: ['always-multiline'], errors: MISSING_I}, - - {code: 'f(...a)', output: 'f(...a,)', options: ['always'], errors: MISSING_SE}, - {code: 'f(...a,)', output: 'f(...a)', options: ['always-multiline'], errors: UNEXPECTED_SE}, - {code: 'f(...a,)', output: 'f(...a)', options: ['only-multiline'], errors: UNEXPECTED_SE}, - {code: 'f(...a,)', output: 'f(...a)', options: ['never'], errors: UNEXPECTED_SE}, - {code: 'f(...a,\n)', output: 'f(...a\n)', options: ['never'], errors: UNEXPECTED_SE}, - {code: 'f(...a\n)', output: 'f(...a,\n)', options: ['always'], errors: MISSING_SE}, - {code: 'f(...a\n)', output: 'f(...a,\n)', options: ['always-multiline'], errors: MISSING_SE}, - - // NewExpression - {code: 'new F(a)', output: 'new F(a,)', options: ['always'], errors: MISSING_I}, - {code: 'new F(a,)', output: 'new F(a)', options: ['always-multiline'], errors: UNEXPECTED_I}, - {code: 'new F(a,)', output: 'new F(a)', options: ['only-multiline'], errors: UNEXPECTED_I}, - {code: 'new F(a,)', output: 'new F(a)', options: ['never'], errors: UNEXPECTED_I}, - {code: 'new F(a,\n)', output: 'new F(a\n)', options: ['never'], errors: UNEXPECTED_I}, - {code: 'new F(a\n)', output: 'new F(a,\n)', options: ['always'], errors: MISSING_I}, - {code: 'new F(a\n)', output: 'new F(a,\n)', options: ['always-multiline'], errors: MISSING_I}, - - {code: 'new F(...a)', output: 'new F(...a,)', options: ['always'], errors: MISSING_SE}, - {code: 'new F(...a,)', output: 'new F(...a)', options: ['always-multiline'], errors: UNEXPECTED_SE}, - {code: 'new F(...a,)', output: 'new F(...a)', options: ['only-multiline'], errors: UNEXPECTED_SE}, - {code: 'new F(...a,)', output: 'new F(...a)', options: ['never'], errors: UNEXPECTED_SE}, - {code: 'new F(...a,\n)', output: 'new F(...a\n)', options: ['never'], errors: UNEXPECTED_SE}, - {code: 'new F(...a\n)', output: 'new F(...a,\n)', options: ['always'], errors: MISSING_SE}, - {code: 'new F(...a\n)', output: 'new F(...a,\n)', options: ['always-multiline'], errors: MISSING_SE}, - ], -}); diff --git a/tests/rules/generator-star-spacing.js b/tests/rules/generator-star-spacing.js deleted file mode 100644 index 86438a9..0000000 --- a/tests/rules/generator-star-spacing.js +++ /dev/null @@ -1,1176 +0,0 @@ -/* eslint-disable */ -var rule = require('../../rules/generator-star-spacing'), - RuleTester = require('../RuleTester'); - -var features = { - generators: true -}; - -function ok(code, args){ - return { code: code, options: args, parser: 'babel-eslint', ecmaFeatures: features } -} - -function err(code, errors, args){ - var e = ok(code, args) - e.errors = errors - return e -} - -var ruleTester = new RuleTester(); -ruleTester.run('babel/generator-star-spacing', rule, { - valid: [ - // Default ("before") - { - code: "function foo(){}" - }, - { - code: "function *foo(){}", - ecmaFeatures: { generators: true } - }, - { - code: "function *foo(arg1, arg2){}", - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function *foo(){};", - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function *(){};", - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function * (){};", - ecmaFeatures: { generators: true } - }, - { - code: "var foo = { *foo(){} };", - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true } - }, - { - code: "var foo = {*foo(){} };", - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true } - }, - { - code: "class Foo { *foo(){} }", - ecmaFeatures: { classes: true, generators: true } - }, - { - code: "class Foo {*foo(){} }", - ecmaFeatures: { classes: true, generators: true } - }, - { - code: "class Foo { static *foo(){} }", - ecmaFeatures: { classes: true, generators: true } - }, - - // "before" - { - code: "function foo(){}", - options: ["before"] - }, - { - code: "function *foo(){}", - options: ["before"], - ecmaFeatures: { generators: true } - }, - { - code: "function *foo(arg1, arg2){}", - options: ["before"], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function *foo(){};", - options: ["before"], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function *(){};", - options: ["before"], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function * (){};", - options: ["before"], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = { *foo(){} };", - options: ["before"], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true } - }, - { - code: "var foo = {*foo(){} };", - options: ["before"], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true } - }, - { - code: "class Foo { *foo(){} }", - options: ["before"], - ecmaFeatures: { classes: true, generators: true } - }, - { - code: "class Foo {*foo(){} }", - options: ["before"], - ecmaFeatures: { classes: true, generators: true } - }, - { - code: "class Foo { static *foo(){} }", - options: ["before"], - ecmaFeatures: { classes: true, generators: true } - }, - - // "after" - { - code: "function foo(){}", - options: ["after"] - }, - { - code: "function* foo(){}", - options: ["after"], - ecmaFeatures: { generators: true } - }, - { - code: "function* foo(arg1, arg2){}", - options: ["after"], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function* foo(){};", - options: ["after"], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function* (){};", - options: ["after"], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function*(){};", - options: ["after"], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = {* foo(){} };", - options: ["after"], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true } - }, - { - code: "var foo = { * foo(){} };", - options: ["after"], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true } - }, - { - code: "class Foo {* foo(){} }", - options: ["after"], - ecmaFeatures: { classes: true, generators: true } - }, - { - code: "class Foo { * foo(){} }", - options: ["after"], - ecmaFeatures: { classes: true, generators: true } - }, - { - code: "class Foo { static* foo(){} }", - options: ["after"], - ecmaFeatures: { classes: true, generators: true } - }, - - // "both" - { - code: "function foo(){}", - options: ["both"] - }, - { - code: "function * foo(){}", - options: ["both"], - ecmaFeatures: { generators: true } - }, - { - code: "function * foo(arg1, arg2){}", - options: ["both"], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function * foo(){};", - options: ["both"], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function * (){};", - options: ["both"], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function *(){};", - options: ["both"], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = { * foo(){} };", - options: ["both"], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true } - }, - { - code: "var foo = {* foo(){} };", - options: ["both"], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true } - }, - { - code: "class Foo { * foo(){} }", - options: ["both"], - ecmaFeatures: { classes: true, generators: true } - }, - { - code: "class Foo {* foo(){} }", - options: ["both"], - ecmaFeatures: { classes: true, generators: true } - }, - { - code: "class Foo { static * foo(){} }", - options: ["both"], - ecmaFeatures: { classes: true, generators: true } - }, - - // "neither" - { - code: "function foo(){}", - options: ["neither"] - }, - { - code: "function*foo(){}", - options: ["neither"], - ecmaFeatures: { generators: true } - }, - { - code: "function*foo(arg1, arg2){}", - options: ["neither"], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function*foo(){};", - options: ["neither"], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function*(){};", - options: ["neither"], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function* (){};", - options: ["neither"], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = {*foo(){} };", - options: ["neither"], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true } - }, - { - code: "var foo = { *foo(){} };", - options: ["neither"], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true } - }, - { - code: "class Foo {*foo(){} }", - options: ["neither"], - ecmaFeatures: { classes: true, generators: true } - }, - { - code: "class Foo { *foo(){} }", - options: ["neither"], - ecmaFeatures: { classes: true, generators: true } - }, - { - code: "class Foo { static*foo(){} }", - options: ["neither"], - ecmaFeatures: { classes: true, generators: true } - }, - - // {"before": true, "after": false} - { - code: "function foo(){}", - options: [{"before": true, "after": false}] - }, - { - code: "function *foo(){}", - options: [{"before": true, "after": false}], - ecmaFeatures: { generators: true } - }, - { - code: "function *foo(arg1, arg2){}", - options: [{"before": true, "after": false}], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function *foo(){};", - options: [{"before": true, "after": false}], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function *(){};", - options: [{"before": true, "after": false}], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function * (){};", - options: [{"before": true, "after": false}], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = { *foo(){} };", - options: [{"before": true, "after": false}], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true } - }, - { - code: "var foo = {*foo(){} };", - options: [{"before": true, "after": false}], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true } - }, - { - code: "class Foo { *foo(){} }", - options: [{"before": true, "after": false}], - ecmaFeatures: { classes: true, generators: true } - }, - { - code: "class Foo {*foo(){} }", - options: [{"before": true, "after": false}], - ecmaFeatures: { classes: true, generators: true } - }, - { - code: "class Foo { static *foo(){} }", - options: [{"before": true, "after": false}], - ecmaFeatures: { classes: true, generators: true } - }, - - // {"before": false, "after": true} - { - code: "function foo(){}", - options: [{"before": false, "after": true}] - }, - { - code: "function* foo(){}", - options: [{"before": false, "after": true}], - ecmaFeatures: { generators: true } - }, - { - code: "function* foo(arg1, arg2){}", - options: [{"before": false, "after": true}], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function* foo(){};", - options: [{"before": false, "after": true}], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function* (){};", - options: [{"before": false, "after": true}], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function*(){};", - options: [{"before": false, "after": true}], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = {* foo(){} };", - options: [{"before": false, "after": true}], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true } - }, - { - code: "var foo = { * foo(){} };", - options: [{"before": false, "after": true}], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true } - }, - { - code: "class Foo {* foo(){} }", - options: [{"before": false, "after": true}], - ecmaFeatures: { classes: true, generators: true } - }, - { - code: "class Foo { * foo(){} }", - options: [{"before": false, "after": true}], - ecmaFeatures: { classes: true, generators: true } - }, - { - code: "class Foo { static* foo(){} }", - options: [{"before": false, "after": true}], - ecmaFeatures: { classes: true, generators: true } - }, - - // {"before": true, "after": true} - { - code: "function foo(){}", - options: [{"before": true, "after": true}] - }, - { - code: "function * foo(){}", - options: [{"before": true, "after": true}], - ecmaFeatures: { generators: true } - }, - { - code: "function * foo(arg1, arg2){}", - options: [{"before": true, "after": true}], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function * foo(){};", - options: [{"before": true, "after": true}], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function * (){};", - options: [{"before": true, "after": true}], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function *(){};", - options: [{"before": true, "after": true}], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = { * foo(){} };", - options: [{"before": true, "after": true}], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true } - }, - { - code: "var foo = {* foo(){} };", - options: [{"before": true, "after": true}], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true } - }, - { - code: "class Foo { * foo(){} }", - options: [{"before": true, "after": true}], - ecmaFeatures: { classes: true, generators: true } - }, - { - code: "class Foo {* foo(){} }", - options: [{"before": true, "after": true}], - ecmaFeatures: { classes: true, generators: true } - }, - { - code: "class Foo { static * foo(){} }", - options: [{"before": true, "after": true}], - ecmaFeatures: { classes: true, generators: true } - }, - - // {"before": false, "after": false} - { - code: "function foo(){}", - options: [{"before": false, "after": false}] - }, - { - code: "function*foo(){}", - options: [{"before": false, "after": false}], - ecmaFeatures: { generators: true } - }, - { - code: "function*foo(arg1, arg2){}", - options: [{"before": false, "after": false}], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function*foo(){};", - options: [{"before": false, "after": false}], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function*(){};", - options: [{"before": false, "after": false}], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = function* (){};", - options: [{"before": false, "after": false}], - ecmaFeatures: { generators: true } - }, - { - code: "var foo = {*foo(){} };", - options: [{"before": false, "after": false}], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true } - }, - { - code: "var foo = { *foo(){} };", - options: [{"before": false, "after": false}], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true } - }, - { - code: "class Foo {*foo(){} }", - options: [{"before": false, "after": false}], - ecmaFeatures: { classes: true, generators: true } - }, - { - code: "class Foo { *foo(){} }", - options: [{"before": false, "after": false}], - ecmaFeatures: { classes: true, generators: true } - }, - { - code: "class Foo { static*foo(){} }", - options: [{"before": false, "after": false}], - ecmaFeatures: { classes: true, generators: true } - }, - - ok('var test = async function(){}'), - ok('async function test(){}'), - ok('var test = async function *(){}'), - ok('async function *test(){}', ["before"]) , - ok('async function* test(){}', ["after"]), - ok('async function * test(){}', ["both"]), - ok('async function*test(){}', ["neither"]), - ], - - invalid: [ - // Default ("before") - { - code: "function*foo(){}", - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }] - }, - { - code: "function* foo(arg1, arg2){}", - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }, { - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - { - code: "var foo = function*foo(){};", - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }] - }, - { - code: "var foo = function* (){};", - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }] - }, - { - code: "var foo = {* foo(){} };", - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true }, - errors: [{ - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - { - code: "class Foo {* foo(){} }", - ecmaFeatures: { classes: true, generators: true }, - errors: [{ - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - { - code: "class Foo { static* foo(){} }", - ecmaFeatures: { classes: true, generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }, { - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - - // "before" - { - code: "function*foo(){}", - options: ["before"], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }] - }, - { - code: "function* foo(arg1, arg2){}", - options: ["before"], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }, { - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - { - code: "var foo = function*foo(){};", - options: ["before"], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }] - }, - { - code: "var foo = function* (){};", - options: ["before"], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }] - }, - { - code: "var foo = {* foo(){} };", - options: ["before"], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true }, - errors: [{ - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - { - code: "class Foo {* foo(){} }", - options: ["before"], - ecmaFeatures: { classes: true, generators: true }, - errors: [{ - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - - // "after" - { - code: "function*foo(){}", - options: ["after"], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space after *.", - type: "Punctuator" - }] - }, - { - code: "function *foo(arg1, arg2){}", - options: ["after"], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Unexpected space before *.", - type: "Punctuator" - }, { - message: "Missing space after *.", - type: "Punctuator" - }] - }, - { - code: "var foo = function *foo(){};", - options: ["after"], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Unexpected space before *.", - type: "Punctuator" - }, { - message: "Missing space after *.", - type: "Punctuator" - }] - }, - { - code: "var foo = function *(){};", - options: ["after"], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Unexpected space before *.", - type: "Punctuator" - }] - }, - { - code: "var foo = { *foo(){} };", - options: ["after"], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true }, - errors: [{ - message: "Missing space after *.", - type: "Punctuator" - }] - }, - { - code: "class Foo { *foo(){} }", - options: ["after"], - ecmaFeatures: { classes: true, generators: true }, - errors: [{ - message: "Missing space after *.", - type: "Punctuator" - }] - }, - { - code: "class Foo { static *foo(){} }", - options: ["after"], - ecmaFeatures: { classes: true, generators: true }, - errors: [{ - message: "Unexpected space before *.", - type: "Punctuator" - }, { - message: "Missing space after *.", - type: "Punctuator" - }] - }, - - // "both" - { - code: "function*foo(){}", - options: ["both"], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }, { - message: "Missing space after *.", - type: "Punctuator" - }] - }, - { - code: "function*foo(arg1, arg2){}", - options: ["both"], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }, { - message: "Missing space after *.", - type: "Punctuator" - }] - }, - { - code: "var foo = function*foo(){};", - options: ["both"], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }, { - message: "Missing space after *.", - type: "Punctuator" - }] - }, - { - code: "var foo = function*(){};", - options: ["both"], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }] - }, - { - code: "var foo = {*foo(){} };", - options: ["both"], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true }, - errors: [{ - message: "Missing space after *.", - type: "Punctuator" - }] - }, - { - code: "class Foo {*foo(){} }", - options: ["both"], - ecmaFeatures: { classes: true, generators: true }, - errors: [{ - message: "Missing space after *.", - type: "Punctuator" - }] - }, - { - code: "class Foo { static*foo(){} }", - options: ["both"], - ecmaFeatures: { classes: true, generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }, { - message: "Missing space after *.", - type: "Punctuator" - }] - }, - - // "neither" - { - code: "function * foo(){}", - options: ["neither"], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Unexpected space before *.", - type: "Punctuator" - }, { - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - { - code: "function * foo(arg1, arg2){}", - options: ["neither"], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Unexpected space before *.", - type: "Punctuator" - }, { - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - { - code: "var foo = function * foo(){};", - options: ["neither"], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Unexpected space before *.", - type: "Punctuator" - }, { - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - { - code: "var foo = function * (){};", - options: ["neither"], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Unexpected space before *.", - type: "Punctuator" - }] - }, - { - code: "var foo = { * foo(){} };", - options: ["neither"], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true }, - errors: [{ - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - { - code: "class Foo { * foo(){} }", - options: ["neither"], - ecmaFeatures: { classes: true, generators: true }, - errors: [{ - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - { - code: "class Foo { static * foo(){} }", - options: ["neither"], - ecmaFeatures: { classes: true, generators: true }, - errors: [{ - message: "Unexpected space before *.", - type: "Punctuator" - }, { - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - - // {"before": true, "after": false} - { - code: "function*foo(){}", - options: [{"before": true, "after": false}], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }] - }, - { - code: "function* foo(arg1, arg2){}", - options: [{"before": true, "after": false}], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }, { - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - { - code: "var foo = function*foo(){};", - options: [{"before": true, "after": false}], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }] - }, - { - code: "var foo = function* (){};", - options: [{"before": true, "after": false}], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }] - }, - { - code: "var foo = {* foo(){} };", - options: [{"before": true, "after": false}], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true }, - errors: [{ - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - { - code: "class Foo {* foo(){} }", - options: [{"before": true, "after": false}], - ecmaFeatures: { classes: true, generators: true }, - errors: [{ - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - - // {"before": false, "after": true} - { - code: "function*foo(){}", - options: [{"before": false, "after": true}], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space after *.", - type: "Punctuator" - }] - }, - { - code: "function *foo(arg1, arg2){}", - options: [{"before": false, "after": true}], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Unexpected space before *.", - type: "Punctuator" - }, { - message: "Missing space after *.", - type: "Punctuator" - }] - }, - { - code: "var foo = function *foo(){};", - options: [{"before": false, "after": true}], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Unexpected space before *.", - type: "Punctuator" - }, { - message: "Missing space after *.", - type: "Punctuator" - }] - }, - { - code: "var foo = function *(){};", - options: [{"before": false, "after": true}], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Unexpected space before *.", - type: "Punctuator" - }] - }, - { - code: "var foo = { *foo(){} };", - options: [{"before": false, "after": true}], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true }, - errors: [{ - message: "Missing space after *.", - type: "Punctuator" - }] - }, - { - code: "class Foo { *foo(){} }", - options: [{"before": false, "after": true}], - ecmaFeatures: { classes: true, generators: true }, - errors: [{ - message: "Missing space after *.", - type: "Punctuator" - }] - }, - { - code: "class Foo { static *foo(){} }", - options: [{"before": false, "after": true}], - ecmaFeatures: { classes: true, generators: true }, - errors: [{ - message: "Unexpected space before *.", - type: "Punctuator" - }, { - message: "Missing space after *.", - type: "Punctuator" - }] - }, - - // {"before": true, "after": true} - { - code: "function*foo(){}", - options: [{"before": true, "after": true}], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }, { - message: "Missing space after *.", - type: "Punctuator" - }] - }, - { - code: "function*foo(arg1, arg2){}", - options: [{"before": true, "after": true}], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }, { - message: "Missing space after *.", - type: "Punctuator" - }] - }, - { - code: "var foo = function*foo(){};", - options: [{"before": true, "after": true}], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }, { - message: "Missing space after *.", - type: "Punctuator" - }] - }, - { - code: "var foo = function*(){};", - options: [{"before": true, "after": true}], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }] - }, - { - code: "var foo = {*foo(){} };", - options: [{"before": true, "after": true}], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true }, - errors: [{ - message: "Missing space after *.", - type: "Punctuator" - }] - }, - { - code: "class Foo {*foo(){} }", - options: [{"before": true, "after": true}], - ecmaFeatures: { classes: true, generators: true }, - errors: [{ - message: "Missing space after *.", - type: "Punctuator" - }] - }, - { - code: "class Foo { static*foo(){} }", - options: [{"before": true, "after": true}], - ecmaFeatures: { classes: true, generators: true }, - errors: [{ - message: "Missing space before *.", - type: "Punctuator" - }, { - message: "Missing space after *.", - type: "Punctuator" - }] - }, - - // {"before": false, "after": false} - { - code: "function * foo(){}", - options: [{"before": false, "after": false}], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Unexpected space before *.", - type: "Punctuator" - }, { - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - { - code: "function * foo(arg1, arg2){}", - options: [{"before": false, "after": false}], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Unexpected space before *.", - type: "Punctuator" - }, { - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - { - code: "var foo = function * foo(){};", - options: [{"before": false, "after": false}], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Unexpected space before *.", - type: "Punctuator" - }, { - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - { - code: "var foo = function * (){};", - options: [{"before": false, "after": false}], - ecmaFeatures: { generators: true }, - errors: [{ - message: "Unexpected space before *.", - type: "Punctuator" - }] - }, - { - code: "var foo = { * foo(){} };", - options: [{"before": false, "after": false}], - ecmaFeatures: { generators: true, objectLiteralShorthandMethods: true }, - errors: [{ - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - { - code: "class Foo { * foo(){} }", - options: [{"before": false, "after": false}], - ecmaFeatures: { classes: true, generators: true }, - errors: [{ - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - { - code: "class Foo { static * foo(){} }", - options: [{"before": false, "after": false}], - ecmaFeatures: { classes: true, generators: true }, - errors: [{ - message: "Unexpected space before *.", - type: "Punctuator" - }, { - message: "Unexpected space after *.", - type: "Punctuator" - }] - }, - err('async function*test(){}', [ - { message: 'Missing space before *.' }, - ]), - err('async function* test(){}', [ - { - message: "Missing space before *.", - type: "Punctuator" - }, { - message: "Unexpected space after *.", - type: "Punctuator" - } - ]), - - ] -}); diff --git a/tests/rules/object-shorthand.js b/tests/rules/object-shorthand.js deleted file mode 100644 index c35c086..0000000 --- a/tests/rules/object-shorthand.js +++ /dev/null @@ -1,108 +0,0 @@ -/* eslint-disable */ -var rule = require('../../rules/object-shorthand'), - RuleTester = require('../RuleTester'); - -var features = { - objectLiteralShorthandMethods: true, - objectLiteralComputedProperties: true, - objectLiteralShorthandProperties: true, - arrowFunctions: true, - destructuring: true, - generators: true -}; - -function ok(code, args){ - return { code: code, parser: 'babel-eslint', ecmaFeatures: features} -} - - -var ruleTester = new RuleTester(); -ruleTester.run('babel/object-shorthand', rule, { - valid: [ - //original test cases - { code: "var x = {y() {}}", ecmaFeatures: features }, - { code: "var x = {y}", ecmaFeatures: features }, - { code: "var x = {a: b}", ecmaFeatures: features }, - { code: "var x = {a: 'a'}", ecmaFeatures: features }, - { code: "var x = {'a': 'a'}", ecmaFeatures: features }, - { code: "var x = {'a': b}", ecmaFeatures: features }, - { code: "var x = {y(x) {}}", ecmaFeatures: features }, - { code: "var {x,y,z} = x", ecmaFeatures: features }, - { code: "var {x: {y}} = z", ecmaFeatures: features }, - { code: "var x = {*x() {}}", ecmaFeatures: features }, - { code: "var x = {x: y}", ecmaFeatures: features }, - { code: "var x = {x: y, y: z}", ecmaFeatures: features}, - { code: "var x = {x: y, y: z, z: 'z'}", ecmaFeatures: features}, - { code: "var x = {x() {}, y: z, l(){}}", ecmaFeatures: features}, - { code: "var x = {x: y, y: z, a: b}", ecmaFeatures: features}, - { code: "var x = {x: y, y: z, 'a': b}", ecmaFeatures: features}, - { code: "var x = {x: y, y() {}, z: a}", ecmaFeatures: features}, - { code: "doSomething({x: y})", ecmaFeatures: features}, - { code: "doSomething({'x': y})", ecmaFeatures: features}, - { code: "doSomething({x: 'x'})", ecmaFeatures: features}, - { code: "doSomething({'x': 'x'})", ecmaFeatures: features}, - { code: "doSomething({y() {}})", ecmaFeatures: features}, - { code: "doSomething({x: y, y() {}})", ecmaFeatures: features}, - { code: "doSomething({y() {}, z: a})", ecmaFeatures: features}, - { code: "!{ a: function a(){} };", ecmaFeatures: features }, - - // arrows functions are still alright - { code: "var x = {y: (x)=>x}", ecmaFeatures: features }, - { code: "doSomething({y: (x)=>x})", ecmaFeatures: features }, - { code: "var x = {y: (x)=>x, y: a}", ecmaFeatures: features }, - { code: "doSomething({x, y: (x)=>x})", ecmaFeatures: features }, - - // getters and setters are ok - { code: "var x = {get y() {}}", ecmaFeatures: features }, - { code: "var x = {set y(z) {}}", ecmaFeatures: features }, - { code: "var x = {get y() {}, set y(z) {}}", ecmaFeatures: features }, - { code: "doSomething({get y() {}})", ecmaFeatures: features }, - { code: "doSomething({set y(z) {}})", ecmaFeatures: features }, - { code: "doSomething({get y() {}, set y(z) {}})", ecmaFeatures: features }, - - // object literal computed properties - { code: "var x = {[y]: y}", ecmaFeatures: features, options: ["properties"] }, - { code: "var x = {['y']: 'y'}", ecmaFeatures: features, options: ["properties"] }, - { code: "var x = {['y']: y}", ecmaFeatures: features, options: ["properties"] }, - - // options - { code: "var x = {y() {}}", ecmaFeatures: features, options: ["methods"] }, - { code: "var x = {x, y() {}, a:b}", ecmaFeatures: features, options: ["methods"] }, - { code: "var x = {y}", ecmaFeatures: features, options: ["properties"] }, - { code: "var x = {y: {b}}", ecmaFeatures: features, options: ["properties"] }, - { code: "var x = {a: n, c: d, f: g}", ecmaFeatures: features, options: ["never"] }, - { code: "var x = {a: function(){}, b: {c: d}}", ecmaFeatures: features, options: ["never"] }, - - // Babel test cases. - ok('let { ...spread } = obj'), - ok('let { ...spread } = obj', [2, 'never']), - ], - - invalid: [ - { code: "var x = {x: x}", ecmaFeatures: features, errors: [{ message: "Expected property shorthand.", type: "Property" }] }, - { code: "var x = {'x': x}", ecmaFeatures: features, errors: [{ message: "Expected property shorthand.", type: "Property" }] }, - { code: "var x = {y: y, x: x}", ecmaFeatures: features, errors: [{ message: "Expected property shorthand.", type: "Property" }, { message: "Expected property shorthand.", type: "Property" }] }, - { code: "var x = {y: z, x: x, a: b}", ecmaFeatures: features, errors: [{ message: "Expected property shorthand.", type: "Property" }] }, - { code: "var x = {y: function() {}}", ecmaFeatures: features, errors: [{ message: "Expected method shorthand.", type: "Property" }] }, - { code: "var x = {y: function*() {}}", ecmaFeatures: features, errors: [{ message: "Expected method shorthand.", type: "Property" }] }, - { code: "var x = {x: y, y: z, a: a}", ecmaFeatures: features, errors: [{ message: "Expected property shorthand.", type: "Property" }] }, - { code: "var x = {x: y, y: z, a: function(){}, b() {}}", ecmaFeatures: features, errors: [{ message: "Expected method shorthand.", type: "Property" }] }, - { code: "var x = {x: x, y: function() {}}", ecmaFeatures: features, errors: [{ message: "Expected property shorthand.", type: "Property" }, { message: "Expected method shorthand.", type: "Property" }]}, - { code: "doSomething({x: x})", ecmaFeatures: features, errors: [{ message: "Expected property shorthand.", type: "Property" }] }, - { code: "doSomething({'x': x})", ecmaFeatures: features, errors: [{ message: "Expected property shorthand.", type: "Property" }] }, - { code: "doSomething({a: 'a', 'x': x})", ecmaFeatures: features, errors: [{ message: "Expected property shorthand.", type: "Property" }] }, - { code: "doSomething({y: function() {}})", ecmaFeatures: features, errors: [{ message: "Expected method shorthand.", type: "Property" }] }, - - // options - { code: "var x = {y: function() {}}", ecmaFeatures: features, errors: [{ message: "Expected method shorthand.", type: "Property" }], options: ["methods"] }, - { code: "var x = {x, y() {}, z: function() {}}", ecmaFeatures: features, errors: [{ message: "Expected method shorthand.", type: "Property" }], options: ["methods"] }, - { code: "var x = {x: x}", ecmaFeatures: features, errors: [{ message: "Expected property shorthand.", type: "Property" }], options: ["properties"] }, - { code: "var x = {a, b, c(){}, x: x}", ecmaFeatures: features, errors: [{ message: "Expected property shorthand.", type: "Property" }], options: ["properties"] }, - { code: "var x = {y() {}}", ecmaFeatures: features, errors: [{ message: "Expected longform method syntax.", type: "Property" }], options: ["never"] }, - { code: "var x = {*y() {}}", ecmaFeatures: features, errors: [{ message: "Expected longform method syntax.", type: "Property" }], options: ["never"] }, - { code: "var x = {y}", ecmaFeatures: features, errors: [{ message: "Expected longform property syntax.", type: "Property" }], options: ["never"]}, - { code: "var x = {y, a: b, *x(){}}", ecmaFeatures: features, errors: [{ message: "Expected longform property syntax.", type: "Property" }, { message: "Expected longform method syntax.", type: "Property" }], options: ["never"]}, - { code: "var x = {y: {x}}", ecmaFeatures: features, errors: [{ message: "Expected longform property syntax.", type: "Property" }], options: ["never"]} - - ] -}); From 614a3dc0abc24807debf277850c3b951edb5c548 Mon Sep 17 00:00:00 2001 From: Henry Zhu Date: Thu, 17 Nov 2016 15:48:47 -0500 Subject: [PATCH 2/3] fix update to babel-eslint 7 with awaitexpression --- rules/no-await-in-loop.js | 74 ++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 39 deletions(-) diff --git a/rules/no-await-in-loop.js b/rules/no-await-in-loop.js index cc3bb91..037f29b 100644 --- a/rules/no-await-in-loop.js +++ b/rules/no-await-in-loop.js @@ -23,48 +23,44 @@ var boundaryTypes = { module.exports = function(context) { return { - // babel-eslint transpiles AwaitExpressions to YieldExpressions, but the actual node kind is - // still available in _babelType. - YieldExpression: function(node) { - if (node._babelType === 'AwaitExpression') { - var ancestors = context.getAncestors(); - // Reverse so that we can traverse from the deepest node upwards. - ancestors.reverse(); - // Create a set of all the ancestors plus this node so that we can check - // if this use of await appears in the body of the loop as opposed to - // the right-hand side of a for...of, for example. - // - // Implement the set with an Array since there are likely to be very few - // elements. An Object would not be appropriate since the elements are - // not strings. - var ancestorSet = [].concat(ancestors, [node]); - var ancestorSetHas = function(element) { - return ancestorSet.indexOf(element) !== -1; + AwaitExpression(node) { + var ancestors = context.getAncestors(); + // Reverse so that we can traverse from the deepest node upwards. + ancestors.reverse(); + // Create a set of all the ancestors plus this node so that we can check + // if this use of await appears in the body of the loop as opposed to + // the right-hand side of a for...of, for example. + // + // Implement the set with an Array since there are likely to be very few + // elements. An Object would not be appropriate since the elements are + // not strings. + var ancestorSet = [].concat(ancestors, [node]); + var ancestorSetHas = function(element) { + return ancestorSet.indexOf(element) !== -1; + } + for (var i = 0; i < ancestors.length; i++) { + var ancestor = ancestors[i]; + if (boundaryTypes.hasOwnProperty(ancestor.type)) { + // Short-circuit out if we encounter a boundary type. Loops above + // this do not matter. + return; } - for (var i = 0; i < ancestors.length; i++) { - var ancestor = ancestors[i]; - if (boundaryTypes.hasOwnProperty(ancestor.type)) { - // Short-circuit out if we encounter a boundary type. Loops above - // this do not matter. + if (loopTypes.hasOwnProperty(ancestor.type)) { + // Only report if we are actually in the body or another part that gets executed on + // every iteration. + if ( + ancestorSetHas(ancestor.body) || + ancestorSetHas(ancestor.test) || + ancestorSetHas(ancestor.update) + ) { + context.report( + node, + 'Avoid using await inside a loop. Consider refactoring to use Promise.all. If ' + + 'you are sure you want to do this, add `// eslint-disable-line ' + + context.id + '` at the end of this line.' + ); return; } - if (loopTypes.hasOwnProperty(ancestor.type)) { - // Only report if we are actually in the body or another part that gets executed on - // every iteration. - if ( - ancestorSetHas(ancestor.body) || - ancestorSetHas(ancestor.test) || - ancestorSetHas(ancestor.update) - ) { - context.report( - node, - 'Avoid using await inside a loop. Consider refactoring to use Promise.all. If ' + - 'you are sure you want to do this, add `// eslint-disable-line ' + - context.id + '` at the end of this line.' - ); - return; - } - } } } }, From 5b0160d1f9276f6bd1519498030fd39a12ced1cb Mon Sep 17 00:00:00 2001 From: Henry Zhu Date: Thu, 17 Nov 2016 16:09:35 -0500 Subject: [PATCH 3/3] deprecate flow-object-type --- README.md | 15 +++---- rules/flow-object-type.js | 57 +++++++-------------------- tests/rules/flow-object-type.js | 69 --------------------------------- 3 files changed, 21 insertions(+), 120 deletions(-) delete mode 100644 tests/rules/flow-object-type.js diff --git a/README.md b/README.md index e837566..5a84afb 100644 --- a/README.md +++ b/README.md @@ -48,15 +48,12 @@ The following rules are not in `eslint`, but are relevant only to syntax that is the current JavaScript standard or supported by `eslint`. - `babel/no-await-in-loop`: guard against awaiting async functions inside of a loop -- `babel/flow-object-type`: Require a particular separator between properties in Flow object types. (🛠 ) - - Use the option `semicolon` to require semicolons (e.g. `type Foo = { bar: number; baz: string }`). - - Use the option `comma` to require commas (e.g. `type Foo = { bar: number, baz: string }`). - #### Deprecated -- `babel/generator-star-spacing`: Handles async/await functions correctly -- `babel/object-shorthand`: doesn't fail when using object spread (`...obj`) -- `babel/arrow-parens`: Handles async functions correctly (🛠 ) -- `babel/func-params-comma-dangle`: Require or forbid trailing commas for function paramater lists. Behaves like, and takes the same options as, `eslint`'s [`comma-dangle`](http://eslint.org/docs/rules/comma-dangle). (🛠 ) -- `babel/array-bracket-spacing`: Handles destructuring arrays with flow type in function parameters (🛠 ) +- `babel/generator-star-spacing`: Use [`generator-star-spacing`](http://eslint.org/docs/rules/generator-star-spacing). +- `babel/object-shorthand`: Use [`object-shorthand`](http://eslint.org/docs/rules/object-shorthand). +- `babel/arrow-parens`: Use [`arrow-parens`](http://eslint.org/docs/rules/arrow-parens). +- `babel/func-params-comma-dangle`: Use [`comma-dangle`](http://eslint.org/docs/rules/comma-dangle). +- `babel/array-bracket-spacing`: Use [`array-bracket-spacing`](http://eslint.org/docs/rules/array-bracket-spacing). +- `babel/flow-object-type`: Use [`flowtype/object-type-delimiter`](https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype-rules-object-type-delimiter). diff --git a/rules/flow-object-type.js b/rules/flow-object-type.js index f452832..530efb8 100644 --- a/rules/flow-object-type.js +++ b/rules/flow-object-type.js @@ -1,48 +1,21 @@ -/** - * @fileoverview Enforces a choice between semicolons and commas in Flow object and class types. - * @author Nat Mote - */ "use strict"; -var SEMICOLON = { - char: ';', - name: 'semicolon', -} +var isWarnedForDeprecation = false; +module.exports = function() { + return { + Program() { + if (isWarnedForDeprecation || /\=-(f|-format)=/.test(process.argv.join('='))) { + return; + } -var COMMA = { - char: ',', - name: 'comma', -}; - -module.exports = function(context) { - var GOOD; - var BAD; - if (context.options[0] === undefined || context.options[0] === SEMICOLON.name) { - GOOD = SEMICOLON; - BAD = COMMA; - } else { - GOOD = COMMA; - BAD = SEMICOLON; - } - function requireProperPunctuation(node) { - var tokens = context.getSourceCode().getTokens(node); - var lastToken = tokens[tokens.length - 1]; - if (lastToken.type === 'Punctuator') { - if (lastToken.value === BAD.char) { - context.report({ - message: 'Prefer ' + GOOD.name + 's to ' + BAD.name + 's in object and class types', - node: lastToken, - fix: function(fixer) { - return fixer.replaceText(lastToken, GOOD.char); - }, - }); - } - } - } - - return { - ObjectTypeProperty: requireProperPunctuation, - }; + /* eslint-disable no-console */ + console.log('The babel/flow-object-type rule is deprecated. Please ' + + 'use the flowtype/object-type-delimiter rule instead.\n' + + 'Check out https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype-rules-object-type-delimiter'); + /* eslint-enable no-console */ + isWarnedForDeprecation = true; + } + }; }; module.exports.schema = [ diff --git a/tests/rules/flow-object-type.js b/tests/rules/flow-object-type.js deleted file mode 100644 index 9d34036..0000000 --- a/tests/rules/flow-object-type.js +++ /dev/null @@ -1,69 +0,0 @@ -/** - * @fileoverview Tests for flow-object-type. - * @author Nat Mote - */ - -"use strict"; - -var rule = require("../../rules/flow-object-type"), - RuleTester = require('../RuleTester'); - -var features = { -}; - -function test(code, options, errors, output){ - var result = { - code: code, - parser: 'babel-eslint', - ecmaFeatures: features, - }; - if (options != null) { - result.options = options; - } - if (errors != null) { - result.errors = errors; - } - if (output != null) { - result.output = output; - } - return result; -} - -var commaMessage = 'Prefer commas to semicolons in object and class types'; -var semiMessage = 'Prefer semicolons to commas in object and class types'; - -function ok(code, commaOrSemi) { - return test(code, [commaOrSemi]); -} - -function err(code, commaOrSemi, errorMessage, output) { - return test(code, [commaOrSemi], [errorMessage], output); -} - -var cases = [ - ok('type Foo = { a: Foo; b: Bar }', 'semicolon'), - err('type Foo = { a: Foo, b: Bar }', 'semicolon', semiMessage, 'type Foo = { a: Foo; b: Bar }'), - - ok('type Foo = { a: Foo, b: Bar }', 'comma'), - err('type Foo = { a: Foo; b: Bar }', 'comma', commaMessage, 'type Foo = { a: Foo, b: Bar }'), - - ok('declare class Foo { a: Foo; }', 'semicolon'), - err('declare class Foo { a: Foo, }', 'semicolon', semiMessage, 'declare class Foo { a: Foo; }'), - - ok('declare class Foo { a: Foo, }', 'comma'), - err('declare class Foo { a: Foo; }', 'comma', commaMessage, 'declare class Foo { a: Foo, }'), -]; - -function hasError(testCase) { - return testCase.errors != null && testCase.errors.length > 0; -} - -function hasNoError(testCase) { - return !hasError(testCase); -} - -var ruleTester = new RuleTester(); -ruleTester.run('flow-object-type', rule, { - valid: cases.filter(hasNoError), - invalid: cases.filter(hasError), -});