Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rules): replace context deprecations #416

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/rules/consistent-test-filename.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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