镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit 381fc1a

Browse files
committed
chore: wip
chore: wip
1 parent f99a37a commit 381fc1a

8 files changed

Lines changed: 215 additions & 48 deletions

File tree

File renamed without changes.

‎.config/pickier.config.json‎

Lines changed: 0 additions & 39 deletions
This file was deleted.

‎packages/pickier/src/linter.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,9 @@ function parseDisableDirectives(content: string): DisableDirectives {
307307
// Match disable-next-line comments
308308
const nextLineMatch = t.match(/^\/\/\s*(?:eslint|pickier)-disable-next-line\s+(\S.*)$/)
309309
if (nextLineMatch) {
310-
const list = nextLineMatch[1].split(',').map(s => s.trim()).filter(Boolean)
310+
// Strip trailing -- comments (e.g., "rule-name -- explanation")
311+
const ruleText = nextLineMatch[1].replace(/\s+--\s.*$/, '')
312+
const list = ruleText.split(',').map(s => s.trim()).filter(Boolean)
311313
if (list.length > 0) {
312314
const target = lineNo + 1 // next line (1-indexed)
313315
const set = nextLine.get(target) || new Set<string>()
@@ -321,7 +323,7 @@ function parseDisableDirectives(content: string): DisableDirectives {
321323
// eslint-disable-next-line regexp/no-super-linear-backtracking
322324
const blockDisableMatch = t.match(/^\/\*\s*(?:eslint|pickier)-disable(?:\s+([^*]+))?\s*\*\//)
323325
if (blockDisableMatch) {
324-
const ruleList = blockDisableMatch[1]?.trim()
326+
const ruleList = blockDisableMatch[1]?.trim().replace(/\s+--\s.*$/, '')
325327
if (!ruleList || ruleList === '') {
326328
// Disable all rules for the entire file from this line onwards
327329
rangeDisable.set(lineNo, new Set(['*']))
@@ -376,7 +378,7 @@ function parseDisableDirectives(content: string): DisableDirectives {
376378
// Match inline comment disable directives (// eslint-disable or // pickier-disable)
377379
const inlineDisableMatch = t.match(/^\/\/\s*(?:eslint|pickier)-disable(?:\s+(\S.*))?$/)
378380
if (inlineDisableMatch) {
379-
const ruleList = inlineDisableMatch[1]?.trim()
381+
const ruleList = inlineDisableMatch[1]?.trim().replace(/\s+--\s.*$/, '')
380382
if (!ruleList || ruleList === '') {
381383
rangeDisable.set(lineNo, new Set(['*']))
382384
currentlyDisabled.add('*')

‎packages/pickier/src/rules/general/no-unused-vars.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,9 @@ export const noUnusedVarsRule: RuleModule = {
486486
else if (ch === '`') {
487487
inString = 'template'
488488
}
489-
else if (ch === '/' && i > 0) {
490-
const before = str.slice(0, i).trimEnd()
491-
if (/[=([{,:;!&|?]$/.test(before) || before.endsWith('return')) {
489+
else if (ch === '/') {
490+
const before = i > 0 ? str.slice(0, i).trimEnd() : ''
491+
if (!before || /[=([{,:;!&|?]$/.test(before) || before.endsWith('return')) {
492492
// This is a regex - skip it
493493
i++ // skip opening /
494494
while (i < str.length) {
@@ -767,9 +767,9 @@ export const noUnusedVarsRule: RuleModule = {
767767
let result = ''
768768
let i = 0
769769
while (i < str.length) {
770-
if (str[i] === '/' && i > 0) {
771-
const before = str.slice(0, i).trimEnd()
772-
if (/[=([{,:;!&|?]$/.test(before) || before.endsWith('return')) {
770+
if (str[i] === '/') {
771+
const before = i > 0 ? str.slice(0, i).trimEnd() : ''
772+
if (!before || /[=([{,:;!&|?]$/.test(before) || before.endsWith('return')) {
773773
i++ // skip opening /
774774
while (i < str.length) {
775775
if (str[i] === '\\') {
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { describe, expect, it } from 'bun:test'
2+
import { mkdtempSync, writeFileSync } from 'node:fs'
3+
import { tmpdir } from 'node:os'
4+
import { join } from 'node:path'
5+
import { runLint } from '../../src/linter'
6+
7+
function tmp(): string {
8+
return mkdtempSync(join(tmpdir(), 'pickier-lint-disable-trailing-'))
9+
}
10+
11+
describe('disable directives with trailing -- comments', () => {
12+
it('eslint-disable-next-line with -- explanation suppresses rule', async () => {
13+
const dir = tmp()
14+
const file = 'a.ts'
15+
const src = [
16+
'// eslint-disable-next-line no-console -- needed for debugging',
17+
'console.log(1)',
18+
'',
19+
].join('\n')
20+
writeFileSync(join(dir, file), src, 'utf8')
21+
22+
const code = await runLint([dir], { reporter: 'json' })
23+
expect(code).toBe(0)
24+
})
25+
26+
it('pickier-disable-next-line with -- explanation suppresses rule', async () => {
27+
const dir = tmp()
28+
const file = 'b.ts'
29+
const src = [
30+
'// pickier-disable-next-line no-console -- this is intentional logging',
31+
'console.log(1)',
32+
'',
33+
].join('\n')
34+
writeFileSync(join(dir, file), src, 'utf8')
35+
36+
const code = await runLint([dir], { reporter: 'json' })
37+
expect(code).toBe(0)
38+
})
39+
40+
it('disable-next-line with -- and plugin-prefixed rule suppresses', async () => {
41+
const dir = tmp()
42+
const file = 'c.ts'
43+
const src = [
44+
'// eslint-disable-next-line pickier/no-unused-vars -- false positive in regex',
45+
'const _patterns = [',
46+
' /test/g,',
47+
']',
48+
'',
49+
].join('\n')
50+
writeFileSync(join(dir, file), src, 'utf8')
51+
52+
const cfgPath = join(dir, 'pickier.config.json')
53+
writeFileSync(cfgPath, JSON.stringify({
54+
verbose: false,
55+
ignores: [],
56+
lint: { extensions: ['ts'], reporter: 'json', cache: false, maxWarnings: -1 },
57+
format: { extensions: ['ts'], trimTrailingWhitespace: true, maxConsecutiveBlankLines: 1, finalNewline: 'one', indent: 2, quotes: 'single', semi: false },
58+
rules: { noDebugger: 'off', noConsole: 'off' },
59+
pluginRules: { 'pickier/no-unused-vars': 'error' },
60+
}, null, 2), 'utf8')
61+
62+
const code = await runLint([dir], { config: cfgPath, reporter: 'json' })
63+
expect(code).toBe(0)
64+
})
65+
66+
it('disable-next-line with comma-separated rules and -- comment', async () => {
67+
const dir = tmp()
68+
const file = 'd.ts'
69+
const src = [
70+
'// eslint-disable-next-line no-console, no-debugger -- both needed here',
71+
'console.log(1)',
72+
'',
73+
].join('\n')
74+
writeFileSync(join(dir, file), src, 'utf8')
75+
76+
const code = await runLint([dir], { reporter: 'json' })
77+
expect(code).toBe(0)
78+
})
79+
80+
it('block disable with -- comment suppresses rule', async () => {
81+
const dir = tmp()
82+
const file = 'e.ts'
83+
const src = [
84+
'/* eslint-disable no-console -- logging section */',
85+
'console.log(1)',
86+
'console.log(2)',
87+
'/* eslint-enable no-console */',
88+
'',
89+
].join('\n')
90+
writeFileSync(join(dir, file), src, 'utf8')
91+
92+
const code = await runLint([dir], { reporter: 'json' })
93+
expect(code).toBe(0)
94+
})
95+
96+
it('inline disable with -- comment suppresses rule', async () => {
97+
const dir = tmp()
98+
const file = 'f.ts'
99+
const src = [
100+
'// eslint-disable no-console -- entire file needs console',
101+
'console.log(1)',
102+
'console.log(2)',
103+
'',
104+
].join('\n')
105+
writeFileSync(join(dir, file), src, 'utf8')
106+
107+
const code = await runLint([dir], { reporter: 'json' })
108+
expect(code).toBe(0)
109+
})
110+
})
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { describe, expect, it } from 'bun:test'
2+
import { noUnusedVarsRule } from '../../../src/rules/general/no-unused-vars'
3+
4+
const defaultCtx = {
5+
filePath: 'test.ts',
6+
config: {} as any,
7+
options: {},
8+
}
9+
10+
describe('no-unused-vars: regex literals containing function keyword', () => {
11+
it('should not flag character classes inside regex as unused params', () => {
12+
// Regex like /function\s+([a-zA-Z_$][\w$]*)/g contains identifiers
13+
// (a, zA, Z_$, w$) inside character classes — these are NOT function parameters
14+
const src = [
15+
'export function removeLocallyDefined(strippedCode: string, usedNames: Set<string>): void {',
16+
' const patterns = [',
17+
' /(?:export\\s+)?(?:async\\s+)?function\\s+([a-zA-Z_$][\\w$]*)/g,',
18+
' /(?:export\\s+)?(?:const|let|var)\\s+([a-zA-Z_$][\\w$]*)/g,',
19+
' /(?:export\\s+)?class\\s+([a-zA-Z_$][\\w$]*)/g,',
20+
' ]',
21+
'',
22+
' for (const pattern of patterns) {',
23+
' let match',
24+
' while ((match = pattern.exec(strippedCode)) !== null) {',
25+
' if (match[1]) usedNames.delete(match[1])',
26+
' }',
27+
' }',
28+
'}',
29+
'',
30+
].join('\n')
31+
32+
const issues = noUnusedVarsRule.check(src, defaultCtx)
33+
// Should NOT report a, zA, Z_$, w$ as unused function parameters
34+
const falsePositives = issues.filter(i =>
35+
['a', 'zA', 'Z_$', 'w$'].includes(i.message.match(/'(\w+\$?)'/)?.[1] ?? ''),
36+
)
37+
expect(falsePositives).toHaveLength(0)
38+
})
39+
40+
it('should not flag regex at start of line in array as function declaration', () => {
41+
// When a regex starts with only whitespace before it (common in arrays),
42+
// stripRegex must still detect it as a regex literal
43+
const src = [
44+
'const _patterns = [',
45+
' /function\\s+(\\w+)/g,',
46+
' /class\\s+(\\w+)/g,',
47+
']',
48+
'',
49+
].join('\n')
50+
51+
const issues = noUnusedVarsRule.check(src, defaultCtx)
52+
const funcParamIssues = issues.filter(i => i.message.includes('function parameter'))
53+
expect(funcParamIssues).toHaveLength(0)
54+
})
55+
56+
it('should not flag regex assigned after equals as function declaration', () => {
57+
const src = [
58+
'const _re = /function\\s+([a-zA-Z_$][\\w$]*)/g',
59+
'',
60+
].join('\n')
61+
62+
const issues = noUnusedVarsRule.check(src, defaultCtx)
63+
const funcParamIssues = issues.filter(i => i.message.includes('function parameter'))
64+
expect(funcParamIssues).toHaveLength(0)
65+
})
66+
67+
it('should still detect real unused function parameters', () => {
68+
const src = [
69+
'function process(used: string, unused: number): string {',
70+
' return used',
71+
'}',
72+
'',
73+
].join('\n')
74+
75+
const issues = noUnusedVarsRule.check(src, defaultCtx)
76+
const unusedParam = issues.filter(i =>
77+
i.message.includes('\'unused\'') && i.message.includes('function parameter'),
78+
)
79+
expect(unusedParam).toHaveLength(1)
80+
})
81+
82+
it('should not flag regex at very start of line (position 0)', () => {
83+
// Edge case: regex literal is the very first character on the line
84+
const src = [
85+
'const _results = []',
86+
'/function\\s+(\\w+)/g.exec("function foo")',
87+
'',
88+
].join('\n')
89+
90+
const issues = noUnusedVarsRule.check(src, defaultCtx)
91+
const funcParamIssues = issues.filter(i => i.message.includes('function parameter'))
92+
expect(funcParamIssues).toHaveLength(0)
93+
})
94+
})

0 commit comments

Comments
 (0)