Skip to content

Commit fe95fc6

Browse files
committed
(func-style) Support generator
1 parent 7b1654e commit fe95fc6

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

lib/rules/func-style.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ module.exports = {
9393
result.defStart = node.range[0];
9494
result.defEnd = node.body.range[0];
9595
result.async = node.async || false;
96+
result.generator = node.generator || false;
9697
result.body = node.body;
9798
result.returnType = node.returnType ? sourceCode.getText(node.returnType).replace(PREFIX_COLON, "") : null;
9899
result.typeParameters = node.typeParameters && node.typeParameters.params && node.typeParameters.params.length ? node.typeParameters.params.map(t => sourceCode.getText(t)).join(", ") : null;
@@ -103,6 +104,7 @@ module.exports = {
103104
result.defStart = parent.range[0];
104105
result.defEnd = node.init.body.range[0];
105106
result.async = node.init.async || false;
107+
result.generator = node.init.generator || false;
106108
result.typeAnnotation = node.id.typeAnnotation ? sourceCode.getText(node.id.typeAnnotation).replace(PREFIX_COLON, "") : null;
107109
result.returnType = node.init.returnType ? sourceCode.getText(node.init.returnType).replace(PREFIX_COLON, "") : null;
108110
result.typeParameters = node.init.typeParameters && node.init.typeParameters.params && node.init.typeParameters.params.length ? node.init.typeParameters.params.map(t => sourceCode.getText(t)).join(", ") : null;
@@ -121,17 +123,19 @@ module.exports = {
121123
/**
122124
* Generate modified text from analysis data
123125
* @param {Object} data analysis data
124-
* @param {string} to convert to
126+
* @param {string} config convert to
125127
* @returns {string} fixed text
126128
*/
127-
function generateFixedText(data, to) {
129+
function generateFixedText(data, config) {
130+
const to = data.generator && config !== "declaration" ? "expression" : config;
131+
128132
switch (to) {
129133
case "declaration":
130-
return `${data.async ? "async " : ""}function${data.name ? ` ${data.name}` : ""}${data.typeParameters ? `<${data.typeParameters}>` : ""}(${data.params ? data.params : ""})${data.returnType ? `: ${data.returnType}` : ""} ${data.body.type !== "BlockStatement" ? `{return ${data.bodyText}}` : ""}`;
134+
return `${data.async ? "async " : ""}function${data.generator ? "* " : ""}${data.name ? ` ${data.name}` : ""}${data.typeParameters ? `<${data.typeParameters}>` : ""}(${data.params ? data.params : ""})${data.returnType ? `: ${data.returnType}` : ""} ${data.body.type !== "BlockStatement" ? `{return ${data.bodyText}}` : ""}`;
131135
case "expression":
132-
return `${data.kind} ${data.name}${data.typeAnnotation ? `: ${data.typeAnnotation}` : ""} = ${data.async ? "async " : ""}function${data.typeParameters ? `<${data.typeParameters}${IS_GENERIC_PARSEABLE.test(data.typeParameters) ? "" : ","}>` : ""}(${data.params ? data.params : ""})${data.returnType ? `: ${data.returnType}` : ""} ${data.body.type !== "BlockStatement" ? `{return ${data.bodyText}}` : ""}`;
136+
return `${data.kind} ${data.name}${data.typeAnnotation ? `: ${data.typeAnnotation}` : ""} = ${data.async ? "async " : ""}function${data.generator ? "* " : ""}${data.typeParameters ? `<${data.typeParameters}${IS_GENERIC_PARSEABLE.test(data.typeParameters) ? "" : ","}>` : ""}(${data.params ? data.params : ""})${data.returnType ? `: ${data.returnType}` : ""} ${data.body.type !== "BlockStatement" ? `{return ${data.bodyText}}` : ""}`;
133137
case "arrow":
134-
return `${data.kind} ${data.name}${data.typeAnnotation ? `: ${data.typeAnnotation}` : ""} = ${data.async ? "async " : ""}${data.typeParameters ? `<${data.typeParameters}${IS_GENERIC_PARSEABLE.test(data.typeParameters) ? "" : ","}>` : ""}(${data.params ? data.params : ""})${data.returnType ? `: ${data.returnType}` : ""} => ${data.body.type !== "BlockStatement" ? data.bodyText : ""}`;
138+
return `${data.kind} ${data.name}${data.typeAnnotation ? `: ${data.typeAnnotation}` : ""} = ${data.async ? "async " : ""}${data.generator ? "* " : ""}${data.typeParameters ? `<${data.typeParameters}${IS_GENERIC_PARSEABLE.test(data.typeParameters) ? "" : ","}>` : ""}(${data.params ? data.params : ""})${data.returnType ? `: ${data.returnType}` : ""} => ${data.body.type !== "BlockStatement" ? data.bodyText : ""}`;
135139
default:
136140
return null;
137141
}
@@ -179,12 +183,13 @@ module.exports = {
179183
FunctionExpression(node) {
180184
stack.push(false);
181185
const isTopLevel = stack.length === 1;
186+
const isGenerator = node.generator || false;
182187

183-
if (!isTopLevel && !enforceExpressions && node.parent.type === "VariableDeclarator") {
188+
if (!isTopLevel && !enforceExpressions && node.parent.type === "VariableDeclarator" && (!enforceArrows || !isGenerator)) {
184189
report({ node: node.parent, messageId: style, isTopLevel });
185190
}
186191

187-
if (isTopLevel && !enforceExpressionTopLevel && node.parent.type === "VariableDeclarator") {
192+
if (isTopLevel && !enforceExpressionTopLevel && node.parent.type === "VariableDeclarator" && (!enforceArrowTopLevel || !isGenerator)) {
188193

189194
report({ node: node.parent, messageId: `${topLevelStyle}-in-top-level`, isTopLevel });
190195
}

tests/lib/rules/func-style.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ new RuleTester({
1616
}
1717
}).run("func-style", rule, {
1818
valid: [
19-
2019
{
2120
code: "function foo() {}",
2221
options: ["declaration"]
@@ -52,6 +51,10 @@ new RuleTester({
5251
{
5352
code: "export default function foo() {const bar = () => {};}",
5453
options: ["arrow", { topLevelStyle: "arrow", allowExportDefaultFunctionDeclaration: true }]
54+
},
55+
{
56+
code: "var foo = function* () {}",
57+
options: ["arrow"]
5558
}
5659
],
5760
invalid: [
@@ -121,6 +124,18 @@ new RuleTester({
121124
output: "var foo = function<T,>(bar:T): void {}",
122125
options: ["expression"],
123126
errors: [{ message: messages["expression-in-top-level"] }]
127+
},
128+
{
129+
code: "function* foo() {}",
130+
output: "var foo = function* () {}",
131+
options: ["expression"],
132+
errors: [{ message: messages["expression-in-top-level"] }]
133+
},
134+
{
135+
code: "function* foo() {}",
136+
output: "var foo = function* () {}",
137+
options: ["arrow"],
138+
errors: [{ message: messages["arrow-in-top-level"] }]
124139
}
125140
]
126141
});

0 commit comments

Comments
 (0)