@talkpilot/eslint-config 0.0.0-stage → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -2
- package/dist/bin/index.d.ts +2 -0
- package/dist/bin/index.js +84 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +16 -0
- package/dist/lib/ast.constants.d.ts +4 -0
- package/dist/lib/ast.constants.js +17 -0
- package/dist/lib/ast.utils.d.ts +13 -0
- package/dist/lib/ast.utils.js +62 -0
- package/dist/lib/cli.constants.d.ts +3 -0
- package/dist/lib/cli.constants.js +3 -0
- package/dist/lib/defaults.constants.d.ts +17 -0
- package/dist/lib/defaults.constants.js +51 -0
- package/dist/lib/fileNaming.utils.d.ts +38 -0
- package/dist/lib/fileNaming.utils.js +54 -0
- package/dist/lib/git.utils.d.ts +8 -0
- package/dist/lib/git.utils.js +42 -0
- package/dist/lib/lint.types.d.ts +30 -0
- package/dist/lib/lint.types.js +1 -0
- package/dist/lib/presets.constants.d.ts +2 -0
- package/dist/lib/presets.constants.js +3 -0
- package/dist/lib/presets.utils.d.ts +8 -0
- package/dist/lib/presets.utils.js +67 -0
- package/dist/lib/react.constants.d.ts +3 -0
- package/dist/lib/react.constants.js +3 -0
- package/dist/lib/ruleSets.constants.d.ts +217 -0
- package/dist/lib/ruleSets.constants.js +93 -0
- package/dist/lib/versionCheck.utils.d.ts +5 -0
- package/dist/lib/versionCheck.utils.js +13 -0
- package/dist/rules/constantsInConstantsFile.rule.d.ts +5 -0
- package/dist/rules/constantsInConstantsFile.rule.js +36 -0
- package/dist/rules/dataInDataFile.rule.d.ts +6 -0
- package/dist/rules/dataInDataFile.rule.js +44 -0
- package/dist/rules/index.d.ts +3 -0
- package/dist/rules/index.js +14 -0
- package/dist/rules/oneExportedComponent.rule.d.ts +5 -0
- package/dist/rules/oneExportedComponent.rule.js +106 -0
- package/dist/rules/rule.utils.d.ts +3 -0
- package/dist/rules/rule.utils.js +3 -0
- package/dist/rules/typesInTypesFile.rule.d.ts +2 -0
- package/dist/rules/typesInTypesFile.rule.js +23 -0
- package/package.json +45 -4
package/README.md
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @talkpilot/eslint-config
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Company-wide ESLint rules for TalkPilot TypeScript repos.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install -D @talkpilot/eslint-config eslint@10 typescript
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```javascript
|
|
10
|
+
// eslint.config.mjs
|
|
11
|
+
import { backend } from '@talkpilot/eslint-config'; // React apps: { frontend }
|
|
12
|
+
|
|
13
|
+
export default [...backend /* , repo rules */];
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
```json
|
|
17
|
+
"lint": "talkpilot-lint",
|
|
18
|
+
"lint:all": "talkpilot-lint --all"
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Written in TypeScript (`src/`), compiled to `dist/` on `npm pack` / `npm publish`.
|
|
22
|
+
|
|
23
|
+
Repos also run lint before every push (`.husky/pre-push` → `npm run lint`):
|
|
24
|
+
|
|
25
|
+
- `git push --dry-run` - runs the hook without sending anything (good for testing it).
|
|
26
|
+
- `git push --no-verify` - skips the hook; CI on the PR still checks.
|
|
27
|
+
|
|
28
|
+
Full setup, building, the PR workflow and publishing steps: see the `release-management` README.
|
|
29
|
+
Rules and decisions: Notion → DEV / Architecture & Design / Maintainments / ESLint.
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Usage:
|
|
3
|
+
// talkpilot-lint lint files changed vs. main (whole files) + block new .js files
|
|
4
|
+
// talkpilot-lint --all lint the whole repo
|
|
5
|
+
// talkpilot-lint --all --summary print only {"errors","warnings","files"} as JSON (for the PR comment)
|
|
6
|
+
// In CI, set LINT_BASE to the PR base commit.
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import { ESLint } from 'eslint';
|
|
9
|
+
import { LINTABLE_FILE, NEW_JS_FILE } from '../lib/cli.constants.js';
|
|
10
|
+
import { changedFiles } from '../lib/git.utils.js';
|
|
11
|
+
const args = new Set(process.argv.slice(2));
|
|
12
|
+
/** Adds up errors and warnings across all linted files. */
|
|
13
|
+
function summarize(results) {
|
|
14
|
+
return {
|
|
15
|
+
errors: results.reduce((sum, result) => sum + result.errorCount, 0),
|
|
16
|
+
warnings: results.reduce((sum, result) => sum + result.warningCount, 0),
|
|
17
|
+
files: results.filter((result) => result.errorCount + result.warningCount > 0).length,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/** Prints lint results in ESLint's standard readable format. */
|
|
21
|
+
async function printResults(eslint, results) {
|
|
22
|
+
const formatter = await eslint.loadFormatter('stylish');
|
|
23
|
+
const output = await formatter.format(results);
|
|
24
|
+
if (output)
|
|
25
|
+
process.stdout.write(`${output}\n`);
|
|
26
|
+
}
|
|
27
|
+
/** `--all`: lints the whole repo. With `--summary` prints only the counts as JSON (used by the PR comment). */
|
|
28
|
+
async function lintAll(eslint) {
|
|
29
|
+
const results = await eslint.lintFiles(['.']);
|
|
30
|
+
const summary = summarize(results);
|
|
31
|
+
if (args.has('--summary')) {
|
|
32
|
+
process.stdout.write(`${JSON.stringify(summary)}\n`);
|
|
33
|
+
return 0;
|
|
34
|
+
}
|
|
35
|
+
await printResults(eslint, results);
|
|
36
|
+
process.stdout.write(`Lint violations in repo: ${summary.errors} errors, ${summary.warnings} warnings in ${summary.files} files\n`);
|
|
37
|
+
return summary.errors > 0 ? 1 : 0;
|
|
38
|
+
}
|
|
39
|
+
/** Fails when the branch adds a `.js` file under `src/` (new code must be TypeScript). */
|
|
40
|
+
function hasNoNewJsFiles() {
|
|
41
|
+
const newJsFiles = changedFiles('AR').filter((file) => NEW_JS_FILE.test(file));
|
|
42
|
+
if (newJsFiles.length === 0)
|
|
43
|
+
return true;
|
|
44
|
+
process.stderr.write('New .js files are not allowed - new code must be TypeScript:\n');
|
|
45
|
+
for (const file of newJsFiles)
|
|
46
|
+
process.stderr.write(` ${file}\n`);
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
/** Changed TypeScript files that still exist and aren't ignored by the config (tests, dist, ...). */
|
|
50
|
+
async function findLintableChangedFiles(eslint) {
|
|
51
|
+
const candidates = changedFiles('ACMR').filter((file) => LINTABLE_FILE.test(file) && fs.existsSync(file));
|
|
52
|
+
const files = [];
|
|
53
|
+
for (const file of candidates) {
|
|
54
|
+
if (!(await eslint.isPathIgnored(file)))
|
|
55
|
+
files.push(file);
|
|
56
|
+
}
|
|
57
|
+
return files;
|
|
58
|
+
}
|
|
59
|
+
/** Lints the changed files - the whole file, not only the changed lines. */
|
|
60
|
+
async function isChangedCodeClean(eslint) {
|
|
61
|
+
const files = await findLintableChangedFiles(eslint);
|
|
62
|
+
if (files.length === 0) {
|
|
63
|
+
process.stdout.write('No changed files to lint.\n');
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
const results = await eslint.lintFiles(files);
|
|
67
|
+
await printResults(eslint, results);
|
|
68
|
+
return summarize(results).errors === 0;
|
|
69
|
+
}
|
|
70
|
+
/** Entry point: `--all` mode, or the default PR mode (new-.js check + lint changed files). */
|
|
71
|
+
async function main() {
|
|
72
|
+
const eslint = new ESLint();
|
|
73
|
+
if (args.has('--all'))
|
|
74
|
+
return lintAll(eslint);
|
|
75
|
+
const isJsCheckPassing = hasNoNewJsFiles();
|
|
76
|
+
const isLintPassing = await isChangedCodeClean(eslint);
|
|
77
|
+
return isJsCheckPassing && isLintPassing ? 0 : 1;
|
|
78
|
+
}
|
|
79
|
+
main()
|
|
80
|
+
.then((exitCode) => process.exit(exitCode))
|
|
81
|
+
.catch((error) => {
|
|
82
|
+
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
83
|
+
process.exit(2);
|
|
84
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { BACKEND_CONFIG, FRONTEND_CONFIG } from './lib/presets.constants.js';
|
|
2
|
+
/**
|
|
3
|
+
* Company-wide ESLint rules, one set per type of code. Repos spread one set first and add repo rules after:
|
|
4
|
+
* import { backend } from '@talkpilot/eslint-config'; // Node services and packages
|
|
5
|
+
* import { frontend } from '@talkpilot/eslint-config'; // React apps
|
|
6
|
+
* export default [...backend, { files: ['**\/*.ts', '**\/*.tsx'], rules: { ... } }];
|
|
7
|
+
* The default export is the backend set (kept for repos set up with 1.0.x).
|
|
8
|
+
*/
|
|
9
|
+
export default BACKEND_CONFIG;
|
|
10
|
+
export { BACKEND_CONFIG as backend, FRONTEND_CONFIG as frontend };
|
|
11
|
+
export { talkpilotPlugin as plugin } from './rules/index.js';
|
|
12
|
+
export { buildFileNamingRules, buildFrontendFileNamingRules } from './lib/fileNaming.utils.js';
|
|
13
|
+
export { EXEMPT_FILE_NAMES, LIMITS, ROLE_SUFFIXES } from './lib/defaults.constants.js';
|
|
14
|
+
export { NO_MONGODB_IMPORT } from './lib/ruleSets.constants.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { BACKEND_CONFIG, FRONTEND_CONFIG } from './lib/presets.constants.js';
|
|
2
|
+
import { assertSupportedEslintVersion } from './lib/versionCheck.utils.js';
|
|
3
|
+
assertSupportedEslintVersion();
|
|
4
|
+
/**
|
|
5
|
+
* Company-wide ESLint rules, one set per type of code. Repos spread one set first and add repo rules after:
|
|
6
|
+
* import { backend } from '@talkpilot/eslint-config'; // Node services and packages
|
|
7
|
+
* import { frontend } from '@talkpilot/eslint-config'; // React apps
|
|
8
|
+
* export default [...backend, { files: ['**\/*.ts', '**\/*.tsx'], rules: { ... } }];
|
|
9
|
+
* The default export is the backend set (kept for repos set up with 1.0.x).
|
|
10
|
+
*/
|
|
11
|
+
export default BACKEND_CONFIG;
|
|
12
|
+
export { BACKEND_CONFIG as backend, FRONTEND_CONFIG as frontend };
|
|
13
|
+
export { talkpilotPlugin as plugin } from './rules/index.js';
|
|
14
|
+
export { buildFileNamingRules, buildFrontendFileNamingRules } from './lib/fileNaming.utils.js';
|
|
15
|
+
export { EXEMPT_FILE_NAMES, LIMITS, ROLE_SUFFIXES } from './lib/defaults.constants.js';
|
|
16
|
+
export { NO_MONGODB_IMPORT } from './lib/ruleSets.constants.js';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
|
|
2
|
+
export const UPPER_CASE_NAME = /^[A-Z][A-Z0-9_]*$/;
|
|
3
|
+
export const FUNCTION_NODE_TYPES = new Set([
|
|
4
|
+
AST_NODE_TYPES.ArrowFunctionExpression,
|
|
5
|
+
AST_NODE_TYPES.FunctionExpression,
|
|
6
|
+
AST_NODE_TYPES.ClassExpression,
|
|
7
|
+
]);
|
|
8
|
+
export const TS_WRAPPER_NODE_TYPES = new Set([
|
|
9
|
+
AST_NODE_TYPES.TSAsExpression,
|
|
10
|
+
AST_NODE_TYPES.TSSatisfiesExpression,
|
|
11
|
+
AST_NODE_TYPES.TSTypeAssertion,
|
|
12
|
+
AST_NODE_TYPES.TSNonNullExpression,
|
|
13
|
+
]);
|
|
14
|
+
export const DATA_NODE_TYPES = new Set([
|
|
15
|
+
AST_NODE_TYPES.ArrayExpression,
|
|
16
|
+
AST_NODE_TYPES.ObjectExpression,
|
|
17
|
+
]);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import type { DataSize, Scope } from './lint.types.js';
|
|
3
|
+
/**
|
|
4
|
+
* True when a top-level `const` is a constant by our standard:
|
|
5
|
+
* its value is fixed, or its name is UPPER_CASE. Functions and classes never count.
|
|
6
|
+
*/
|
|
7
|
+
export declare function isModuleConstant(declarator: TSESTree.VariableDeclarator, scope: Scope): boolean;
|
|
8
|
+
/** Returns the name and line count of a top-level fixed array/object, or null if it isn't one. */
|
|
9
|
+
export declare function getDataSize(declarator: TSESTree.VariableDeclarator, scope: Scope): DataSize | null;
|
|
10
|
+
/** Collects every top-level `const x = ...` in a file (exported or not). Inner scopes are ignored. */
|
|
11
|
+
export declare function getTopLevelConstDeclarators(program: TSESTree.Program): TSESTree.VariableDeclarator[];
|
|
12
|
+
/** True when the file name ends with one of the given suffixes, e.g. `.types.ts`. */
|
|
13
|
+
export declare function hasSuffix(filename: string, suffixes: string[]): boolean;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { AST_NODE_TYPES, ASTUtils } from '@typescript-eslint/utils';
|
|
2
|
+
import { DATA_NODE_TYPES, FUNCTION_NODE_TYPES, TS_WRAPPER_NODE_TYPES, UPPER_CASE_NAME, } from './ast.constants.js';
|
|
3
|
+
/** Strips TypeScript-only wrappers (`as`, `satisfies`, `<T>x`, `x!`) and returns the real value node underneath. */
|
|
4
|
+
function unwrap(node) {
|
|
5
|
+
let current = node ?? null;
|
|
6
|
+
while (current && TS_WRAPPER_NODE_TYPES.has(current.type)) {
|
|
7
|
+
current = current.expression;
|
|
8
|
+
}
|
|
9
|
+
return current;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* True when the value is fixed when the code is written (not computed at runtime).
|
|
13
|
+
* Uses ESLint's own `getStaticValue`, which also follows references to other constants.
|
|
14
|
+
*/
|
|
15
|
+
function isStaticValue(node, scope) {
|
|
16
|
+
return ASTUtils.getStaticValue(node, scope) !== null;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* True when a top-level `const` is a constant by our standard:
|
|
20
|
+
* its value is fixed, or its name is UPPER_CASE. Functions and classes never count.
|
|
21
|
+
*/
|
|
22
|
+
export function isModuleConstant(declarator, scope) {
|
|
23
|
+
if (declarator.id.type !== AST_NODE_TYPES.Identifier)
|
|
24
|
+
return false;
|
|
25
|
+
const init = unwrap(declarator.init);
|
|
26
|
+
if (!init || FUNCTION_NODE_TYPES.has(init.type))
|
|
27
|
+
return false;
|
|
28
|
+
return UPPER_CASE_NAME.test(declarator.id.name) || isStaticValue(init, scope);
|
|
29
|
+
}
|
|
30
|
+
/** True for a fixed array or object, i.e. something that could live in a *.data.ts file. */
|
|
31
|
+
function isDataLiteral(node, scope) {
|
|
32
|
+
const value = unwrap(node);
|
|
33
|
+
return value !== null && DATA_NODE_TYPES.has(value.type) && isStaticValue(value, scope);
|
|
34
|
+
}
|
|
35
|
+
/** Returns the name and line count of a top-level fixed array/object, or null if it isn't one. */
|
|
36
|
+
export function getDataSize(declarator, scope) {
|
|
37
|
+
const { id, init } = declarator;
|
|
38
|
+
if (id.type !== AST_NODE_TYPES.Identifier || !init || !isDataLiteral(init, scope))
|
|
39
|
+
return null;
|
|
40
|
+
return { name: id.name, lines: init.loc.end.line - init.loc.start.line + 1 };
|
|
41
|
+
}
|
|
42
|
+
/** Gets the declaration out of `export const ...`, or returns the statement as-is. */
|
|
43
|
+
function toDeclaration(statement) {
|
|
44
|
+
if (statement.type === AST_NODE_TYPES.ExportNamedDeclaration)
|
|
45
|
+
return statement.declaration;
|
|
46
|
+
return statement;
|
|
47
|
+
}
|
|
48
|
+
/** Collects every top-level `const x = ...` in a file (exported or not). Inner scopes are ignored. */
|
|
49
|
+
export function getTopLevelConstDeclarators(program) {
|
|
50
|
+
const declarators = [];
|
|
51
|
+
for (const statement of program.body) {
|
|
52
|
+
const declaration = toDeclaration(statement);
|
|
53
|
+
if (declaration?.type === AST_NODE_TYPES.VariableDeclaration && declaration.kind === 'const') {
|
|
54
|
+
declarators.push(...declaration.declarations);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return declarators;
|
|
58
|
+
}
|
|
59
|
+
/** True when the file name ends with one of the given suffixes, e.g. `.types.ts`. */
|
|
60
|
+
export function hasSuffix(filename, suffixes) {
|
|
61
|
+
return suffixes.some((suffix) => filename.endsWith(suffix));
|
|
62
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { LintLimits } from './lint.types.js';
|
|
2
|
+
export declare const SUPPORTED_ESLINT_MAJOR = 10;
|
|
3
|
+
export declare const LINTED_FILES: string[];
|
|
4
|
+
export declare const IGNORED_PATHS: string[];
|
|
5
|
+
export declare const ROLE_SUFFIXES: string[];
|
|
6
|
+
export declare const EXEMPT_FILE_NAMES: string[];
|
|
7
|
+
export declare const FRONTEND_EXEMPT_FILE_NAMES: string[];
|
|
8
|
+
export declare const TSX_FILES: string[];
|
|
9
|
+
export declare const FRONTEND_IGNORED_PATHS: string[];
|
|
10
|
+
export declare const CAMEL_CASE_GLOB = "+([a-z])*([a-zA-Z0-9])";
|
|
11
|
+
export declare const PASCAL_CASE_GLOB = "+([A-Z])*([a-zA-Z0-9])";
|
|
12
|
+
export declare const IGNORED_FOLDER_NAMES: string[];
|
|
13
|
+
export declare const LIMITS: LintLimits;
|
|
14
|
+
export declare const BOOLEAN_PREFIXES: string[];
|
|
15
|
+
export declare const TYPE_FILE_SUFFIXES: string[];
|
|
16
|
+
export declare const CONSTANT_FILE_SUFFIXES: string[];
|
|
17
|
+
export declare const DATA_FILE_SUFFIX = ".data.ts";
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export const SUPPORTED_ESLINT_MAJOR = 10;
|
|
2
|
+
export const LINTED_FILES = ['**/*.ts', '**/*.tsx'];
|
|
3
|
+
export const IGNORED_PATHS = [
|
|
4
|
+
'**/node_modules/**',
|
|
5
|
+
'**/dist/**',
|
|
6
|
+
'**/build/**',
|
|
7
|
+
'**/coverage/**',
|
|
8
|
+
'**/*.d.ts',
|
|
9
|
+
'**/*.spec.ts',
|
|
10
|
+
'**/*.spec.tsx',
|
|
11
|
+
'**/*.test.ts',
|
|
12
|
+
'**/*.test.tsx',
|
|
13
|
+
'**/__tests__/**',
|
|
14
|
+
'**/__mocks__/**',
|
|
15
|
+
];
|
|
16
|
+
export const ROLE_SUFFIXES = [
|
|
17
|
+
'types',
|
|
18
|
+
'constants',
|
|
19
|
+
'data',
|
|
20
|
+
'utils',
|
|
21
|
+
'service',
|
|
22
|
+
'routes',
|
|
23
|
+
'controllers',
|
|
24
|
+
'schema',
|
|
25
|
+
'spec',
|
|
26
|
+
'client',
|
|
27
|
+
'errors',
|
|
28
|
+
'api',
|
|
29
|
+
'resolvers',
|
|
30
|
+
];
|
|
31
|
+
export const EXEMPT_FILE_NAMES = ['index.ts', 'app.ts'];
|
|
32
|
+
export const FRONTEND_EXEMPT_FILE_NAMES = ['index.ts', 'app.ts', 'use*.ts'];
|
|
33
|
+
export const TSX_FILES = ['**/*.tsx'];
|
|
34
|
+
export const FRONTEND_IGNORED_PATHS = ['**/*.stories.ts', '**/*.stories.tsx'];
|
|
35
|
+
export const CAMEL_CASE_GLOB = '+([a-z])*([a-zA-Z0-9])';
|
|
36
|
+
export const PASCAL_CASE_GLOB = '+([A-Z])*([a-zA-Z0-9])';
|
|
37
|
+
export const IGNORED_FOLDER_NAMES = ['__tests__', '__mocks__'];
|
|
38
|
+
export const LIMITS = {
|
|
39
|
+
FUNCTION_LINES: 40,
|
|
40
|
+
FILE_LINES: 200,
|
|
41
|
+
DATA_FILE_LINES: 5000,
|
|
42
|
+
NESTING_DEPTH: 3,
|
|
43
|
+
FUNCTION_PARAMS: 5,
|
|
44
|
+
COMPLEXITY: 5,
|
|
45
|
+
INLINE_DATA_LINES: 50,
|
|
46
|
+
TSX_FILE_LINES: 100,
|
|
47
|
+
};
|
|
48
|
+
export const BOOLEAN_PREFIXES = ['is', 'has', 'should', 'can'];
|
|
49
|
+
export const TYPE_FILE_SUFFIXES = ['.types.ts', '.d.ts'];
|
|
50
|
+
export const CONSTANT_FILE_SUFFIXES = ['.constants.ts', '.data.ts'];
|
|
51
|
+
export const DATA_FILE_SUFFIX = '.data.ts';
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { FileNamingOptions } from './lint.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Backend file/folder naming rules (eslint-plugin-check-file):
|
|
4
|
+
* - `.ts` files: camelCase name parts and a role suffix, e.g. `phoneNumbers.routes.ts`
|
|
5
|
+
* - folders: camelCase
|
|
6
|
+
* Repos that use extra roles call it again with their own list, e.g.
|
|
7
|
+
* { files: ['**\/*.ts', '**\/*.tsx'], rules: buildFileNamingRules({ roles: [...ROLE_SUFFIXES, 'getters'] }) }
|
|
8
|
+
*/
|
|
9
|
+
export declare function buildFileNamingRules(options?: FileNamingOptions): {
|
|
10
|
+
'check-file/filename-naming-convention': (string | Record<string, string>)[];
|
|
11
|
+
'check-file/folder-naming-convention': (string | {
|
|
12
|
+
'**/': string;
|
|
13
|
+
ignoreWords?: undefined;
|
|
14
|
+
errorMessage?: undefined;
|
|
15
|
+
} | {
|
|
16
|
+
ignoreWords: string[];
|
|
17
|
+
errorMessage: string;
|
|
18
|
+
'**/'?: undefined;
|
|
19
|
+
})[];
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Frontend (React) file/folder naming rules:
|
|
23
|
+
* - `.ts` files: same as backend; React hook files (`useX.ts`) are exempt
|
|
24
|
+
* - `.tsx` files: PascalCase or camelCase parts, e.g. `CallCard.tsx`, `Login.screen.tsx`, `useAuth.tsx`
|
|
25
|
+
* - folders: camelCase or PascalCase (component folders)
|
|
26
|
+
*/
|
|
27
|
+
export declare function buildFrontendFileNamingRules(options?: FileNamingOptions): {
|
|
28
|
+
'check-file/filename-naming-convention': (string | Record<string, string>)[];
|
|
29
|
+
'check-file/folder-naming-convention': (string | {
|
|
30
|
+
'**/': string;
|
|
31
|
+
ignoreWords?: undefined;
|
|
32
|
+
errorMessage?: undefined;
|
|
33
|
+
} | {
|
|
34
|
+
ignoreWords: string[];
|
|
35
|
+
errorMessage: string;
|
|
36
|
+
'**/'?: undefined;
|
|
37
|
+
})[];
|
|
38
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { CAMEL_CASE_GLOB, EXEMPT_FILE_NAMES, FRONTEND_EXEMPT_FILE_NAMES, IGNORED_FOLDER_NAMES, PASCAL_CASE_GLOB, ROLE_SUFFIXES, } from './defaults.constants.js';
|
|
2
|
+
/** Builds the check-file rules from the allowed `.ts` / `.tsx` name patterns and folder pattern. */
|
|
3
|
+
function buildCheckFileRules(filePatterns, folderPattern, options, fileMessage) {
|
|
4
|
+
return {
|
|
5
|
+
'check-file/filename-naming-convention': [
|
|
6
|
+
'error',
|
|
7
|
+
filePatterns,
|
|
8
|
+
{
|
|
9
|
+
errorMessage: fileMessage,
|
|
10
|
+
},
|
|
11
|
+
],
|
|
12
|
+
'check-file/folder-naming-convention': [
|
|
13
|
+
'error',
|
|
14
|
+
{ '**/': folderPattern },
|
|
15
|
+
{
|
|
16
|
+
ignoreWords: options.ignoredFolders ?? IGNORED_FOLDER_NAMES,
|
|
17
|
+
errorMessage: `Folder '{{ target }}' must be ${folderPattern === 'CAMEL_CASE' ? 'camelCase' : 'camelCase or PascalCase'}.`,
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** Pattern for `.ts` files: camelCase name parts and a role suffix, e.g. `phoneNumbers.routes`. */
|
|
23
|
+
function buildTsFilePattern(options, defaultExempt) {
|
|
24
|
+
const roles = options.roles ?? ROLE_SUFFIXES;
|
|
25
|
+
const exemptFiles = options.exemptFiles ?? defaultExempt;
|
|
26
|
+
const exemptNames = exemptFiles.map((file) => file.replace(/\.ts$/, '')).join('|');
|
|
27
|
+
const namePattern = `${CAMEL_CASE_GLOB}*(.${CAMEL_CASE_GLOB}).@(${roles.join('|')})`;
|
|
28
|
+
return { roles, patterns: { [`**/!(${exemptNames}).ts`]: namePattern } };
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Backend file/folder naming rules (eslint-plugin-check-file):
|
|
32
|
+
* - `.ts` files: camelCase name parts and a role suffix, e.g. `phoneNumbers.routes.ts`
|
|
33
|
+
* - folders: camelCase
|
|
34
|
+
* Repos that use extra roles call it again with their own list, e.g.
|
|
35
|
+
* { files: ['**\/*.ts', '**\/*.tsx'], rules: buildFileNamingRules({ roles: [...ROLE_SUFFIXES, 'getters'] }) }
|
|
36
|
+
*/
|
|
37
|
+
export function buildFileNamingRules(options = {}) {
|
|
38
|
+
const { roles, patterns } = buildTsFilePattern(options, EXEMPT_FILE_NAMES);
|
|
39
|
+
const message = `File name '{{ target }}' must be camelCase plus a role suffix: <name>.<role>.ts (roles: ${roles.join(', ')}).`;
|
|
40
|
+
return buildCheckFileRules(patterns, 'CAMEL_CASE', options, message);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Frontend (React) file/folder naming rules:
|
|
44
|
+
* - `.ts` files: same as backend; React hook files (`useX.ts`) are exempt
|
|
45
|
+
* - `.tsx` files: PascalCase or camelCase parts, e.g. `CallCard.tsx`, `Login.screen.tsx`, `useAuth.tsx`
|
|
46
|
+
* - folders: camelCase or PascalCase (component folders)
|
|
47
|
+
*/
|
|
48
|
+
export function buildFrontendFileNamingRules(options = {}) {
|
|
49
|
+
const { roles, patterns } = buildTsFilePattern(options, FRONTEND_EXEMPT_FILE_NAMES);
|
|
50
|
+
const namePart = `@(${CAMEL_CASE_GLOB}|${PASCAL_CASE_GLOB})`;
|
|
51
|
+
const tsxPatterns = { ...patterns, '**/*.tsx': `${namePart}*(.${namePart})` };
|
|
52
|
+
const message = `File name '{{ target }}' breaks the naming rule. .ts: camelCase + role suffix (<name>.<role>.ts, roles: ${roles.join(', ')}). .tsx: PascalCase or camelCase parts.`;
|
|
53
|
+
return buildCheckFileRules(tsxPatterns, namePart, options, message);
|
|
54
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Runs a git command and returns its output as a list of lines (empty list on error). */
|
|
2
|
+
export declare function git(gitArgs: string[]): string[];
|
|
3
|
+
/**
|
|
4
|
+
* Lists files changed vs the base, relative to the current folder.
|
|
5
|
+
* `diffFilter` is git's filter: A = added, C = copied, M = modified, R = renamed.
|
|
6
|
+
* Locally it also includes uncommitted, staged and new untracked files; in CI only commits count.
|
|
7
|
+
*/
|
|
8
|
+
export declare function changedFiles(diffFilter: string): string[];
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { DEFAULT_BRANCHES } from './cli.constants.js';
|
|
3
|
+
/** Runs a git command and returns its output as a list of lines (empty list on error). */
|
|
4
|
+
export function git(gitArgs) {
|
|
5
|
+
try {
|
|
6
|
+
return execFileSync('git', gitArgs, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] })
|
|
7
|
+
.split('\n')
|
|
8
|
+
.map((line) => line.trim())
|
|
9
|
+
.filter(Boolean);
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return [];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Finds the commit to compare against: `LINT_BASE` in CI (the PR base), otherwise
|
|
17
|
+
* where this branch split from `origin/main` / `main`.
|
|
18
|
+
*/
|
|
19
|
+
function findBase() {
|
|
20
|
+
if (process.env.LINT_BASE)
|
|
21
|
+
return process.env.LINT_BASE;
|
|
22
|
+
for (const branch of DEFAULT_BRANCHES) {
|
|
23
|
+
const [base] = git(['merge-base', 'HEAD', branch]);
|
|
24
|
+
if (base)
|
|
25
|
+
return base;
|
|
26
|
+
}
|
|
27
|
+
return '';
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Lists files changed vs the base, relative to the current folder.
|
|
31
|
+
* `diffFilter` is git's filter: A = added, C = copied, M = modified, R = renamed.
|
|
32
|
+
* Locally it also includes uncommitted, staged and new untracked files; in CI only commits count.
|
|
33
|
+
*/
|
|
34
|
+
export function changedFiles(diffFilter) {
|
|
35
|
+
const base = findBase();
|
|
36
|
+
const diff = (extra) => git(['diff', '--name-only', '--relative', `--diff-filter=${diffFilter}`, ...extra]);
|
|
37
|
+
const committed = base ? diff([`${base}...HEAD`]) : [];
|
|
38
|
+
const local = process.env.CI
|
|
39
|
+
? []
|
|
40
|
+
: [...diff([]), ...diff(['--cached']), ...git(['ls-files', '--others', '--exclude-standard'])];
|
|
41
|
+
return [...new Set([...committed, ...local])];
|
|
42
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
export type LintSummary = {
|
|
3
|
+
errors: number;
|
|
4
|
+
warnings: number;
|
|
5
|
+
files: number;
|
|
6
|
+
};
|
|
7
|
+
export type LintLimits = {
|
|
8
|
+
FUNCTION_LINES: number;
|
|
9
|
+
FILE_LINES: number;
|
|
10
|
+
DATA_FILE_LINES: number;
|
|
11
|
+
NESTING_DEPTH: number;
|
|
12
|
+
FUNCTION_PARAMS: number;
|
|
13
|
+
COMPLEXITY: number;
|
|
14
|
+
INLINE_DATA_LINES: number;
|
|
15
|
+
TSX_FILE_LINES: number;
|
|
16
|
+
};
|
|
17
|
+
export type FileNamingOptions = {
|
|
18
|
+
roles?: string[];
|
|
19
|
+
exemptFiles?: string[];
|
|
20
|
+
ignoredFolders?: string[];
|
|
21
|
+
};
|
|
22
|
+
export type DataInDataFileOptions = {
|
|
23
|
+
maxLines?: number;
|
|
24
|
+
};
|
|
25
|
+
export type WrappedExpression = TSESTree.TSAsExpression | TSESTree.TSSatisfiesExpression | TSESTree.TSTypeAssertion | TSESTree.TSNonNullExpression;
|
|
26
|
+
export type DataSize = {
|
|
27
|
+
name: string;
|
|
28
|
+
lines: number;
|
|
29
|
+
};
|
|
30
|
+
export type Scope = TSESLint.Scope.Scope;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare const BACKEND_CONFIG: import("eslint").Linter.Config<import("@eslint/core", { with: { "resolution-mode": "require" } }).RulesConfig>[];
|
|
2
|
+
export declare const FRONTEND_CONFIG: import("eslint").Linter.Config<import("@eslint/core", { with: { "resolution-mode": "require" } }).RulesConfig>[];
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Linter } from 'eslint';
|
|
2
|
+
/** Backend set: base + camelCase naming, camelCase files/folders, no console at all. */
|
|
3
|
+
export declare function buildBackendConfig(): Linter.Config[];
|
|
4
|
+
/**
|
|
5
|
+
* Frontend (React) set: base + PascalCase components, camelCase/PascalCase files and folders,
|
|
6
|
+
* React hooks rules, console allowed, and for .tsx files: one exported component, 100 lines max.
|
|
7
|
+
*/
|
|
8
|
+
export declare function buildFrontendConfig(): Linter.Config[];
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import checkFile from 'eslint-plugin-check-file';
|
|
2
|
+
import reactHooks from 'eslint-plugin-react-hooks';
|
|
3
|
+
import tseslint from 'typescript-eslint';
|
|
4
|
+
import { talkpilotPlugin } from '../rules/index.js';
|
|
5
|
+
import { FRONTEND_IGNORED_PATHS, IGNORED_PATHS, LINTED_FILES, TSX_FILES, } from './defaults.constants.js';
|
|
6
|
+
import { buildFileNamingRules, buildFrontendFileNamingRules } from './fileNaming.utils.js';
|
|
7
|
+
import { BACKEND_RULES, DATA_FILE_RULES, FRONTEND_NAMING_CONVENTION, FRONTEND_RULES, LOGGING_RULES, NAMING_CONVENTION, SIZE_RULES, STRUCTURE_RULES, TSX_RULES, TYPESCRIPT_RULES, } from './ruleSets.constants.js';
|
|
8
|
+
/**
|
|
9
|
+
* Rules shared by all TypeScript code (backend and frontend):
|
|
10
|
+
* - lints *.ts / *.tsx only; tests, dist and *.d.ts are ignored (IGNORED_PATHS)
|
|
11
|
+
* - `noInlineConfig` turns off eslint-disable comments: exceptions go in the repo config instead
|
|
12
|
+
* - *.data.ts files get the bigger line limit
|
|
13
|
+
*/
|
|
14
|
+
function buildBaseConfig() {
|
|
15
|
+
return [
|
|
16
|
+
{ ignores: IGNORED_PATHS },
|
|
17
|
+
{
|
|
18
|
+
files: LINTED_FILES,
|
|
19
|
+
languageOptions: {
|
|
20
|
+
parser: tseslint.parser,
|
|
21
|
+
parserOptions: { projectService: true, tsconfigRootDir: process.cwd() },
|
|
22
|
+
},
|
|
23
|
+
linterOptions: { noInlineConfig: true },
|
|
24
|
+
plugins: {
|
|
25
|
+
'@typescript-eslint': tseslint.plugin,
|
|
26
|
+
'check-file': checkFile,
|
|
27
|
+
talkpilot: talkpilotPlugin,
|
|
28
|
+
},
|
|
29
|
+
rules: { ...SIZE_RULES, ...TYPESCRIPT_RULES, ...STRUCTURE_RULES, ...LOGGING_RULES },
|
|
30
|
+
},
|
|
31
|
+
{ files: ['**/*.data.ts'], rules: DATA_FILE_RULES },
|
|
32
|
+
];
|
|
33
|
+
}
|
|
34
|
+
/** Backend set: base + camelCase naming, camelCase files/folders, no console at all. */
|
|
35
|
+
export function buildBackendConfig() {
|
|
36
|
+
return [
|
|
37
|
+
...buildBaseConfig(),
|
|
38
|
+
{
|
|
39
|
+
files: LINTED_FILES,
|
|
40
|
+
rules: {
|
|
41
|
+
'@typescript-eslint/naming-convention': ['error', ...NAMING_CONVENTION],
|
|
42
|
+
...buildFileNamingRules(),
|
|
43
|
+
...BACKEND_RULES,
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
];
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Frontend (React) set: base + PascalCase components, camelCase/PascalCase files and folders,
|
|
50
|
+
* React hooks rules, console allowed, and for .tsx files: one exported component, 100 lines max.
|
|
51
|
+
*/
|
|
52
|
+
export function buildFrontendConfig() {
|
|
53
|
+
return [
|
|
54
|
+
{ ignores: FRONTEND_IGNORED_PATHS },
|
|
55
|
+
...buildBaseConfig(),
|
|
56
|
+
{
|
|
57
|
+
files: LINTED_FILES,
|
|
58
|
+
plugins: { 'react-hooks': reactHooks },
|
|
59
|
+
rules: {
|
|
60
|
+
'@typescript-eslint/naming-convention': ['error', ...FRONTEND_NAMING_CONVENTION],
|
|
61
|
+
...buildFrontendFileNamingRules(),
|
|
62
|
+
...FRONTEND_RULES,
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
{ files: TSX_FILES, rules: TSX_RULES },
|
|
66
|
+
];
|
|
67
|
+
}
|