forked from solidjs-community/eslint-plugin-solid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-proxy-apis.ts
96 lines (91 loc) · 3.48 KB
/
no-proxy-apis.ts
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
/**
* FIXME: remove this comments and import when below issue is fixed.
* This import is necessary for type generation due to a bug in the TypeScript compiler.
* See: https://github.com/microsoft/TypeScript/issues/42873
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import type { TSESLint } from "@typescript-eslint/utils";
import { TSESTree as T, ESLintUtils } from "@typescript-eslint/utils";
import { isFunctionNode, trackImports, isPropsByName, trace } from "../utils";
const createRule = ESLintUtils.RuleCreator.withoutDocs;
export default createRule({
meta: {
type: "problem",
docs: {
description:
"Disallow usage of APIs that use ES6 Proxies, only to target environments that don't support them.",
url: "https://github.com/solidjs-community/eslint-plugin-solid/blob/main/docs/no-proxy-apis.md",
},
schema: [],
messages: {
noStore: "Solid Store APIs use Proxies, which are incompatible with your target environment.",
spreadCall:
"Using a function call in JSX spread makes Solid use Proxies, which are incompatible with your target environment.",
spreadMember:
"Using a property access in JSX spread makes Solid use Proxies, which are incompatible with your target environment.",
proxyLiteral: "Proxies are incompatible with your target environment.",
mergeProps:
"If you pass a function to `mergeProps`, it will create a Proxy, which are incompatible with your target environment.",
},
},
defaultOptions: [],
create(context) {
const { matchImport, handleImportDeclaration } = trackImports();
return {
ImportDeclaration(node) {
handleImportDeclaration(node); // track import aliases
const source = node.source.value;
if (source === "solid-js/store") {
context.report({
node,
messageId: "noStore",
});
}
},
"JSXSpreadAttribute MemberExpression"(node: T.MemberExpression) {
context.report({ node, messageId: "spreadMember" });
},
"JSXSpreadAttribute CallExpression"(node: T.CallExpression) {
context.report({ node, messageId: "spreadCall" });
},
CallExpression(node) {
if (node.callee.type === "Identifier") {
if (matchImport("mergeProps", node.callee.name)) {
node.arguments
.filter((arg) => {
if (arg.type === "SpreadElement") return true;
const traced = trace(arg, context);
return (
(traced.type === "Identifier" && !isPropsByName(traced.name)) ||
isFunctionNode(traced)
);
})
.forEach((badArg) => {
context.report({
node: badArg,
messageId: "mergeProps",
});
});
}
} else if (node.callee.type === "MemberExpression") {
if (
node.callee.object.type === "Identifier" &&
node.callee.object.name === "Proxy" &&
node.callee.property.type === "Identifier" &&
node.callee.property.name === "revocable"
) {
context.report({
node,
messageId: "proxyLiteral",
});
}
}
},
NewExpression(node) {
if (node.callee.type === "Identifier" && node.callee.name === "Proxy") {
context.report({ node, messageId: "proxyLiteral" });
}
},
};
},
});