Skip to content

Commit

Permalink
prefer-default-parameters: Fix non-iterable visitorKeys (#1013)
Browse files Browse the repository at this point in the history
Co-authored-by: fisker <lionkay@gmail.com>
  • Loading branch information
medusalix and fisker committed Jan 25, 2021
1 parent 83575a8 commit d707e83
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
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;
}

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';
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: []
});

0 comments on commit d707e83

Please sign in to comment.