-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathdisallowSideEffects.js
More file actions
220 lines (208 loc) · 8.23 KB
/
disallowSideEffects.js
File metadata and controls
220 lines (208 loc) · 8.23 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import path from 'node:path'
export default {
meta: {
docs: {
description:
'Disallow potential side effects when evaluating modules, to ensure modules content are tree-shakable.',
recommended: false,
},
schema: [],
},
create(context) {
const filename = context.getFilename()
if (pathsWithSideEffect.has(filename)) {
return {}
}
return {
Program(node) {
reportPotentialSideEffect(context, node)
},
}
},
}
const packagesRoot = path.resolve(import.meta.dirname, '..', 'packages')
// Those modules are known to have side effects when evaluated
const pathsWithSideEffect = new Set([
`${packagesRoot}/logs/src/entries/main.ts`,
`${packagesRoot}/rum/src/entries/main.ts`,
`${packagesRoot}/rum-slim/src/entries/main.ts`,
`${packagesRoot}/debugger/src/entries/main.ts`,
])
// Those packages are known to have no side effects when evaluated
const packagesWithoutSideEffect = new Set([
'@datadog/browser-core',
'@datadog/browser-rum-core',
'@datadog/browser-rum-react/internal',
'react',
'react-router-dom',
'vue',
'vue-router',
'@angular/core',
'@angular/router',
'rxjs',
])
/**
* Iterate over the given node and its children, and report any node that may have a side effect
* when evaluated.
*
* @example
* const foo = 1 // OK, this statement can't have any side effect
* foo() // KO, we don't know what 'foo' does, report this
* function bar() { // OK, a function declaration doesn't have side effects
* foo() // OK, this statement won't be executed when evaluating the module code
* }
*/
function reportPotentialSideEffect(context, node) {
// This acts like an authorized list of syntax nodes to use directly in the body of a module. All
// those nodes should not have a side effect when evaluated.
//
// This list is probably not complete, feel free to add more cases if you encounter an unhandled
// node.
switch (node.type) {
case 'Program':
node.body.forEach((child) => reportPotentialSideEffect(context, child))
return
case 'TemplateLiteral':
node.expressions.forEach((child) => reportPotentialSideEffect(context, child))
return
case 'ExportNamedDeclaration':
case 'ExportAllDeclaration':
case 'ImportDeclaration':
if (node.declaration) {
reportPotentialSideEffect(context, node.declaration)
} else if (
node.source &&
node.importKind !== 'type' &&
!isAllowedImport(context.getFilename(), node.source.value)
) {
context.report({
node: node.source,
message: 'This file cannot import modules with side-effects',
})
}
return
case 'VariableDeclaration':
node.declarations.forEach((child) => reportPotentialSideEffect(context, child))
return
case 'VariableDeclarator':
if (node.init) {
reportPotentialSideEffect(context, node.init)
}
return
case 'ArrayExpression':
node.elements.forEach((child) => reportPotentialSideEffect(context, child))
return
case 'UnaryExpression':
reportPotentialSideEffect(context, node.argument)
return
case 'ObjectExpression':
node.properties.forEach((child) => reportPotentialSideEffect(context, child))
return
case 'SpreadElement':
reportPotentialSideEffect(context, node.argument)
return
case 'Property':
reportPotentialSideEffect(context, node.key)
reportPotentialSideEffect(context, node.value)
return
case 'BinaryExpression':
case 'LogicalExpression':
reportPotentialSideEffect(context, node.left)
reportPotentialSideEffect(context, node.right)
return
case 'TSAsExpression':
case 'ExpressionStatement':
reportPotentialSideEffect(context, node.expression)
return
case 'MemberExpression':
reportPotentialSideEffect(context, node.object)
reportPotentialSideEffect(context, node.property)
return
case 'ConditionalExpression':
case 'FunctionExpression':
case 'ArrowFunctionExpression':
case 'FunctionDeclaration':
case 'ClassDeclaration':
case 'TSEnumDeclaration':
case 'TSInterfaceDeclaration':
case 'TSTypeAliasDeclaration':
case 'TSModuleDeclaration':
case 'TSDeclareFunction':
case 'TSInstantiationExpression':
case 'Literal':
case 'Identifier':
return
case 'CallExpression':
if (isAllowedCallExpression(node)) {
return
}
break
case 'NewExpression':
if (isAllowedNewExpression(node)) {
return
}
break
}
// If the node doesn't match any of the condition above, report it
context.report({
node,
message: `${node.type} can have side effects when the module is evaluated. \
Maybe move it in a function declaration?`,
})
}
/**
* Make sure an 'import' statement does not pull a module or package with side effects.
*/
function isAllowedImport(basePath, source) {
if (source.startsWith('.')) {
const resolvedPath = `${path.resolve(path.dirname(basePath), source)}.ts`
return !pathsWithSideEffect.has(resolvedPath)
}
return packagesWithoutSideEffect.has(source)
}
/**
* Authorize some call expressions. Feel free to add more exceptions here. Good candidates would
* be functions that are known to be ECMAScript functions without side effects, that are likely to
* be considered as pure functions by the bundler.
*
* You can experiment with Rollup tree-shaking strategy to ensure your function is known to be pure.
* https://rollupjs.org/repl/?version=2.38.5&shareable=JTdCJTIybW9kdWxlcyUyMiUzQSU1QiU3QiUyMm5hbWUlMjIlM0ElMjJtYWluLmpzJTIyJTJDJTIyY29kZSUyMiUzQSUyMiUyRiUyRiUyMFB1cmUlMjBmdW5jdGlvbnMlNUNubmV3JTIwV2Vha01hcCgpJTVDbk9iamVjdC5rZXlzKCklNUNuJTVDbiUyRiUyRiUyMFNpZGUlMjBlZmZlY3QlMjBmdW5jdGlvbnMlNUNuZm9vKCklMjAlMkYlMkYlMjB1bmtub3duJTIwZnVuY3Rpb25zJTIwYXJlJTIwY29uc2lkZXJlZCUyMHRvJTIwaGF2ZSUyMHNpZGUlMjBlZmZlY3RzJTVDbmFsZXJ0KCdhYWEnKSU1Q25uZXclMjBNdXRhdGlvbk9ic2VydmVyKCgpJTIwJTNEJTNFJTIwJTdCJTdEKSUyMiUyQyUyMmlzRW50cnklMjIlM0F0cnVlJTdEJTVEJTJDJTIyb3B0aW9ucyUyMiUzQSU3QiUyMmZvcm1hdCUyMiUzQSUyMmVzJTIyJTJDJTIybmFtZSUyMiUzQSUyMm15QnVuZGxlJTIyJTJDJTIyYW1kJTIyJTNBJTdCJTIyaWQlMjIlM0ElMjIlMjIlN0QlMkMlMjJnbG9iYWxzJTIyJTNBJTdCJTdEJTdEJTJDJTIyZXhhbXBsZSUyMiUzQW51bGwlN0Q=
*
* Webpack is not as smart as Rollup, and it usually treat all call expressions as impure, but it
* could be fine to allow it nonetheless at it pulls very little code.
*/
function isAllowedCallExpression({ callee }) {
// Allow "Object.keys()"
if (callee.type === 'MemberExpression' && callee.object.name === 'Object' && callee.property.name === 'keys') {
return true
}
// Allow ".concat()"
if (callee.type === 'MemberExpression' && callee.property.name === 'concat') {
return true
}
return false
}
/**
* Authorize some 'new' expressions. Feel free to add more exceptions here. Good candidates would
* be functions that are known to be ECMAScript functions without side effects, that are likely to
* be considered as pure functions by the bundler.
*
* You can experiment with Rollup tree-shaking strategy to ensure your function is known to be pure.
* https://rollupjs.org/repl/?version=2.38.5&shareable=JTdCJTIybW9kdWxlcyUyMiUzQSU1QiU3QiUyMm5hbWUlMjIlM0ElMjJtYWluLmpzJTIyJTJDJTIyY29kZSUyMiUzQSUyMiUyRiUyRiUyMFB1cmUlMjBmdW5jdGlvbnMlNUNubmV3JTIwV2Vha01hcCgpJTVDbk9iamVjdC5rZXlzKCklNUNuJTVDbiUyRiUyRiUyMFNpZGUlMjBlZmZlY3QlMjBmdW5jdGlvbnMlNUNuZm9vKCklMjAlMkYlMkYlMjB1bmtub3duJTIwZnVuY3Rpb25zJTIwYXJlJTIwY29uc2lkZXJlZCUyMHRvJTIwaGF2ZSUyMHNpZGUlMjBlZmZlY3RzJTVDbmFsZXJ0KCdhYWEnKSU1Q25uZXclMjBNdXRhdGlvbk9ic2VydmVyKCgpJTIwJTNEJTNFJTIwJTdCJTdEKSUyMiUyQyUyMmlzRW50cnklMjIlM0F0cnVlJTdEJTVEJTJDJTIyb3B0aW9ucyUyMiUzQSU3QiUyMmZvcm1hdCUyMiUzQSUyMmVzJTIyJTJDJTIybmFtZSUyMiUzQSUyMm15QnVuZGxlJTIyJTJDJTIyYW1kJTIyJTNBJTdCJTIyaWQlMjIlM0ElMjIlMjIlN0QlMkMlMjJnbG9iYWxzJTIyJTNBJTdCJTdEJTdEJTJDJTIyZXhhbXBsZSUyMiUzQW51bGwlN0Q=
*
* Webpack is not as smart as Rollup, and it usually treat all 'new' expressions as impure, but it
* could be fine to allow it nonetheless at it pulls very little code.
*/
function isAllowedNewExpression({ callee }) {
switch (callee.name) {
// Allow some native constructors
case 'RegExp':
case 'WeakMap':
case 'WeakSet':
case 'Set':
case 'Map':
return true
default:
return false
}
}