-
How to write AST selector to match only object key (and not also object value) ? my rule (partially working) is:
here is example: /*eslint no-restricted-syntax: ["error",
{
selector: 'Property[computed=true]:has(MemberExpression) > MemberExpression',
message: 'compute object properties are not allowed!',
}
]*/
example[T.err] = { [S.stopped]: S.running };
/* ~~~~~~~~~~~
↪ ❗️ match only object key in case it's computed
*/
example[T.okk] = { nameOk: S.running }; // ✅ here is all ok, no match
const m = $({
[S.ready]: { // ✅ here is correctly matched only object key (computed)
actions: { onEnter, onExit },
transitions: { start },
},
['notOk']: notOkValue,
nameOk: { stuff },
}); My attempts:
I tried to use
I'm using also https://astexplorer.net/ to see generated tree from code. Thank you in advance for help. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Ok, I found solution finally. ✅ SOLUTION:
✅ SOLUTION (final)://update:
BTW this is nice place to try AST selector with ESLint, however it's for Typescript: https://typescript-eslint.io/play/... |
Beta Was this translation helpful? Give feedback.
Ok, I found solution finally.
I hope that this information will be beneficial to those who encounter it in the future.
✅ SOLUTION:
ObjectExpression> Property[computed=true] > MemberExpression.key
Property[computed=true] > :expression.key
✅ SOLUTION (final):
//update:
Thank you @fisker, it's even shorter and working as expected:
Property[computed=true] > .key
BTW this is nice place to try AST selector with ESLint, however it's for Typescript: https://typescript-eslint.io/play/...