-
Notifications
You must be signed in to change notification settings - Fork 2
/
no-invalid-config.ts
122 lines (112 loc) · 3.44 KB
/
no-invalid-config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import {
AST_NODE_TYPES,
ESLintUtils,
TSESTree
} from '@typescript-eslint/experimental-utils';
import { ESLintError, ESLintErrorType, getConfigInfo } from './utils';
const getNodeValue = (
node: TSESTree.Identifier | TSESTree.LiteralExpression
): string => {
switch (node.type) {
case AST_NODE_TYPES.Identifier:
return node.name;
case AST_NODE_TYPES.TemplateLiteral:
return node.quasis[0].value.raw;
default:
return node.value?.toString() ?? 'null';
}
};
export = ESLintUtils.RuleCreator(name => name)({
name: __filename,
meta: {
type: 'problem',
docs: {
description: 'Checks that the config exported by a file is valid',
recommended: 'error'
},
messages: {
FailedToLoadModule:
"Cannot load {{ kind }} '{{ name }}' - is the package installed?",
InvalidRuleConfig:
"The config for '{{ ruleId }}' is invalid: {{ reason }}",
ProcessorNotFound: "Cannot load processor '{{ name }}'",
FailedToExtend:
"Cannot extend '{{ name }}' - ensure file exists and exports a valid config",
InvalidConfig: 'This config is invalid: {{ reason }}',
UnknownError: 'An unknown error occurred: {{ message }}'
},
schema: []
},
defaultOptions: [],
create(context) {
const reportedErrors = new Set<ESLintError>();
const createErrorReporter = (
node: TSESTree.Node
): ((error: ESLintError) => void) => {
const errorsReportedForNode = new Set<string>();
return (error): void => {
reportedErrors.add(error);
const stringifiedError = JSON.stringify({ ...error, path: undefined });
if (errorsReportedForNode.has(stringifiedError)) {
return;
}
errorsReportedForNode.add(stringifiedError);
context.report({
messageId: error.type,
data: { ...error },
node,
loc: node.loc
});
};
};
try {
const { errors } = getConfigInfo(context.getSourceCode().getText());
if (errors.length === 0) {
return {};
}
return {
[[
AST_NODE_TYPES.Identifier,
AST_NODE_TYPES.TemplateLiteral,
AST_NODE_TYPES.Literal
].join()](
node: TSESTree.Identifier | TSESTree.LiteralExpression
): void {
const reportError = createErrorReporter(node);
const value = getNodeValue(node);
errors
.filter(error => {
switch (error.type) {
case ESLintErrorType.InvalidRuleConfig:
return error.ruleId === value;
case ESLintErrorType.FailedToExtend:
case ESLintErrorType.FailedToLoadModule:
case ESLintErrorType.ProcessorNotFound:
return error.name === value;
case ESLintErrorType.InvalidConfig:
default:
return false;
}
})
.forEach(reportError);
},
[`${AST_NODE_TYPES.Program}:exit`](node): void {
const reportError = createErrorReporter(node);
errors
.filter(error => !reportedErrors.has(error))
.forEach(reportError);
}
};
} catch (error) {
return {
Program(node): void {
context.report({
messageId: 'UnknownError',
data: { message: (error as Error).message },
node
});
}
};
}
}
});