Skip to content

Commit 9e35a04

Browse files
authored
feat: add support for css,json,markdown parsers (#55)
* feat(processor): enable autofix support (cherry picked from commit f36aa23) * feat(rules): extend rules to process root nodes for different AST types (cherry picked from commit 050ca31) * docs: update README.md to clarify processor usage
1 parent eb30d9e commit 9e35a04

File tree

7 files changed

+287
-258
lines changed

7 files changed

+287
-258
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@ This plugin supports ESLint's [flat configuration](https://eslint.org/docs/lates
3333
import checkFile from 'eslint-plugin-check-file';
3434

3535
export default [
36+
{
37+
// optional: add this processor to files which not processed by other processors
38+
files: ['**/*.yaml', '**/*.webp', '**/.gitignore'],
39+
processor: 'check-file/eslint-processor-check-file',
40+
},
3641
{
3742
files: ['src/**/*.*'],
3843
plugins: {
3944
'check-file': checkFile,
4045
},
41-
// optional: add this processor if you want to lint non-js/ts files (images, styles, etc.)
42-
processor: 'check-file/eslint-processor-check-file',
4346
rules: {
4447
'check-file/no-index': 'error',
4548
'check-file/filename-blocklist': [

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const plugin = {
3636
postprocess(messages) {
3737
return [].concat(...messages);
3838
},
39+
supportsAutofix: true,
3940
},
4041
},
4142
};

lib/rules/filename-blocklist.js

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -51,59 +51,64 @@ export default {
5151
},
5252

5353
create(context) {
54-
return {
55-
Program: (node) => {
56-
const rules = context.options[0];
57-
const errorMessage = context.options[1]?.errorMessage ?? '';
58-
const error = validateNamingPatternObject(
59-
rules,
60-
globPatternValidator,
61-
isEmpty(errorMessage) ? globPatternValidator : () => true
62-
);
54+
const rule = (node) => {
55+
const rules = context.options[0];
56+
const errorMessage = context.options[1]?.errorMessage ?? '';
57+
const error = validateNamingPatternObject(
58+
rules,
59+
globPatternValidator,
60+
isEmpty(errorMessage) ? globPatternValidator : () => true
61+
);
6362

64-
if (error) {
65-
context.report({
66-
node,
67-
messageId: error.type,
68-
data: {
69-
value: error.payload,
70-
},
71-
});
72-
return;
73-
}
63+
if (error) {
64+
context.report({
65+
node,
66+
messageId: error.type,
67+
data: {
68+
value: error.payload,
69+
},
70+
});
71+
return;
72+
}
7473

75-
const filenameWithPath = getFilePath(context);
76-
const filename = getFilename(filenameWithPath);
74+
const filenameWithPath = getFilePath(context);
75+
const filename = getFilename(filenameWithPath);
7776

78-
for (const [blockListPattern, useInsteadPattern] of Object.entries(
79-
rules
80-
)) {
81-
const matchResult = matchRule(filenameWithPath, blockListPattern);
77+
for (const [blockListPattern, useInsteadPattern] of Object.entries(
78+
rules
79+
)) {
80+
const matchResult = matchRule(filenameWithPath, blockListPattern);
8281

83-
if (matchResult) {
84-
isNotEmpty(errorMessage)
85-
? context.report({
86-
node,
87-
// eslint-disable-next-line eslint-plugin/prefer-message-ids
88-
message: errorMessage,
89-
data: {
90-
target: filename,
91-
pattern: blockListPattern,
92-
},
93-
})
94-
: context.report({
95-
node,
96-
messageId: 'noMatch',
97-
data: {
98-
filename,
99-
blockListPattern,
100-
suggestion: useInsteadPattern,
101-
},
102-
});
103-
return;
104-
}
82+
if (matchResult) {
83+
isNotEmpty(errorMessage)
84+
? context.report({
85+
node,
86+
// eslint-disable-next-line eslint-plugin/prefer-message-ids
87+
message: errorMessage,
88+
data: {
89+
target: filename,
90+
pattern: blockListPattern,
91+
},
92+
})
93+
: context.report({
94+
node,
95+
messageId: 'noMatch',
96+
data: {
97+
filename,
98+
blockListPattern,
99+
suggestion: useInsteadPattern,
100+
},
101+
});
102+
return;
105103
}
106-
},
104+
}
105+
};
106+
107+
return {
108+
Document: rule,
109+
Program: rule,
110+
root: rule,
111+
StyleSheet: rule,
107112
};
108113
},
109114
};

lib/rules/filename-naming-convention.js

Lines changed: 72 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -59,80 +59,85 @@ export default {
5959
},
6060

6161
create(context) {
62-
return {
63-
Program: (node) => {
64-
const rules = context.options[0];
65-
const error = validateNamingPatternObject(
66-
rules,
67-
globPatternValidator,
68-
(p) =>
69-
filenameNamingPatternValidator(p) ||
70-
nextJsFilenameNamingPatternValidator(p)
71-
);
72-
73-
if (error) {
74-
context.report({
75-
node,
76-
messageId: error.type,
77-
data: {
78-
value: error.payload,
79-
},
80-
});
81-
return;
82-
}
62+
const rule = (node) => {
63+
const rules = context.options[0];
64+
const error = validateNamingPatternObject(
65+
rules,
66+
globPatternValidator,
67+
(p) =>
68+
filenameNamingPatternValidator(p) ||
69+
nextJsFilenameNamingPatternValidator(p)
70+
);
8371

84-
const filenameWithPath = getFilePath(context);
85-
const filename = getFilename(filenameWithPath);
86-
const ignoreMiddleExtensions =
87-
context.options[1]?.ignoreMiddleExtensions ?? false;
88-
const errorMessage = context.options[1]?.errorMessage ?? '';
72+
if (error) {
73+
context.report({
74+
node,
75+
messageId: error.type,
76+
data: {
77+
value: error.payload,
78+
},
79+
});
80+
return;
81+
}
8982

90-
for (const [
91-
originalFilenamePattern,
92-
originalNamingPattern,
93-
] of Object.entries(rules)) {
94-
try {
95-
const [filenamePattern, namingPattern] =
96-
transformRuleWithPrefinedMatchSyntax(
97-
[originalFilenamePattern, originalNamingPattern],
98-
filenameWithPath
99-
);
83+
const filenameWithPath = getFilePath(context);
84+
const filename = getFilename(filenameWithPath);
85+
const ignoreMiddleExtensions =
86+
context.options[1]?.ignoreMiddleExtensions ?? false;
87+
const errorMessage = context.options[1]?.errorMessage ?? '';
10088

101-
const matchResult = matchRule(
102-
filenameWithPath,
103-
filenamePattern,
104-
getBasename(filename, ignoreMiddleExtensions),
105-
namingPattern
89+
for (const [
90+
originalFilenamePattern,
91+
originalNamingPattern,
92+
] of Object.entries(rules)) {
93+
try {
94+
const [filenamePattern, namingPattern] =
95+
transformRuleWithPrefinedMatchSyntax(
96+
[originalFilenamePattern, originalNamingPattern],
97+
filenameWithPath
10698
);
10799

108-
if (matchResult) {
109-
throw {
110-
type: 'noMatch',
111-
payload: {
112-
filename,
113-
originalNamingPattern,
114-
},
115-
};
116-
}
117-
} catch (error) {
118-
isNotEmpty(errorMessage) && error.type === 'noMatch'
119-
? context.report({
120-
node,
121-
// eslint-disable-next-line eslint-plugin/prefer-message-ids
122-
message: errorMessage,
123-
data: {
124-
target: error.payload.filename,
125-
pattern: error.payload.originalNamingPattern,
126-
},
127-
})
128-
: context.report({
129-
node,
130-
messageId: error.type,
131-
data: error.payload,
132-
});
100+
const matchResult = matchRule(
101+
filenameWithPath,
102+
filenamePattern,
103+
getBasename(filename, ignoreMiddleExtensions),
104+
namingPattern
105+
);
106+
107+
if (matchResult) {
108+
throw {
109+
type: 'noMatch',
110+
payload: {
111+
filename,
112+
originalNamingPattern,
113+
},
114+
};
133115
}
116+
} catch (error) {
117+
isNotEmpty(errorMessage) && error.type === 'noMatch'
118+
? context.report({
119+
node,
120+
// eslint-disable-next-line eslint-plugin/prefer-message-ids
121+
message: errorMessage,
122+
data: {
123+
target: error.payload.filename,
124+
pattern: error.payload.originalNamingPattern,
125+
},
126+
})
127+
: context.report({
128+
node,
129+
messageId: error.type,
130+
data: error.payload,
131+
});
134132
}
135-
},
133+
}
134+
};
135+
136+
return {
137+
Document: rule,
138+
Program: rule,
139+
root: rule,
140+
StyleSheet: rule,
136141
};
137142
},
138143
};

0 commit comments

Comments
 (0)