-
Notifications
You must be signed in to change notification settings - Fork 2
Feat: Add no-member-access rule #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+718
−8
Merged
Changes from 16 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
2a05148
initial work on no-repeated-member-access rule
meowbmw 00f0549
second attempt; still trying to figure this out
meowbmw 434d4e8
Add no repeated member access rule
meowbmw 3fac4ff
Add more documentation on how this rule works
meowbmw 9b6f232
Merge branch 'main' into main
xpirad 7667f9d
change default minOcurrences to 3; small code clean up
meowbmw 21732a1
clean up test case
meowbmw 6221add
enable debug support
meowbmw 4f44e12
add barebone rewrite
meowbmw defaea0
still wip;
xpirad 500b0ca
wip 2: refactor old version
meowbmw edecf9f
wip 3; near completion??
xpirad 00709fb
ready for review
meowbmw a84bc60
clearer naming
meowbmw af3a92e
clean up comment
meowbmw b6e7556
clean up draft file
xpirad dd0221a
remove files to support ts debug; add force curly after if; minor adj…
meowbmw 2c569fa
refactor memberaccess using tree structures
meowbmw 7cfe59d
change test case
meowbmw 8608146
fix logic in mark modified
meowbmw 802b6d4
set class members to private & add chains cache
meowbmw 337bb8d
set modified in constructor to ensure the flag is inhereited; constru…
meowbmw f9382c6
clean up class member & rewrite path generate logic & add visitor fun…
meowbmw c105abb
Update readme
meowbmw 857cca2
Merge branch 'main' into main
xpirad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,4 @@ dist/ | |
sample_cases/ | ||
wasm-toolchain | ||
coverage | ||
.vscode | ||
reference |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"args": [ | ||
"--no-timeouts", | ||
"--colors", | ||
"--inspect-brk", | ||
"tests/rules/playground.ts" | ||
], | ||
"runtimeArgs": [ | ||
"--experimental-specifier-resolution=node", | ||
"--experimental-loader", | ||
"ts-node/esm", | ||
"--experimental-loader", | ||
"./loader.mjs", | ||
"--no-warnings" | ||
], | ||
"internalConsoleOptions": "openOnSessionStart", | ||
"name": "Mocha Tests", | ||
"program": "${workspaceRoot}/node_modules/.bin/_mocha", | ||
"request": "launch", | ||
"skipFiles": [ | ||
"${workspaceFolder}/node_modules/**/*.js", | ||
"<node_internals>/**" | ||
], | ||
"type": "node" | ||
} | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"mochaExplorer.files": "dist/tests/**/*.test.js" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# no-repeated-member-access | ||
|
||
> Optimize repeated member access patterns by extracting variables | ||
|
||
## Rule Details | ||
|
||
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. | ||
|
||
This rule doesn't extract computed properties/array index. These can change unexpectedly and therefore should be avoid for extraction. Examples include: | ||
|
||
```ts | ||
arr[0]; | ||
arr[0][1]; | ||
arr[0].property; | ||
obj.arr[0].value; | ||
data.items[0].config; | ||
obj["prop"]; | ||
obj[getKey()]; | ||
``` | ||
|
||
The rule will also avoid to warn when functions are invoked upon properties, as this could have implications that alter the extracted value. | ||
Examples include: | ||
|
||
```ts | ||
x = a.b.c; | ||
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 | ||
y = a.b.c; | ||
z = a.b.c; | ||
``` | ||
|
||
## Rule Options | ||
|
||
This rule accepts an options object with the following properties: | ||
|
||
```json | ||
{ | ||
"minOccurrences": 3 | ||
} | ||
``` | ||
|
||
- `minOccurrences` (default: 3): Minimum number of times a member chain must be accessed before triggering the rule | ||
|
||
## Examples | ||
|
||
### Incorrect | ||
|
||
```ts | ||
// Repeated access to the same property chain (3+ times) | ||
function processData(obj: MyObject): void { | ||
if (obj.config.settings.enabled) { | ||
obj.config.settings.value = 10; | ||
console.log(obj.config.settings.name); | ||
obj.config.settings.timestamp = Date.now(); | ||
} | ||
} | ||
|
||
// Deep property chains accessed multiple times | ||
function renderUI(app: Application): void { | ||
app.ui.layout.header.title.text = "New Title"; | ||
app.ui.layout.header.title.fontSize = 16; | ||
app.ui.layout.header.title.color = "blue"; | ||
} | ||
``` | ||
|
||
### Correct | ||
|
||
```ts | ||
// Extract repeated property access to variables | ||
function processData(obj: MyObject): void { | ||
const settings = obj.config.settings; | ||
if (settings.enabled) { | ||
settings.value = 10; | ||
console.log(settings.name); | ||
settings.timestamp = Date.now(); | ||
} | ||
} | ||
|
||
// Extract deep property chains | ||
function renderUI(app: Application): void { | ||
const title = app.ui.layout.header.title; | ||
title.text = "New Title"; | ||
title.fontSize = 16; | ||
title.color = "blue"; | ||
} | ||
|
||
// Single or infrequent access is allowed | ||
function singleAccess(obj: MyObject): void { | ||
console.log(obj.config.settings.enabled); // Only accessed once | ||
} | ||
``` | ||
|
||
## Benefits | ||
|
||
- **Performance**: Reduces redundant property lookups, especially in tight loops | ||
- **Readability**: Makes code more readable by giving meaningful names to complex property chains | ||
- **Maintainability**: Easier to update property references when extracted to variables | ||
|
||
## When Not To Use | ||
|
||
- If the property chains are very short (single level) and performance is not critical | ||
- When the object properties are frequently modified, making extraction less beneficial | ||
- In very simple functions where the overhead of variable extraction outweighs the benefits | ||
|
||
## Related Rules | ||
|
||
- Consider using this rule alongside other performance-focused rules for optimal AssemblyScript code generation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
let is_main = true; | ||
|
||
export const load = (url, context, loadNext) => { | ||
xpirad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (is_main) { | ||
is_main = false; | ||
|
||
if (!context.format) { | ||
context.format = "commonjs"; | ||
} | ||
} | ||
|
||
return loadNext(url, context, loadNext); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.