Skip to content

Commit defaea0

Browse files
committed
still wip;
1 parent 4f44e12 commit defaea0

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

plugins/rules/rewriteMember.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,39 +27,55 @@ const noRepeatedMemberAccess = createRule({
2727
defaultOptions: [{ minOccurrences: 3 }],
2828

2929
create(context, [options]) {
30+
// consider:
31+
// 1. a map to store [[scope, memberExpression[]], { count: number, modified: boolean }]
32+
// 2. process every statement we have
33+
// if it is a memberExpression, go through all its nodes and increase their count in the map
34+
// in case of assignment, update the modified flag for all nodes in the chain
35+
// 3. at the end of the scope, check if any of the chains have count >= minOccurrences
36+
// if so, report the issue and provide a fix to extract the chain into a variable
3037
const sourceCode = context.sourceCode;
3138
const minOccurrences = options.minOccurrences;
3239

33-
type scopeKey = [Scope.Scope, TSESTree.MemberExpression[]];
40+
// type scopeKey = [Scope.Scope, TSESTree.MemberExpression[]];
3441
type scopeValue = { count: number; modified: boolean };
35-
const scopeMap = new WeakMap<scopeKey, scopeValue>();
42+
const scopeMap = new WeakMap<TSESTree.MemberExpression[], scopeValue>();
43+
44+
function trackModification(node: TSESTree.MemberExpression) {
45+
scopeMap[node].modified = true;
46+
}
47+
function processMemberExpression(node: TSESTree.MemberExpression) {}
48+
3649
// ======================
3750
// Rule Listeners
3851
// ======================
3952
// These event handlers process different AST node types and track chain usage
4053
//
4154
// Examples of what each listener detects:
42-
// - MemberExpression: obj.prop.val
4355
// - AssignmentExpression: obj.prop.val = 5
4456
// - UpdateExpression: obj.prop.val++
4557
// - CallExpression: obj.prop.method()
58+
// - MemberExpression: obj.prop.val
4659
return {
4760
// Track assignment expression
4861
// Example: obj.prop.val = 5
4962
AssignmentExpression: (node) => {
50-
// track left expression and mark all of them to be modified
63+
if (node.left.type === AST_NODE_TYPES.MemberExpression) {
64+
}
5165
},
5266

5367
// Track increment/decrement operations
5468
// Example: obj.prop.counter++ modifies "obj.prop.counter"
5569
UpdateExpression: (node) => {
56-
// track expression and mark all of them to be modified
70+
if (node.argument.type === AST_NODE_TYPES.MemberExpression) {
71+
}
5772
},
5873

5974
// Track function calls that might modify their arguments
6075
// Example: obj.methods.update() might modify the "obj.methods" chain
6176
CallExpression: (node) => {
62-
// track expression and mark all of them to be modified
77+
if (node.callee.type === AST_NODE_TYPES.MemberExpression) {
78+
}
6379
},
6480

6581
// Process member expressions to identify repeated patterns

tests/rules/playground.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe("Rule: no-spread", () => {
99
ruleTester.run("no-repeated-member-access", noRepeatedMemberAccess, {
1010
valid: [
1111
`
12-
const x=a.b.c;
12+
x.y=a.b.c;
1313
`,
1414
],
1515

0 commit comments

Comments
 (0)