Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit540296f

Browse files
mdjermanovicilyavolodin
authored andcommitted
Update: enforceForClassMembers option to accessor-pairs (fixes#12063) (#12192)
1 parentd3c2334 commit540296f

File tree

3 files changed

+1034
-18
lines changed

3 files changed

+1034
-18
lines changed

‎docs/rules/accessor-pairs.md‎

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Enforces getter/setter pairs in objects (accessor-pairs)
1+
#Enforces getter/setter pairs in objectsand classes(accessor-pairs)
22

33
It's a common mistake in JavaScript to create an object with just a setter for a property but never have a corresponding getter defined for it. Without a getter, you cannot read the property, so it ends up not being used.
44

@@ -32,10 +32,14 @@ This rule enforces a style where it requires to have a getter for every property
3232

3333
By activating the option`getWithoutSet` it enforces the presence of a setter for every property which has a getter defined.
3434

35+
By default, this rule checks only object literals and property descriptors. If you want this rule
36+
to also check class declarations and class expressions, activate the option`enforceForClassMembers`.
37+
3538
##Options
3639

3740
*`setWithoutGet` set to`true` will warn for setters without getters (Default`true`).
3841
*`getWithoutSet` set to`true` will warn for getters without setters (Default`false`).
42+
*`enforceForClassMembers` set to`true` additionally applies this rule to class getters/setters (Default`false`).
3943

4044
###setWithoutGet
4145

@@ -143,6 +147,61 @@ Object.defineProperty(o, 'c', {
143147

144148
```
145149

150+
###enforceForClassMembers
151+
152+
By default, this rule does not enforce getter/setter pairs in class declarations and class expressions,
153+
as the default value for`enforceForClassMembers` is`false`.
154+
155+
When`enforceForClassMembers` is set to`true`:
156+
157+
*`"getWithoutSet": true` will also warn for getters without setters in classes.
158+
*`"setWithoutGet": true` will also warn for setters without getters in classes.
159+
160+
Examples of**incorrect** code for`{ "getWithoutSet": true, "enforceForClassMembers": true }`:
161+
162+
```js
163+
/*eslint accessor-pairs: ["error", { "getWithoutSet": true, "enforceForClassMembers": true }]*/
164+
165+
classFoo {
166+
geta() {
167+
returnthis.val;
168+
}
169+
}
170+
171+
classBar {
172+
staticgeta() {
173+
returnthis.val;
174+
}
175+
}
176+
177+
constBaz=class {
178+
geta() {
179+
returnthis.val;
180+
}
181+
staticseta(value) {
182+
this.val= value;
183+
}
184+
}
185+
```
186+
187+
Examples of**incorrect** code for`{ "setWithoutGet": true, "enforceForClassMembers": true }`:
188+
189+
```js
190+
/*eslint accessor-pairs: ["error", { "setWithoutGet": true, "enforceForClassMembers": true }]*/
191+
192+
classFoo {
193+
seta(value) {
194+
this.val= value;
195+
}
196+
}
197+
198+
constBar=class {
199+
staticseta(value) {
200+
this.val= value;
201+
}
202+
}
203+
```
204+
146205
##Known Limitations
147206

148207
Due to the limits of static analysis, this rule does not account for possible side effects and in certain cases
@@ -164,7 +223,7 @@ var o = {
164223
};
165224
```
166225

167-
Also, this rule does not disallow duplicate keys in object literals, and in certain cases with duplicate keys
226+
Also, this rule does not disallow duplicate keys in object literals and class definitions, and in certain cases with duplicate keys
168227
might not report a missing pair for a getter/setter, like in the following example:
169228

170229
```js
@@ -186,6 +245,8 @@ The code above creates an object with just a setter for the property `"a"`.
186245

187246
See[no-dupe-keys](no-dupe-keys.md) if you also want to disallow duplicate keys in object literals.
188247

248+
See[no-dupe-class-members](no-dupe-class-members.md) if you also want to disallow duplicate names in class definitions.
249+
189250
##When Not To Use It
190251

191252
You can turn this rule off if you are not concerned with the simultaneous presence of setters and getters on objects.

‎lib/rules/accessor-pairs.js‎

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ module.exports = {
152152
type:"suggestion",
153153

154154
docs:{
155-
description:"enforce getter and setter pairs in objects",
155+
description:"enforce getter and setter pairs in objects and classes",
156156
category:"Best Practices",
157157
recommended:false,
158158
url:"https://eslint.org/docs/rules/accessor-pairs"
@@ -168,6 +168,10 @@ module.exports = {
168168
setWithoutGet:{
169169
type:"boolean",
170170
default:true
171+
},
172+
enforceForClassMembers:{
173+
type:"boolean",
174+
default:false
171175
}
172176
},
173177
additionalProperties:false
@@ -177,13 +181,16 @@ module.exports = {
177181
missingGetterInPropertyDescriptor:"Getter is not present in property descriptor.",
178182
missingSetterInPropertyDescriptor:"Setter is not present in property descriptor.",
179183
missingGetterInObjectLiteral:"Getter is not present for {{ name }}.",
180-
missingSetterInObjectLiteral:"Setter is not present for {{ name }}."
184+
missingSetterInObjectLiteral:"Setter is not present for {{ name }}.",
185+
missingGetterInClass:"Getter is not present for class {{ name }}.",
186+
missingSetterInClass:"Setter is not present for class {{ name }}."
181187
}
182188
},
183189
create(context){
184190
constconfig=context.options[0]||{};
185191
constcheckGetWithoutSet=config.getWithoutSet===true;
186192
constcheckSetWithoutGet=config.setWithoutGet!==false;
193+
constenforceForClassMembers=config.enforceForClassMembers===true;
187194
constsourceCode=context.getSourceCode();
188195

189196
/**
@@ -201,6 +208,13 @@ module.exports = {
201208
loc:astUtils.getFunctionHeadLoc(node.value,sourceCode),
202209
data:{name:astUtils.getFunctionNameWithKind(node.value)}
203210
});
211+
}elseif(node.type==="MethodDefinition"){
212+
context.report({
213+
node,
214+
messageId:`${messageKind}InClass`,
215+
loc:astUtils.getFunctionHeadLoc(node.value,sourceCode),
216+
data:{name:astUtils.getFunctionNameWithKind(node.value)}
217+
});
204218
}else{
205219
context.report({
206220
node,
@@ -313,15 +327,41 @@ module.exports = {
313327
}
314328
}
315329

316-
return{
317-
ObjectExpression(node){
318-
if(checkSetWithoutGet||checkGetWithoutSet){
319-
checkObjectLiteral(node);
320-
if(isPropertyDescriptor(node)){
321-
checkPropertyDescriptor(node);
322-
}
323-
}
330+
/**
331+
* Checks the given object expression as an object literal and as a possible property descriptor.
332+
*@param {ASTNode} node `ObjectExpression` node to check.
333+
*@returns {void}
334+
*@private
335+
*/
336+
functioncheckObjectExpression(node){
337+
checkObjectLiteral(node);
338+
if(isPropertyDescriptor(node)){
339+
checkPropertyDescriptor(node);
324340
}
325-
};
341+
}
342+
343+
/**
344+
* Checks the given class body.
345+
*@param {ASTNode} node `ClassBody` node to check.
346+
*@returns {void}
347+
*@private
348+
*/
349+
functioncheckClassBody(node){
350+
constmethodDefinitions=node.body.filter(m=>m.type==="MethodDefinition");
351+
352+
checkList(methodDefinitions.filter(m=>m.static));
353+
checkList(methodDefinitions.filter(m=>!m.static));
354+
}
355+
356+
constlisteners={};
357+
358+
if(checkSetWithoutGet||checkGetWithoutSet){
359+
listeners.ObjectExpression=checkObjectExpression;
360+
if(enforceForClassMembers){
361+
listeners.ClassBody=checkClassBody;
362+
}
363+
}
364+
365+
returnlisteners;
326366
}
327367
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp