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

prefer-default-parameters: Fix non-iterable visitorKeys #1013

Merged
merged 9 commits into from Jan 25, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion rules/prefer-default-parameters.js
@@ -1,4 +1,5 @@
'use strict';
const eslintVisitorKeys = require('eslint-visitor-keys');
const {findVariable} = require('eslint-utils');
const getDocumentationUrl = require('./utils/get-documentation-url');

Expand Down Expand Up @@ -33,7 +34,12 @@ const containsCallExpression = (source, node) => {
return true;
}

medusalix marked this conversation as resolved.
Show resolved Hide resolved
for (const key of source.visitorKeys[node.type]) {
// The Babel AST doesn't have visitor keys for certain types of nodes
// Use `eslint-visitor-keys` in those cases
// TODO: Remove this when we drop support for `babel-eslint` #1040
const keys = source.visitorKeys[node.type] || eslintVisitorKeys.KEYS[node.type];

for (const key of keys) {
const value = node[key];

if (Array.isArray(value)) {
Expand Down
37 changes: 27 additions & 10 deletions test/prefer-default-parameters.js
@@ -1,13 +1,5 @@
import test from 'ava';
medusalix marked this conversation as resolved.
Show resolved Hide resolved
import avaRuleTester from 'eslint-ava-rule-tester';
import {outdent} from 'outdent';
import rule from '../rules/prefer-default-parameters.js';

const ruleTester = avaRuleTester(test, {
parserOptions: {
ecmaVersion: 2020
}
});
import {test} from './utils/test.js';

const invalidTestCase = ({code, suggestions}) => {
if (!suggestions) {
Expand All @@ -33,7 +25,7 @@ const invalidTestCase = ({code, suggestions}) => {
};
};

ruleTester.run('prefer-default-parameters', rule, {
test({
valid: [
'function abc(foo = { bar: 123 }) { }',
'function abc({ bar } = { bar: 123 }) { }',
Expand Down Expand Up @@ -655,3 +647,28 @@ ruleTester.run('prefer-default-parameters', rule, {
})
]
});

test.babelLegacy({
valid: [
// These tests verify that the fallback to `eslint-visitor-keys` is working correctly
outdent`
function abc(foo, bar) {
const { baz, ...rest } = bar;
foo = foo || 123;
}
`,
outdent`
function abc(foo, bar) {
const baz = foo?.bar;
foo = foo || 123;
}
`,
outdent`
function abc(foo, bar) {
import('foo');
foo = foo || 123;
}
`
],
invalid: []
});