Repository navigation
Expand file tree
/
Copy patheslint-local-rules.js
More file actions
98 lines (90 loc) · 2.91 KB
/
Copy patheslint-local-rules.js
File metadata and controls
98 lines (90 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict';
/**
* no-shared-value-dot-value
*
* Forbids `.value` access on reanimated shared values — use `.get()` / `.set()`
* instead. Type-aware: only fires when the object's TypeScript type resolves
* to a reanimated SharedValue/DerivedValue, so `.value` on TextInputs, events,
* atoms or plain objects is untouched.
*
* Auto-fix: plain reads become `.get()`, plain assignments become `.set(expr)`.
* Compound assignments (`+=`, `-=`, …) are reported without a fix.
*/
const noSharedValueDotValue = {
meta: {
type: 'suggestion',
docs: {
description:
'Use .get()/.set() instead of .value on reanimated shared values',
},
fixable: 'code',
schema: [],
messages: {
read: 'Read shared values with .get() instead of .value.',
write: 'Write shared values with .set(...) instead of assigning .value.',
},
},
create(context) {
const services = context.parserServices;
if (!services || !services.program || !services.esTreeNodeToTSNodeMap) {
// No type information available (non-TS file) — skip.
return {};
}
const checker = services.program.getTypeChecker();
const isSharedValueType = node => {
const tsNode = services.esTreeNodeToTSNodeMap.get(node);
if (!tsNode) return false;
const type = checker.getTypeAtLocation(tsNode);
const name = checker.typeToString(type);
return /\b(SharedValue|DerivedValue|Mutable)\b/.test(name);
};
return {
MemberExpression(node) {
if (node.computed) return;
if (!node.property || node.property.name !== 'value') return;
if (!isSharedValueType(node.object)) return;
const sourceCode = context.getSourceCode();
const parent = node.parent;
// Write: x.value = expr
if (
parent.type === 'AssignmentExpression' &&
parent.left === node &&
parent.operator === '='
) {
context.report({
node,
messageId: 'write',
fix(fixer) {
const objectText = sourceCode.getText(node.object);
const rightText = sourceCode.getText(parent.right);
return fixer.replaceText(
parent,
`${objectText}.set(${rightText})`,
);
},
});
return;
}
// Compound write (+=, -=, …) or update (++/--): report, no auto-fix.
if (
(parent.type === 'AssignmentExpression' && parent.left === node) ||
parent.type === 'UpdateExpression'
) {
context.report({ node, messageId: 'write' });
return;
}
// Read.
context.report({
node,
messageId: 'read',
fix(fixer) {
return fixer.replaceText(node.property, 'get()');
},
});
},
};
},
};
module.exports = {
'no-shared-value-dot-value': noSharedValueDotValue,
};