Skip to content

Commit 8263e83

Browse files
committed
refactor: extract all message constants
1 parent 1e9332b commit 8263e83

File tree

10 files changed

+111
-22
lines changed

10 files changed

+111
-22
lines changed

lib/constants/message.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @file Message constants
3+
* @author Duke Luo
4+
*/
5+
'use strict';
6+
7+
const { template } = require('../utils/utility');
8+
9+
const NAMING_PATTERN_OBJECT_ERROR_MESSAGE = template(
10+
`The naming pattern object "$0" doesn't appear to be an Object type, please double-check it and try again`
11+
);
12+
13+
const PATTERN_ERROR_MESSAGE = template(
14+
'There is an invalid pattern "$0", please check it'
15+
);
16+
17+
const PREFINED_MATCH_SYNTAX_ERROR_MESSAGE = template(
18+
'The capture group "$0" is not found in the glob "$1"'
19+
);
20+
21+
const FILENAME_BLOCKLIST_ERROR_MESSAGE = template(
22+
'The filename "$0" matches the blocklisted "$1" pattern. Use a pattern like "$2" instead.'
23+
);
24+
25+
const FILENAME_NAMING_CONVENTION_ERROR_MESSAGE = template(
26+
'The filename "$0" does not match the "$1" style'
27+
);
28+
29+
const FOLDER_MATCH_WITH_FEX_ERROR_MESSAGE = template(
30+
'The folder of the file "$0" does not match "$1"'
31+
);
32+
33+
const FOLDER_NAMING_CONVENTION_ERROR_MESSAGE = template(
34+
'The folder "$0" does not match the "$1" style'
35+
);
36+
37+
const NO_INDEX_ERROR_MESSAGE =
38+
'The filename "index" is not allowed, please use another one';
39+
40+
module.exports = {
41+
NAMING_PATTERN_OBJECT_ERROR_MESSAGE,
42+
PATTERN_ERROR_MESSAGE,
43+
PREFINED_MATCH_SYNTAX_ERROR_MESSAGE,
44+
FILENAME_BLOCKLIST_ERROR_MESSAGE,
45+
FILENAME_NAMING_CONVENTION_ERROR_MESSAGE,
46+
FOLDER_MATCH_WITH_FEX_ERROR_MESSAGE,
47+
FOLDER_NAMING_CONVENTION_ERROR_MESSAGE,
48+
NO_INDEX_ERROR_MESSAGE,
49+
};

lib/constants/regex.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ const PREFINED_MATCH_SYNTAX_REGEXP = /^<(\d+)>$/;
1414
*/
1515
const WINDOWS_DRIVE_LETTER_REGEXP = /^[A-Za-z]:\\/;
1616

17+
/**
18+
* @example $0
19+
*/
20+
const TEMPLATE_VARIABLE_REGEXP = /\$(\d*)/;
21+
1722
module.exports = {
1823
PREFINED_MATCH_SYNTAX_REGEXP,
1924
WINDOWS_DRIVE_LETTER_REGEXP,
25+
TEMPLATE_VARIABLE_REGEXP,
2026
};

lib/rules/filename-blocklist.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
} = require('../utils/settings');
1212
const { getDocUrl } = require('../utils/doc');
1313
const { matchRule } = require('../utils/match');
14+
const { FILENAME_BLOCKLIST_ERROR_MESSAGE } = require('../constants/message');
1415

