8000 Fix: check assignment property target in camelcase (fixes #13025) (#1… · eslint/eslint@0c20bc0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c20bc0

Browse files
authored
Fix: check assignment property target in camelcase (fixes #13025) (#13027)
* Fix: check assignment property target in camelcase (fixes #13025) * check camelcase * remove useless check * fix is isAssignmentTargetPropertyInDestructuring
1 parent 8d50a7d commit 0c20bc0

File tree

2 files changed

+247
-0
lines changed

2 files changed

+247
-0
lines changed

lib/rules/camelcase.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,40 @@ module.exports = {
125125
return false;
126126
}
127127

128+
/**
129+
* Checks whether the given node represents assignment target property in destructuring.
130+
*
131+
* For examples:
132+
* ({a: b.foo} = c); // => true for `foo`
133+
* ([a.foo] = b); // => true for `foo`
134+
* ([a.foo = 1] = b); // => true for `foo`
135+
* ({...a.foo} = b); // => true for `foo`
136+
* @param {ASTNode} node An Identifier node to check
137+
* @returns {boolean} True if the node is an assignment target property in destructuring.
138+
*/
139+
function isAssignmentTargetPropertyInDestructuring(node) {
140+
if (
141+
node.parent.type === "MemberExpression" &&
142+
node.parent.property === node &&
143+
!node.parent.computed
144+
) {
145+
const effectiveParent = node.parent.parent;
146+
147+
return (
148+
effectiveParent.type === "Property" &&
149+
effectiveParent.value === node.parent &&
150+
effectiveParent.parent.type === "ObjectPattern" ||
151+
effectiveParent.type === "ArrayPattern" ||
152+
effectiveParent.type === "RestElement" ||
153+
(
154+
effectiveParent.type === "AssignmentPattern" &&
155+
effectiveParent.left === node.parent
156+
)
157+
);
158+
}
159+
return false;
160+
}
161+
128162
/**
129163
* Reports an AST node as a rule violation.
130164
* @param {ASTNode} node The node to report.
@@ -170,6 +204,9 @@ module.exports = {
170204
// Report AssignmentExpressions only if they are the left side of the assignment
171205
} else if (effectiveParent.type === "AssignmentExpression" && nameIsUnderscored && (effectiveParent.right.type !== "MemberExpression" || effectiveParent.left.type === "MemberExpression" && effectiveParent.left.property.name === node.name)) {
172206
report(node);
207+
208+
} else if (isAssignmentTargetPropertyInDestructuring(node) && nameIsUnderscored) {
209+
report(node);
173210
}
174211

175212
/*

tests/lib/rules/camelcase.js

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,59 @@ ruleTester.run("camelcase", rule, {
225225
code: "foo = { [computedBar]: 0 };",
226226
options: [{ ignoreDestructuring: true }],
227227
parserOptions: { ecmaVersion: 6 }
228+
},
229+
{
230+
code: "({ a: obj.fo_o } = bar);",
231+
options: [{ allow: ["fo_o"] }],
232+
parserOptions: { ecmaVersion: 6 }
233+
},
234+
{
235+
code: "({ a: obj.foo } = bar);",
236+
options: [{ allow: ["fo_o"] }],
237+
parserOptions: { ecmaVersion: 6 }
238+
},
239+
{
240+
code: "({ a: obj.fo_o } = bar);",
241+
options: [{ properties: "never" }],
242+
parserOptions: { ecmaVersion: 6 }
243+
},
244+
{
245+
code: "({ a: obj.fo_o.b_ar } = bar);",
246+
options: [{ properties: "never" }],
247+
parserOptions: { ecmaVersion: 6 }
248+
},
249+
{
250+
code: "({ a: { b: obj.fo_o } } = bar);",
251+
options: [{ properties: "never" }],
252+
parserOptions: { ecmaVersion: 6 }
253+
},
254+
{
255+
code: "([obj.fo_o] = bar);",
256+
options: [{ properties: "never" }],
257+
parserOptions: { ecmaVersion: 6 }
258+
},
259+
{
260+
code: "({ c: [ob.fo_o]} = bar);",
261+
options: [{ properties: "never" }],
262+
parserOptions: { ecmaVersion: 6 }
263+
},
264+
{
265+
code: "([obj.fo_o.b_ar] = bar);",
266+
options: [{ properties: "never" }],
267+
parserOptions: { ecmaVersion: 6 }
268+
},
269+
{
270+
code: "({obj} = baz.fo_o);",
271+
parserOptions: { ecmaVersion: 6 }
272+
},
273+
{
274+
code: "([obj] = baz.fo_o);",
275+
parserOptions: { ecmaVersion: 6 }
276+
},
277+
{
278+
code: "([obj.foo = obj.fo_o] = bar);",
279+
options: [{ properties: "always" }],
280+
parserOptions: { ecmaVersion: 6 }
228281
}
229282
],
230283
invalid: [
@@ -736,6 +789,163 @@ ruleTester.run("camelcase", rule, {
736789
type: "Identifier"
737790
}
738791
]
792+
},
793+
{
794+
code: "({ a: obj.fo_o } = bar);",
795+
parserOptions: { ecmaVersion: 6 },
796+
errors: [
797+
{
798+
messageId: "notCamelCase",
799+
data: { name: "fo_o" },
800+
type: "Identifier"
801+
}
802+
]
803+
},
804+
{
805+
code: "({ a: obj.fo_o } = bar);",
806+
options: [{ ignoreDestructuring: true }],
807+
parserOptions: { ecmaVersion: 6 },
808+
errors: [
809+
{
810+
messageId: "notCamelCase",
811+
data: { name: "fo_o" },
812+
type: "Identifier"
813+
}
814+
]
815+
},
816+
{
817+
code: "({ a: obj.fo_o.b_ar } = baz);",
818+
parserOptions: { ecmaVersion: 6 },
819+
errors: [
820+
{
821+
messageId: "notCamelCase",
822+
data: { name: "b_ar" },
823+
type: "Identifier"
824+
}
825+
]
826+
},
827+
{
828+
code: "({ a: { b: { c: obj.fo_o } } } = bar);",
829+
parserOptions: { ecmaVersion: 6 },
830+
errors: [
831+
{
832+
messageId: "notCamelCase",
833+
data: { name: "fo_o" },
834+
type: "Identifier"
835+
}
836+
]
837+
},
838+
{
839+
code: "({ a: { b: { c: obj.fo_o.b_ar } } } = baz);",
840+
parserOptions: { ecmaVersion: 6 },
841+
errors: [
842+
{
843+
messageId: "notCamelCase",
844+
data: { name: "b_ar" },
845+
type: "Identifier"
846+
}
847+
]
848+
},
849+
{
850+
code: "([obj.fo_o] = bar);",
851+
parserOptions: { ecmaVersion: 6 },
852+
errors: [
853+
{
854+
messageId: "notCamelCase",
855+
data: { name: "fo_o" },
856+
type: "Identifier"
857+
}
858+
]
859+
},
860+
{
861+
code: "([obj.fo_o] = bar);",
862+
options: [{ ignoreDestructuring: true }],
863+
parserOptions: { ecmaVersion: 6 },
864+
errors: [
865+
{
866+
messageId: "notCamelCase",
867+
data: { name: "fo_o" },
868+
type: "Identifier"
869+
}
870+
]
871+
},
872+
{
873+
code: "([obj.fo_o = 1] = bar);",
874+
options: [{ properties: "always" }],
875+
parserOptions: { ecmaVersion: 6 },
876+
errors: [
877+
{
878+
messageId: "notCamelCase",
879+
data: { name: "fo_o" },
880+
type: "Identifier"
881+
}
882+
]
883+
},
884+
{
885+
code: "({ a: [obj.fo_o] } = bar);",
886+
parserOptions: { ecmaVersion: 6 },
887+
errors: [
888+
{
889+
messageId: "notCamelCase",
890+
data: { name: "fo_o" },
891+
type: "Identifier"
892+
}
893+
]
894+
},
895+
{
896+
code: "({ a: { b: [obj.fo_o] } } = bar);",
897+
parserOptions: { ecmaVersion: 6 },
898+
errors: [
899+
{
900+
messageId: "notCamelCase",
901+
data: { name: "fo_o" },
902+
type: "Identifier"
903+
}
904+
]
905+
},
906+
{
907+
code: "([obj.fo_o.ba_r] = baz);",
908+
parserOptions: { ecmaVersion: 6 },
909+
errors: [
910+
{
911+
messageId: "notCamelCase",
912+
data: { name: "ba_r" },
913+
type: "Identifier"
914+
}
915+
]
916+
},
917+
{
918+
code: "({...obj.fo_o} = baz);",
919+
parserOptions: { ecmaVersion: 9 },
920+
errors: [
921+
{
922+
messageId: "notCamelCase",
923+
data: { name: "fo_o" },
924+
type: "Identifier"
925+
}
926+
]
927+
},
928+
{
929+
code: "({...obj.fo_o.ba_r} = baz);",
930+
parserOptions: { ecmaVersion: 9 },
931+
errors: [
932+
{
933+
messageId: "notCamelCase",
934+
data: { name: "ba_r" },
935+
type: "Identifier"
936+
}
937+
]
938+
},
939+
{
940+
code: "({c: {...obj.fo_o }} = baz);",
941+
parserOptions: { ecmaVersion: 9 },
942+
errors: [
943+
{
944+
messageId: "notCamelCase",
945+
data: { name: "fo_o" },
946+
type: "Identifier"
947+
}
948+
]
739949
}
740950
]
741951
});

0 commit comments

Comments
 (0)
0