Skip to content

Commit

Permalink
feat: support desctructured properties (#68)
Browse files Browse the repository at this point in the history
* feat: suppot desctructure properties

* feat: support destructured properties

---------

Co-authored-by: sloops77 <andres.olave@thinkingbytes.net>
  • Loading branch information
sloops77 and sloops77 authored Jun 11, 2024
1 parent 229e1c7 commit e649359
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions rules/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ function getIdentifierDeclaration(identifier, node) {
const id = _.get('id', n);
if (_.get('type', id) === 'ObjectPattern') {
const destructuredProperties = _.get('properties', id) || [];
return _.find({value: {name: identifier}}, destructuredProperties);
return _.find(prop => prop.value?.name === identifier || (prop.type === 'RestElement' && prop.argument?.name === identifier), destructuredProperties);
}

if (_.get('type', id) === 'ArrayPattern') {
const destructuredElements = _.get('elements', id) || [];
return _.find({name: identifier}, destructuredElements);
return _.find(element => element.name === identifier || (element.type === 'RestElement' && element.argument?.name === identifier), destructuredElements);
}

return _.get('name', id) === identifier;
Expand Down
2 changes: 1 addition & 1 deletion test/no-mutating-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const rule = require('../rules/no-mutating-functions');

const ruleTester = avaRuleTester(test, {
env: {
es6: true,
es2020: true,
},
parserOptions: {
sourceType: 'module',
Expand Down
2 changes: 1 addition & 1 deletion test/no-mutating-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const rule = require('../rules/no-mutating-methods');

const ruleTester = avaRuleTester(test, {
env: {
es6: true,
es2020: true,
},
parserOptions: {
sourceType: 'module',
Expand Down
4 changes: 3 additions & 1 deletion test/no-mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const rule = require('../rules/no-mutation');

const ruleTester = avaRuleTester(test, {
env: {
es6: true,
es2020: true,
},
parserOptions: {
sourceType: 'module',
Expand Down Expand Up @@ -43,7 +43,9 @@ ruleTester.run('no-mutation', rule, {
'function foo(bar) { let a = bar; if (true) {a = {}}}',
'function foo(bar) { let a = bar; while (true) { if (true) { a = {} } } }',
'const a = []; a[0] = 2;',
'const [omittedIndex, ...a] = []; a[0] = 2;',
'const o = {}; o["name"] = 2;',
'const {a, b, ...o} = {a: 10, b: 20, c: 30, d: 40, e: 50}; o["b"] = 2;',
// 'let a = 2; function() { a += 2; }',
'_.reduce((acc, x) => { acc[2] = 1; return acc; }, [], [1,2,3])',
'[1,2,3].reduce((acc, x) => { acc += x; return acc; }, 0)',
Expand Down

0 comments on commit e649359

Please sign in to comment.