Skip to content

Commit 26765f9

Browse files
MatiPl01ljharb
andcommitted
[Refactor] create getSourceCode helper, since context.getSourceCode is deprecated
Co-authored-by: Mateusz Łopaciński <[email protected]> Co-authored-by: Jordan Harband <[email protected]>
1 parent d97e3ed commit 26765f9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+166
-91
lines changed

lib/rules/boolean-prop-naming.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const propsUtil = require('../util/props');
1313
const docsUrl = require('../util/docsUrl');
1414
const propWrapperUtil = require('../util/propWrapper');
1515
const report = require('../util/report');
16+
const getSourceCode = require('../util/eslint').getSourceCode;
1617

1718
// ------------------------------------------------------------------------------
1819
// Rule Definition
@@ -115,7 +116,7 @@ module.exports = {
115116
// we can't get the name of the Flow object key name. So we have
116117
// to hack around it for now.
117118
if (node.type === 'ObjectTypeProperty') {
118-
return context.getSourceCode().getFirstToken(node).value;
119+
return getSourceCode(context).getFirstToken(node).value;
119120
}
120121

121122
return node.key.name;
@@ -308,7 +309,7 @@ module.exports = {
308309
&& node.value.type === 'CallExpression'
309310
&& propWrapperUtil.isPropWrapperFunction(
310311
context,
311-
context.getSourceCode().getText(node.value.callee)
312+
getSourceCode(context).getText(node.value.callee)
312313
)
313314
) {
314315
checkPropWrapperArguments(node, node.value.arguments);
@@ -334,7 +335,7 @@ module.exports = {
334335
right.type === 'CallExpression'
335336
&& propWrapperUtil.isPropWrapperFunction(
336337
context,
337-
context.getSourceCode().getText(right.callee)
338+
getSourceCode(context).getText(right.callee)
338339
)
339340
) {
340341
checkPropWrapperArguments(component.node, right.arguments);

lib/rules/destructuring-assignment.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
const Components = require('../util/Components');
88
const docsUrl = require('../util/docsUrl');
9+
const eslintUtil = require('../util/eslint');
910
const isAssignmentLHS = require('../util/ast').isAssignmentLHS;
1011
const report = require('../util/report');
1112

13+
const getSourceCode = eslintUtil.getSourceCode;
14+
1215
const DEFAULT_OPTION = 'always';
1316

1417
function createSFCParams() {
@@ -269,7 +272,7 @@ module.exports = {
269272
param.typeAnnotation ? param.typeAnnotation.range[0] : param.range[1],
270273
];
271274
return [
272-
fixer.replaceTextRange(replaceRange, context.getSourceCode().getText(node.id)),
275+
fixer.replaceTextRange(replaceRange, getSourceCode(context).getText(node.id)),
273276
fixer.remove(node.parent),
274277
];
275278
},

lib/rules/forbid-elements.js

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

88
const has = require('object.hasown/polyfill')();
99
const docsUrl = require('../util/docsUrl');
10+
const getSourceCode = require('../util/eslint').getSourceCode;
1011
const isCreateElement = require('../util/isCreateElement');
1112
const report = require('../util/report');
1213

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

9192
return {
9293
JSXOpeningElement(node) {
93-
reportIfForbidden(context.getSourceCode().getText(node.name), node.name);
94+
reportIfForbidden(getSourceCode(context).getText(node.name), node.name);
9495
},
9596

9697
CallExpression(node) {
@@ -110,7 +111,7 @@ module.exports = {
110111
} else if (argType === 'Literal' && /^[a-z][^.]*$/.test(argument.value)) {
111112
reportIfForbidden(argument.value, argument);
112113
} else if (argType === 'MemberExpression') {
113-
reportIfForbidden(context.getSourceCode().getText(argument), argument);
114+
reportIfForbidden(getSourceCode(context).getText(argument), argument);
114115
}
115116
},
116117
};

lib/rules/forbid-prop-types.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const astUtil = require('../util/ast');
1010
const docsUrl = require('../util/docsUrl');
1111
const propWrapperUtil = require('../util/propWrapper');
1212
const report = require('../util/report');
13+
const getSourceCode = require('../util/eslint').getSourceCode;
1314

1415
// ------------------------------------------------------------------------------
1516
// Constants
@@ -171,7 +172,7 @@ module.exports = {
171172
case 'CallExpression': {
172173
const innerNode = node.arguments && node.arguments[0];
173174
if (
174-
propWrapperUtil.isPropWrapperFunction(context, context.getSourceCode().getText(node.callee))
175+
propWrapperUtil.isPropWrapperFunction(context, getSourceCode(context).getText(node.callee))
175176
&& innerNode
176177
) {
177178
checkNode(innerNode);

lib/rules/function-component-definition.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const arrayIncludes = require('array-includes');
99
const Components = require('../util/Components');
1010
const docsUrl = require('../util/docsUrl');
1111
const reportC = require('../util/report');
12+
const getSourceCode = require('../util/eslint').getSourceCode;
1213

1314
// ------------------------------------------------------------------------------
1415
// Rule Definition
@@ -181,7 +182,7 @@ module.exports = {
181182
);
182183

183184
function getFixer(node, options) {
184-
const sourceCode = context.getSourceCode();
185+
const sourceCode = getSourceCode(context);
185186
const source = sourceCode.getText();
186187

187188
const typeAnnotation = getTypeAnnotation(node, source);

lib/rules/hook-use-state.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const Components = require('../util/Components');
99
const docsUrl = require('../util/docsUrl');
1010
const report = require('../util/report');
1111
const getMessageData = require('../util/message');
12+
const getSourceCode = require('../util/eslint').getSourceCode;
1213

1314
// ------------------------------------------------------------------------------
1415
// Rule Definition
@@ -160,14 +161,14 @@ module.exports = {
160161
fix: (fixer) => [
161162
// Add useMemo import, if necessary
162163
useStateReactImportSpecifier
163-
&& (!useMemoReactImportSpecifier || defaultReactImportName)
164-
&& fixer.insertTextAfter(useStateReactImportSpecifier, ', useMemo'),
164+
&& (!useMemoReactImportSpecifier || defaultReactImportName)
165+
&& fixer.insertTextAfter(useStateReactImportSpecifier, ', useMemo'),
165166
// Convert single-value destructure to simple assignment
166167
fixer.replaceTextRange(node.parent.id.range, valueVariableName),
167168
// Convert useState call to useMemo + arrow function + dependency array
168169
fixer.replaceTextRange(
169170
node.range,
170-
`${useMemoCode}(() => ${context.getSourceCode().getText(node.arguments[0])}, [])`
171+
`${useMemoCode}(() => ${getSourceCode(context).getText(node.arguments[0])}, [])`
171172
),
172173
].filter(Boolean),
173174
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
const has = require('object.hasown/polyfill')();
99
const docsUrl = require('../util/docsUrl');
10+
const getSourceCode = require('../util/eslint').getSourceCode;
1011
const report = require('../util/report');
1112

1213
// ------------------------------------------------------------------------------
@@ -170,11 +171,11 @@ module.exports = {
170171
let spaces = [];
171172
switch (expectedLocation) {
172173
case 'props-aligned':
173-
indentation = /^\s*/.exec(context.getSourceCode().lines[tokens.lastProp.firstLine - 1])[0];
174+
indentation = /^\s*/.exec(getSourceCode(context).lines[tokens.lastProp.firstLine - 1])[0];
174175
break;
175176
case 'tag-aligned':
176177
case 'line-aligned':
177-
indentation = /^\s*/.exec(context.getSourceCode().lines[tokens.opening.line - 1])[0];
178+
indentation = /^\s*/.exec(getSourceCode(context).lines[tokens.opening.line - 1])[0];
178179
break;
179180
default:
180181
indentation = '';
@@ -194,7 +195,7 @@ module.exports = {
194195
* prop and start of opening line.
195196
*/
196197
function getTokensLocations(node) {
197-
const sourceCode = context.getSourceCode();
198+
const sourceCode = getSourceCode(context);
198199
const opening = sourceCode.getFirstToken(node).loc.start;
199200
const closing = sourceCode.getLastTokens(node, node.selfClosing ? 2 : 1)[0].loc.start;
200201
const tag = sourceCode.getFirstToken(node.name).loc.start;

lib/rules/jsx-curly-brace-presence.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const arrayIncludes = require('array-includes');
1111
const docsUrl = require('../util/docsUrl');
1212
const jsxUtil = require('../util/jsx');
1313
const report = require('../util/report');
14+
const getSourceCode = require('../util/eslint').getSourceCode;
1415

1516
// ------------------------------------------------------------------------------
1617
// Constants
@@ -176,7 +177,7 @@ module.exports = {
176177

177178
let textToReplace;
178179
if (jsxUtil.isJSX(expression)) {
179-
const sourceCode = context.getSourceCode();
180+
const sourceCode = getSourceCode(context);
180181
textToReplace = sourceCode.getText(expression);
181182
} else {
182183
const expressionType = expression && expression.type;
@@ -188,7 +189,7 @@ module.exports = {
188189
: expression.raw.slice(1, -1)
189190
}"`;
190191
} else if (jsxUtil.isJSX(expression)) {
191-
const sourceCode = context.getSourceCode();
192+
const sourceCode = getSourceCode(context);
192193

193194
textToReplace = sourceCode.getText(expression);
194195
} else {
@@ -207,7 +208,7 @@ module.exports = {
207208
node: literalNode,
208209
fix(fixer) {
209210
if (jsxUtil.isJSX(literalNode)) {
210-
return fixer.replaceText(literalNode, `{${context.getSourceCode().getText(literalNode)}}`);
211+
return fixer.replaceText(literalNode, `{${getSourceCode(context).getText(literalNode)}}`);
211212
}
212213

213214
// If a HTML entity name is found, bail out because it can be fixed
@@ -251,7 +252,7 @@ module.exports = {
251252
const expression = JSXExpressionNode.expression;
252253
const expressionType = expression.type;
253254

254-
const sourceCode = context.getSourceCode();
255+
const sourceCode = getSourceCode(context);
255256
// Curly braces containing comments are necessary
256257
if (sourceCode.getCommentsInside && sourceCode.getCommentsInside(JSXExpressionNode).length > 0) {
257258
return;

lib/rules/jsx-curly-newline.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
'use strict';
66

77
const docsUrl = require('../util/docsUrl');
8+
const getSourceCode = require('../util/eslint').getSourceCode;
89
const report = require('../util/report');
910

1011
// ------------------------------------------------------------------------------
@@ -77,7 +78,7 @@ module.exports = {
7778
},
7879

7980
create(context) {
80-
const sourceCode = context.getSourceCode();
81+
const sourceCode = getSourceCode(context);
8182
const option = getNormalizedOption(context);
8283

8384
// ----------------------------------------------------------------------

lib/rules/jsx-curly-spacing.js

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

1414
const has = require('object.hasown/polyfill')();
1515
const docsUrl = require('../util/docsUrl');
16+
const getSourceCode = require('../util/eslint').getSourceCode;
1617
const report = require('../util/report');
1718

1819
// ------------------------------------------------------------------------------
@@ -175,7 +176,7 @@ module.exports = {
175176
* @returns {Object|*|{range, text}}
176177
*/
177178
function fixByTrimmingWhitespace(fixer, fromLoc, toLoc, mode, spacing) {
178-
let replacementText = context.getSourceCode().text.slice(fromLoc, toLoc);
179+
let replacementText = getSourceCode(context).text.slice(fromLoc, toLoc);
179180
if (mode === 'start') {
180181
replacementText = replacementText.replace(/^\s+/gm, '');
181182
} else {
@@ -206,7 +207,7 @@ module.exports = {
206207
token: token.value,
207208
},
208209
fix(fixer) {
209-
const nextToken = context.getSourceCode().getTokenAfter(token);
210+
const nextToken = getSourceCode(context).getTokenAfter(token);
210211
return fixByTrimmingWhitespace(fixer, token.range[1], nextToken.range[0], 'start', spacing);
211212
},
212213
});
@@ -227,7 +228,7 @@ module.exports = {
227228
token: token.value,
228229
},
229230
fix(fixer) {
230-
const previousToken = context.getSourceCode().getTokenBefore(token);
231+
const previousToken = getSourceCode(context).getTokenBefore(token);
231232
return fixByTrimmingWhitespace(fixer, previousToken.range[1], token.range[0], 'end', spacing);
232233
},
233234
});
@@ -247,7 +248,7 @@ module.exports = {
247248
token: token.value,
248249
},
249250
fix(fixer) {
250-
const sourceCode = context.getSourceCode();
251+
const sourceCode = getSourceCode(context);
251252
const nextToken = sourceCode.getTokenAfter(token);
252253
let nextComment;
253254

@@ -284,7 +285,7 @@ module.exports = {
284285
token: token.value,
285286
},
286287
fix(fixer) {
287-
const sourceCode = context.getSourceCode();
288+
const sourceCode = getSourceCode(context);
288289
const previousToken = sourceCode.getTokenBefore(token);
289290
let previousComment;
290291

@@ -370,7 +371,7 @@ module.exports = {
370371
return;
371372
}
372373

373-
const sourceCode = context.getSourceCode();
374+
const sourceCode = getSourceCode(context);
374375
const first = sourceCode.getFirstToken(node);
375376
const last = sourceCode.getLastToken(node);
376377
let second = sourceCode.getTokenAfter(first, { includeComments: true });

lib/rules/jsx-equals-spacing.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
'use strict';
77

88
const docsUrl = require('../util/docsUrl');
9+
const getSourceCode = require('../util/eslint').getSourceCode;
910
const report = require('../util/report');
1011

1112
// ------------------------------------------------------------------------------
@@ -59,7 +60,7 @@ module.exports = {
5960
return;
6061
}
6162

62-
const sourceCode = context.getSourceCode();
63+
const sourceCode = getSourceCode(context);
6364
const equalToken = sourceCode.getTokenAfter(attrNode.name);
6465
const spacedBefore = sourceCode.isSpaceBetweenTokens(attrNode.name, equalToken);
6566
const spacedAfter = sourceCode.isSpaceBetweenTokens(equalToken, attrNode.value);

lib/rules/jsx-fragments.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const variableUtil = require('../util/variable');
1111
const testReactVersion = require('../util/version').testReactVersion;
1212
const docsUrl = require('../util/docsUrl');
1313
const report = require('../util/report');
14+
const getSourceCode = require('../util/eslint').getSourceCode;
1415

1516
// ------------------------------------------------------------------------------
1617
// Rule Definition
@@ -65,7 +66,7 @@ module.exports = {
6566
}
6667

6768
function getFixerToLong(jsxFragment) {
68-
const sourceCode = context.getSourceCode();
69+
const sourceCode = getSourceCode(context);
6970
if (!jsxFragment.closingFragment || !jsxFragment.openingFragment) {
7071
// the old TS parser crashes here
7172
// TODO: FIXME: can we fake these two descriptors?
@@ -83,7 +84,7 @@ module.exports = {
8384
}
8485

8586
function getFixerToShort(jsxElement) {
86-
const sourceCode = context.getSourceCode();
87+
const sourceCode = getSourceCode(context);
8788
return function fix(fixer) {
8889
let source = sourceCode.getText();
8990
let lengthDiff;

lib/rules/jsx-handler-names.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
'use strict';
77

88
const docsUrl = require('../util/docsUrl');
9+
const getSourceCode = require('../util/eslint').getSourceCode;
910
const report = require('../util/report');
1011

1112
// ------------------------------------------------------------------------------
@@ -129,7 +130,7 @@ module.exports = {
129130

130131
const propKey = typeof node.name === 'object' ? node.name.name : node.name;
131132
const expression = node.value.expression;
132-
const propValue = context.getSourceCode()
133+
const propValue = getSourceCode(context)
133134
.getText(checkInlineFunction && isInlineHandler(node) ? expression.body.callee : expression)
134135
.replace(/\s*/g, '')
135136
.replace(/^this\.|.*::/, '');

lib/rules/jsx-indent-props.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
const astUtil = require('../util/ast');
3434
const docsUrl = require('../util/docsUrl');
35+
const getSourceCode = require('../util/eslint').getSourceCode;
3536
const reportC = require('../util/report');
3637

3738
// ------------------------------------------------------------------------------
@@ -140,7 +141,7 @@ module.exports = {
140141
* @return {Number} Indent
141142
*/
142143
function getNodeIndent(node) {
143-
let src = context.getSourceCode().getText(node, node.loc.start.column + extraColumnStart);
144+
let src = getSourceCode(context).getText(node, node.loc.start.column + extraColumnStart);
144145
const lines = src.split('\n');
145146
src = lines[0];
146147

0 commit comments

Comments
 (0)