Skip to content

Commit 06a8374

Browse files
committed
refactor: replace usages of context.getSourceCode
1 parent 098d639 commit 06a8374

7 files changed

+31
-24
lines changed

src/rules/prefer-empty.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @fileoverview Prefer toBeEmpty over checking innerHTML
33
* @author Ben Monro
44
*/
5+
import { getSourceCode } from '../context';
56

67
export const meta = {
78
docs: {
@@ -16,7 +17,7 @@ export const meta = {
1617
export const create = (context) => {
1718
function isNonEmptyStringOrTemplateLiteral(node) {
1819
return !['""', "''", "``", "null"].includes(
19-
context.getSourceCode().getText(node)
20+
getSourceCode(context).getText(node)
2021
);
2122
}
2223

src/rules/prefer-in-document.js

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

88
import { queries } from "../queries";
99
import { getAssignmentForIdentifier } from "../assignment-ast";
10+
import { getSourceCode } from '../context';
1011

1112
export const meta = {
1213
type: "suggestion",
@@ -186,7 +187,7 @@ export const create = (context) => {
186187

187188
// Remove any arguments in the matcher
188189
for (const argument of Array.from(matcherArguments)) {
189-
const sourceCode = context.getSourceCode();
190+
const sourceCode = getSourceCode(context);
190191
const token = sourceCode.getTokenAfter(argument);
191192
if (token.value === "," && token.type === "Punctuator") {
192193
// Remove commas if toHaveLength had more than one argument or a trailing comma

src/rules/prefer-to-have-attribute.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @fileoverview prefer toHaveAttribute over checking getAttribute/hasAttribute
33
* @author Ben Monro
44
*/
5+
import { getSourceCode } from '../context';
56

67
//------------------------------------------------------------------------------
78
// Rule Definition
@@ -42,7 +43,7 @@ export const create = (context) => ({
4243
[`CallExpression[callee.property.name='getAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toContain$|toMatch$/]`](
4344
node
4445
) {
45-
const sourceCode = context.getSourceCode();
46+
const sourceCode = getSourceCode(context);
4647
context.report({
4748
node: node.parent,
4849
message: `Use toHaveAttribute instead of asserting on getAttribute`,
@@ -66,7 +67,7 @@ export const create = (context) => ({
6667
const arg = node.parent.parent.parent.arguments;
6768
const isNull = arg.length > 0 && arg[0].value === null;
6869

69-
const sourceCode = context.getSourceCode();
70+
const sourceCode = getSourceCode(context);
7071
context.report({
7172
node: node.parent,
7273
message: `Use toHaveAttribute instead of asserting on getAttribute`,
@@ -127,7 +128,7 @@ export const create = (context) => ({
127128
),
128129
fixer.replaceText(
129130
node.parent.parent.parent.arguments[0],
130-
context.getSourceCode().getText(node.arguments[0])
131+
getSourceCode(context).getText(node.arguments[0])
131132
),
132133
],
133134
});

src/rules/prefer-to-have-class.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import { getQueryNodeFrom } from "../assignment-ast";
7+
import { getSourceCode } from '../context';
78

89
//------------------------------------------------------------------------------
910
// Rule Definition
@@ -48,11 +49,11 @@ export const create = (context) => ({
4849
matcherArg
4950
? fixer.replaceText(
5051
matcherArg,
51-
context.getSourceCode().getText(classValue)
52+
getSourceCode(context).getText(classValue)
5253
)
5354
: fixer.insertTextBefore(
54-
context.getSourceCode().getTokenAfter(matcher, { skip: 1 }),
55-
context.getSourceCode().getText(classValue)
55+
getSourceCode(context).getTokenAfter(matcher, { skip: 1 }),
56+
getSourceCode(context).getText(classValue)
5657
),
5758
];
5859
},
@@ -82,7 +83,7 @@ export const create = (context) => ({
8283
fixer.replaceText(matcher, "toHaveClass"),
8384
fixer.replaceText(
8485
classValue,
85-
context.getSourceCode().getText(classValue)
86+
getSourceCode(context).getText(classValue)
8687
),
8788
];
8889
},
@@ -133,7 +134,7 @@ export const create = (context) => ({
133134
fixer.replaceText(matcher, "toHaveClass"),
134135
fixer.replaceText(
135136
classValue,
136-
`${context.getSourceCode().getText(classValue)}${
137+
`${getSourceCode(context).getText(classValue)}${
137138
matcher.name === "toContain" ? "" : ", { exact: true }"
138139
}`
139140
),
@@ -164,7 +165,7 @@ export const create = (context) => ({
164165
fixer.replaceText(matcher, "toHaveClass"),
165166
fixer.replaceText(
166167
node.arguments[0],
167-
`${context.getSourceCode().getText(classValue)}`
168+
`${getSourceCode(context).getText(classValue)}`
168169
),
169170
];
170171
},
@@ -195,7 +196,7 @@ export const create = (context) => ({
195196
fixer.replaceText(matcher, "toHaveClass"),
196197
fixer.replaceText(
197198
classValue,
198-
`${context.getSourceCode().getText(classValue)}${
199+
`${getSourceCode(context).getText(classValue)}${
199200
matcher.name === "toContain" ? "" : ", { exact: true }"
200201
}`
201202
),
@@ -237,7 +238,7 @@ export const create = (context) => ({
237238
fixer.replaceText(matcher, "toHaveClass"),
238239
fixer.replaceText(
239240
classArg,
240-
context.getSourceCode().getText(classValueArg)
241+
getSourceCode(context).getText(classValueArg)
241242
),
242243
fixer.replaceText(classValueArg, `{ exact: true }`),
243244
];
@@ -278,7 +279,7 @@ export const create = (context) => ({
278279
fixer.replaceText(matcher, "toHaveClass"),
279280
fixer.replaceText(
280281
classArg,
281-
context.getSourceCode().getText(classValueArg)
282+
getSourceCode(context).getText(classValueArg)
282283
),
283284
fixer.replaceText(classValueArg, `{ exact: true }`),
284285
];
@@ -321,7 +322,7 @@ export const create = (context) => ({
321322
fixer.replaceText(matcher, "toHaveClass"),
322323
fixer.replaceText(
323324
classArg,
324-
context.getSourceCode().getText(classValueArg)
325+
getSourceCode(context).getText(classValueArg)
325326
),
326327
fixer.removeRange([classArg.range[1], classValue.range[1]]),
327328
];

src/rules/prefer-to-have-style.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @fileoverview prefer toHaveStyle over checking element style
33
* @author Ben Monro
44
*/
5+
import { getSourceCode } from '../context';
56

67
//------------------------------------------------------------------------------
78
// Rule Definition
@@ -23,16 +24,16 @@ export const create = (context) => {
2324
return camelCase(styleName.value);
2425
}
2526

26-
return `[${context.getSourceCode().getText(styleName)}]`;
27+
return `[${getSourceCode(context).getText(styleName)}]`;
2728
}
2829
function getReplacementStyleParam(styleName, styleValue) {
2930
return styleName.type === "Literal"
3031
? `{${camelCase(styleName.value)}: ${context
3132
.getSourceCode()
3233
.getText(styleValue)}}`
33-
: `${context.getSourceCode().getText(styleName).slice(0, -1)}: ${
34+
: `${getSourceCode(context).getText(styleName).slice(0, -1)}: ${
3435
styleValue.type === "TemplateLiteral"
35-
? context.getSourceCode().getText(styleValue).substring(1)
36+
? getSourceCode(context).getText(styleValue).substring(1)
3637
: `${styleValue.value}\``
3738
}`;
3839
}
@@ -104,7 +105,7 @@ export const create = (context) => {
104105
styleName,
105106
styleName.type === "Literal"
106107
? `{${camelCase(styleName.value)}: expect.anything()}`
107-
: context.getSourceCode().getText(styleName)
108+
: getSourceCode(context).getText(styleName)
108109
),
109110
];
110111
},
@@ -128,7 +129,7 @@ export const create = (context) => {
128129
styleName,
129130
styleName.type === "Literal"
130131
? `{${camelCase(styleName.value)}: expect.anything()}`
131-
: context.getSourceCode().getText(styleName)
132+
: getSourceCode(context).getText(styleName)
132133
),
133134
];
134135
},

src/rules/prefer-to-have-text-content.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @fileoverview prefer toHaveAttribute over checking getAttribute/hasAttribute
33
* @author Ben Monro
44
*/
5+
import { getSourceCode } from '../context';
56

67
export const meta = {
78
docs: {
@@ -19,7 +20,7 @@ export const create = (context) => ({
1920
) {
2021
const expectedArg = node.parent.parent.parent.arguments[0];
2122

22-
const expectedArgSource = context.getSourceCode().getText(expectedArg);
23+
const expectedArgSource = getSourceCode(context).getText(expectedArg);
2324
context.report({
2425
node: node.parent,
2526
message: `Use toHaveTextContent instead of asserting on DOM node attributes`,
@@ -80,7 +81,7 @@ export const create = (context) => ({
8081
node
8182
) {
8283
const expectedArg = node.parent.parent.parent.parent.arguments[0];
83-
const expectedArgSource = context.getSourceCode().getText(expectedArg);
84+
const expectedArgSource = getSourceCode(context).getText(expectedArg);
8485
context.report({
8586
node: node.parent,
8687
message: `Use toHaveTextContent instead of asserting on DOM node attributes`,

src/rules/prefer-to-have-value.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import { getQueryNodeFrom } from "../assignment-ast";
7+
import { getSourceCode } from '../context';
78

89
//------------------------------------------------------------------------------
910
// Rule Definition
@@ -53,7 +54,7 @@ export const create = (context) => {
5354
node,
5455
fix(fixer) {
5556
return [
56-
fixer.remove(context.getSourceCode().getTokenBefore(valueProp)),
57+
fixer.remove(getSourceCode(context).getTokenBefore(valueProp)),
5758
fixer.remove(valueProp),
5859
fixer.replaceText(matcher, "toHaveValue"),
5960
];
@@ -79,7 +80,7 @@ export const create = (context) => {
7980
fix(fixer) {
8081
return [
8182
fixer.removeRange([
82-
context.getSourceCode().getTokenBefore(valueProp).range[0],
83+
getSourceCode(context).getTokenBefore(valueProp).range[0],
8384
valueProp.range[1],
8485
]),
8586
fixer.replaceText(matcher, "toHaveValue"),

0 commit comments

Comments
 (0)