Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for flat configs #1503

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@
"email": "[email protected]",
"url": "jkimbo.com"
},
"main": "lib/index.js",
"main": "lib/legacy.js",
"exports": {
".": {
"import": "./lib/index.js",
"require": "./lib/legacy.js"
},
"./new": "./lib/index.js",
"./all": "./lib/configs/all.js",
"./recommended": "./lib/configs/recommended.js",
"./style": "./lib/configs/style.js",
"./globals": "./lib/globals.json",
"./globals.json": "./lib/globals.json"
},
"files": [
"docs/",
"lib/"
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/rules.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync } from 'fs';
import { resolve } from 'path';
import plugin from '../';
import plugin from '../legacy';

const numberOfRules = 53;
const ruleNames = Object.keys(plugin.rules);
Expand Down
26 changes: 26 additions & 0 deletions src/configs/all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import globals from '../globals.json';
import jest from '../index';
import legacy from '../legacy';

export default [
{
files: ['**/*.snap'],
plugins: {
jest,
},
processor: 'jest/.snap',
},
{
files: [
'**/*.{test,spec}.{js,cjs,mjs,jsx,ts,tsx}',
'**/__tests__/*.{js,cjs,mjs,jsx,ts,tsx}',
],
languageOptions: {
globals,
},
plugins: {
jest,
},
rules: legacy.configs.all.rules,
},
];
12 changes: 12 additions & 0 deletions src/configs/recommended.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import legacy from '../legacy';
import all from './all';

const [snap, test] = all;

export default [
snap,
{
...test,
rules: legacy.configs.recommended.rules,
},
];
12 changes: 12 additions & 0 deletions src/configs/style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import legacy from '../legacy';
import all from './all';

const [snap, test] = all;

export default [
snap,
{
...test,
rules: legacy.configs.style.rules,
},
];
106 changes: 4 additions & 102 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,109 +1,11 @@
import { readdirSync } from 'fs';
import { join, parse } from 'path';
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import {
name as packageName,
version as packageVersion,
} from '../package.json';
import globals from './globals.json';
import * as snapshotProcessor from './processors/snapshot-processor';
import legacy from './legacy';

type RuleModule = TSESLint.RuleModule<string, unknown[]> & {
meta: Required<Pick<TSESLint.RuleMetaData<string>, 'docs'>>;
};

// v5 of `@typescript-eslint/experimental-utils` removed this
declare module '@typescript-eslint/utils/dist/ts-eslint/Rule' {
export interface RuleMetaDataDocs {
category: 'Best Practices' | 'Possible Errors';
}
}

declare module '@typescript-eslint/utils/dist/ts-eslint/SourceCode' {
export interface SourceCode {
/**
* Returns the scope of the given node.
* This information can be used track references to variables.
* @since 8.37.0
*/
getScope(node: TSESTree.Node): TSESLint.Scope.Scope;
/**
* Returns an array of the ancestors of the given node, starting at
* the root of the AST and continuing through the direct parent of the current node.
* This array does not include the currently-traversed node itself.
* @since 8.38.0
*/
getAncestors(node: TSESTree.Node): TSESTree.Node[];
/**
* Returns a list of variables declared by the given node.
* This information can be used to track references to variables.
* @since 8.38.0
*/
getDeclaredVariables(
node: TSESTree.Node,
): readonly TSESLint.Scope.Variable[];
}
}

// copied from https://github.com/babel/babel/blob/d8da63c929f2d28c401571e2a43166678c555bc4/packages/babel-helpers/src/helpers.js#L602-L606
/* istanbul ignore next */
const interopRequireDefault = (obj: any): { default: any } =>
obj && obj.__esModule ? obj : { default: obj };

const importDefault = (moduleName: string) =>
// eslint-disable-next-line @typescript-eslint/no-require-imports
interopRequireDefault(require(moduleName)).default;

const rulesDir = join(__dirname, 'rules');
const excludedFiles = ['__tests__', 'detectJestVersion', 'utils'];

const rules = Object.fromEntries(
readdirSync(rulesDir)
.map(rule => parse(rule).name)
.filter(rule => !excludedFiles.includes(rule))
.map(rule => [rule, importDefault(join(rulesDir, rule)) as RuleModule]),
);

const recommendedRules = Object.fromEntries(
Object.entries(rules)
.filter(([, rule]) => rule.meta.docs.recommended)
.map(([name, rule]) => [
`jest/${name}`,
rule.meta.docs.recommended as TSESLint.Linter.RuleLevel,
]),
);

