eslint-plugin-storybook
Advanced tools
Comparing version 0.0.1-alpha.5 to 0.0.1-alpha.6
@@ -6,3 +6,2 @@ "use strict"; | ||
*/ | ||
const utils_1 = require("../utils"); | ||
const create_storybook_rule_1 = require("../utils/create-storybook-rule"); | ||
@@ -25,2 +24,3 @@ const constants_1 = require("../utils/constants"); | ||
type: 'problem', | ||
fixable: 'code', | ||
schema: [], | ||
@@ -43,40 +43,17 @@ }, | ||
]; | ||
const getNonAwaitedCallExpressions = (body = []) => { | ||
return (body | ||
.filter((b) => { | ||
//@ts-ignore | ||
return (0, ast_1.isExpressionStatement)(b) && (0, ast_1.isCallExpression)(b.expression); | ||
}) | ||
//@ts-ignore | ||
.map((d) => d.expression)); | ||
}; | ||
const getNonAwaitedInitializations = (body = []) => { | ||
const initializations = body | ||
//@ts-ignore | ||
.flatMap((b) => { | ||
return ((0, ast_1.isVariableDeclaration)(b) && | ||
b.declarations | ||
.filter((d) => (0, ast_1.isVariableDeclarator)(d) && !(0, ast_1.isAwaitExpression)(d.init)) | ||
.map((d) => d.init)); | ||
}) | ||
.filter(Boolean); | ||
return initializations; | ||
}; | ||
//@ts-ignore | ||
const getMethodThatShouldBeAwaited = (expression) => { | ||
const getMethodThatShouldBeAwaited = (expr) => { | ||
const shouldAwait = (name) => { | ||
//@ts-ignore | ||
return FUNCTIONS_TO_BE_AWAITED.includes(name) || name.startsWith('findBy'); | ||
}; | ||
if ((0, ast_1.isCallExpression)(expression) && | ||
(0, ast_1.isMemberExpression)(expression.callee) && | ||
(0, ast_1.isIdentifier)(expression.callee.object) && | ||
shouldAwait(expression.callee.object.name)) { | ||
return expression.callee.object; | ||
if ((0, ast_1.isCallExpression)(expr) && | ||
(0, ast_1.isMemberExpression)(expr.callee) && | ||
(0, ast_1.isIdentifier)(expr.callee.object) && | ||
shouldAwait(expr.callee.object.name)) { | ||
return expr.callee.object; | ||
} | ||
if ((0, ast_1.isCallExpression)(expression) && | ||
(0, ast_1.isMemberExpression)(expression.callee) && | ||
(0, ast_1.isIdentifier)(expression.callee.property) && | ||
shouldAwait(expression.callee.property.name)) { | ||
return expression.callee.property; | ||
if ((0, ast_1.isCallExpression)(expr) && | ||
(0, ast_1.isMemberExpression)(expr.callee) && | ||
(0, ast_1.isIdentifier)(expr.callee.property) && | ||
shouldAwait(expr.callee.property.name)) { | ||
return expr.callee.property; | ||
} | ||
@@ -93,25 +70,11 @@ return null; | ||
return { | ||
AssignmentExpression(node) { | ||
if (!(0, ast_1.isExpressionStatement)(node.parent)) { | ||
return null; | ||
CallExpression(node) { | ||
const method = getMethodThatShouldBeAwaited(node); | ||
if (method && !(0, ast_1.isAwaitExpression)(node.parent)) { | ||
invocationsThatShouldBeAwaited.push({ node, method }); | ||
} | ||
if ((0, utils_1.isPlayFunction)(node)) { | ||
const { right } = node; | ||
const expressionBody = (right.body && right.body.body) || []; | ||
const callExpressions = [ | ||
...getNonAwaitedCallExpressions(expressionBody), | ||
...getNonAwaitedInitializations(expressionBody), | ||
]; | ||
callExpressions.forEach((expression) => { | ||
const method = getMethodThatShouldBeAwaited(expression); | ||
if (method) { | ||
invocationsThatShouldBeAwaited.push(method); | ||
} | ||
}); | ||
} | ||
}, | ||
'Program:exit': function () { | ||
if (invocationsThatShouldBeAwaited.length) { | ||
//@ts-ignore | ||
invocationsThatShouldBeAwaited.forEach((node) => { | ||
invocationsThatShouldBeAwaited.forEach(({ node, method }) => { | ||
context.report({ | ||
@@ -121,18 +84,15 @@ node, | ||
data: { | ||
method: node.name, | ||
method: method.name, | ||
}, | ||
// @TODO: make this auto-fixable. Currently it's pretty dumb so something like this can happen: | ||
// canvas.findByText => canvas.await findByText | ||
// instead of the correct: await canvas.findByText | ||
// fix: function (fixer) { | ||
// return fixer.insertTextBefore(node, 'await ') | ||
// }, | ||
// suggest: [ | ||
// { | ||
// messageId: 'fixSuggestion', | ||
// fix: function (fixer) { | ||
// return fixer.insertTextBefore(node, 'await ') | ||
// }, | ||
// }, | ||
// ], | ||
suggest: [ | ||
{ | ||
messageId: 'fixSuggestion', | ||
fix: function (fixer) { | ||
return fixer.insertTextBefore(node, 'await '); | ||
}, | ||
}, | ||
], | ||
}); | ||
@@ -139,0 +99,0 @@ }); |
@@ -6,3 +6,2 @@ "use strict"; | ||
*/ | ||
const utils_1 = require("../utils"); | ||
const constants_1 = require("../utils/constants"); | ||
@@ -49,2 +48,14 @@ const ast_1 = require("../utils/ast"); | ||
}; | ||
const checkExpectInvocations = (blockStatement) => { | ||
if (!(0, ast_1.isBlockStatement)(blockStatement)) { | ||
return; | ||
} | ||
const expressionBody = blockStatement.body || []; | ||
const expressionStatements = getExpressionStatements(expressionBody); | ||
expressionStatements.forEach(({ expression }) => { | ||
if (isExpect(expression)) { | ||
expectInvocations.push(expression); | ||
} | ||
}); | ||
}; | ||
//---------------------------------------------------------------------- | ||
@@ -61,15 +72,8 @@ // Public | ||
}, | ||
AssignmentExpression(node) { | ||
if (!(0, ast_1.isExpressionStatement)(node.parent)) { | ||
CallExpression(node) { | ||
if (!(0, ast_1.isIdentifier)(node.callee)) { | ||
return null; | ||
} | ||
if ((0, utils_1.isPlayFunction)(node)) { | ||
const { right } = node; | ||
const expressionBody = (right.body && right.body.body) || []; | ||
const expressionStatements = getExpressionStatements(expressionBody); | ||
expressionStatements.forEach(({ expression }) => { | ||
if (isExpect(expression)) { | ||
expectInvocations.push(expression); | ||
} | ||
}); | ||
if (node.callee.name === 'expect') { | ||
expectInvocations.push(node.callee); | ||
} | ||
@@ -76,0 +80,0 @@ }, |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isFunctionExpression = exports.isReturnStatement = exports.isProperty = exports.isObjectPattern = exports.isObjectExpression = exports.isNewExpression = exports.isMemberExpression = exports.isLiteral = exports.isJSXAttribute = exports.isImportSpecifier = exports.isImportNamespaceSpecifier = exports.isImportDefaultSpecifier = exports.isImportDeclaration = exports.isSequenceExpression = exports.isAssignmentExpression = exports.isVariableDeclaration = exports.isExpressionStatement = exports.isCallExpression = exports.isBlockStatement = exports.isArrowFunctionExpression = exports.isArrayExpression = exports.isVariableDeclarator = exports.isIdentifier = exports.isAwaitExpression = exports.ASTUtils = void 0; | ||
exports.isProgram = exports.isFunctionExpression = exports.isReturnStatement = exports.isProperty = exports.isObjectPattern = exports.isObjectExpression = exports.isNewExpression = exports.isMemberExpression = exports.isLiteral = exports.isJSXAttribute = exports.isImportSpecifier = exports.isImportNamespaceSpecifier = exports.isImportDefaultSpecifier = exports.isImportDeclaration = exports.isSequenceExpression = exports.isAssignmentExpression = exports.isVariableDeclaration = exports.isExpressionStatement = exports.isCallExpression = exports.isBlockStatement = exports.isArrowFunctionExpression = exports.isArrayExpression = exports.isVariableDeclarator = exports.isIdentifier = exports.isAwaitExpression = exports.ASTUtils = void 0; | ||
const experimental_utils_1 = require("@typescript-eslint/experimental-utils"); | ||
@@ -32,1 +32,2 @@ var experimental_utils_2 = require("@typescript-eslint/experimental-utils"); | ||
exports.isFunctionExpression = isNodeOfType(experimental_utils_1.AST_NODE_TYPES.FunctionExpression); | ||
exports.isProgram = isNodeOfType(experimental_utils_1.AST_NODE_TYPES.Program); |
{ | ||
"name": "eslint-plugin-storybook", | ||
"version": "0.0.1-alpha.5", | ||
"version": "0.0.1-alpha.6", | ||
"description": "Best practice rules for Storybook", | ||
@@ -37,3 +37,4 @@ "keywords": [ | ||
"start": "tsc --watch", | ||
"test": "mocha -r ts-node/register 'tests/**/*.ts'", | ||
"test": "jest", | ||
"test:ci": "jest --ci", | ||
"generate-rule": "ts-node ./tools/generate-rule", | ||
@@ -54,3 +55,3 @@ "update-configs": "ts-node ./tools/update-configs", | ||
"@types/eslint": "^7.28.2", | ||
"@types/mocha": "^9.0.0", | ||
"@types/jest": "^27.0.2", | ||
"@types/node": "^16.11.6", | ||
@@ -65,4 +66,4 @@ "@typescript-eslint/parser": "^5.3.0", | ||
"husky": ">=6", | ||
"jest": "^27.3.1", | ||
"lint-staged": ">=10", | ||
"mocha": "^9.0.0", | ||
"prettier": "^2.4.0", | ||
@@ -72,2 +73,3 @@ "prompts": "^2.4.2", | ||
"ts-dedent": "^2.2.0", | ||
"ts-jest": "^27.0.7", | ||
"ts-migrate": "^0.1.26", | ||
@@ -74,0 +76,0 @@ "ts-node": "^10.4.0", |
50797
23
972