Skip to content

Commit

Permalink
consistent-destructuring: Add ExperimentalRestProperty check (#1060)
Browse files Browse the repository at this point in the history
  • Loading branch information
medusalix committed Jan 24, 2021
1 parent 75c477f commit aca2ec5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
5 changes: 4 additions & 1 deletion rules/consistent-destructuring.js
Expand Up @@ -92,7 +92,10 @@ const create = context => {
property.value.type === 'Identifier'
);
const lastProperty = objectPattern.properties[objectPattern.properties.length - 1];
const hasRest = lastProperty && lastProperty.type === 'RestElement';

// TODO: Remove `ExperimentalRestProperty` check when we drop support for `babel-eslint` #1040
const hasRest = lastProperty &&
(lastProperty.type === 'RestElement' || lastProperty.type === 'ExperimentalRestProperty');

const expression = source.getText(node);
const member = source.getText(node.property);
Expand Down
33 changes: 23 additions & 10 deletions test/consistent-destructuring.js
@@ -1,13 +1,5 @@
import test from 'ava';
import avaRuleTester from 'eslint-ava-rule-tester';
import {outdent} from 'outdent';
import rule from '../rules/consistent-destructuring.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('consistent-destructuring', rule, {
test({
valid: [
'console.log(foo.a, foo.b);',
'const foo = 10;',
Expand Down Expand Up @@ -476,3 +468,24 @@ ruleTester.run('consistent-destructuring', rule, {
}
]
});

test.babelLegacy({
valid: [
outdent`
const {a, ...b} = bar;
console.log(bar.c);
`
],
invalid: [
invalidTestCase({
code: outdent`
const {a, ...b} = bar;
console.log(bar.a);
`,
suggestions: [outdent`
const {a, ...b} = bar;
console.log(a);
`]
})
]
});

0 comments on commit aca2ec5

Please sign in to comment.