Skip to content

Commit f633057

Browse files
committed
[Refactor] avoid caching context.getSourceNode()
This function is effectively free to call - eslint memoizes it - and inlining it allows for easier extraction of common utility functions.
1 parent ef7f6eb commit f633057

23 files changed

+65
-66
lines changed

lib/rules/boolean-prop-naming.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ module.exports = {
5353
},
5454

5555
create: Components.detect((context, components, utils) => {
56-
const sourceCode = context.getSourceCode();
5756
const config = context.options[0] || {};
5857
const rule = config.rule ? new RegExp(config.rule) : null;
5958
const propTypeNames = config.propTypeNames || ['bool'];
@@ -103,7 +102,7 @@ module.exports = {
103102
// we can't get the name of the Flow object key name. So we have
104103
// to hack around it for now.
105104
if (node.type === 'ObjectTypeProperty') {
106-
return sourceCode.getFirstToken(node).value;
105+
return context.getSourceCode().getFirstToken(node).value;
107106
}
108107

109108
return node.key.name;
@@ -220,7 +219,14 @@ module.exports = {
220219
if (!rule || !propsUtil.isPropTypesDeclaration(node)) {
221220
return;
222221
}
223-
if (node.value && node.value.type === 'CallExpression' && propWrapperUtil.isPropWrapperFunction(context, sourceCode.getText(node.value.callee))) {
222+
if (
223+
node.value &&
224+
node.value.type === 'CallExpression' &&
225+
propWrapperUtil.isPropWrapperFunction(
226+
context,
227+
context.getSourceCode().getText(node.value.callee)
228+
)
229+
) {
224230
checkPropWrapperArguments(node, node.value.arguments);
225231
}
226232
if (node.value && node.value.properties) {
@@ -240,7 +246,13 @@ module.exports = {
240246
return;
241247
}
242248
const right = node.parent.right;
243-
if (right.type === 'CallExpression' && propWrapperUtil.isPropWrapperFunction(context, sourceCode.getText(right.callee))) {
249+
if (
250+
right.type === 'CallExpression' &&
251+
propWrapperUtil.isPropWrapperFunction(
252+
context,
253+
context.getSourceCode().getText(right.callee)
254+
)
255+
) {
244256
checkPropWrapperArguments(component.node, right.arguments);
245257
return;
246258
}

lib/rules/forbid-elements.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ module.exports = {
4747
},
4848

4949
create(context) {
50-
const sourceCode = context.getSourceCode();
5150
const configuration = context.options[0] || {};
5251
const forbidConfiguration = configuration.forbid || [];
5352

@@ -91,7 +90,7 @@ module.exports = {
9190

9291
return {
9392
JSXOpeningElement(node) {
94-
reportIfForbidden(sourceCode.getText(node.name), node.name);
93+
reportIfForbidden(context.getSourceCode().getText(node.name), node.name);
9594
},
9695

9796
CallExpression(node) {
@@ -107,7 +106,7 @@ module.exports = {
107106
} else if (argType === 'Literal' && /^[a-z][^.]*$/.test(argument.value)) {
108107
reportIfForbidden(argument.value, argument);
109108
} else if (argType === 'MemberExpression') {
110-
reportIfForbidden(sourceCode.getText(argument), argument);
109+
reportIfForbidden(context.getSourceCode().getText(argument), argument);
111110
}
112111
}
113112
};

lib/rules/jsx-closing-bracket-location.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ module.exports = {
6161
};
6262
const DEFAULT_LOCATION = 'tag-aligned';
6363

64-
const sourceCode = context.getSourceCode();
6564
const config = context.options[0];
6665
const options = {
6766
nonEmpty: DEFAULT_LOCATION,
@@ -164,11 +163,11 @@ module.exports = {
164163
let spaces = [];
165164
switch (expectedLocation) {
166165
case 'props-aligned':
167-
indentation = /^\s*/.exec(sourceCode.lines[tokens.lastProp.firstLine - 1])[0];
166+
indentation = /^\s*/.exec(context.getSourceCode().lines[tokens.lastProp.firstLine - 1])[0];
168167
break;
169168
case 'tag-aligned':
170169
case 'line-aligned':
171-
indentation = /^\s*/.exec(sourceCode.lines[tokens.opening.line - 1])[0];
170+
indentation = /^\s*/.exec(context.getSourceCode().lines[tokens.opening.line - 1])[0];
172171
break;
173172
default:
174173
indentation = '';
@@ -188,6 +187,7 @@ module.exports = {
188187
* prop and start of opening line.
189188
*/
190189
function getTokensLocations(node) {
190+
const sourceCode = context.getSourceCode();
191191
const opening = sourceCode.getFirstToken(node).loc.start;
192192
const closing = sourceCode.getLastTokens(node, node.selfClosing ? 2 : 1)[0].loc.start;
193193
const tag = sourceCode.getFirstToken(node.name).loc.start;

lib/rules/jsx-curly-spacing.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ module.exports = {
126126
const DEFAULT_ATTRIBUTES = true;
127127
const DEFAULT_CHILDREN = false;
128128

129-
const sourceCode = context.getSourceCode();
130129
let originalConfig = context.options[0] || {};
131130
if (SPACING_VALUES.indexOf(originalConfig) !== -1) {
132131
originalConfig = Object.assign({when: context.options[0]}, context.options[1]);
@@ -164,7 +163,7 @@ module.exports = {
164163
* @returns {Object|*|{range, text}}
165164
*/
166165
function fixByTrimmingWhitespace(fixer, fromLoc, toLoc, mode, spacing) {
167-
let replacementText = sourceCode.text.slice(fromLoc, toLoc);
166+
let replacementText = context.getSourceCode().text.slice(fromLoc, toLoc);
168167
if (mode === 'start') {
169168
replacementText = replacementText.replace(/^\s+/gm, '');
170169
} else {
@@ -193,7 +192,7 @@ module.exports = {
193192
loc: token.loc.start,
194193
message: `There should be no newline after '${token.value}'`,
195194
fix(fixer) {
196-
const nextToken = sourceCode.getTokenAfter(token);
195+
const nextToken = context.getSourceCode().getTokenAfter(token);
197196
return fixByTrimmingWhitespace(fixer, token.range[1], nextToken.range[0], 'start', spacing);
198197
}
199198
});
@@ -212,7 +211,7 @@ module.exports = {
212211
loc: token.loc.start,
213212
message: `There should be no newline before '${token.value}'`,
214213
fix(fixer) {
215-
const previousToken = sourceCode.getTokenBefore(token);
214+
const previousToken = context.getSourceCode().getTokenBefore(token);
216215
return fixByTrimmingWhitespace(fixer, previousToken.range[1], token.range[0], 'end', spacing);
217216
}
218217
});
@@ -230,6 +229,7 @@ module.exports = {
230229
loc: token.loc.start,
231230
message: `There should be no space after '${token.value}'`,
232231
fix(fixer) {
232+
const sourceCode = context.getSourceCode();
233233
const nextToken = sourceCode.getTokenAfter(token);
234234
let nextComment;
235235

@@ -264,6 +264,7 @@ module.exports = {
264264
loc: token.loc.start,
265265
message: `There should be no space before '${token.value}'`,
266266
fix(fixer) {
267+
const sourceCode = context.getSourceCode();
267268
const previousToken = sourceCode.getTokenBefore(token);
268269
let previousComment;
269270

@@ -345,6 +346,7 @@ module.exports = {
345346
return;
346347
}
347348

349+
const sourceCode = context.getSourceCode();
348350
const first = context.getFirstToken(node);
349351
const last = sourceCode.getLastToken(node);
350352
let second = context.getTokenAfter(first, {includeComments: true});

lib/rules/jsx-equals-spacing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ module.exports = {
2828

2929
create(context) {
3030
const config = context.options[0];
31-
const sourceCode = context.getSourceCode();
3231

3332
/**
3433
* Determines a given attribute node has an equal sign.
@@ -50,6 +49,7 @@ module.exports = {
5049
return;
5150
}
5251

52+
const sourceCode = context.getSourceCode();
5353
const equalToken = sourceCode.getTokenAfter(attrNode.name);
5454
const spacedBefore = sourceCode.isSpaceBetweenTokens(attrNode.name, equalToken);
5555
const spacedAfter = sourceCode.isSpaceBetweenTokens(equalToken, attrNode.value);

lib/rules/jsx-fragments.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ module.exports = {
3636

3737
create(context) {
3838
const configuration = context.options[0] || 'syntax';
39-
const sourceCode = context.getSourceCode();
4039
const reactPragma = pragmaUtil.getFromContext(context);
4140
const fragmentPragma = pragmaUtil.getFragmentFromContext(context);
4241
const openFragShort = '<>';
@@ -58,6 +57,7 @@ module.exports = {
5857
}
5958

6059
function getFixerToLong(jsxFragment) {
60+
const sourceCode = context.getSourceCode();
6161
return function (fixer) {
6262
let source = sourceCode.getText();
6363
source = replaceNode(source, jsxFragment.closingFragment, closeFragLong);
@@ -70,6 +70,7 @@ module.exports = {
7070
}
7171

7272
function getFixerToShort(jsxElement) {
73+
const sourceCode = context.getSourceCode();
7374
return function (fixer) {
7475
let source = sourceCode.getText();
7576
let lengthDiff;

lib/rules/jsx-handler-names.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ module.exports = {
3535
},
3636

3737
create(context) {
38-
const sourceCode = context.getSourceCode();
3938
const configuration = context.options[0] || {};
4039
const eventHandlerPrefix = configuration.eventHandlerPrefix || 'handle';
4140
const eventHandlerPropPrefix = configuration.eventHandlerPropPrefix || 'on';
@@ -50,7 +49,7 @@ module.exports = {
5049
}
5150

5251
const propKey = typeof node.name === 'object' ? node.name.name : node.name;
53-
const propValue = sourceCode.getText(node.value.expression).replace(/^this\.|.*::/, '');
52+
const propValue = context.getSourceCode().getText(node.value.expression).replace(/^this\.|.*::/, '');
5453

5554
if (propKey === 'ref') {
5655
return;

lib/rules/jsx-indent-props.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ module.exports = {
6363
/** @type {number|'first'} */
6464
let indentSize = 4;
6565

66-
const sourceCode = context.getSourceCode();
67-
6866
if (context.options.length) {
6967
if (context.options[0] === 'first') {
7068
indentSize = 'first';
@@ -109,7 +107,7 @@ module.exports = {
109107
* @return {Number} Indent
110108
*/
111109
function getNodeIndent(node) {
112-
let src = sourceCode.getText(node, node.loc.start.column + extraColumnStart);
110+
let src = context.getSourceCode().getText(node, node.loc.start.column + extraColumnStart);
113111
const lines = src.split('\n');
114112
src = lines[0];
115113

lib/rules/jsx-indent.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ module.exports = {
7272
let indentType = 'space';
7373
let indentSize = 4;
7474

75-
const sourceCode = context.getSourceCode();
76-
7775
if (context.options.length) {
7876
if (context.options[0] === 'tab') {
7977
indentSize = 1;
@@ -150,7 +148,7 @@ module.exports = {
150148
byLastLine = byLastLine || false;
151149
excludeCommas = excludeCommas || false;
152150

153-
let src = sourceCode.getText(node, node.loc.start.column + extraColumnStart);
151+
let src = context.getSourceCode().getText(node, node.loc.start.column + extraColumnStart);
154152
const lines = src.split('\n');
155153
if (byLastLine) {
156154
src = lines[lines.length - 1];
@@ -197,7 +195,7 @@ module.exports = {
197195
node.parent.parent &&
198196
node.parent.parent.type === 'ConditionalExpression' &&
199197
node.parent.parent.alternate === node.parent &&
200-
sourceCode.getTokenBefore(node).value !== '('
198+
context.getSourceCode().getTokenBefore(node).value !== '('
201199
);
202200
}
203201

@@ -222,6 +220,7 @@ module.exports = {
222220
}
223221

224222
function handleOpeningElement(node) {
223+
const sourceCode = context.getSourceCode();
225224
let prevToken = sourceCode.getTokenBefore(node);
226225
if (!prevToken) {
227226
return;
@@ -263,7 +262,7 @@ module.exports = {
263262
return;
264263
}
265264
const nameIndent = getNodeIndent(node.name);
266-
const lastToken = sourceCode.getLastToken(node.value);
265+
const lastToken = context.getSourceCode().getLastToken(node.value);
267266
const firstInLine = astUtil.getFirstNodeInLine(context, lastToken);
268267
const indent = node.name.loc.start.line === firstInLine.loc.start.line ? 0 : nameIndent;
269268
checkNodesIndent(firstInLine, indent);

lib/rules/jsx-max-props-per-line.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ module.exports = {
3636
},
3737

3838
create(context) {
39-
const sourceCode = context.getSourceCode();
4039
const configuration = context.options[0] || {};
4140
const maximum = configuration.maximum || 1;
4241
const when = configuration.when || 'always';
4342

4443
function getPropName(propNode) {
4544
if (propNode.type === 'JSXSpreadAttribute') {
46-
return sourceCode.getText(propNode.argument);
45+
return context.getSourceCode().getText(propNode.argument);
4746
}
4847
return propNode.name.name;
4948
}
5049

5150
function generateFixFunction(line, max) {
51+
const sourceCode = context.getSourceCode();
5252
const output = [];
5353
const front = line[0].range[0];
5454
const back = line[line.length - 1].range[1];

0 commit comments

Comments
 (0)