Skip to content

Commit

Permalink
chore: remove all eslint plugin eslint warnnings
Browse files Browse the repository at this point in the history
  • Loading branch information
dukeluo committed Feb 11, 2024
1 parent cb6754a commit b444988
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 94 deletions.
37 changes: 14 additions & 23 deletions lib/constants/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,25 @@
* @author Huan Luo
*/

import { template } from '../utils/utility.js';
export const NAMING_PATTERN_OBJECT_ERROR_MESSAGE =
'The naming pattern object "{{ value }}" does not appear to be an Object type, please double-check it and try again';

export const NAMING_PATTERN_OBJECT_ERROR_MESSAGE = template(
'The naming pattern object "$0" does not appear to be an Object type, please double-check it and try again'
);
export const PATTERN_ERROR_MESSAGE =
'There is an invalid pattern "{{ value }}", please double-check it and try again';

export const PATTERN_ERROR_MESSAGE = template(
'There is an invalid pattern "$0", please double-check it and try again'
);
export const PREFINED_MATCH_SYNTAX_ERROR_MESSAGE =
'The prefined match "{{ namingPattern }}" is not found in the pattern "{{ filenamePattern }}", please double-check it and try again';

export const PREFINED_MATCH_SYNTAX_ERROR_MESSAGE = template(
'The prefined match "$0" is not found in the pattern "$1", please double-check it and try again'
);
export const FILENAME_BLOCKLIST_ERROR_MESSAGE =
'The filename "{{ filename }}" matches the blocklisted "{{ blockListPattern }}" pattern, use a pattern like "{{ useInsteadPattern }}" instead';

export const FILENAME_BLOCKLIST_ERROR_MESSAGE = template(
'The filename "$0" matches the blocklisted "$1" pattern, use a pattern like "$2" instead'
);
export const FILENAME_NAMING_CONVENTION_ERROR_MESSAGE =
'The filename "{{ filename }}" does not match the "{{ originalNamingPattern }}" pattern';

export const FILENAME_NAMING_CONVENTION_ERROR_MESSAGE = template(
'The filename "$0" does not match the "$1" pattern'
);
export const FOLDER_MATCH_WITH_FEX_ERROR_MESSAGE =
'The folder of the file "{{ filenameWithPath }}" does not match the "{{ folderPattern }}" pattern';

export const FOLDER_MATCH_WITH_FEX_ERROR_MESSAGE = template(
'The folder of the file "$0" does not match the "$1" pattern'
);

export const FOLDER_NAMING_CONVENTION_ERROR_MESSAGE = template(
'The folder "$0" does not match the "$1" pattern'
);
export const FOLDER_NAMING_CONVENTION_ERROR_MESSAGE =
'The folder "{{ folder }}" does not match the "{{ namingPattern }}" pattern';

export const NO_INDEX_ERROR_MESSAGE = 'The filename "index" is not allowed';
5 changes: 0 additions & 5 deletions lib/constants/regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,3 @@ export const PREFINED_MATCH_SYNTAX_REGEXP = /^<(\d+)>$/;
* @example C:\
*/
export const WINDOWS_DRIVE_LETTER_REGEXP = /^[A-Za-z]:\\/;

/**
* @example $0
*/
export const TEMPLATE_VARIABLE_REGEXP = /\$(\d*)/;
27 changes: 20 additions & 7 deletions lib/rules/filename-blocklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
* @author Florian Ehmke, Huan Luo
*/

import { FILENAME_BLOCKLIST_ERROR_MESSAGE } from '../constants/message.js';
import {
FILENAME_BLOCKLIST_ERROR_MESSAGE,
NAMING_PATTERN_OBJECT_ERROR_MESSAGE,
PATTERN_ERROR_MESSAGE,
} from '../constants/message.js';
import { getDocUrl } from '../utils/doc.js';
import { getFilePath, getFilename } from '../utils/filename.js';
import { matchRule } from '../utils/rule.js';
Expand Down Expand Up @@ -32,22 +36,30 @@ export default {
},
},
],
messages: {
invalidObject: NAMING_PATTERN_OBJECT_ERROR_MESSAGE,
invalidPattern: PATTERN_ERROR_MESSAGE,
noMatch: FILENAME_BLOCKLIST_ERROR_MESSAGE,
},
},

