Skip to content

Commit 18e561e

Browse files
committed
feat: report an error when naming pattern object isnt a object type
1 parent bcd6664 commit 18e561e

14 files changed

+206
-48
lines changed

lib/rules/filename-blocklist.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
/**
22
* @file The filename should be blocklisted.
3-
* @author Florian Ehmke
3+
* @author Florian Ehmke, Duke Luo
44
*/
55
'use strict';
66

77
const { getFilename, getFilePath } = require('../utils/filename');
8-
const { checkSettings, globPatternValidator } = require('../utils/settings');
8+
const {
9+
validateNamingPatternObject,
10+
globPatternValidator,
11+
} = require('../utils/settings');
912
const { getDocUrl } = require('../utils/doc');
1013
const { matchRule } = require('../utils/match');
1114

@@ -36,21 +39,16 @@ module.exports = {
3639
return {
3740
Program: (node) => {
3841
const rules = context.options[0];
39-
40-
const invalidPattern = checkSettings(
42+
const message = validateNamingPatternObject(
4143
rules,
4244
globPatternValidator,
4345
globPatternValidator
4446
);
4547

46-
if (invalidPattern) {
48+
if (message) {
4749
context.report({
4850
node,
49-
message:
50-
'There is an invalid pattern "{{invalidPattern}}", please check it',
51-
data: {
52-
invalidPattern,
53-
},
51+
message,
5452
});
5553
return;
5654
}

lib/rules/filename-naming-convention.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
const { transformRuleWithPrefinedMatchSyntax } = require('../utils/transform');
88
const { getFilename, getBasename, getFilePath } = require('../utils/filename');
99
const {
10-
checkSettings,
10+
validateNamingPatternObject,
1111
filenameNamingPatternValidator,
1212
globPatternValidator,
1313
} = require('../utils/settings');
@@ -46,30 +46,25 @@ module.exports = {
4646
create(context) {
4747
return {
4848
Program: (node) => {
49-
const filenameWithPath = getFilePath(context);
50-
const filename = getFilename(filenameWithPath);
51-
52-
const rules = context.options[0] || {};
53-
const { ignoreMiddleExtensions } = context.options[1] || {};
54-
55-
const invalidPattern = checkSettings(
49+
const rules = context.options[0];
50+
const message = validateNamingPatternObject(
5651
rules,
5752
globPatternValidator,
5853
filenameNamingPatternValidator
5954
);
6055

61-
if (invalidPattern) {
56+
if (message) {
6257
context.report({
6358
node,
64-
message:
65-
'There is an invalid pattern "{{invalidPattern}}", please check it',
66-
data: {
67-
invalidPattern,
68-
},
59+
message,
6960
});
7061
return;
7162
}
7263

64+
const filenameWithPath = getFilePath(context);
65+
const filename = getFilename(filenameWithPath);
66+
const { ignoreMiddleExtensions } = context.options[1] || {};
67+
7368
for (const [
7469
originalFilenamePattern,
7570
originalNamingPattern,

lib/rules/folder-match-with-fex.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ const {
99
getFilename,
1010
getFilePath,
1111
} = require('../utils/filename');
12-
const { checkSettings, globPatternValidator } = require('../utils/settings');
12+
const {
13+
validateNamingPatternObject,
14+
globPatternValidator,
15+
} = require('../utils/settings');
1316
const { getDocUrl } = require('../utils/doc');
1417
const { matchRule } = require('../utils/match');
1518

@@ -41,21 +44,16 @@ module.exports = {
4144
return {
4245
Program: (node) => {
4346
const rules = context.options[0];
44-
45-
const invalidPattern = checkSettings(
47+
const message = validateNamingPatternObject(
4648
rules,
4749
globPatternValidator,
4850
globPatternValidator
4951
);
5052

51-
if (invalidPattern) {
53+
if (message) {
5254
context.report({
5355
node,
54-
message:
55-
'There is an invalid pattern "{{invalidPattern}}", please check it',
56-
data: {
57-
invalidPattern,
58-
},
56+
message,
5957
});
6058
return;
6159
}

lib/rules/folder-naming-convention.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const {
1212
getFilePath,
1313
} = require('../utils/filename');
1414
const {
15-
checkSettings,
15+
validateNamingPatternObject,
1616
namingPatternValidator,
1717
globPatternValidator,
1818
} = require('../utils/settings');
@@ -46,20 +46,16 @@ module.exports = {
4646
return {
4747
Program: (node) => {
4848
const rules = context.options[0];
49-
const invalidPattern = checkSettings(
49+
const message = validateNamingPatternObject(
5050
rules,
5151
globPatternValidator,
5252
namingPatternValidator
5353
);
5454

55-
if (invalidPattern) {
55+
if (message) {
5656
context.report({
5757
node,
58-
message:
59-
'There is an invalid pattern "{{invalidPattern}}", please check it',
60-
data: {
61-
invalidPattern,
62-
},
58+
message,
6359
});
6460
return;
6561
}

lib/utils/settings.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
const isGlob = require('is-glob');
88
const NAMING_CONVENTION = require('../constants/naming-convention');
99
const { PREFINED_MATCH_SYNTAX_REGEXP } = require('../constants/regex');
10+
const { isObject } = require('./utility');
1011

1112
/**
1213
* Validator
@@ -15,17 +16,20 @@ const { PREFINED_MATCH_SYNTAX_REGEXP } = require('../constants/regex');
1516
* @param {string} p pattern string
1617
*/
1718
/**
18-
* @returns {string | undefined} undefined or invalid field
19-
* @param {object} settings rule settings configurated by user
19+
* @returns {string | undefined} undefined or error message
20+
* @param {any} config naming pattern object configurated by user
2021
* @param {validator} keyValidator settings key validator
2122
* @param {validator} valueValidator settings value validator
2223
*/
23-
const checkSettings = (settings, keyValidator, valueValidator) => {
24-
for (const [key, value] of Object.entries(settings)) {
24+
const validateNamingPatternObject = (config, keyValidator, valueValidator) => {
25+
if (!isObject(config)) {
26+
return `The naming pattern object "${config}" doesn't appear to be an Object type, please double-check it and try again`;
27+
}
28+
for (const [key, value] of Object.entries(config)) {
2529
if (!keyValidator(key)) {
26-
return key;
30+
return `There is an invalid pattern "${key}", please check it`;
2731
} else if (!valueValidator(value)) {
28-
return value;
32+
return `There is an invalid pattern "${value}", please check it`;
2933
}
3034
}
3135
};
@@ -58,7 +62,7 @@ const filenameNamingPatternValidator = (namingPattern) => {
5862
const globPatternValidator = isGlob;
5963

6064
module.exports = {
61-
checkSettings,
65+
validateNamingPatternObject,
6266
namingPatternValidator,
6367
filenameNamingPatternValidator,
6468
globPatternValidator,

lib/utils/utility.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @file A utility file containing useful functions
3+
* @author Duke Luo
4+
*/
5+
'use strict';
6+
7+
/**
8+
* Checks if the given argument is an object
9+
*
10+
* @param {any} x - The argument to check
11+
* @returns {boolean} - True if the argument is an object, false otherwise
12+
*/
13+
const isObject = (x) => Object.prototype.toString.call(x) === '[object Object]';
14+
15+
module.exports = {
16+
isObject,
17+
};

tests/lib/rules/filename-blocklist.posix.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,22 @@ ruleTester.run(
212212
],
213213
}
214214
);
215+
216+
ruleTester.run('filename-blocklist with option: []', rule, {
217+
valid: [],
218+
219+
invalid: [
220+
{
221+
code: "var foo = 'bar';",
222+
filename: 'src/foo.models.ts',
223+
options: [],
224+
errors: [
225+
{
226+
message: `The naming pattern object "undefined" doesn't appear to be an Object type, please double-check it and try again`,
227+
column: 1,
228+
line: 1,
229+
},
230+
],
231+
},
232+
],
233+
});

tests/lib/rules/filename-blocklist.windows.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,22 @@ ruleTester.run(
140140
],
141141
}
142142
);
143+
144+
ruleTester.run('filename-blocklist with option on Windows: []', rule, {
145+
valid: [],
146+
147+
invalid: [
148+
{
149+
code: "var foo = 'bar';",
150+
filename: 'src\\foo.models.ts',
151+
options: [],
152+
errors: [
153+
{
154+
message: `The naming pattern object "undefined" doesn't appear to be an Object type, please double-check it and try again`,
155+
column: 1,
156+
line: 1,
157+
},
158+
],
159+
},
160+
],
161+
});

tests/lib/rules/filename-naming-convention.posix.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,3 +1362,21 @@ ruleTester.run(
13621362
],
13631363
}
13641364
);
1365+
1366+
ruleTester.run('filename-naming-convention with option: []', rule, {
1367+
valid: [],
1368+
invalid: [
1369+
{
1370+
code: "var foo = 'bar';",
1371+
filename: 'src/components/featureA/featureA.jsx',
1372+
options: [],
1373+
errors: [
1374+
{
1375+
message: `The naming pattern object "undefined" doesn't appear to be an Object type, please double-check it and try again`,
1376+
column: 1,
1377+
line: 1,
1378+
},
1379+
],
1380+
},
1381+
],
1382+
});

tests/lib/rules/filename-naming-convention.windows.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,3 +919,21 @@ ruleTester.run(
919919
],
920920
}
921921
);
922+
923+
ruleTester.run('filename-naming-convention with option: []', rule, {
924+
valid: [],
925+
invalid: [
926+
{
927+
code: "var foo = 'bar';",
928+
filename: 'src\\components\\featureA\\featureA.jsx',
929+
options: [],
930+
errors: [
931+
{
932+
message: `The naming pattern object "undefined" doesn't appear to be an Object type, please double-check it and try again`,
933+
column: 1,
934+
line: 1,
935+
},
936+
],
937+
},
938+
],
939+
});

0 commit comments

Comments
 (0)