-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: standardize debug namespaces to file paths (#10599)
* feat: standardize debug namespaces to file paths * test: just a bit more * Replace unseemly regex with simpler packages split
- Loading branch information
1 parent
a175189
commit 49a94b3
Showing
14 changed files
with
167 additions
and
10 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
65 changes: 65 additions & 0 deletions
65
packages/eslint-plugin-internal/src/rules/debug-namespace.ts
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,65 @@ | ||
import type { TSESTree } from '@typescript-eslint/utils'; | ||
|
||
import { getStaticValue } from '@typescript-eslint/utils/ast-utils'; | ||
|
||
import { createRule } from '../util'; | ||
|
||
function filePathToNamespace(filePath: string) { | ||
const relativePath = filePath | ||
.split(/packages[\\/]+/) | ||
.slice(1) | ||
.join(''); | ||
|
||
const relativeNamespace = relativePath | ||
.replace(/^[\\/]/, '') | ||
.replace(/(?:dist|lib|src)\//, '') | ||
.replace(/\.\w+$/, '') | ||
.replaceAll(/[^a-z0-9-]+/gi, ':'); | ||
|
||
return `typescript-eslint:${relativeNamespace}`; | ||
} | ||
|
||
export default createRule({ | ||
name: __filename, | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: | ||
'Enforce consistent debug() namespaces based on package name and file path', | ||
}, | ||
fixable: 'code', | ||
messages: { | ||
mismatched: | ||
'debug() namespace should match package and file. Use the fixer to update it.', | ||
}, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const expected = filePathToNamespace(context.filename); | ||
if (!expected) { | ||
return {}; | ||
} | ||
|
||
return { | ||
'VariableDeclarator[id.name="log"] > CallExpression[arguments.length=1][callee.name="debug"]'( | ||
node: TSESTree.CallExpression, | ||
) { | ||
const [argument] = node.arguments; | ||
const staticValue = getStaticValue(argument); | ||
if ( | ||
typeof staticValue?.value !== 'string' || | ||
staticValue.value === expected | ||
) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node: argument, | ||
messageId: 'mismatched', | ||
fix: fixer => fixer.replaceText(argument, `'${expected}'`), | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
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
75 changes: 75 additions & 0 deletions
75
packages/eslint-plugin-internal/tests/rules/debug-namespace.test.ts
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,75 @@ | ||
import { RuleTester } from '@typescript-eslint/rule-tester'; | ||
|
||
import rule from '../../src/rules/debug-namespace'; | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('debug-namespace', rule, { | ||
invalid: [ | ||
{ | ||
code: "const log = debug('not:correct');", | ||
errors: [ | ||
{ | ||
column: 19, | ||
endColumn: 32, | ||
line: 1, | ||
messageId: 'mismatched', | ||
}, | ||
], | ||
filename: 'typescript-eslint/packages/example/file.ts', | ||
output: "const log = debug('typescript-eslint:example:file');", | ||
}, | ||
{ | ||
code: "const log = debug('not:correct');", | ||
errors: [ | ||
{ | ||
column: 19, | ||
endColumn: 32, | ||
line: 1, | ||
messageId: 'mismatched', | ||
}, | ||
], | ||
filename: '/Users/example/typescript-eslint/packages/example/file.ts', | ||
output: "const log = debug('typescript-eslint:example:file');", | ||
}, | ||
{ | ||
code: "const log = debug('not:correct');", | ||
errors: [ | ||
{ | ||
column: 19, | ||
endColumn: 32, | ||
line: 1, | ||
messageId: 'mismatched', | ||
}, | ||
], | ||
filename: 'C:\\Code\\typescript-eslint\\packages\\example\\file.ts', | ||
output: "const log = debug('typescript-eslint:example:file');", | ||
}, | ||
], | ||
valid: [ | ||
{ | ||
code: "const log = debug('typescript-eslint:example:file');", | ||
filename: 'typescript-eslint/packages/example/file.ts', | ||
}, | ||
{ | ||
code: "const logCustom = debug('typescript-eslint:example:file');", | ||
filename: 'typescript-eslint/packages/example/file.ts', | ||
}, | ||
{ | ||
code: "const logCustom = debug('...');", | ||
filename: 'typescript-eslint/packages/example/file.ts', | ||
}, | ||
{ | ||
code: "debug('...');", | ||
filename: 'typescript-eslint/packages/example/file.ts', | ||
}, | ||
{ | ||
code: 'const log = debug(null);', | ||
filename: 'typescript-eslint/packages/example/file.ts', | ||
}, | ||
{ | ||
code: 'const log = debug(123);', | ||
filename: 'typescript-eslint/packages/example/file.ts', | ||
}, | ||
], | ||
}); |
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
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
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
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
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