create(context) {
return {
Program: (node) => {
const rules = context.options[0];
const message = validateNamingPatternObject(
const error = validateNamingPatternObject(
rules,
globPatternValidator,
globPatternValidator
);

if (message) {
if (error) {
context.report({
node,
message,
messageId: error.type,
data: {
value: error.payload,
},
});
return;
}
Expand All @@ -69,11 +81,12 @@ export default {
if (matchResult) {
context.report({
node,
message: FILENAME_BLOCKLIST_ERROR_MESSAGE(
messageId: 'noMatch',
data: {
filename,
blockListPattern,
useInsteadPattern
),
useInsteadPattern,
},
});
return;
}
Expand Down
36 changes: 26 additions & 10 deletions lib/rules/filename-naming-convention.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
* @author Huan Luo
*/

import { FILENAME_NAMING_CONVENTION_ERROR_MESSAGE } from '../constants/message.js';
import {
FILENAME_NAMING_CONVENTION_ERROR_MESSAGE,
NAMING_PATTERN_OBJECT_ERROR_MESSAGE,
PATTERN_ERROR_MESSAGE,
PREFINED_MATCH_SYNTAX_ERROR_MESSAGE,
} from '../constants/message.js';
import { getDocUrl } from '../utils/doc.js';
import { getBasename, getFilePath, getFilename } from '../utils/filename.js';
import {
Expand Down Expand Up @@ -42,22 +47,31 @@ export default {
},
},
],
messages: {
invalidObject: NAMING_PATTERN_OBJECT_ERROR_MESSAGE,
invalidPattern: PATTERN_ERROR_MESSAGE,
invalidPrefinedMatch: PREFINED_MATCH_SYNTAX_ERROR_MESSAGE,
noMatch: FILENAME_NAMING_CONVENTION_ERROR_MESSAGE,
},
},

create(context) {
return {
Program: (node) => {
const rules = context.options[0];
const message = validateNamingPatternObject(
const error = validateNamingPatternObject(
rules,
globPatternValidator,
filenameNamingPatternValidator
);

if (message) {
if (error) {
context.report({
node,
message,
messageId: error.type,
data: {
value: error.payload,
},
});
return;
}
Expand Down Expand Up @@ -85,17 +99,19 @@ export default {
);

if (matchResult) {
throw new Error(
FILENAME_NAMING_CONVENTION_ERROR_MESSAGE(
throw {
type: 'noMatch',
payload: {
filename,
originalNamingPattern
)
);
originalNamingPattern,
},
};
}
} catch (error) {
context.report({
node,
message: error.message,
messageId: error.type,
data: error.payload,
});
}
}
Expand Down
27 changes: 20 additions & 7 deletions lib/rules/folder-match-with-fex.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
* @author Huan Luo
*/

import { FOLDER_MATCH_WITH_FEX_ERROR_MESSAGE } from '../constants/message.js';
import {
FOLDER_MATCH_WITH_FEX_ERROR_MESSAGE,
NAMING_PATTERN_OBJECT_ERROR_MESSAGE,
PATTERN_ERROR_MESSAGE,
} from '../constants/message.js';
import { getDocUrl } from '../utils/doc.js';
import { getFilePath, getFilename, getFolderPath } from '../utils/filename.js';
import { matchRule } from '../utils/rule.js';
Expand Down Expand Up @@ -33,22 +37,30 @@ export default {
},
},
],
messages: {
invalidObject: NAMING_PATTERN_OBJECT_ERROR_MESSAGE,
invalidPattern: PATTERN_ERROR_MESSAGE,
noMatch: FOLDER_MATCH_WITH_FEX_ERROR_MESSAGE,
},
},

create(context) {
return {
Program: (node) => {
const rules = context.options[0];
const message = validateNamingPatternObject(
const error = validateNamingPatternObject(
rules,
globPatternValidator,
globPatternValidator
);

if (message) {
if (error) {
context.report({
node,
message,
messageId: error.type,
data: {
value: error.payload,
},
});
return;
}
Expand All @@ -68,10 +80,11 @@ export default {
if (matchResult) {
context.report({
node,
message: FOLDER_MATCH_WITH_FEX_ERROR_MESSAGE(
messageId: 'noMatch',
data: {
filenameWithPath,
folderPattern
),
folderPattern,
},
});
return;
}
Expand Down
27 changes: 20 additions & 7 deletions lib/rules/folder-naming-convention.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
*/

import micromatch from 'micromatch';
import { FOLDER_NAMING_CONVENTION_ERROR_MESSAGE } from '../constants/message.js';
import {
FOLDER_NAMING_CONVENTION_ERROR_MESSAGE,
NAMING_PATTERN_OBJECT_ERROR_MESSAGE,
PATTERN_ERROR_MESSAGE,
} from '../constants/message.js';
import * as BASIC_NAMING_CONVENTION from '../constants/naming-convention.js';
import * as NEXT_JS_NAMING_CONVENTION from '../constants/next-js-naming-convention.js';
import { getDocUrl } from '../utils/doc.js';
Expand Down Expand Up @@ -41,22 +45,30 @@ export default {
},
},
],
messages: {
invalidObject: NAMING_PATTERN_OBJECT_ERROR_MESSAGE,
invalidPattern: PATTERN_ERROR_MESSAGE,
noMatch: FOLDER_NAMING_CONVENTION_ERROR_MESSAGE,
},
},

create(context) {
return {
Program: (node) => {
const rules = context.options[0];
const message = validateNamingPatternObject(
const error = validateNamingPatternObject(
rules,
globPatternValidator,
folderNamingPatternValidator
);

if (message) {
if (error) {
context.report({
node,
message,
messageId: error.type,
data: {
value: error.payload,
},
});
return;
}
Expand Down Expand Up @@ -92,10 +104,11 @@ export default {
) {
context.report({
node,
message: FOLDER_NAMING_CONVENTION_ERROR_MESSAGE(
messageId: 'noMatch',
data: {
folder,
namingPattern
),
namingPattern,
},
});
return;
}
Expand Down
5 changes: 4 additions & 1 deletion lib/rules/no-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export default {
},
},
],
messages: {
noIndex: NO_INDEX_ERROR_MESSAGE,
},
},

create(context) {
Expand All @@ -41,7 +44,7 @@ export default {
if (basename === 'index') {
context.report({
node,
message: NO_INDEX_ERROR_MESSAGE,
messageId: 'noIndex',
});
return;
}
Expand Down
13 changes: 8 additions & 5 deletions lib/utils/rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/

import micromatch from 'micromatch';
import { PREFINED_MATCH_SYNTAX_ERROR_MESSAGE } from '../constants/message.js';
import * as NAMING_CONVENTION from '../constants/naming-convention.js';
import { PREFINED_MATCH_SYNTAX_REGEXP } from '../constants/regex.js';
import { isEmpty, isNil } from './utility.js';
Expand All @@ -17,7 +16,7 @@ import { isEmpty, isNil } from './utility.js';
* @param {rule} rule original rule
* @param {string} filenameWithPath filename with path
* @returns {rule} new rule
* @throws {Error} if a prefined match syntax referenced in the naming pattern is not found in the filename pattern
* @throws {import("./validation.js").ValidationError} if a prefined match syntax referenced in the naming pattern is not found in the filename pattern
*/
export const transformRuleWithPrefinedMatchSyntax = (
[filenamePattern, namingPattern],
Expand All @@ -44,9 +43,13 @@ export const transformRuleWithPrefinedMatchSyntax = (
const groupIndex = +group[1];

if (isNil(keyCaptureGroups[groupIndex])) {
throw new Error(
PREFINED_MATCH_SYNTAX_ERROR_MESSAGE(namingPattern, filenamePattern)
);
throw {
type: 'invalidPrefinedMatch',
payload: {
namingPattern,
filenamePattern,
},
};
}

return value.replace(group[0], keyCaptureGroups[groupIndex]);
Expand Down
Loading

0 comments on commit b444988

Please sign in to comment.