1516
/** @typedef {module:eslint} ESLint */
1617
/**
@@ -69,13 +70,11 @@ module.exports = {
6970
if (matchResult) {
7071
context.report({
7172
node,
72-
message:
73-
'The filename "{{ filename }}" matches the blocklisted "{{ blockListPattern }}" pattern. Use a pattern like "{{ useInsteadPattern }}" instead.',
74-
data: {
73+
message: FILENAME_BLOCKLIST_ERROR_MESSAGE(
7574
filename,
7675
blockListPattern,
77-
useInsteadPattern,
78-
},
76+
useInsteadPattern
77+
),
7978
});
8079
return;
8180
}

lib/rules/filename-naming-convention.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ const {
1313
} = require('../utils/settings');
1414
const { getDocUrl } = require('../utils/doc');
1515
const { matchRule } = require('../utils/match');
16+
const {
17+
FILENAME_NAMING_CONVENTION_ERROR_MESSAGE,
18+
} = require('../constants/message');
1619

1720
/** @typedef {module:eslint} ESLint */
1821
/**
@@ -85,7 +88,10 @@ module.exports = {
8588

8689
if (matchResult) {
8790
throw new Error(
88-
`The filename "${filename}" does not match the "${originalNamingPattern}" style`
91+
FILENAME_NAMING_CONVENTION_ERROR_MESSAGE(
92+
filename,
93+
originalNamingPattern
94+
)
8995
);
9096
}
9197
} catch (error) {

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const {
1515
} = require('../utils/settings');
1616
const { getDocUrl } = require('../utils/doc');
1717
const { matchRule } = require('../utils/match');
18+
const { FOLDER_MATCH_WITH_FEX_ERROR_MESSAGE } = require('../constants/message');
1819

1920
/** @typedef {module:eslint} ESLint */
2021
/**
@@ -74,12 +75,10 @@ module.exports = {
7475
const { pattern } = matchResult;
7576
context.report({
7677
node,
77-
message:
78-
'The folder of the file "{{filenameWithPath}}" does not match "{{pattern}}"',
79-
data: {
78+
message: FOLDER_MATCH_WITH_FEX_ERROR_MESSAGE(
8079
filenameWithPath,
81-
pattern,
82-
},
80+
pattern
81+
),
8382
});
8483
return;
8584
}

lib/rules/folder-naming-convention.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const {
1919
const { getDocUrl } = require('../utils/doc');
2020
const { isNotEmpty } = require('../utils/utility');
2121
const NAMING_CONVENTION = require('../constants/naming-convention');
22+
const {
23+
FOLDER_NAMING_CONVENTION_ERROR_MESSAGE,
24+
} = require('../constants/message');
2225

2326
/** @typedef {module:eslint} ESLint */
2427
/**
@@ -85,12 +88,10 @@ module.exports = {
8588
) {
8689
context.report({
8790
node,
88-
message:
89-
'The folder "{{folder}}" does not match the "{{namingPattern}}" style',
90-
data: {
91+
message: FOLDER_NAMING_CONVENTION_ERROR_MESSAGE(
9192
folder,
92-
namingPattern,
93-
},
93+
namingPattern
94+
),
9495
});
9596
return;
9697
}

lib/rules/no-index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const { getDocUrl } = require('../utils/doc');
88
const { getFilename, getBasename, getFilePath } = require('../utils/filename');
9+
const { NO_INDEX_ERROR_MESSAGE } = require('../constants/message');
910

1011
/** @typedef {module:eslint} ESLint */
1112
/**
@@ -34,8 +35,7 @@ module.exports = {
3435
if (basename === 'index') {
3536
context.report({
3637
node,
37-
message:
38-
'The filename "index" is not allowed, please use another one',
38+
message: NO_INDEX_ERROR_MESSAGE,
3939
});
4040
return;
4141
}

lib/utils/settings.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ const isGlob = require('is-glob');
88
const NAMING_CONVENTION = require('../constants/naming-convention');
99
const { PREFINED_MATCH_SYNTAX_REGEXP } = require('../constants/regex');
1010
const { isObject } = require('./utility');
11+
const {
12+
NAMING_PATTERN_OBJECT_ERROR_MESSAGE,
13+
PATTERN_ERROR_MESSAGE,
14+
} = require('../constants/message');
1115

1216
/**
1317
* Validator
@@ -23,13 +27,13 @@ const { isObject } = require('./utility');
2327
*/
2428
const validateNamingPatternObject = (config, keyValidator, valueValidator) => {
2529
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`;
30+
return NAMING_PATTERN_OBJECT_ERROR_MESSAGE(config);
2731
}
2832
for (const [key, value] of Object.entries(config)) {
2933
if (!keyValidator(key)) {
30-
return `There is an invalid pattern "${key}", please check it`;
34+
return PATTERN_ERROR_MESSAGE(key);
3135
} else if (!valueValidator(value)) {
32-
return `There is an invalid pattern "${value}", please check it`;
36+
return PATTERN_ERROR_MESSAGE(value);
3337
}
3438
}
3539
};

lib/utils/transform.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
const micromatch = require('micromatch');
99
const { isNil, isEmpty } = require('../utils/utility');
1010
const { PREFINED_MATCH_SYNTAX_REGEXP } = require('../constants/regex');
11+
const { PREFINED_MATCH_SYNTAX_ERROR_MESSAGE } = require('../constants/message');
1112

1213
/**
1314
* Takes in a rule and transforms it if it contains prefined match syntax
@@ -46,7 +47,7 @@ const transformRuleWithPrefinedMatchSyntax = (
4647

4748
if (isNil(keyCaptureGroups[groupIndex])) {
4849
throw new Error(
49-
`The capture group "${namingPattern}" is not found in the glob "${filenamePattern}"`
50+
PREFINED_MATCH_SYNTAX_ERROR_MESSAGE(namingPattern, filenamePattern)
5051
);
5152
}
5253

lib/utils/utility.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55
'use strict';
66

7+
const { TEMPLATE_VARIABLE_REGEXP } = require('../constants/regex');
8+
79
/**
810
* Checks if the given argument is an object
911
*
@@ -73,10 +75,32 @@ const pipe =
7375
*/
7476
const isNotEmpty = pipe(isEmpty, not);
7577

78+
/**
79+
* Template function with the specified template
80+
*
81+
* @callback templateFunction
82+
* @param {...string} data - param array
83+
* @returns {string} - String with replaced template variables
84+
*/
85+
/**
86+
* Get template function for a template string
87+
*
88+
* @param {string} tpl - The template string to be replaced
89+
* @returns {templateFunction} - A function that takes in data and replaces the template variables with the corresponding values from the data object
90+
*/
91+
const template =
92+
(tpl) =>
93+
(...data) =>
94+
tpl.replace(
95+
new RegExp(TEMPLATE_VARIABLE_REGEXP, 'g'),
96+
(_, key) => data[key.trim()]
97+
);
98+
7699
module.exports = {
77100
isObject,
78101
isNil,
79102
isEmpty,
80103
isNotEmpty,
81104
pipe,
105+
template,
82106
};

0 commit comments

Comments
 (0)