-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #450 from ryan-williams-nourish/feature/ignore-rul…
…es-for-file Feature/ignore rules for file
- Loading branch information
Showing
7 changed files
with
92 additions
and
3 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type minimatch from 'minimatch' | ||
import { describe, expect, it, vi } from 'vitest' | ||
import { checkFileIgnoreRules } from './setupFileIgnoreList' | ||
|
||
// Use `importActual` to partially mock `minimatch` | ||
vi.mock('minimatch', async () => { | ||
const actual = await vi.importActual<typeof minimatch>('minimatch') | ||
return { | ||
...actual, | ||
default: (filePath: string, pattern: string) => filePath === pattern, | ||
} | ||
}) | ||
|
||
describe('checkFileIgnoreRules', () => { | ||
it('should return apply unchanged if no matching rules in fileIgnoreRules (Scenario 1)', () => { | ||
const filePath = 'test/file/path' | ||
const fileIgnoreRules = {} // No ignore rules | ||
const _apply = ['rule1', 'rule3'] | ||
const result = checkFileIgnoreRules(filePath, fileIgnoreRules, _apply) | ||
expect(result).toEqual(_apply) // Should return unchanged | ||
}) | ||
|
||
it('should remove matching rules from apply if they exist in fileIgnoreRules (Scenario 2)', () => { | ||
const filePath = 'test/file/path' | ||
const fileIgnoreRules = { | ||
'test/file/path': 'rule1, rule3', // Matching rules | ||
} | ||
const _apply = ['rule1', 'rule2', 'rule3'] | ||
const result = checkFileIgnoreRules(filePath, fileIgnoreRules, _apply) | ||
expect(result).toEqual(['rule2']) // Should remove 'rule1' and 'rule3' | ||
}) | ||
|
||
it('should remove rules from apply if fileIgnoreRules has rulesets containing those rules (Scenario 3)', () => { | ||
const filePath = 'test/file/path' | ||
const fileIgnoreRules = { | ||
'test/file/path': 'vue-caution', // ruleSet1 contains rule1 and rule2 | ||
} | ||
const _apply = ['rule1', 'rule2', 'elementSelectorsWithScoped', 'implicitParentChildCommunication'] | ||
const result = checkFileIgnoreRules(filePath, fileIgnoreRules, _apply) | ||
expect(result).toEqual(['rule1', 'rule2']) // Should remove 'elementSelectorsWithScoped' and 'implicitParentChildCommunication' | ||
}) | ||
}) |
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,28 @@ | ||
import type { RuleSetType } from '../rules/rules' | ||
import { minimatch } from 'minimatch' | ||
import { RULES, RULESETS } from '../rules/rules' | ||
|
||
export const checkFileIgnoreRules = (filePath: string, fileIgnoreRules: { [key: string]: string }, _apply: string[]) => { | ||
let apply = [..._apply] // Here we get a list of rules only - no rule sets | ||
|
||
for (const [pattern, rules] of Object.entries(fileIgnoreRules)) { | ||
if (minimatch(filePath, pattern, { matchBase: true })) { | ||
const ignoreRules = rules.split(',').map(rule => rule.trim()) | ||
// Expand ignoreRules by replacing rulesets with individual rules | ||
const expandedIgnoreRules = ignoreRules.flatMap((rule) => { | ||
if (RULESETS.includes(rule as RuleSetType)) { | ||
return RULES[rule as RuleSetType] | ||
} | ||
else { | ||
return rule | ||
} | ||
}) | ||
|
||
// Remove duplicates by converting to a Set and back to an array | ||
const uniqueIgnoreRules = Array.from(new Set(expandedIgnoreRules)) | ||
apply = apply.filter(rule => !uniqueIgnoreRules.includes(rule)) | ||
} | ||
} | ||
|
||
return apply | ||
} |
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