@lvce-editor/eslint-plugin-rpc 17.6.0 → 17.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +93 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,3 +1,94 @@
|
|
|
1
|
+
const rpcRegistryModule = '@lvce-editor/rpc-registry';
|
|
2
|
+
const rendererWorkerName = 'RendererWorker';
|
|
3
|
+
const meta$1 = {
|
|
4
|
+
docs: {
|
|
5
|
+
description: 'Disallow destructuring RendererWorker to preserve tree shaking'
|
|
6
|
+
},
|
|
7
|
+
messages: {
|
|
8
|
+
noRendererWorkerDestructuring: 'Do not destructure `RendererWorker`; call its methods directly so they can be tree-shaken.'
|
|
9
|
+
},
|
|
10
|
+
type: 'problem'
|
|
11
|
+
};
|
|
12
|
+
const findVariable = (scope, name) => {
|
|
13
|
+
let currentScope = scope;
|
|
14
|
+
while (currentScope) {
|
|
15
|
+
const variable = currentScope.set.get(name);
|
|
16
|
+
if (variable) {
|
|
17
|
+
return variable;
|
|
18
|
+
}
|
|
19
|
+
currentScope = currentScope.upper;
|
|
20
|
+
}
|
|
21
|
+
return undefined;
|
|
22
|
+
};
|
|
23
|
+
const isImportFromRpcRegistry = definition => {
|
|
24
|
+
return definition.type === 'ImportBinding' && definition.parent?.source?.value === rpcRegistryModule;
|
|
25
|
+
};
|
|
26
|
+
const isRendererWorkerImport = variable => {
|
|
27
|
+
return variable?.defs.some(definition => {
|
|
28
|
+
if (!isImportFromRpcRegistry(definition) || definition.node?.type !== 'ImportSpecifier') {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
const {
|
|
32
|
+
imported
|
|
33
|
+
} = definition.node;
|
|
34
|
+
return imported?.name === rendererWorkerName || imported?.value === rendererWorkerName;
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
const isRpcRegistryNamespaceImport = variable => {
|
|
38
|
+
return variable?.defs.some(definition => {
|
|
39
|
+
return isImportFromRpcRegistry(definition) && definition.node?.type === 'ImportNamespaceSpecifier';
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
const unwrapExpression = node => {
|
|
43
|
+
if (['ChainExpression', 'TSAsExpression', 'TSNonNullExpression', 'TSSatisfiesExpression', 'TSTypeAssertion'].includes(node?.type)) {
|
|
44
|
+
return unwrapExpression(node.expression);
|
|
45
|
+
}
|
|
46
|
+
return node;
|
|
47
|
+
};
|
|
48
|
+
const isRendererWorkerProperty = node => {
|
|
49
|
+
if (node.computed) {
|
|
50
|
+
return node.property?.type === 'Literal' && node.property.value === rendererWorkerName;
|
|
51
|
+
}
|
|
52
|
+
return node.property?.type === 'Identifier' && node.property.name === rendererWorkerName;
|
|
53
|
+
};
|
|
54
|
+
const isRendererWorkerExpression = (context, expression, node) => {
|
|
55
|
+
const unwrappedExpression = unwrapExpression(expression);
|
|
56
|
+
const scope = context.sourceCode.getScope(node);
|
|
57
|
+
if (unwrappedExpression?.type === 'Identifier') {
|
|
58
|
+
return isRendererWorkerImport(findVariable(scope, unwrappedExpression.name));
|
|
59
|
+
}
|
|
60
|
+
if (unwrappedExpression?.type !== 'MemberExpression' || unwrappedExpression.object?.type !== 'Identifier' || !isRendererWorkerProperty(unwrappedExpression)) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
return isRpcRegistryNamespaceImport(findVariable(scope, unwrappedExpression.object.name));
|
|
64
|
+
};
|
|
65
|
+
const reportObjectPattern = (context, pattern) => {
|
|
66
|
+
context.report({
|
|
67
|
+
messageId: 'noRendererWorkerDestructuring',
|
|
68
|
+
node: pattern
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
const create$1 = context => {
|
|
72
|
+
return {
|
|
73
|
+
AssignmentExpression(node) {
|
|
74
|
+
if (node.left.type === 'ObjectPattern' && isRendererWorkerExpression(context, node.right, node)) {
|
|
75
|
+
reportObjectPattern(context, node.left);
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
VariableDeclarator(node) {
|
|
79
|
+
if (node.id.type === 'ObjectPattern' && isRendererWorkerExpression(context, node.init, node)) {
|
|
80
|
+
reportObjectPattern(context, node.id);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const noRendererWorkerDestructuring = {
|
|
87
|
+
__proto__: null,
|
|
88
|
+
create: create$1,
|
|
89
|
+
meta: meta$1
|
|
90
|
+
};
|
|
91
|
+
|
|
1
92
|
const isMockRpcVariable = declaration => {
|
|
2
93
|
return declaration?.id?.type === 'Identifier' && declaration.id.name === 'mockRpc';
|
|
3
94
|
};
|
|
@@ -46,6 +137,7 @@ const plugin = {
|
|
|
46
137
|
version: '0.0.1'
|
|
47
138
|
},
|
|
48
139
|
rules: {
|
|
140
|
+
'no-renderer-worker-destructuring': noRendererWorkerDestructuring,
|
|
49
141
|
'prefer-using-mock-rpc': preferUsingMockRpc
|
|
50
142
|
}
|
|
51
143
|
};
|
|
@@ -55,6 +147,7 @@ const recommended = [{
|
|
|
55
147
|
rpc: plugin
|
|
56
148
|
},
|
|
57
149
|
rules: {
|
|
150
|
+
'rpc/no-renderer-worker-destructuring': 'error',
|
|
58
151
|
'rpc/prefer-using-mock-rpc': 'error'
|
|
59
152
|
}
|
|
60
153
|
}];
|