Skip to content

Commit cb2b9d4

Browse files
authored
fix: avoid invalid object-shorthand fixes for super methods (#21323)
1 parent d23a7f9 commit cb2b9d4

3 files changed

Lines changed: 117 additions & 2 deletions

File tree

‎docs/src/rules/object-shorthand.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ Additionally, the rule takes an optional object configuration:
111111
* `"methodsIgnorePattern"` (`string`) for methods whose names match this regex pattern, the method shorthand will not be enforced. Note that this option can only be used when the string option is set to `"always"` or `"methods"`.
112112
* `"avoidExplicitReturnArrows": true` indicates that methods are preferred over explicit-return arrow functions for function properties. (By default, the rule allows either of these.) Note that this option can only be enabled when the string option is set to `"always"` or `"methods"`.
113113

114+
Object methods that provide the binding for a `super` reference are ignored when `"never"` or `"avoidQuotes"` would otherwise require long-form syntax because converting them would produce invalid JavaScript.
115+
114116
### avoidQuotes
115117

116118
```json

‎lib/rules/object-shorthand.js‎

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,42 @@ function isShorthand(property) {
104104
return property.shorthand || property.method;
105105
}
106106

107+
/**
108+
* Finds the object method that provides the `super` binding for a node.
109+
* @param {ASTNode} node A node that may be inside an object method.
110+
* @param {SourceCode} sourceCode The source code object.
111+
* @returns {ASTNode|null} The containing object method, or `null` if the `super` reference is in another scope.
112+
* @private
113+
*/
114+
function getContainingObjectMethod(node, sourceCode) {
115+
for (
116+
let scope = sourceCode.getScope(node).variableScope;
117+
scope;
118+
scope = scope.upper?.variableScope
119+
) {
120+
if (
121+
scope.type === "class-field-initializer" ||
122+
scope.type === "class-static-block"
123+
) {
124+
return null;
125+
}
126+
127+
const functionNode = scope.block;
128+
129+
if (
130+
functionNode.type === "FunctionExpression" ||
131+
functionNode.type === "FunctionDeclaration"
132+
) {
133+
return functionNode.parent.type === "Property" &&
134+
functionNode.parent.method
135+
? functionNode.parent
136+
: null;
137+
}
138+
}
139+
140+
return null;
141+
}
142+
107143
/**
108144
* Determines if the property's key and method or value are named equally.
109145
* @param {ASTNode} property Property AST node
@@ -426,6 +462,7 @@ module.exports = {
426462
*/
427463
const lexicalScopeStack = [];
428464
const arrowsWithLexicalIdentifiers = new WeakSet();
465+
const methodsWithSuper = new WeakSet();
429466
const argumentsIdentifiers = new WeakSet();
430467

431468
/**
@@ -487,7 +524,14 @@ module.exports = {
487524
},
488525

489526
ThisExpression: reportLexicalIdentifier,
490-
Super: reportLexicalIdentifier,
527+
Super(node) {
528+
reportLexicalIdentifier();
529+
530+
const method = getContainingObjectMethod(node, sourceCode);
531+
if (method) {
532+
methodsWithSuper.add(method);
533+
}
534+
},
491535
MetaProperty(node) {
492536
if (
493537
node.meta.name === "new" &&
@@ -541,6 +585,7 @@ module.exports = {
541585
if (isConciseProperty) {
542586
if (
543587
node.method &&
588+
!methodsWithSuper.has(node) &&
544589
(APPLY_NEVER ||
545590
(AVOID_QUOTES && isStringLiteral(node.key)))
546591
) {
@@ -554,7 +599,7 @@ module.exports = {
554599
messageId,
555600
fix: fixer => makeFunctionLongform(fixer, node),
556601
});
557-
} else if (APPLY_NEVER) {
602+
} else if (APPLY_NEVER && !node.method) {
558603
// { x } should be written as { x: x }
559604
context.report({
560605
node,

‎tests/lib/rules/object-shorthand.js‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,37 @@ ruleTester.run("object-shorthand", rule, {
141141
options: ["never"],
142142
},
143143

144+
// https://github.com/eslint/eslint/issues/21320
145+
{
146+
code: "const obj = { describe() { return super.toString(); } };",
147+
options: ["never"],
148+
},
149+
{
150+
code: "const obj = { async *f() { yield super.x; } };",
151+
options: ["never"],
152+
},
153+
{
154+
code: "const obj = { f(a = super.x) {} };",
155+
options: ["never"],
156+
},
157+
{
158+
code: "const obj = { outer() { super.x; return { inner() { return super.y; } }; } };",
159+
options: ["never"],
160+
},
161+
{
162+
code: "const obj = { describe() { return () => super.toString(); } };",
163+
options: ["never"],
164+
},
165+
{
166+
code: "const obj = { 'describe'() { return super.toString(); } };",
167+
options: ["always", { avoidQuotes: true }],
168+
},
169+
{
170+
code: "const obj = { outer() { return class { [super.key] = 1; }; } };",
171+
options: ["never"],
172+
languageOptions: { ecmaVersion: 2022 },
173+
},
174+
144175
// ignoreConstructors
145176
{
146177
code: "var x = {ConstructorFunction: function(){}, a: b}",
@@ -920,6 +951,31 @@ ruleTester.run("object-shorthand", rule, {
920951
options: ["never"],
921952
errors: [LONGFORM_METHOD_ERROR],
922953
},
954+
{
955+
code: "const obj = { outer() { return { inner() { return super.value; } }; } };",
956+
output: "const obj = { outer: function() { return { inner() { return super.value; } }; } };",
957+
options: ["never"],
958+
errors: [LONGFORM_METHOD_ERROR],
959+
},
960+
{
961+
code: "const obj = { outer() { return class extends Base { inner() { return super.value; } }; } };",
962+
output: "const obj = { outer: function() { return class extends Base { inner() { return super.value; } }; } };",
963+
options: ["never"],
964+
errors: [LONGFORM_METHOD_ERROR],
965+
},
966+
{
967+
code: "const obj = { outer() { return class extends Base { field = super.value; static { super.value; } }; } };",
968+
output: "const obj = { outer: function() { return class extends Base { field = super.value; static { super.value; } }; } };",
969+
options: ["never"],
970+
languageOptions: { ecmaVersion: 2022 },
971+
errors: [LONGFORM_METHOD_ERROR],
972+
},
973+
{
974+
code: "const obj = { outer() { return { [super.key]() {} }; } };",
975+
output: "const obj = { outer() { return { [super.key]: function() {} }; } };",
976+
options: ["never"],
977+
errors: [LONGFORM_METHOD_ERROR],
978+
},
923979
{
924980
code: "var x = {notConstructorFunction(){}, b: c}",
925981
output: "var x = {notConstructorFunction: function(){}, b: c}",
@@ -1080,6 +1136,18 @@ ruleTester.run("object-shorthand", rule, {
10801136
options: ["always", { avoidQuotes: true }],
10811137
errors: [LONGFORM_METHOD_STRING_LITERAL_ERROR],
10821138
},
1139+
{
1140+
code: "const obj = { 'ok'() {}, 'bad'() { return super.x; } };",
1141+
output: "const obj = { 'ok': function() {}, 'bad'() { return super.x; } };",
1142+
options: ["always", { avoidQuotes: true }],
1143+
errors: [
1144+
{
1145+
...LONGFORM_METHOD_STRING_LITERAL_ERROR,
1146+
column: 15,
1147+
endColumn: 24,
1148+
},
1149+
],
1150+
},
10831151
{
10841152
code: "var x = {['a'](){}}",
10851153
output: "var x = {['a']: function(){}}",

0 commit comments

Comments
 (0)