Skip to content

Commit

Permalink
Add support for ESLint v9 flat config
Browse files Browse the repository at this point in the history
  • Loading branch information
levibuzolic committed Aug 15, 2024
1 parent 8fb57fc commit 6084aa0
Show file tree
Hide file tree
Showing 9 changed files with 528 additions and 578 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ jobs:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: yarn install
- name: TypeScript
run: yarn tsc
- name: ESLint
run: yarn eslint .
- name: Biome
run: yarn biome check
- name: Tests
run: yarn run test
env:
Expand Down
11 changes: 11 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
"organizeImports": { "enabled": true },
"linter": {
"enabled": true,
"rules": { "recommended": true }
},
"formatter": {
"enabled": true
}
}
20 changes: 20 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const plugin = require('./index');
const js = require('@eslint/js');

/** @type {import('eslint').Linter.Config} */
module.exports = [
js.configs.recommended,
{
files: ['**/*.js'],
languageOptions: {
sourceType: 'commonjs',
ecmaVersion: 'latest',
},
plugins: {
'no-only-tests': plugin,
},
rules: {
'no-only-tests/no-only-tests': 'error',
},
},
];
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

module.exports = {
rules: {
'no-only-tests': require('./rules/no-only-tests')
}
'no-only-tests': require('./rules/no-only-tests'),
},
};
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
"test": "node tests.js"
},
"devDependencies": {
"eslint": ">=3.0.0"
"@biomejs/biome": "^1.8.3",
"@types/eslint": "^9.6.0",
"@types/eslint__js": "^8.42.3",
"@types/node": "^22.3.0",
"eslint": ">=9.0.0",
"typescript": "^5.5.4"
},
"engines": {
"node": ">=5.0.0"
Expand Down
45 changes: 31 additions & 14 deletions rules/no-only-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
* @author Levi Buzolic
*/

'use strict';

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
/** @typedef {{block?: string[], focus?: string[], functions?: string[], fix?: boolean}} Options */

/** @type {Options} */
const defaultOptions = {
block: [
'describe',
Expand All @@ -30,6 +27,7 @@ const defaultOptions = {
fix: false,
};

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
docs: {
Expand All @@ -38,7 +36,7 @@ module.exports = {
recommended: true,
url: 'https://github.com/levibuzolic/eslint-plugin-no-only-tests',
},
fixable: true,
fixable: 'code',
schema: [
{
type: 'object',
Expand Down Expand Up @@ -77,6 +75,7 @@ module.exports = {
],
},
create(context) {
/** @type {Options} */
const options = Object.assign({}, defaultOptions, context.options[0]);
const blocks = options.block || [];
const focus = options.focus || [];
Expand All @@ -88,11 +87,11 @@ module.exports = {
if (functions.length && functions.indexOf(node.name) > -1) {
context.report({
node,
message: node.name + ' not permitted',
message: `${node.name} not permitted`,
});
}

const parentObject = node.parent && node.parent.object;
const parentObject = 'object' in node.parent ? node.parent.object : undefined;
if (parentObject == null) return;
if (focus.indexOf(node.name) === -1) return;

Expand All @@ -106,23 +105,41 @@ module.exports = {
return callPath.startsWith(`${block}.`);
})
) {
const rangeStart = node.range?.[0];
const rangeEnd = node.range?.[1];

context.report({
node,
message: callPath + ' not permitted',
fix: fix ? fixer => fixer.removeRange([node.range[0] - 1, node.range[1]]) : undefined,
message: `${callPath} not permitted`,
fix:
fix && rangeStart != null && rangeEnd != null
? fixer => fixer.removeRange([rangeStart - 1, rangeEnd])
: undefined,
});
}
},
};
},
};

/**
*
* @param {import('estree').Node} node
* @param {string[]} path
* @returns
*/
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];
const nodeName =
'name' in node && node.name
? node.name
: 'property' in node && node.property && 'name' in node.property
? node.property?.name
: undefined;
if ('object' in node && node.object && nodeName) return getCallPath(node.object, [nodeName, ...path]);
if ('callee' in node && node.callee) return getCallPath(node.callee, path);
if (nodeName) return [nodeName, ...path];
return path;
}
return path;
}
20 changes: 7 additions & 13 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,60 +33,49 @@ ruleTester.run('no-only-tests', rules['no-only-tests'], {
invalid: [
{
code: 'describe.only("Some describe block", function() {});',
output: 'describe.only("Some describe block", function() {});',
errors: [{message: 'describe.only not permitted'}],
},
{
code: 'it.only("Some assertion", function() {});',
output: 'it.only("Some assertion", function() {});',
errors: [{message: 'it.only not permitted'}],
},
{
code: 'context.only("Some context", function() {});',
output: 'context.only("Some context", function() {});',
errors: [{message: 'context.only not permitted'}],
},
{
code: 'test.only("Some test", function() {});',
output: 'test.only("Some test", function() {});',
errors: [{message: 'test.only not permitted'}],
},
{
code: 'tape.only("A tape", function() {});',
output: 'tape.only("A tape", function() {});',
errors: [{message: 'tape.only not permitted'}],
},
{
code: 'fixture.only("A fixture", function() {});',
output: 'fixture.only("A fixture", function() {});',
errors: [{message: 'fixture.only not permitted'}],
},
{
code: 'serial.only("A serial test", function() {});',
output: 'serial.only("A serial test", function() {});',
errors: [{message: 'serial.only not permitted'}],
},
{
options: [{block: ['obscureTestBlock']}],
code: 'obscureTestBlock.only("An obscure testing library test", function() {});',
output: 'obscureTestBlock.only("An obscure testing library test", function() {});',
errors: [{message: 'obscureTestBlock.only not permitted'}],
},
{
options: [{block: ['ava.default']}],
code: 'ava.default.only("Block with dot", function() {});',
output: 'ava.default.only("Block with dot", function() {});',
errors: [{message: 'ava.default.only not permitted'}],
},
{
code: 'it.default.before(console.log).only("Some describe block", function() {});',
output: 'it.default.before(console.log).only("Some describe block", function() {});',
errors: [{message: 'it.default.before.only not permitted'}],
},
{
options: [{focus: ['focus']}],
code: 'test.focus("An alternative focus function", function() {});',
output: 'test.focus("An alternative focus function", function() {});',
errors: [{message: 'test.focus not permitted'}],
},
// As above, but with fix: true option to enable auto-fixing
Expand Down Expand Up @@ -159,7 +148,6 @@ ruleTester.run('no-only-tests', rules['no-only-tests'], {
{
options: [{block: ['test*']}],
code: 'testResource.only("A test resource block", function() {});',
output: 'testResource.only("A test resource block", function() {});',
errors: [{message: 'testResource.only not permitted'}],
},
{
Expand Down Expand Up @@ -203,7 +191,13 @@ ruleTester.run('no-only-tests', rules['no-only-tests'], {
code: 'xit("No skipped tests", function() {});',
errors: [{message: 'xit not permitted'}],
},
{
options: [{functions: ['fit', 'xit'], fix: true}],
code: 'xit("No skipped tests", function() {});',
errors: [{message: 'xit not permitted'}],
},
],
});

console.log('Tests completed successfully');
/* global process */
process.stderr.write('\n✅ Tests completed successfully\n');
20 changes: 20 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
/* Base Options: */
"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"]
}
}
Loading

0 comments on commit 6084aa0

Please sign in to comment.