From d1eddb29631df7496f2bc521fc32454dab5f1c47 Mon Sep 17 00:00:00 2001 From: Ethan Rutherford Date: Fri, 21 Jul 2017 22:48:59 -0700 Subject: [PATCH] Update: Add configurability to generator star spacing --- docs/rules/generator-star-spacing.md | 48 ++- lib/rules/generator-star-spacing.js | 132 +++++++-- tests/lib/rules/generator-star-spacing.js | 341 +++++++++++++--------- 3 files changed, 360 insertions(+), 161 deletions(-) diff --git a/docs/rules/generator-star-spacing.md b/docs/rules/generator-star-spacing.md index 91fa1213bbd..6a19e05827d 100644 --- a/docs/rules/generator-star-spacing.md +++ b/docs/rules/generator-star-spacing.md @@ -75,6 +75,25 @@ An example of shorthand configuration: "generator-star-spacing": ["error", "after"] ``` +Additionally, this rule allows further configurability via overrides per function type + +* `named` provides overrides for named functions +* `anonymous` provides overrides for anonymous functions +* `method` provides overrides for class methods or property function shorthand +* `static` provides overrides for static class methods + +An example of a configuration with overrides: + +```json +"generator-star-spacing": ["error", { + "before": false, + "after": true, + "anonymous": "neither", + "method": "neither", + "static": {"before": true, "after": true} +}] +``` + ## Examples ### before @@ -85,7 +104,7 @@ Examples of **correct** code for this rule with the `"before"` option: /*eslint generator-star-spacing: ["error", {"before": true, "after": false}]*/ /*eslint-env es6*/ -function *generator() {} +function *generator() {}; var anonymous = function *() {}; @@ -100,7 +119,7 @@ Examples of **correct** code for this rule with the `"after"` option: /*eslint generator-star-spacing: ["error", {"before": false, "after": true}]*/ /*eslint-env es6*/ -function* generator() {} +function* generator() {}; var anonymous = function* () {}; @@ -115,7 +134,7 @@ Examples of **correct** code for this rule with the `"both"` option: /*eslint generator-star-spacing: ["error", {"before": true, "after": true}]*/ /*eslint-env es6*/ -function * generator() {} +function * generator() {}; var anonymous = function * () {}; @@ -130,13 +149,34 @@ Examples of **correct** code for this rule with the `"neither"` option: /*eslint generator-star-spacing: ["error", {"before": false, "after": false}]*/ /*eslint-env es6*/ -function*generator() {} +function*generator() {}; var anonymous = function*() {}; var shorthand = { *generator() {} }; ``` +Examples of **correct** code for this rule with overrides present + +```js +/*eslint generator-star-spacing: ["error", { + "before": false, + "after": true, + "anonymous": "neither", + "method": "neither", + "static": {"before": true, "after": true} +}]*/ +/*eslint-env es6*/ + +function* generator() {}; + +var anonymous = function*() {}; + +var shorthand = { *generator() {} }; + +class Class { static * method() {} }; +``` + ## When Not To Use It If your project will not be using generators or you are not concerned with spacing consistency, you do not need this rule. diff --git a/lib/rules/generator-star-spacing.js b/lib/rules/generator-star-spacing.js index 9836e4ead97..51271821b34 100644 --- a/lib/rules/generator-star-spacing.js +++ b/lib/rules/generator-star-spacing.js @@ -29,7 +29,67 @@ module.exports = { type: "object", properties: { before: { type: "boolean" }, - after: { type: "boolean" } + after: { type: "boolean" }, + named: { + oneOf: [ + { + enum: ["before", "after", "both", "neither"] + }, + { + type: "object", + properties: { + before: { type: "boolean" }, + after: { type: "boolean" } + }, + additionalProperties: false + } + ] + }, + anonymous: { + oneOf: [ + { + enum: ["before", "after", "both", "neither"] + }, + { + type: "object", + properties: { + before: { type: "boolean" }, + after: { type: "boolean" } + }, + additionalProperties: false + } + ] + }, + method: { + oneOf: [ + { + enum: ["before", "after", "both", "neither"] + }, + { + type: "object", + properties: { + before: { type: "boolean" }, + after: { type: "boolean" } + }, + additionalProperties: false + } + ] + }, + static: { + oneOf: [ + { + enum: ["before", "after", "both", "neither"] + }, + { + type: "object", + properties: { + before: { type: "boolean" }, + after: { type: "boolean" } + }, + additionalProperties: false + } + ] + } }, additionalProperties: false } @@ -40,16 +100,41 @@ module.exports = { create(context) { - const mode = (function(option) { - if (!option || 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"]; + const optionDefinitions = { + before: { before: true, after: false }, + after: { before: false, after: true }, + both: { before: true, after: true }, + neither: { before: false, after: false } + }; + + /** + * Returns resolved option definitions based on an option and defaults + * + * @param {any} option - The option object or string value + * @param {Object} defaults - The defaults to use if options are not present + * @returns {Object} the resolved object definition + */ + function optionToDefinition(option, defaults) { + if (!option) { + return defaults; } - return option; + + return typeof option === "string" ? optionDefinitions[option] : { + before: "before" in option ? option.before : defaults.before, + after: "after" in option ? option.after : defaults.after + }; + } + + const modes = (function(option) { + option = option || {}; + const defaults = optionToDefinition(option, optionDefinitions.before); + + return { + named: optionToDefinition(option.named, defaults), + anonymous: optionToDefinition(option.anonymous, defaults), + method: optionToDefinition(option.method, defaults), + static: optionToDefinition(option.static, defaults) + }; }(context.options[0])); const sourceCode = context.getSourceCode(); @@ -79,6 +164,8 @@ module.exports = { /** * Checks the spacing between two tokens before or after the star token. + * + * @param {string} kind Either "named", "anonymous", "method", or "static" * @param {string} side Either "before" or "after". * @param {Token} leftToken `function` keyword token if side is "before", or * star token if side is "after". @@ -86,10 +173,10 @@ module.exports = { * token if side is "after". * @returns {void} */ - function checkSpacing(side, leftToken, rightToken) { - if (!!(rightToken.range[0] - leftToken.range[1]) !== mode[side]) { + function checkSpacing(kind, side, leftToken, rightToken) { + if (!!(rightToken.range[0] - leftToken.range[1]) !== modes[kind][side]) { const after = leftToken.value === "*"; - const spaceRequired = mode[side]; + const spaceRequired = modes[kind][side]; const node = after ? leftToken : rightToken; const type = spaceRequired ? "Missing" : "Unexpected"; const message = "{{type}} space {{side}} *."; @@ -117,6 +204,7 @@ module.exports = { /** * Enforces the spacing around the star if node is a generator function. + * * @param {ASTNode} node A function expression or declaration node. * @returns {void} */ @@ -126,17 +214,23 @@ module.exports = { } const starToken = getStarToken(node); - - // Only check before when preceded by `function`|`static` keyword const prevToken = sourceCode.getTokenBefore(starToken); + const nextToken = sourceCode.getTokenAfter(starToken); - if (prevToken.value === "function" || prevToken.value === "static") { - checkSpacing("before", prevToken, starToken); + let kind = "named"; + + if (node.type === "FunctionExpression" && (node.parent.type === "MethodDefinition" || node.parent.type === "Property")) { + kind = prevToken.value === "static" ? "static" : "method"; + } else if (nextToken.value === "(") { + kind = "anonymous"; } - const nextToken = sourceCode.getTokenAfter(starToken); + // Only check before when preceded by `function`|`static` keyword + if (kind !== "method") { + checkSpacing(kind, "before", prevToken, starToken); + } - checkSpacing("after", starToken, nextToken); + checkSpacing(kind, "after", starToken, nextToken); } return { diff --git a/tests/lib/rules/generator-star-spacing.js b/tests/lib/rules/generator-star-spacing.js index 34075485eab..584f7b51944 100644 --- a/tests/lib/rules/generator-star-spacing.js +++ b/tests/lib/rules/generator-star-spacing.js @@ -23,30 +23,30 @@ ruleTester.run("generator-star-spacing", rule, { valid: [ // Default ("before") - "function foo(){}", - "function *foo(){}", - "function *foo(arg1, arg2){}", + "function foo(){};", + "function *foo(){};", + "function *foo(arg1, arg2){};", "var foo = function *foo(){};", "var foo = function *(){};", "var foo = { *foo(){} };", "var foo = {*foo(){} };", - "class Foo { *foo(){} }", - "class Foo {*foo(){} }", - "class Foo { static *foo(){} }", + "class Foo { *foo(){} };", + "class Foo {*foo(){} };", + "class Foo { static *foo(){} };", "var foo = {*[ foo ](){} };", - "class Foo {*[ foo ](){} }", + "class Foo {*[ foo ](){} };", // "before" { - code: "function foo(){}", + code: "function foo(){};", options: ["before"] }, { - code: "function *foo(){}", + code: "function *foo(){};", options: ["before"] }, { - code: "function *foo(arg1, arg2){}", + code: "function *foo(arg1, arg2){};", options: ["before"] }, { @@ -66,19 +66,19 @@ ruleTester.run("generator-star-spacing", rule, { options: ["before"] }, { - code: "class Foo { *foo(){} }", + code: "class Foo { *foo(){} };", options: ["before"] }, { - code: "class Foo {*foo(){} }", + code: "class Foo {*foo(){} };", options: ["before"] }, { - code: "class Foo { static *foo(){} }", + code: "class Foo { static *foo(){} };", options: ["before"] }, { - code: "class Foo {*[ foo ](){} }", + code: "class Foo {*[ foo ](){} };", options: ["before"] }, { @@ -88,15 +88,15 @@ ruleTester.run("generator-star-spacing", rule, { // "after" { - code: "function foo(){}", + code: "function foo(){};", options: ["after"] }, { - code: "function* foo(){}", + code: "function* foo(){};", options: ["after"] }, { - code: "function* foo(arg1, arg2){}", + code: "function* foo(arg1, arg2){};", options: ["after"] }, { @@ -116,15 +116,15 @@ ruleTester.run("generator-star-spacing", rule, { options: ["after"] }, { - code: "class Foo {* foo(){} }", + code: "class Foo {* foo(){} };", options: ["after"] }, { - code: "class Foo { * foo(){} }", + code: "class Foo { * foo(){} };", options: ["after"] }, { - code: "class Foo { static* foo(){} }", + code: "class Foo { static* foo(){} };", options: ["after"] }, { @@ -132,21 +132,21 @@ ruleTester.run("generator-star-spacing", rule, { options: ["after"] }, { - code: "class Foo {* [foo](){} }", + code: "class Foo {* [foo](){} };", options: ["after"] }, // "both" { - code: "function foo(){}", + code: "function foo(){};", options: ["both"] }, { - code: "function * foo(){}", + code: "function * foo(){};", options: ["both"] }, { - code: "function * foo(arg1, arg2){}", + code: "function * foo(arg1, arg2){};", options: ["both"] }, { @@ -166,15 +166,15 @@ ruleTester.run("generator-star-spacing", rule, { options: ["both"] }, { - code: "class Foo { * foo(){} }", + code: "class Foo { * foo(){} };", options: ["both"] }, { - code: "class Foo {* foo(){} }", + code: "class Foo {* foo(){} };", options: ["both"] }, { - code: "class Foo { static * foo(){} }", + code: "class Foo { static * foo(){} };", options: ["both"] }, { @@ -182,21 +182,21 @@ ruleTester.run("generator-star-spacing", rule, { options: ["both"] }, { - code: "class Foo {* [foo](){} }", + code: "class Foo {* [foo](){} };", options: ["both"] }, // "neither" { - code: "function foo(){}", + code: "function foo(){};", options: ["neither"] }, { - code: "function*foo(){}", + code: "function*foo(){};", options: ["neither"] }, { - code: "function*foo(arg1, arg2){}", + code: "function*foo(arg1, arg2){};", options: ["neither"] }, { @@ -216,15 +216,15 @@ ruleTester.run("generator-star-spacing", rule, { options: ["neither"] }, { - code: "class Foo {*foo(){} }", + code: "class Foo {*foo(){} };", options: ["neither"] }, { - code: "class Foo { *foo(){} }", + code: "class Foo { *foo(){} };", options: ["neither"] }, { - code: "class Foo { static*foo(){} }", + code: "class Foo { static*foo(){} };", options: ["neither"] }, { @@ -232,21 +232,21 @@ ruleTester.run("generator-star-spacing", rule, { options: ["neither"] }, { - code: "class Foo {*[ foo ](){} }", + code: "class Foo {*[ foo ](){} };", options: ["neither"] }, // {"before": true, "after": false} { - code: "function foo(){}", + code: "function foo(){};", options: [{ before: true, after: false }] }, { - code: "function *foo(){}", + code: "function *foo(){};", options: [{ before: true, after: false }] }, { - code: "function *foo(arg1, arg2){}", + code: "function *foo(arg1, arg2){};", options: [{ before: true, after: false }] }, { @@ -266,29 +266,29 @@ ruleTester.run("generator-star-spacing", rule, { options: [{ before: true, after: false }] }, { - code: "class Foo { *foo(){} }", + code: "class Foo { *foo(){} };", options: [{ before: true, after: false }] }, { - code: "class Foo {*foo(){} }", + code: "class Foo {*foo(){} };", options: [{ before: true, after: false }] }, { - code: "class Foo { static *foo(){} }", + code: "class Foo { static *foo(){} };", options: [{ before: true, after: false }] }, // {"before": false, "after": true} { - code: "function foo(){}", + code: "function foo(){};", options: [{ before: false, after: true }] }, { - code: "function* foo(){}", + code: "function* foo(){};", options: [{ before: false, after: true }] }, { - code: "function* foo(arg1, arg2){}", + code: "function* foo(arg1, arg2){};", options: [{ before: false, after: true }] }, { @@ -308,29 +308,29 @@ ruleTester.run("generator-star-spacing", rule, { options: [{ before: false, after: true }] }, { - code: "class Foo {* foo(){} }", + code: "class Foo {* foo(){} };", options: [{ before: false, after: true }] }, { - code: "class Foo { * foo(){} }", + code: "class Foo { * foo(){} };", options: [{ before: false, after: true }] }, { - code: "class Foo { static* foo(){} }", + code: "class Foo { static* foo(){} };", options: [{ before: false, after: true }] }, // {"before": true, "after": true} { - code: "function foo(){}", + code: "function foo(){};", options: [{ before: true, after: true }] }, { - code: "function * foo(){}", + code: "function * foo(){};", options: [{ before: true, after: true }] }, { - code: "function * foo(arg1, arg2){}", + code: "function * foo(arg1, arg2){};", options: [{ before: true, after: true }] }, { @@ -350,29 +350,29 @@ ruleTester.run("generator-star-spacing", rule, { options: [{ before: true, after: true }] }, { - code: "class Foo { * foo(){} }", + code: "class Foo { * foo(){} };", options: [{ before: true, after: true }] }, { - code: "class Foo {* foo(){} }", + code: "class Foo {* foo(){} };", options: [{ before: true, after: true }] }, { - code: "class Foo { static * foo(){} }", + code: "class Foo { static * foo(){} };", options: [{ before: true, after: true }] }, // {"before": false, "after": false} { - code: "function foo(){}", + code: "function foo(){};", options: [{ before: false, after: false }] }, { - code: "function*foo(){}", + code: "function*foo(){};", options: [{ before: false, after: false }] }, { - code: "function*foo(arg1, arg2){}", + code: "function*foo(arg1, arg2){};", options: [{ before: false, after: false }] }, { @@ -392,21 +392,39 @@ ruleTester.run("generator-star-spacing", rule, { options: [{ before: false, after: false }] }, { - code: "class Foo {*foo(){} }", + code: "class Foo {*foo(){} };", options: [{ before: false, after: false }] }, { - code: "class Foo { *foo(){} }", + code: "class Foo { *foo(){} };", options: [{ before: false, after: false }] }, { - code: "class Foo { static*foo(){} }", + code: "class Foo { static*foo(){} };", options: [{ before: false, after: false }] }, + // full configurability + { + code: "function * foo(){};", + options: [{ before: false, after: false, named: "both" }] + }, + { + code: "var foo = function * (){};", + options: [{ before: false, after: false, anonymous: "both" }] + }, + { + code: "class Foo { * foo(){} };", + options: [{ before: false, after: false, method: "both" }] + }, + { + code: "class Foo { static * foo(){} };", + options: [{ before: false, after: false, static: "both" }] + }, + // https://github.com/eslint/eslint/issues/7101#issuecomment-246080531 { - code: "async function foo() { }", + code: "async function foo() { };", parserOptions: { ecmaVersion: 8 } }, { @@ -414,7 +432,7 @@ ruleTester.run("generator-star-spacing", rule, { parserOptions: { ecmaVersion: 8 } }, { - code: "async () => { }", + code: "async () => { };", parserOptions: { ecmaVersion: 8 } }, { @@ -422,7 +440,7 @@ ruleTester.run("generator-star-spacing", rule, { parserOptions: { ecmaVersion: 8 } }, { - code: "class A {async foo() { }}", + code: "class A {async foo() { }};", parserOptions: { ecmaVersion: 8 } }, { @@ -435,16 +453,16 @@ ruleTester.run("generator-star-spacing", rule, { // Default ("before") { - code: "function*foo(){}", - output: "function *foo(){}", + code: "function*foo(){};", + output: "function *foo(){};", errors: [{ message: "Missing space before *.", type: "Punctuator" }] }, { - code: "function* foo(arg1, arg2){}", - output: "function *foo(arg1, arg2){}", + code: "function* foo(arg1, arg2){};", + output: "function *foo(arg1, arg2){};", errors: [{ message: "Missing space before *.", type: "Punctuator" @@ -481,16 +499,16 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "class Foo {* foo(){} }", - output: "class Foo {*foo(){} }", + code: "class Foo {* foo(){} };", + output: "class Foo {*foo(){} };", errors: [{ message: "Unexpected space after *.", type: "Punctuator" }] }, { - code: "class Foo { static* foo(){} }", - output: "class Foo { static *foo(){} }", + code: "class Foo { static* foo(){} };", + output: "class Foo { static *foo(){} };", errors: [{ message: "Missing space before *.", type: "Punctuator" @@ -502,8 +520,8 @@ ruleTester.run("generator-star-spacing", rule, { // "before" { - code: "function*foo(){}", - output: "function *foo(){}", + code: "function*foo(){};", + output: "function *foo(){};", options: ["before"], errors: [{ message: "Missing space before *.", @@ -511,8 +529,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "function* foo(arg1, arg2){}", - output: "function *foo(arg1, arg2){}", + code: "function* foo(arg1, arg2){};", + output: "function *foo(arg1, arg2){};", options: ["before"], errors: [{ message: "Missing space before *.", @@ -553,8 +571,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "class Foo {* foo(){} }", - output: "class Foo {*foo(){} }", + code: "class Foo {* foo(){} };", + output: "class Foo {*foo(){} };", options: ["before"], errors: [{ message: "Unexpected space after *.", @@ -571,8 +589,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "class Foo {* [ foo ](){} }", - output: "class Foo {*[ foo ](){} }", + code: "class Foo {* [ foo ](){} };", + output: "class Foo {*[ foo ](){} };", options: ["before"], errors: [{ message: "Unexpected space after *.", @@ -582,8 +600,8 @@ ruleTester.run("generator-star-spacing", rule, { // "after" { - code: "function*foo(){}", - output: "function* foo(){}", + code: "function*foo(){};", + output: "function* foo(){};", options: ["after"], errors: [{ message: "Missing space after *.", @@ -591,8 +609,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "function *foo(arg1, arg2){}", - output: "function* foo(arg1, arg2){}", + code: "function *foo(arg1, arg2){};", + output: "function* foo(arg1, arg2){};", options: ["after"], errors: [{ message: "Unexpected space before *.", @@ -636,8 +654,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "class Foo { *foo(){} }", - output: "class Foo { * foo(){} }", + code: "class Foo { *foo(){} };", + output: "class Foo { * foo(){} };", options: ["after"], errors: [{ message: "Missing space after *.", @@ -645,8 +663,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "class Foo { static *foo(){} }", - output: "class Foo { static* foo(){} }", + code: "class Foo { static *foo(){} };", + output: "class Foo { static* foo(){} };", options: ["after"], errors: [{ message: "Unexpected space before *.", @@ -666,8 +684,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "class Foo { *[foo](){} }", - output: "class Foo { * [foo](){} }", + code: "class Foo { *[foo](){} };", + output: "class Foo { * [foo](){} };", options: ["after"], errors: [{ message: "Missing space after *.", @@ -677,8 +695,8 @@ ruleTester.run("generator-star-spacing", rule, { // "both" { - code: "function*foo(){}", - output: "function * foo(){}", + code: "function*foo(){};", + output: "function * foo(){};", options: ["both"], errors: [{ message: "Missing space before *.", @@ -689,8 +707,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "function*foo(arg1, arg2){}", - output: "function * foo(arg1, arg2){}", + code: "function*foo(arg1, arg2){};", + output: "function * foo(arg1, arg2){};", options: ["both"], errors: [{ message: "Missing space before *.", @@ -734,8 +752,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "class Foo {*foo(){} }", - output: "class Foo {* foo(){} }", + code: "class Foo {*foo(){} };", + output: "class Foo {* foo(){} };", options: ["both"], errors: [{ message: "Missing space after *.", @@ -743,8 +761,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "class Foo { static*foo(){} }", - output: "class Foo { static * foo(){} }", + code: "class Foo { static*foo(){} };", + output: "class Foo { static * foo(){} };", options: ["both"], errors: [{ message: "Missing space before *.", @@ -764,8 +782,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "class Foo {*[foo](){} }", - output: "class Foo {* [foo](){} }", + code: "class Foo {*[foo](){} };", + output: "class Foo {* [foo](){} };", options: ["both"], errors: [{ message: "Missing space after *.", @@ -775,8 +793,8 @@ ruleTester.run("generator-star-spacing", rule, { // "neither" { - code: "function * foo(){}", - output: "function*foo(){}", + code: "function * foo(){};", + output: "function*foo(){};", options: ["neither"], errors: [{ message: "Unexpected space before *.", @@ -787,8 +805,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "function * foo(arg1, arg2){}", - output: "function*foo(arg1, arg2){}", + code: "function * foo(arg1, arg2){};", + output: "function*foo(arg1, arg2){};", options: ["neither"], errors: [{ message: "Unexpected space before *.", @@ -832,8 +850,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "class Foo { * foo(){} }", - output: "class Foo { *foo(){} }", + code: "class Foo { * foo(){} };", + output: "class Foo { *foo(){} };", options: ["neither"], errors: [{ message: "Unexpected space after *.", @@ -841,8 +859,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "class Foo { static * foo(){} }", - output: "class Foo { static*foo(){} }", + code: "class Foo { static * foo(){} };", + output: "class Foo { static*foo(){} };", options: ["neither"], errors: [{ message: "Unexpected space before *.", @@ -862,8 +880,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "class Foo { * [ foo ](){} }", - output: "class Foo { *[ foo ](){} }", + code: "class Foo { * [ foo ](){} };", + output: "class Foo { *[ foo ](){} };", options: ["neither"], errors: [{ message: "Unexpected space after *.", @@ -873,8 +891,8 @@ ruleTester.run("generator-star-spacing", rule, { // {"before": true, "after": false} { - code: "function*foo(){}", - output: "function *foo(){}", + code: "function*foo(){};", + output: "function *foo(){};", options: [{ before: true, after: false }], errors: [{ message: "Missing space before *.", @@ -882,8 +900,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "function* foo(arg1, arg2){}", - output: "function *foo(arg1, arg2){}", + code: "function* foo(arg1, arg2){};", + output: "function *foo(arg1, arg2){};", options: [{ before: true, after: false }], errors: [{ message: "Missing space before *.", @@ -924,8 +942,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "class Foo {* foo(){} }", - output: "class Foo {*foo(){} }", + code: "class Foo {* foo(){} };", + output: "class Foo {*foo(){} };", options: [{ before: true, after: false }], errors: [{ message: "Unexpected space after *.", @@ -935,8 +953,8 @@ ruleTester.run("generator-star-spacing", rule, { // {"before": false, "after": true} { - code: "function*foo(){}", - output: "function* foo(){}", + code: "function*foo(){};", + output: "function* foo(){};", options: [{ before: false, after: true }], errors: [{ message: "Missing space after *.", @@ -944,8 +962,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "function *foo(arg1, arg2){}", - output: "function* foo(arg1, arg2){}", + code: "function *foo(arg1, arg2){};", + output: "function* foo(arg1, arg2){};", options: [{ before: false, after: true }], errors: [{ message: "Unexpected space before *.", @@ -989,8 +1007,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "class Foo { *foo(){} }", - output: "class Foo { * foo(){} }", + code: "class Foo { *foo(){} };", + output: "class Foo { * foo(){} };", options: [{ before: false, after: true }], errors: [{ message: "Missing space after *.", @@ -998,8 +1016,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "class Foo { static *foo(){} }", - output: "class Foo { static* foo(){} }", + code: "class Foo { static *foo(){} };", + output: "class Foo { static* foo(){} };", options: [{ before: false, after: true }], errors: [{ message: "Unexpected space before *.", @@ -1012,8 +1030,8 @@ ruleTester.run("generator-star-spacing", rule, { // {"before": true, "after": true} { - code: "function*foo(){}", - output: "function * foo(){}", + code: "function*foo(){};", + output: "function * foo(){};", options: [{ before: true, after: true }], errors: [{ message: "Missing space before *.", @@ -1024,8 +1042,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "function*foo(arg1, arg2){}", - output: "function * foo(arg1, arg2){}", + code: "function*foo(arg1, arg2){};", + output: "function * foo(arg1, arg2){};", options: [{ before: true, after: true }], errors: [{ message: "Missing space before *.", @@ -1069,8 +1087,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "class Foo {*foo(){} }", - output: "class Foo {* foo(){} }", + code: "class Foo {*foo(){} };", + output: "class Foo {* foo(){} };", options: [{ before: true, after: true }], errors: [{ message: "Missing space after *.", @@ -1078,8 +1096,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "class Foo { static*foo(){} }", - output: "class Foo { static * foo(){} }", + code: "class Foo { static*foo(){} };", + output: "class Foo { static * foo(){} };", options: [{ before: true, after: true }], errors: [{ message: "Missing space before *.", @@ -1092,8 +1110,8 @@ ruleTester.run("generator-star-spacing", rule, { // {"before": false, "after": false} { - code: "function * foo(){}", - output: "function*foo(){}", + code: "function * foo(){};", + output: "function*foo(){};", options: [{ before: false, after: false }], errors: [{ message: "Unexpected space before *.", @@ -1104,8 +1122,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "function * foo(arg1, arg2){}", - output: "function*foo(arg1, arg2){}", + code: "function * foo(arg1, arg2){};", + output: "function*foo(arg1, arg2){};", options: [{ before: false, after: false }], errors: [{ message: "Unexpected space before *.", @@ -1149,8 +1167,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "class Foo { * foo(){} }", - output: "class Foo { *foo(){} }", + code: "class Foo { * foo(){} };", + output: "class Foo { *foo(){} };", options: [{ before: false, after: false }], errors: [{ message: "Unexpected space after *.", @@ -1158,8 +1176,8 @@ ruleTester.run("generator-star-spacing", rule, { }] }, { - code: "class Foo { static * foo(){} }", - output: "class Foo { static*foo(){} }", + code: "class Foo { static * foo(){} };", + output: "class Foo { static*foo(){} };", options: [{ before: false, after: false }], errors: [{ message: "Unexpected space before *.", @@ -1168,6 +1186,53 @@ ruleTester.run("generator-star-spacing", rule, { message: "Unexpected space after *.", type: "Punctuator" }] + }, + + // full configurability + { + code: "function*foo(){};", + output: "function * foo(){};", + options: [{ before: false, after: false, named: "both" }], + errors: [{ + message: "Missing space before *.", + type: "Punctuator" + }, { + message: "Missing space after *.", + type: "Punctuator" + }] + }, + { + code: "var foo = function*(){};", + output: "var foo = function * (){};", + options: [{ before: false, after: false, anonymous: "both" }], + errors: [{ + message: "Missing space before *.", + type: "Punctuator" + }, { + message: "Missing space after *.", + type: "Punctuator" + }] + }, + { + code: "class Foo { *foo(){} };", + output: "class Foo { * foo(){} };", + options: [{ before: false, after: false, method: "both" }], + errors: [{ + message: "Missing space after *.", + type: "Punctuator" + }] + }, + { + code: "class Foo { static*foo(){} };", + output: "class Foo { static * foo(){} };", + options: [{ before: false, after: false, static: "both" }], + errors: [{ + message: "Missing space before *.", + type: "Punctuator" + }, { + message: "Missing space after *.", + type: "Punctuator" + }] } ]