-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b860dc
commit 99b19bb
Showing
8 changed files
with
634 additions
and
661 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.8.0/schema.json", | ||
"formatter": { | ||
"enabled": true | ||
}, | ||
"organizeImports": { | ||
"enabled": true | ||
}, | ||
"linter": { | ||
"enabled": true, | ||
"rules": { | ||
"recommended": true | ||
} | ||
}, | ||
"javascript": { | ||
"formatter": { | ||
"quoteStyle": "single" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,157 @@ | ||
/** | ||
* @fileoverview Rule to flag use of .only in tests, preventing focused tests being committed accidentally | ||
* @fileoverview Rule to prevent use of focused test blocks, preventing accidental commits that only run a subset of tests | ||
* @author Levi Buzolic | ||
*/ | ||
|
||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
const defaultOptions = { | ||
block: ['describe', 'it', 'context', 'test', 'tape', 'fixture', 'serial', 'Feature', 'Scenario', 'Given', 'And', 'When', 'Then'], | ||
focus: ['only'], | ||
fix: false, | ||
matchers: [ | ||
"{describe,it,context,test,tape,fixture,serial,Feature,Scenario,Given,And,When,Then}.only", | ||
], | ||
}; | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'disallow .only blocks in tests', | ||
category: 'Possible Errors', | ||
recommended: true, | ||
url: 'https://github.com/levibuzolic/eslint-plugin-no-only-tests', | ||
}, | ||
fixable: true, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
block: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
}, | ||
uniqueItems: true, | ||
default: defaultOptions.block, | ||
}, | ||
focus: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
}, | ||
uniqueItems: true, | ||
default: defaultOptions.focus, | ||
}, | ||
fix: { | ||
type: 'boolean', | ||
default: defaultOptions.fix, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
}, | ||
create(context) { | ||
const options = Object.assign({}, defaultOptions, context.options[0]); | ||
const blocks = options.block || []; | ||
const focus = options.focus || []; | ||
const fix = !!options.fix; | ||
meta: { | ||
type: "problem", | ||
docs: { | ||
description: "Disallow focused tests", | ||
category: "Possible Errors", | ||
recommended: true, | ||
url: "https://github.com/levibuzolic/eslint-plugin-no-only-tests", | ||
}, | ||
fixable: "code", | ||
schema: [ | ||
{ | ||
type: "object", | ||
properties: { | ||
matchers: { | ||
type: "array", | ||
items: { | ||
type: "string", | ||
}, | ||
uniqueItems: true, | ||
default: defaultOptions.matchers, | ||
}, | ||
fix: { | ||
type: "boolean", | ||
default: false, | ||
}, | ||
block: { | ||
type: "array", | ||
items: { | ||
type: "string", | ||
}, | ||
uniqueItems: true, | ||
default: [], | ||
deprecated: "Use `matchers` option instead", | ||
}, | ||
focus: { | ||
type: "array", | ||
items: { | ||
type: "string", | ||
}, | ||
uniqueItems: true, | ||
default: [], | ||
deprecated: "Use `matchers` option instead", | ||
}, | ||
}, | ||
additionalProperties: false, | ||
default: {}, | ||
}, | ||
], | ||
}, | ||
create(context) { | ||
console.log("==========", context.options[0]); | ||
return {}; | ||
const providedOptions = context.options[0]; | ||
|
||
if (providedOptions?.block || providedOptions?.focus) { | ||
const blocks = providedOptions.block ?? [ | ||
"describe", | ||
"it", | ||
"context", | ||
"test", | ||
"tape", | ||
"fixture", | ||
"serial", | ||
"Feature", | ||
"Scenario", | ||
"Given", | ||
"And", | ||
"When", | ||
"Then", | ||
]; | ||
const focus = providedOptions.focus ?? ["only"]; | ||
|
||
console.log("==========", providedOptions); | ||
|
||
const migratedConfig = { | ||
fix: providedOptions?.fix, | ||
matchers: [ | ||
// ...(providedOptions?.matchers ?? []), | ||
[matcherGroup(blocks), matcherGroup(focus)] | ||
.filter(Boolean) | ||
.join("."), | ||
], | ||
}; | ||
console.warn( | ||
`The \`block\` and \`focus\` options of \`eslint-no-only-tests\` are deprecated, please use the \`matchers\` option instead. This should provide a more expressive way to define the test methods you want to prevent. Here’s the equivalent of your current configuration:\n${JSON.stringify(migratedConfig)}`, | ||
); | ||
} | ||
|
||
const options = Object.assign({}, defaultOptions, providedOptions); | ||
|
||
return { | ||
Identifier(node) { | ||
const parentObject = node.parent && node.parent.object; | ||
if (parentObject == null) return; | ||
if (focus.indexOf(node.name) === -1) return; | ||
const fix = !!options.fix; | ||
|
||
const callPath = getCallPath(node.parent).join('.'); | ||
const blocks = []; | ||
const focus = []; | ||
|
||
// comparison guarantees that matching is done with the beginning of call path | ||
if ( | ||
blocks.find(block => { | ||
// Allow wildcard tail matching of blocks when ending in a `*` | ||
if (block.endsWith('*')) return callPath.startsWith(block.replace(/\*$/, '')); | ||
return callPath.startsWith(`${block}.`); | ||
}) | ||
) { | ||
context.report({ | ||
node, | ||
message: callPath + ' not permitted', | ||
fix: fix ? fixer => fixer.removeRange([node.range[0] - 1, node.range[1]]) : undefined, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
return { | ||
Identifier(node) { | ||
const parentObject = node.parent?.object; | ||
if (parentObject == null) return; | ||
if (focus.indexOf(node.name) === -1) return; | ||
|
||
const callPath = getCallPath(node.parent).join("."); | ||
|
||
// comparison guarantees that matching is done with the beginning of call path | ||
if ( | ||
blocks.find((block) => { | ||
// Allow wildcard tail matching of blocks when ending in a `*` | ||
if (block.endsWith("*")) | ||
return callPath.startsWith(block.replace(/\*$/, "")); | ||
return callPath.startsWith(`${block}.`); | ||
}) | ||
) { | ||
context.report({ | ||
node, | ||
message: `${callPath} not permitted`, | ||
fix: fix | ||
? (fixer) => fixer.removeRange([node.range[0] - 1, node.range[1]]) | ||
: undefined, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
function getCallPath(node, path = []) { | ||
if (node) { | ||
const nodeName = node.name || (node.property && node.property.name); | ||
if (node.object) return getCallPath(node.object, [nodeName, ...path]); | ||
if (node.callee) return getCallPath(node.callee, path); | ||
return [nodeName, ...path]; | ||
} | ||
return path; | ||
if (node) { | ||
const nodeName = node.name || node.property?.name; | ||
if (node.object) return getCallPath(node.object, [nodeName, ...path]); | ||
if (node.callee) return getCallPath(node.callee, path); | ||
return [nodeName, ...path]; | ||
} | ||
return path; | ||
} | ||
|
||
/** | ||
* @param {string[]} items | ||
* @returns {string | undefined} | ||
*/ | ||
function matcherGroup(items) { | ||
const uniqueItems = [...new Set(items)]; | ||
if (uniqueItems.length <= 1) return uniqueItems[0]; | ||
return `{${uniqueItems.join(",")}}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"compilerOptions": { | ||
"esModuleInterop": true, | ||
"skipLibCheck": true, | ||
"target": "es2022", | ||
"allowJs": true, | ||
"checkJs": true, | ||
"resolveJsonModule": true, | ||
"moduleDetection": "force", | ||
"isolatedModules": true, | ||
"verbatimModuleSyntax": true, | ||
"strict": true, | ||
"noUncheckedIndexedAccess": true, | ||
"noImplicitOverride": true, | ||
"module": "preserve", | ||
"noEmit": true, | ||
"lib": ["es2022"], | ||
"types": ["node"], | ||
} | ||
} |
Oops, something went wrong.