Skip to content

Commit 434d4e8

Browse files
committed
Add no repeated member access rule
1 parent 00f0549 commit 434d4e8

File tree

6 files changed

+370
-695
lines changed

6 files changed

+370
-695
lines changed

cspell/project-words.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ typechecker
1717
tsconfig
1818
tseslint
1919
tses
20+
sonarjs
2021

2122
// AssemblyScript types and keywords
2223
i8
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# no-repeated-member-access
2+
3+
> Optimize repeated member access patterns by extracting variables
4+
5+
## Rule Details
6+
7+
This rule identifies repeated member access patterns in your code and suggests extracting them to variables for better performance and readability. In AssemblyScript, repeated property access can have performance implications (due to when they are compiled to WASM bytecode, they will induce more instructions), especially in loops or frequently called functions.
8+
9+
This rule doesn't extract computed properties/array index. These can change unexpectedly and therefore should be avoid for extraction. Examples include:
10+
11+
```ts
12+
arr[0];
13+
arr[0][1];
14+
arr[0].property;
15+
obj.arr[0].value;
16+
data.items[0].config;
17+
obj["prop"];
18+
obj[getKey()];
19+
```
20+
21+
The rule will also avoid to warn when functions are invoked upon properties, as this could have implications that alter the extracted value.
22+
Examples include:
23+
24+
```ts
25+
x = a.b.c;
26+
a.b.doSomething(); // this line will prevent a.b.c from being warned although it is used multiple times, as doSomething() could potentially change the value of a.b
27+
y = a.b.c;
28+
z = a.b.c;
29+
```
30+
31+
## Rule Options
32+
33+
This rule accepts an options object with the following properties:
34+
35+
```json
36+
{
37+
"minOccurrences": 3
38+
}
39+
```
40+
41+
- `minOccurrences` (default: 3): Minimum number of times a member chain must be accessed before triggering the rule
42+
43+
## Examples
44+
45+
### Incorrect
46+
47+
```ts
48+
// Repeated access to the same property chain (3+ times)
49+
function processData(obj: MyObject): void {
50+
if (obj.config.settings.enabled) {
51+
obj.config.settings.value = 10;
52+
console.log(obj.config.settings.name);
53+
obj.config.settings.timestamp = Date.now();
54+
}
55+
}
56+
57+
// Deep property chains accessed multiple times
58+
function renderUI(app: Application): void {
59+
app.ui.layout.header.title.text = "New Title";
60+
app.ui.layout.header.title.fontSize = 16;
61+
app.ui.layout.header.title.color = "blue";
62+
}
63+
```
64+
65+
### Correct
66+
67+
```ts
68+
// Extract repeated property access to variables
69+
function processData(obj: MyObject): void {
70+
const settings = obj.config.settings;
71+
if (settings.enabled) {
72+
settings.value = 10;
73+
console.log(settings.name);
74+
settings.timestamp = Date.now();
75+
}
76+
}
77+
78+
// Extract deep property chains
79+
function renderUI(app: Application): void {
80+
const title = app.ui.layout.header.title;
81+
title.text = "New Title";
82+
title.fontSize = 16;
83+
title.color = "blue";
84+
}
85+
86+
// Single or infrequent access is allowed
87+
function singleAccess(obj: MyObject): void {
88+
console.log(obj.config.settings.enabled); // Only accessed once
89+
}
90+
```
91+
92+
## Benefits
93+
94+
- **Performance**: Reduces redundant property lookups, especially in tight loops
95+
- **Readability**: Makes code more readable by giving meaningful names to complex property chains
96+
- **Maintainability**: Easier to update property references when extracted to variables
97+
- **Memory Efficiency**: Can reduce memory pressure in performance-critical AssemblyScript code
98+
99+
## When Not To Use
100+
101+
- If the property chains are very short (single level) and performance is not critical
102+
- When the object properties are frequently modified, making extraction less beneficial
103+
- In very simple functions where the overhead of variable extraction outweighs the benefits
104+
105+
## Related Rules
106+
107+
- Consider using this rule alongside other performance-focused rules for optimal AssemblyScript code generation

plugins/rules/incorrectReference.ts

Lines changed: 0 additions & 251 deletions
This file was deleted.

0 commit comments

Comments
 (0)