You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: crates/oxc_minifier/docs/ASSUMPTIONS.md
+43-96Lines changed: 43 additions & 96 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,9 +6,11 @@ These assumptions are validated using ECMAScript operations from [`oxc_ecmascrip
6
6
7
7
## Core Assumptions
8
8
9
-
### 1. No Monkey-Patching Built-ins
9
+
These assumptions are held regardless of the options.
10
10
11
-
Built-in objects and their methods behave as specified in ECMAScript.
11
+
### No Monkey-Patching Built-ins
12
+
13
+
[Built-in objects and their methods and properties](https://tc39.es/ecma262/multipage/global-object.html#sec-global-object) behave as specified in ECMAScript.
The deprecated `document.all` with its special typeof behavior is not used.
25
+
The deprecated [`document.all`](https://tc39.es/ecma262/multipage/additional-ecmascript-features-for-web-browsers.html#sec-IsHTMLDDA-internal-slot) with its special typeof behavior is not used.
24
26
25
27
```javascript
26
28
// The minifier assumes this never happens:
27
29
typeofdocument.all==='undefined'; // true in browsers
28
30
document.all&&console.log('exists but falsy');
29
31
```
30
32
31
-
### 3. No `with` Statement
33
+
### No `with` Statement
32
34
33
35
Code does not use `with` statements which create ambiguous scope.
34
36
@@ -39,43 +41,7 @@ with (obj) {
39
41
}
40
42
```
41
43
42
-
### 4. No Direct `eval` or `Function` Constructor
43
-
44
-
Code doesn't dynamically evaluate strings as code.
45
-
46
-
```javascript
47
-
// The minifier assumes this never happens:
48
-
eval('var x = 1');
49
-
newFunction('return x');
50
-
```
51
-
52
-
### 5. No Arguments Aliasing
53
-
54
-
The `arguments` object is not aliased or modified in ways that affect parameters.
55
-
56
-
```javascript
57
-
// The minifier assumes this never happens:
58
-
functionf(a) {
59
-
arguments[0] =2;
60
-
return a; // Would be affected by arguments modification
61
-
}
62
-
```
63
-
64
-
### 6. Getters/Setters Are Pure
65
-
66
-
Property getters and setters have no side effects.
67
-
68
-
```javascript
69
-
// The minifier assumes this never happens:
70
-
constobj= {
71
-
getprop() {
72
-
console.log('side effect!');
73
-
return1;
74
-
},
75
-
};
76
-
```
77
-
78
-
### 7. Coercion Methods Are Pure
44
+
### Coercion Methods Are Pure
79
45
80
46
`.toString()`, `.valueOf()`, and `[Symbol.toPrimitive]()` have no side effects. The minifier uses `oxc_ecmascript` type conversion utilities that assume standard coercion behavior.
81
47
@@ -90,95 +56,76 @@ const obj = {
90
56
String(obj); // Would trigger side effect
91
57
```
92
58
93
-
### 8. No Reliance on Function.prototype.name
94
-
95
-
Code doesn't depend on function names being preserved.
96
-
97
-
```javascript
98
-
// The minifier assumes this never happens:
99
-
functionmyFunc() {}
100
-
if (myFunc.name!=='myFunc') throwError();
101
-
```
102
-
103
-
### 9. No Reliance on Function.length
59
+
### No TDZ Violation
104
60
105
-
Code doesn't depend on function arity.
61
+
Code doesn't violate Temporal Dead Zones.
106
62
107
63
```javascript
108
64
// The minifier assumes this never happens:
109
-
functionf(a, b, c) {}
110
-
if (f.length!==3) throwError();
65
+
console.log(x); // TDZ violation
66
+
let x =1;
111
67
```
112
68
113
-
### 10. Regular Prototype Chains
69
+
### No errors from Array/String Maximum Length
114
70
115
-
Objects have standard prototype chains without modifications.
71
+
Creating strings or arrays that exceed maximum length can be moved or removed.
116
72
117
73
```javascript
118
-
// The minifier assumes this never happens:
119
-
Object.setPrototypeOf(obj, null);
120
-
obj.__proto__= weird_proto;
74
+
// The minifier may change when this error occurs:
75
+
try {
76
+
newArray(2**32); // RangeError
77
+
} catch {
78
+
console.log('caught');
79
+
}
121
80
```
122
81
123
-
### 11. Special Handling of `__proto__` Property
82
+
### No side effects from extending a class
124
83
125
-
The minifier correctly handles `__proto__` as a special property name when inlining variables.
84
+
Extending a class does not have a side effect.
126
85
127
86
```javascript
128
-
// Before optimization:
129
-
functionwrapper() {
130
-
var __proto__ = [];
131
-
return { __proto__ };
132
-
}
133
-
134
-
// After optimization:
135
-
functionwrapper() {
136
-
return { ['__proto__']: [] };
137
-
}
87
+
constv= [];
88
+
classAextendsv {} // TypeError
138
89
```
139
90
140
-
The minifier converts shorthand `__proto__` properties to computed form to preserve semantics.
141
-
142
-
### 12. No TDZ Violation
91
+
### No Direct `eval` or `Function` Constructor
143
92
144
-
Code doesn't violate Temporal Dead Zones.
93
+
Code doesn't dynamically evaluate strings as code. We intend to change this assumption to optional in the future.
145
94
146
95
```javascript
147
96
// The minifier assumes this never happens:
148
-
console.log(x); // TDZ violation
149
-
let x =1;
97
+
eval('var x = 1');
98
+
newFunction('return x');
150
99
```
151
100
152
-
### 13. typeof-Guarded Global Access
101
+
### `arguments` is always [the arguments object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments)
153
102
154
-
The minifier optimizes `typeof`-guarded global variable access by removing unnecessary checks.
103
+
`arguments` variables are only accessed inside functions and not assigned with a different value. We intend to change this assumption to optional in the future.
155
104
156
105
```javascript
157
-
// Before optimization:
158
-
typeof x !=='undefined'&& x;
159
-
160
-
// After optimization:
161
-
// (removed entirely if x is known to be undefined)
106
+
// The minifier assumes this never happens:
107
+
console.log(arguments); // This is not the arguments object
108
+
functionf(a) {
109
+
arguments=2; // This makes the arguments variable point not to point to the arguments object
110
+
return a;
111
+
}
162
112
```
163
113
164
-
This assumes that `typeof`-guarded expressions are used defensively and can be safely removed when the variable is provably undefined.
114
+
## Optional Assumptions
165
115
166
-
### 14. Errors from Array/String Maximum Length
116
+
### No Reliance on Function.prototype.name
167
117
168
-
Creating strings or arrays that exceed maximum length can be moved or removed.
118
+
Code doesn't depend on function names being preserved. This assumption is held by default. This can be changed by settings `keepNames` option.
169
119
170
120
```javascript
171
-
// The minifier may change when this error occurs:
172
-
try {
173
-
newArray(2**32); // RangeError
174
-
} catch {
175
-
console.log('caught');
176
-
}
121
+
// The minifier assumes this never happens:
122
+
functionmyFunc() {}
123
+
if (myFunc.name!=='myFunc') throwError();
177
124
```
178
125
179
126
## Configuration
180
127
181
-
These assumptions can be configured in the minifier options if your code requires different behavior.
128
+
Optional assumptions can be configured in the minifier options if your code requires different behavior.
0 commit comments