-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfixup-rules.js
259 lines (217 loc) · 7.59 KB
/
fixup-rules.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* @filedescription Object Schema
*/
"use strict";
//-----------------------------------------------------------------------------
// Imports
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Types
//-----------------------------------------------------------------------------
/** @typedef {import("./types.ts").FixupRuleDefinition} FixupRuleDefinition */
/** @typedef {import("./types.ts").FixupLegacyRuleDefinition} FixupLegacyRuleDefinition */
/** @typedef {import("./types.ts").FixupPluginDefinition} FixupPluginDefinition */
/** @typedef {import("./types.ts").FixupConfig} FixupConfig */
/** @typedef {import("./types.ts").FixupConfigArray} FixupConfigArray */
//-----------------------------------------------------------------------------
// Data
//-----------------------------------------------------------------------------
/**
* The removed methods from the `context` object that need to be added back.
* The keys are the name of the method on the `context` object and the values
* are the name of the method on the `sourceCode` object.
* @type {Map<string, string>}
*/
const removedMethodNames = new Map([
["getSource", "getText"],
["getSourceLines", "getLines"],
["getAllComments", "getAllComments"],
["getDeclaredVariables", "getDeclaredVariables"],
["getNodeByRangeIndex", "getNodeByRangeIndex"],
["getCommentsBefore", "getCommentsBefore"],
["getCommentsAfter", "getCommentsAfter"],
["getCommentsInside", "getCommentsInside"],
["getJSDocComment", "getJSDocComment"],
["getFirstToken", "getFirstToken"],
["getFirstTokens", "getFirstTokens"],
["getLastToken", "getLastToken"],
["getLastTokens", "getLastTokens"],
["getTokenAfter", "getTokenAfter"],
["getTokenBefore", "getTokenBefore"],
["getTokenByRangeStart", "getTokenByRangeStart"],
["getTokens", "getTokens"],
["getTokensAfter", "getTokensAfter"],
["getTokensBefore", "getTokensBefore"],
["getTokensBetween", "getTokensBetween"],
]);
/**
* Tracks the original rule definition and the fixed-up rule definition.
* @type {WeakMap<FixupRuleDefinition|FixupLegacyRuleDefinition,FixupRuleDefinition>}
*/
const fixedUpRuleReplacements = new WeakMap();
/**
* Tracks all of the fixed up rule definitions so we don't duplicate effort.
* @type {WeakSet<FixupRuleDefinition>}
*/
const fixedUpRules = new WeakSet();
/**
* Tracks the original plugin definition and the fixed-up plugin definition.
* @type {WeakMap<FixupPluginDefinition,FixupPluginDefinition>}
*/
const fixedUpPluginReplacements = new WeakMap();
/**
* Tracks all of the fixed up plugin definitions so we don't duplicate effort.
* @type {WeakSet<FixupPluginDefinition>}
*/
const fixedUpPlugins = new WeakSet();
//-----------------------------------------------------------------------------
// Exports
//-----------------------------------------------------------------------------
/**
* Takes the given rule and creates a new rule with the `create()` method wrapped
* to provide the missing methods on the `context` object.
* @param {FixupRuleDefinition|FixupLegacyRuleDefinition} ruleDefinition The rule to fix up.
* @returns {FixupRuleDefinition} The fixed-up rule.
*/
export function fixupRule(ruleDefinition) {
// first check if we've already fixed up this rule
if (fixedUpRuleReplacements.has(ruleDefinition)) {
return fixedUpRuleReplacements.get(ruleDefinition);
}
const isLegacyRule = typeof ruleDefinition === "function";
// check to see if this rule definition has already been fixed up
if (!isLegacyRule && fixedUpRules.has(ruleDefinition)) {
return ruleDefinition;
}
const originalCreate = isLegacyRule
? ruleDefinition
: ruleDefinition.create.bind(ruleDefinition);
const ruleCreate = context => {
// if getScope is already there then no need to create old methods
if ("getScope" in context) {
return originalCreate(context);
}
const sourceCode = context.sourceCode;
let currentNode = sourceCode.ast;
const newContext = Object.assign(Object.create(context), {
parserServices: sourceCode.parserServices,
/*
* The following methods rely on the current node in the traversal,
* so we need to add them manually.
*/
getScope() {
return sourceCode.getScope(currentNode);
},
getAncestors() {
return sourceCode.getAncestors(currentNode);
},
markVariableAsUsed(variable) {
sourceCode.markVariableAsUsed(variable, currentNode);
},
});
// add passthrough methods
for (const [
contextMethodName,
sourceCodeMethodName,
] of removedMethodNames) {
newContext[contextMethodName] =
sourceCode[sourceCodeMethodName].bind(sourceCode);
}
// freeze just like the original context
Object.freeze(newContext);
/*
* Create the visitor object using the original create() method.
* This is necessary to ensure that the visitor object is created
* with the correct context.
*/
const visitor = originalCreate(newContext);
/*
* Wrap each method in the visitor object to update the currentNode
* before calling the original method. This is necessary because the
* methods like `getScope()` need to know the current node.
*/
for (const [methodName, method] of Object.entries(visitor)) {
/*
* Node is the second argument to most code path methods,
* and the third argument for onCodePathSegmentLoop.
*/
if (methodName.startsWith("on")) {
visitor[methodName] = (...args) => {
currentNode =
args[methodName === "onCodePathSegmentLoop" ? 2 : 1];
return method.call(visitor, ...args);
};
continue;
}
visitor[methodName] = (...args) => {
currentNode = args[0];
return method.call(visitor, ...args);
};
}
return visitor;
};
const newRuleDefinition = {
...(isLegacyRule ? undefined : ruleDefinition),
create: ruleCreate,
};
// cache the fixed up rule
fixedUpRuleReplacements.set(ruleDefinition, newRuleDefinition);
fixedUpRules.add(newRuleDefinition);
return newRuleDefinition;
}
/**
* Takes the given plugin and creates a new plugin with all of the rules wrapped
* to provide the missing methods on the `context` object.
* @param {FixupPluginDefinition} plugin The plugin to fix up.
* @returns {FixupPluginDefinition} The fixed-up plugin.
*/
export function fixupPluginRules(plugin) {
// first check if we've already fixed up this plugin
if (fixedUpPluginReplacements.has(plugin)) {
return fixedUpPluginReplacements.get(plugin);
}
/*
* If the plugin has already been fixed up, or if the plugin
* doesn't have any rules, we can just return it.
*/
if (fixedUpPlugins.has(plugin) || !plugin.rules) {
return plugin;
}
const newPlugin = {
...plugin,
rules: Object.fromEntries(
Object.entries(plugin.rules).map(([ruleId, ruleDefinition]) => [
ruleId,
fixupRule(ruleDefinition),
]),
),
};
// cache the fixed up plugin
fixedUpPluginReplacements.set(plugin, newPlugin);
fixedUpPlugins.add(newPlugin);
return newPlugin;
}
/**
* Takes the given configuration and creates a new configuration with all of the
* rules wrapped to provide the missing methods on the `context` object.
* @param {FixupConfigArray|FixupConfig} config The configuration to fix up.
* @returns {FixupConfigArray} The fixed-up configuration.
*/
export function fixupConfigRules(config) {
const configs = Array.isArray(config) ? config : [config];
return configs.map(config => {
if (!config.plugins) {
return config;
}
const newPlugins = Object.fromEntries(
Object.entries(config.plugins).map(([pluginName, plugin]) => [
pluginName,
fixupPluginRules(plugin),
]),
);
return {
...config,
plugins: newPlugins,
};
});
}