Skip to content

Commit

Permalink
fix(rules): replace context deprecations (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
driimus committed Apr 8, 2024
1 parent 76f00f6 commit 575951c
Show file tree
Hide file tree
Showing 14 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/rules/consistent-test-filename.ts
Expand Up @@ -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 {}

Expand Down
4 changes: 2 additions & 2 deletions src/rules/expect-expect.ts
Expand Up @@ -72,7 +72,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
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)
Expand Down Expand Up @@ -100,7 +100,7 @@ export default createEslintRule<Options, MESSAGE_ID>({

unchecked.push(node)
} else if (matchesAssertFunctionName(name, assertFunctionNames)) {
checkCallExpression(context.getAncestors())
checkCallExpression(context.sourceCode.getAncestors(node))
}
},
'Program:exit'() {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-commented-out-tests.ts
Expand Up @@ -25,7 +25,7 @@ export default createEslintRule<Options, MESSAGE_IDS>({
},
defaultOptions: [],
create(context) {
const sourceCode = context.getSourceCode()
const { sourceCode } = context

function checkNodeForCommentedOutTests(node: TSESTree.Comment) {
if (!hasTests(node))
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-conditional-expect.ts
Expand Up @@ -37,7 +37,7 @@ export default createEslintRule<Options, MESSAGE_ID>({

return {
FunctionDeclaration(node) {
const declaredVariables = context.getDeclaredVariables(node)
const declaredVariables = context.sourceCode.getDeclaredVariables(node)
const testCallExpressions = getTestCallExpressionsFromDeclaredVariables(declaredVariables, context)

if (testCallExpressions.length > 0)
Expand Down
6 changes: 3 additions & 3 deletions src/rules/no-done-callback.ts
Expand Up @@ -47,7 +47,7 @@ export default createEslintRule<Options, MessageIds>({
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'])
Expand Down Expand Up @@ -97,7 +97,7 @@ export default createEslintRule<Options, MessageIds>({
fix(fixer) {
const { body, params } = callback

const sourceCode = context.getSourceCode()
const { sourceCode } = context
const firstBodyToken = sourceCode.getFirstToken(body)
const lastBodyToken = sourceCode.getLastToken(body)

Expand All @@ -114,7 +114,7 @@ export default createEslintRule<Options, MessageIds>({
!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, '()')

Expand Down
4 changes: 2 additions & 2 deletions src/rules/no-large-snapshots.ts
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-test-return-statement.ts
Expand Up @@ -42,7 +42,7 @@ export default createEslintRule<Options, MessageIds>({
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
Expand Down
2 changes: 1 addition & 1 deletion src/rules/prefer-comparison-matcher.ts
Expand Up @@ -98,7 +98,7 @@ export default createEslintRule<Options, MESSAGE_IDS>({

context.report({
fix(fixer) {
const sourceCode = context.getSourceCode()
const { sourceCode } = context

const modifierText =
modifier && getAccessorValue(modifier) !== 'not'
Expand Down
2 changes: 1 addition & 1 deletion src/rules/prefer-equality-matcher.ts
Expand Up @@ -66,7 +66,7 @@ export default createEslintRule<Options, MESSAGE_IDS>({
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)}` : ''

Expand Down
2 changes: 1 addition & 1 deletion src/rules/prefer-mock-promise-shorthand.ts
Expand Up @@ -57,7 +57,7 @@ export default createEslintRule<Options, MESSAGE_IDS>({
messageId: 'useMockShorthand',
data: { replacement },
fix(fixer) {
const sourceCode = context.getSourceCode()
const { sourceCode } = context

if (innerArgNode.arguments.length > 1)
return null
Expand Down
2 changes: 1 addition & 1 deletion src/rules/prefer-spy-on.ts
Expand Up @@ -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})`
Expand Down
2 changes: 1 addition & 1 deletion src/rules/prefer-to-contain.ts
Expand Up @@ -66,7 +66,7 @@ export default createEslintRule<Options, MESSAGE_IDS>({

context.report({
fix(fixer) {
const sourceCode = context.getSourceCode()
const { sourceCode } = context

const addNotModifier = matcherArg.value === hasNot

Expand Down
Expand Up @@ -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'])
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Expand Up @@ -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)!
Expand Down

0 comments on commit 575951c

Please sign in to comment.