From f35153c4e52e0af29287672c6a40353bd5f72900 Mon Sep 17 00:00:00 2001 From: Cristian Petre Date: Mon, 8 Apr 2024 13:24:47 +0100 Subject: [PATCH] fix(rules): replace `context` deprecations --- src/rules/consistent-test-filename.ts | 2 +- src/rules/expect-expect.ts | 4 ++-- src/rules/no-commented-out-tests.ts | 2 +- src/rules/no-conditional-expect.ts | 2 +- src/rules/no-done-callback.ts | 6 +++--- src/rules/no-large-snapshots.ts | 4 ++-- src/rules/no-test-return-statement.ts | 2 +- src/rules/prefer-comparison-matcher.ts | 2 +- src/rules/prefer-equality-matcher.ts | 2 +- src/rules/prefer-mock-promise-shorthand.ts | 2 +- src/rules/prefer-spy-on.ts | 2 +- src/rules/prefer-to-contain.ts | 2 +- .../require-local-test-context-for-concurrent-snapshots.ts | 2 +- src/utils/index.ts | 2 +- 14 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/rules/consistent-test-filename.ts b/src/rules/consistent-test-filename.ts index 7f7dc3fe..dd139339 100644 --- a/src/rules/consistent-test-filename.ts +++ b/src/rules/consistent-test-filename.ts @@ -51,7 +51,7 @@ export default createEslintRule< const pattern = typeof patternRaw === 'string' ? new RegExp(patternRaw) : patternRaw const testPattern = typeof allTestPatternRaw === 'string' ? new RegExp(allTestPatternRaw) : allTestPatternRaw - const filename = path.basename(context.getFilename()) + const filename = path.basename(context.filename) if (!testPattern.test(filename)) return {} diff --git a/src/rules/expect-expect.ts b/src/rules/expect-expect.ts index b880b51d..5f9ab78c 100644 --- a/src/rules/expect-expect.ts +++ b/src/rules/expect-expect.ts @@ -72,7 +72,7 @@ export default createEslintRule({ const index = node.type === AST_NODE_TYPES.CallExpression ? unchecked.indexOf(node) : -1 if (node.type === AST_NODE_TYPES.FunctionDeclaration) { - const declaredVariables = context.getDeclaredVariables(node) + const declaredVariables = context.sourceCode.getDeclaredVariables(node) const testCallExpressions = getTestCallExpressionsFromDeclaredVariables(declaredVariables, context) checkCallExpression(testCallExpressions) @@ -100,7 +100,7 @@ export default createEslintRule({ unchecked.push(node) } else if (matchesAssertFunctionName(name, assertFunctionNames)) { - checkCallExpression(context.getAncestors()) + checkCallExpression(context.sourceCode.getAncestors(node)) } }, 'Program:exit'() { diff --git a/src/rules/no-commented-out-tests.ts b/src/rules/no-commented-out-tests.ts index 69f5d3d5..1e1fd036 100644 --- a/src/rules/no-commented-out-tests.ts +++ b/src/rules/no-commented-out-tests.ts @@ -25,7 +25,7 @@ export default createEslintRule({ }, defaultOptions: [], create(context) { - const sourceCode = context.getSourceCode() + const { sourceCode } = context function checkNodeForCommentedOutTests(node: TSESTree.Comment) { if (!hasTests(node)) diff --git a/src/rules/no-conditional-expect.ts b/src/rules/no-conditional-expect.ts index 71a68868..a4a62cd1 100644 --- a/src/rules/no-conditional-expect.ts +++ b/src/rules/no-conditional-expect.ts @@ -37,7 +37,7 @@ export default createEslintRule({ return { FunctionDeclaration(node) { - const declaredVariables = context.getDeclaredVariables(node) + const declaredVariables = context.sourceCode.getDeclaredVariables(node) const testCallExpressions = getTestCallExpressionsFromDeclaredVariables(declaredVariables, context) if (testCallExpressions.length > 0) diff --git a/src/rules/no-done-callback.ts b/src/rules/no-done-callback.ts index 36e84524..e0a5a505 100644 --- a/src/rules/no-done-callback.ts +++ b/src/rules/no-done-callback.ts @@ -47,7 +47,7 @@ export default createEslintRule({ if (isVitestEach && node.callee.type !== AST_NODE_TYPES.TaggedTemplateExpression) return - const isInsideConcurrentTestOrDescribe = context.getAncestors().some((ancestor) => { + const isInsideConcurrentTestOrDescribe = context.sourceCode.getAncestors(node).some((ancestor) => { if (ancestor.type !== AST_NODE_TYPES.CallExpression) return false const isNotInsideDescribeOrTest = !isTypeOfVitestFnCall(ancestor, context, ['describe', 'test']) @@ -97,7 +97,7 @@ export default createEslintRule({ fix(fixer) { const { body, params } = callback - const sourceCode = context.getSourceCode() + const { sourceCode } = context const firstBodyToken = sourceCode.getFirstToken(body) const lastBodyToken = sourceCode.getLastToken(body) @@ -114,7 +114,7 @@ export default createEslintRule({ !lastBodyToken || !tokenBeforeFirstParam || !tokenAfterLastParam) - throw new Error(`Unexpected null when attempting to fix ${context.getFilename()} - please file an issue at https://github/veritem/eslint-plugin-vitest`) + throw new Error(`Unexpected null when attempting to fix ${context.filename} - please file an issue at https://github/veritem/eslint-plugin-vitest`) let argumentFix = fixer.replaceText(firstParam, '()') diff --git a/src/rules/no-large-snapshots.ts b/src/rules/no-large-snapshots.ts index 03ab012a..f31d6440 100644 --- a/src/rules/no-large-snapshots.ts +++ b/src/rules/no-large-snapshots.ts @@ -33,7 +33,7 @@ const reportOnViolation = ( node.expression.left.type === AST_NODE_TYPES.MemberExpression && isSupportedAccessor(node.expression.left.property) ) { - const fileName = context.getFilename() + const fileName = context.filename const allowedSnapshotsInFile = allowedSnapshots[fileName] if (allowedSnapshotsInFile) { @@ -93,7 +93,7 @@ export default createEslintRule<[RuleOptions], MESSAGE_IDS>({ }, defaultOptions: [{}], create(context, [options]) { - if (context.getFilename().endsWith('.snap')) { + if (context.filename.endsWith('.snap')) { return { ExpressionStatement(node) { reportOnViolation(context, node, options) diff --git a/src/rules/no-test-return-statement.ts b/src/rules/no-test-return-statement.ts index fd028976..fe4fb6c3 100644 --- a/src/rules/no-test-return-statement.ts +++ b/src/rules/no-test-return-statement.ts @@ -42,7 +42,7 @@ export default createEslintRule({ context.report({ messageId: 'noTestReturnStatement', node: returnStmt }) }, FunctionDeclaration(node) { - const declaredVariables = context.getDeclaredVariables(node) + const declaredVariables = context.sourceCode.getDeclaredVariables(node) const testCallExpressions = getTestCallExpressionsFromDeclaredVariables(declaredVariables, context) if (testCallExpressions.length === 0) return diff --git a/src/rules/prefer-comparison-matcher.ts b/src/rules/prefer-comparison-matcher.ts index 0d967705..93fbf6dd 100644 --- a/src/rules/prefer-comparison-matcher.ts +++ b/src/rules/prefer-comparison-matcher.ts @@ -98,7 +98,7 @@ export default createEslintRule({ context.report({ fix(fixer) { - const sourceCode = context.getSourceCode() + const { sourceCode } = context const modifierText = modifier && getAccessorValue(modifier) !== 'not' diff --git a/src/rules/prefer-equality-matcher.ts b/src/rules/prefer-equality-matcher.ts index c15b6661..2c482aed 100644 --- a/src/rules/prefer-equality-matcher.ts +++ b/src/rules/prefer-equality-matcher.ts @@ -66,7 +66,7 @@ export default createEslintRule({ const addNotModifier = (comparison.operator === '!==' ? !matcherValue : matcherValue) === hasNot const buildFixer = (equalityMatcher: string): TSESLint.ReportFixFunction => fixer => { - const sourceCode = context.getSourceCode() + const { sourceCode } = context let modifierText = modifier && getAccessorValue(modifier) !== 'not' ? `.${getAccessorValue(modifier)}` : '' diff --git a/src/rules/prefer-mock-promise-shorthand.ts b/src/rules/prefer-mock-promise-shorthand.ts index bde7f42b..b0bde96c 100644 --- a/src/rules/prefer-mock-promise-shorthand.ts +++ b/src/rules/prefer-mock-promise-shorthand.ts @@ -57,7 +57,7 @@ export default createEslintRule({ messageId: 'useMockShorthand', data: { replacement }, fix(fixer) { - const sourceCode = context.getSourceCode() + const { sourceCode } = context if (innerArgNode.arguments.length > 1) return null diff --git a/src/rules/prefer-spy-on.ts b/src/rules/prefer-spy-on.ts index 6106ef23..cd30281d 100644 --- a/src/rules/prefer-spy-on.ts +++ b/src/rules/prefer-spy-on.ts @@ -52,7 +52,7 @@ const getAutoFixMockImplementation = ( return '' const [arg] = vitestFnCall.arguments - const argSource = arg && context.getSourceCode().getText(arg) + const argSource = arg && context.sourceCode.getText(arg) return argSource ? `.mockImplementation(${argSource})` diff --git a/src/rules/prefer-to-contain.ts b/src/rules/prefer-to-contain.ts index 6accb6fe..a2b5357c 100644 --- a/src/rules/prefer-to-contain.ts +++ b/src/rules/prefer-to-contain.ts @@ -66,7 +66,7 @@ export default createEslintRule({ context.report({ fix(fixer) { - const sourceCode = context.getSourceCode() + const { sourceCode } = context const addNotModifier = matcherArg.value === hasNot diff --git a/src/rules/require-local-test-context-for-concurrent-snapshots.ts b/src/rules/require-local-test-context-for-concurrent-snapshots.ts index a286a224..6f3fb7e8 100644 --- a/src/rules/require-local-test-context-for-concurrent-snapshots.ts +++ b/src/rules/require-local-test-context-for-concurrent-snapshots.ts @@ -34,7 +34,7 @@ export default createEslintRule({ if (isNotASnapshotAssertion) return - const isInsideSequentialDescribeOrTest = !context.getAncestors().some((ancestor) => { + const isInsideSequentialDescribeOrTest = !context.sourceCode.getAncestors(node).some((ancestor) => { if (ancestor.type !== AST_NODE_TYPES.CallExpression) return false const isNotInsideDescribeOrTest = !isTypeOfVitestFnCall(ancestor, context, ['describe', 'test']) diff --git a/src/utils/index.ts b/src/utils/index.ts index 6c272867..8b570963 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -181,7 +181,7 @@ export const removeExtraArgumentsFixer = ( const firstArg = func.arguments[from] const lastArg = func.arguments[func.arguments.length - 1] - const sourceCode = context.getSourceCode() + const { sourceCode } = context // eslint-disable-next-line @typescript-eslint/no-non-null-assertion let tokenAfterLastParam = sourceCode.getTokenAfter(lastArg)!