const allRules = Object.fromEntries<TSESLint.Linter.RuleLevel>(
Object.entries(rules)
.filter(([, rule]) => !rule.meta.deprecated)
.map(([name]) => [`jest/${name}`, 'error']),
);

const createConfig = (rules: Record<string, TSESLint.Linter.RuleLevel>) => ({
plugins: ['jest'],
env: { 'jest/globals': true },
rules,
});

export = {
export default {
meta: { name: packageName, version: packageVersion },
configs: {
all: createConfig(allRules),
recommended: createConfig(recommendedRules),
style: createConfig({
'jest/no-alias-methods': 'warn',
'jest/prefer-to-be': 'error',
'jest/prefer-to-contain': 'error',
'jest/prefer-to-have-length': 'error',
}),
},
environments: {
globals: {
globals,
},
},
processors: {
'.snap': snapshotProcessor,
},
rules,
rules: legacy.rules,
processors: legacy.processors,
};
109 changes: 109 additions & 0 deletions src/legacy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { readdirSync } from 'fs';
import { join, parse } from 'path';
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import {
name as packageName,
version as packageVersion,
} from '../package.json';
import globals from './globals.json';
import * as snapshotProcessor from './processors/snapshot-processor';

type RuleModule = TSESLint.RuleModule<string, unknown[]> & {
meta: Required<Pick<TSESLint.RuleMetaData<string>, 'docs'>>;
};

// v5 of `@typescript-eslint/experimental-utils` removed this
declare module '@typescript-eslint/utils/dist/ts-eslint/Rule' {
export interface RuleMetaDataDocs {
category: 'Best Practices' | 'Possible Errors';
}
}

declare module '@typescript-eslint/utils/dist/ts-eslint/SourceCode' {
export interface SourceCode {
/**
* Returns the scope of the given node.
* This information can be used track references to variables.
* @since 8.37.0
*/
getScope(node: TSESTree.Node): TSESLint.Scope.Scope;
/**
* Returns an array of the ancestors of the given node, starting at
* the root of the AST and continuing through the direct parent of the current node.
* This array does not include the currently-traversed node itself.
* @since 8.38.0
*/
getAncestors(node: TSESTree.Node): TSESTree.Node[];
/**
* Returns a list of variables declared by the given node.
* This information can be used to track references to variables.
* @since 8.38.0
*/
getDeclaredVariables(
node: TSESTree.Node,
): readonly TSESLint.Scope.Variable[];
}
}

// copied from https://github.com/babel/babel/blob/d8da63c929f2d28c401571e2a43166678c555bc4/packages/babel-helpers/src/helpers.js#L602-L606
/* istanbul ignore next */
const interopRequireDefault = (obj: any): { default: any } =>
obj && obj.__esModule ? obj : { default: obj };

const importDefault = (moduleName: string) =>
// eslint-disable-next-line @typescript-eslint/no-require-imports
interopRequireDefault(require(moduleName)).default;

const rulesDir = join(__dirname, 'rules');
const excludedFiles = ['__tests__', 'detectJestVersion', 'utils'];

const rules = Object.fromEntries(
readdirSync(rulesDir)
.map(rule => parse(rule).name)
.filter(rule => !excludedFiles.includes(rule))
.map(rule => [rule, importDefault(join(rulesDir, rule)) as RuleModule]),
);

const recommendedRules = Object.fromEntries(
Object.entries(rules)
.filter(([, rule]) => rule.meta.docs.recommended)
.map(([name, rule]) => [
`jest/${name}`,
rule.meta.docs.recommended as TSESLint.Linter.RuleLevel,
]),
);

const allRules = Object.fromEntries<TSESLint.Linter.RuleLevel>(
Object.entries(rules)
.filter(([, rule]) => !rule.meta.deprecated)
.map(([name]) => [`jest/${name}`, 'error']),
);

const createConfig = (rules: Record<string, TSESLint.Linter.RuleLevel>) => ({
plugins: ['jest'],
env: { 'jest/globals': true },
rules,
});

export = {
meta: { name: packageName, version: packageVersion },
configs: {
all: createConfig(allRules),
recommended: createConfig(recommendedRules),
style: createConfig({
'jest/no-alias-methods': 'warn',
'jest/prefer-to-be': 'error',
'jest/prefer-to-contain': 'error',
'jest/prefer-to-have-length': 'error',
}),
},
environments: {
globals: {
globals,
},
},
processors: {
'.snap': snapshotProcessor,
},
rules,
};
Loading