eslint-plugin-vest
Advanced tools
Comparing version 0.1.1 to 0.1.2
const constants = { | ||
VEST_HOOK_WARN: "warn", | ||
VEST_HOOK_ONLY: "only", | ||
VEST_HOOK_SKIP: "skip", | ||
VEST_HOOK_DRAFT: "draft", | ||
VEST_IDENTIFIER_VALIDATE: "validate", | ||
VEST_IDENTIFIER_TEST: "test", | ||
VEST_KEYWORD: "vest", | ||
VEST_HOOK_DRAFT: 'draft', | ||
VEST_HOOK_ONLY: 'only', | ||
VEST_HOOK_SKIP: 'skip', | ||
VEST_HOOK_WARN: 'warn', | ||
VEST_IDENTIFIER_TEST: 'test', | ||
VEST_IDENTIFIER_VALIDATE: 'validate', | ||
VEST_KEYWORD: 'vest', | ||
}; | ||
@@ -10,0 +10,0 @@ |
module.exports = { | ||
closest: require("./closest"), | ||
looksLike: require("./looksLike"), | ||
closest: require('./closest'), | ||
looksLike: require('./looksLike'), | ||
}; |
function isPrimitive(val) { | ||
return val === null || ["string", "boolean", "number"].includes(typeof val); | ||
return val === null || ['string', 'boolean', 'number'].includes(typeof val); | ||
} | ||
@@ -16,5 +16,5 @@ | ||
if (typeof bVal === "function") { | ||
if (typeof bVal === 'function') { | ||
return ( | ||
typeof aVal === "function" && bVal.toString() === aVal.toString() | ||
typeof aVal === 'function' && bVal.toString() === aVal.toString() | ||
); | ||
@@ -21,0 +21,0 @@ } |
@@ -5,7 +5,7 @@ /** | ||
*/ | ||
"use strict"; | ||
'use strict'; | ||
module.exports.rules = { | ||
"hook-scope": require("./rules/hook-scope"), | ||
"exclude-before-test": require("./rules/exclude-before-test"), | ||
'hook-scope': require('./rules/hook-scope'), | ||
'exclude-before-test': require('./rules/exclude-before-test'), | ||
}; |
@@ -5,23 +5,23 @@ const { | ||
VEST_KEYWORD, | ||
} = require("../../constants"); | ||
const { looksLike } = require("../../helpers"); | ||
} = require('../../constants'); | ||
const { looksLike } = require('../../helpers'); | ||
const isTestCall = (node) => | ||
const isTestCall = node => | ||
looksLike(node, { | ||
parent: { type: "CallExpression" }, | ||
parent: { type: 'CallExpression' }, | ||
name: VEST_IDENTIFIER_TEST, | ||
}); | ||
const looksLikeExclusion = (node) => { | ||
if (node.type !== "MemberExpression") { | ||
const looksLikeExclusion = node => { | ||
if (node.type !== 'MemberExpression') { | ||
return false; | ||
} | ||
return VEST_EXCLUSIVE_HOOKS.map((hookName) => ({ | ||
object: { name: VEST_KEYWORD, type: "Identifier" }, | ||
property: { name: hookName, type: "Identifier" }, | ||
})).some((shape) => looksLike(node, shape)); | ||
return VEST_EXCLUSIVE_HOOKS.map(hookName => ({ | ||
object: { name: VEST_KEYWORD, type: 'Identifier' }, | ||
property: { name: hookName, type: 'Identifier' }, | ||
})).some(shape => looksLike(node, shape)); | ||
}; | ||
const errorMessage = (name) => | ||
`\`${name}\` hook found after a test call. This may lead to missed exclusion, or unecessary validation runs which may impact performance.`; | ||
const errorMessage = name => | ||
`\`${name}\` hook found after a test call. This may lead to missed exclusion, or unnecessary validation runs which may impact performance.`; | ||
@@ -28,0 +28,0 @@ module.exports = { |
@@ -1,4 +0,4 @@ | ||
const { VEST_KEYWORD, VEST_IDENTIFIER_TEST } = require("../../constants"); | ||
const { closest } = require("../../helpers"); | ||
const { isTestCall, looksLikeExclusion, errorMessage } = require("./helpers"); | ||
const { VEST_KEYWORD, VEST_IDENTIFIER_TEST } = require('../../constants'); | ||
const { closest } = require('../../helpers'); | ||
const { isTestCall, looksLikeExclusion, errorMessage } = require('./helpers'); | ||
@@ -8,12 +8,12 @@ module.exports = { | ||
docs: { | ||
type: "problem", | ||
category: 'Possible Errors', | ||
description: | ||
"Makes sure vest exclusion hooks are not put before your test calls", | ||
category: "Possible Errors", | ||
'Makes sure vest exclusion hooks are not put before your test calls', | ||
recommended: true, | ||
type: 'problem', | ||
}, | ||
fixable: "code", | ||
fixable: 'code', | ||
}, | ||
create: function (context) { | ||
create(context) { | ||
return { | ||
@@ -46,4 +46,4 @@ CallExpression(node) { | ||
// If current reference is the first `test` call | ||
// sets `firstTest` to current index so we know not | ||
// to allow any more exclusion hooks | ||
// Sets `firstTest` to current index so we know not | ||
// To allow any more exclusion hooks | ||
if (isTestCall(identifier)) { | ||
@@ -59,3 +59,3 @@ return { | ||
// If the current reference belongs to the current node | ||
// and `test` was already called, mark shouldWarn as true. | ||
// And `test` was already called, mark shouldWarn as true. | ||
if (accumulator.firstTest) { | ||
@@ -82,3 +82,3 @@ return { | ||
const callExpression = closest(node, "ExpressionStatement"); | ||
const callExpression = closest(node, 'ExpressionStatement'); | ||
@@ -88,3 +88,3 @@ context.report({ | ||
message: errorMessage(name), | ||
fix: function (fixer) { | ||
fix(fixer) { | ||
const sourceCode = context.getSourceCode(); | ||
@@ -95,3 +95,3 @@ const text = sourceCode.getText(callExpression); | ||
fixer.insertTextBefore( | ||
closest(res.firstTest, "ExpressionStatement"), | ||
closest(res.firstTest, 'ExpressionStatement'), | ||
`${text}\n` | ||
@@ -98,0 +98,0 @@ ), |
@@ -1,7 +0,7 @@ | ||
const RuleTester = require("eslint").RuleTester; | ||
const { VEST_HOOK_ONLY, VEST_HOOK_SKIP } = require("../../constants"); | ||
const { errorMessage } = require("./helpers"); | ||
const RuleTester = require('eslint').RuleTester; | ||
const { VEST_HOOK_ONLY, VEST_HOOK_SKIP } = require('../../constants'); | ||
const { errorMessage } = require('./helpers'); | ||
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 } }); | ||
const excludeBeforeTest = require("."); | ||
const excludeBeforeTest = require('.'); | ||
@@ -20,5 +20,5 @@ const valid = [ | ||
});`, | ||
].map((code) => ({ code })); | ||
].map(code => ({ code })); | ||
const INVALID = [VEST_HOOK_ONLY, VEST_HOOK_SKIP].map((hookName) => | ||
const INVALID = [VEST_HOOK_ONLY, VEST_HOOK_SKIP].map(hookName => | ||
[ | ||
@@ -56,5 +56,5 @@ [ | ||
ruleTester.run("vest-exclude-before-test", excludeBeforeTest, { | ||
ruleTester.run('vest-exclude-before-test', excludeBeforeTest, { | ||
valid, | ||
invalid: [].concat(...INVALID), | ||
}); |
@@ -5,8 +5,8 @@ const { | ||
VEST_IDENTIFIER_TEST, | ||
} = require("../../constants"); | ||
} = require('../../constants'); | ||
const correctWrapperName = (name) => | ||
const correctWrapperName = name => | ||
name === VEST_HOOK_WARN ? VEST_IDENTIFIER_TEST : VEST_IDENTIFIER_VALIDATE; | ||
const hookScopeErrorMessgage = (hookName) => | ||
const hookScopeErrorMessage = hookName => | ||
`Vest hook \`${hookName}\` found in the wrong scope. This may lead to wrong validation output and unexpected behavior. Make sure you place it inside your \`${correctWrapperName( | ||
@@ -18,3 +18,3 @@ hookName | ||
correctWrapperName, | ||
hookScopeErrorMessgage, | ||
hookScopeErrorMessage, | ||
}; |
@@ -5,19 +5,19 @@ /** | ||
*/ | ||
"use strict"; | ||
'use strict'; | ||
const { ALL_VEST_HOOKS, VEST_KEYWORD } = require("../../constants"); | ||
const { closest, looksLike } = require("../../helpers"); | ||
const { correctWrapperName, hookScopeErrorMessgage } = require("./helpers"); | ||
const { ALL_VEST_HOOKS, VEST_KEYWORD } = require('../../constants'); | ||
const { closest, looksLike } = require('../../helpers'); | ||
const { correctWrapperName, hookScopeErrorMessage } = require('./helpers'); | ||
const isHookParentShape = (node, name) => | ||
looksLike(node, { | ||
type: "CallExpression", | ||
type: 'CallExpression', | ||
callee: { | ||
type: "MemberExpression", | ||
type: 'MemberExpression', | ||
object: { | ||
type: "Identifier", | ||
type: 'Identifier', | ||
name: VEST_KEYWORD, | ||
}, | ||
property: { | ||
type: "Identifier", | ||
type: 'Identifier', | ||
name, | ||
@@ -28,6 +28,6 @@ }, | ||
const functionScopeShape = (name) => ({ | ||
type: "CallExpression", | ||
const functionScopeShape = name => ({ | ||
type: 'CallExpression', | ||
callee: { | ||
type: "Identifier", | ||
type: 'Identifier', | ||
name: correctWrapperName(name), | ||
@@ -39,6 +39,6 @@ }, | ||
meta: { | ||
type: "problem", | ||
type: 'problem', | ||
docs: { | ||
description: "Makes sure vest hooks are put in the right place", | ||
category: "Possible Errors", | ||
description: 'Makes sure vest hooks are put in the right place', | ||
category: 'Possible Errors', | ||
recommended: true, | ||
@@ -48,3 +48,3 @@ }, | ||
create: function (context) { | ||
create(context) { | ||
return { | ||
@@ -61,3 +61,3 @@ Identifier(node) { | ||
// If correct, it is the hook itself, preceeded with `vest.`. | ||
const closestCallExpression = closest(node, "CallExpression"); | ||
const closestCallExpression = closest(node, 'CallExpression'); | ||
@@ -73,3 +73,3 @@ // Makes sure it looks like `vest.${hook_name}()` - e.g. vest.warn(). | ||
closestCallExpression.parent, | ||
"CallExpression" | ||
'CallExpression' | ||
); | ||
@@ -85,3 +85,3 @@ | ||
node: closestCallExpression, | ||
message: hookScopeErrorMessgage(name), | ||
message: hookScopeErrorMessage(name), | ||
}); | ||
@@ -88,0 +88,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
const RuleTester = require("eslint").RuleTester; | ||
const RuleTester = require('eslint').RuleTester; | ||
const { | ||
@@ -6,11 +6,11 @@ VEST_HOOK_WARN, | ||
VEST_HOOK_SKIP, | ||
} = require("../../constants"); | ||
const { hookScopeErrorMessgage } = require("./helpers"); | ||
const hooksScopeRule = require("."); | ||
} = require('../../constants'); | ||
const { hookScopeErrorMessage } = require('./helpers'); | ||
const hooksScopeRule = require('.'); | ||
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 } }); | ||
const ERROR_WARN = hookScopeErrorMessgage(VEST_HOOK_WARN); | ||
const ERROR_ONLY = hookScopeErrorMessgage(VEST_HOOK_ONLY); | ||
const ERROR_SKIP = hookScopeErrorMessgage(VEST_HOOK_SKIP); | ||
const ERROR_WARN = hookScopeErrorMessage(VEST_HOOK_WARN); | ||
const ERROR_ONLY = hookScopeErrorMessage(VEST_HOOK_ONLY); | ||
const ERROR_SKIP = hookScopeErrorMessage(VEST_HOOK_SKIP); | ||
@@ -32,3 +32,3 @@ const VALID = [ | ||
});`, | ||
].map((code) => ({ code })); | ||
].map(code => ({ code })); | ||
@@ -43,3 +43,3 @@ const INVALID_WARN = [ | ||
})`, | ||
].map((code) => ({ code, errors: [ERROR_WARN] })); | ||
].map(code => ({ code, errors: [ERROR_WARN] })); | ||
@@ -51,3 +51,3 @@ const INVALID_ONLY = [ | ||
});`, | ||
].map((code) => ({ code, errors: [ERROR_ONLY] })); | ||
].map(code => ({ code, errors: [ERROR_ONLY] })); | ||
@@ -59,7 +59,7 @@ const INVALID_SKIP = [ | ||
});`, | ||
].map((code) => ({ code, errors: [ERROR_SKIP] })); | ||
].map(code => ({ code, errors: [ERROR_SKIP] })); | ||
ruleTester.run("vest-hooks-scope", hooksScopeRule, { | ||
ruleTester.run('vest-hooks-scope', hooksScopeRule, { | ||
valid: VALID, | ||
invalid: [].concat(INVALID_WARN, INVALID_ONLY, INVALID_SKIP), | ||
}); |
{ | ||
"name": "eslint-plugin-vest", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Eslint plugin for vest validations.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -51,12 +51,12 @@ # eslint-plugin-vest | ||
```js | ||
validate("MyForm", () => { | ||
test("fieldName1", "message", () => { | ||
validate('MyForm', () => { | ||
test('fieldName1', 'message', () => { | ||
// ... | ||
}); | ||
test("fieldName2", "message", () => { | ||
test('fieldName2', 'message', () => { | ||
// ... | ||
}); | ||
vest.only("fieldName2"); // 🚨Should be called before test() | ||
vest.only('fieldName2'); // 🚨Should be called before test() | ||
}); | ||
@@ -68,6 +68,6 @@ ``` | ||
```js | ||
validate("MyForm", () => { | ||
validate('MyForm', () => { | ||
vest.only(); | ||
test("fieldName", "message", () => { | ||
test('fieldName', 'message', () => { | ||
// ... | ||
@@ -85,11 +85,11 @@ }); | ||
```js | ||
validate("MyForm", () => { | ||
validate('MyForm', () => { | ||
vest.warn(); // 🚨Should be called inside test() | ||
test("fieldName1", "message", () => { | ||
test('fieldName1', 'message', () => { | ||
// ... | ||
}); | ||
test("fieldName2", "message", () => { | ||
vest.only("fieldName2"); // 🚨Should be called inside validate() | ||
test('fieldName2', 'message', () => { | ||
vest.only('fieldName2'); // 🚨Should be called inside validate() | ||
}); | ||
@@ -102,6 +102,6 @@ }); | ||
```js | ||
validate("MyForm", () => { | ||
vest.only("fieldName2"); | ||
validate('MyForm', () => { | ||
vest.only('fieldName2'); | ||
test("fieldName1", "message", () => { | ||
test('fieldName1', 'message', () => { | ||
vest.warn(); | ||
@@ -111,4 +111,4 @@ // ... | ||
test("fieldName2", "message", () => {}); | ||
test('fieldName2', 'message', () => {}); | ||
}); | ||
``` |
12482