dillon1000/react
Commit
Browse files Validate frozen lambdas are actually frozen
Adds validation to reject freezing mutable lambdas, since lambdas cannot _be_
frozen, they're either frozen or not. Example invalid code:
```javascript
function Component(props) {
const x = {value: ""};
const onChange = (e) => {
// MUTATION!!!!!
x.value = e.target.value;
setX(x);
};
return <input value={x.value} onChange={onChange} />;
}
```
Note that there is a separate issue in which we are not detecting lambdas that
would definitely modify immutable values. We may need to distinguish
ConditionallyCapture (captures if the value is mutable, otherwise readonly) from
Capture (definitely mutates) in order to make that case work. But already this
validation helps prevent some invalid code.Changed paths16 files
First-parent comparisoncompiler/forget/packages/snap/src/compiler-worker.ts ModifiedM compiler/forget/src/CompilerPipeline.ts ModifiedM compiler/forget/src/HIR/Environment.ts ModifiedA compiler/forget/src/HIR/ValidateFrozenLambdas.ts AddedM compiler/forget/src/HIR/index.ts ModifiedD compiler/forget/src/__tests__/fixtures/compiler/capture-func-passed-to-jsx.expect.md DeletedM compiler/forget/src/__tests__/fixtures/compiler/destructuring-mixed-scope-and-local-variables-with-default.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/compiler/destructuring-mixed-scope-and-local-variables-with-default.js ModifiedM compiler/forget/src/__tests__/fixtures/compiler/destructuring-mixed-scope-declarations-and-locals.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/compiler/destructuring-mixed-scope-declarations-and-locals.js ModifiedA compiler/forget/src/__tests__/fixtures/compiler/error.invalid-capture-func-passed-to-jsx.expect.md AddedR compiler/forget/src/__tests__/fixtures/compiler/capture-func-passed-to-jsx.js →compiler/forget/src/__tests__/fixtures/compiler/error.invalid-capture-func-passed-to-jsx.js RenamedA compiler/forget/src/__tests__/fixtures/compiler/error.invalid-freeze-mutable-lambda-mutate-local.expect.md AddedA compiler/forget/src/__tests__/fixtures/compiler/error.invalid-freeze-mutable-lambda-mutate-local.js AddedA compiler/forget/src/__tests__/fixtures/compiler/error.invalid-freeze-mutable-lambda-reassign-local.expect.md AddedA compiler/forget/src/__tests__/fixtures/compiler/error.invalid-freeze-mutable-lambda-reassign-local.js AddedPatch
Files changed
Rendering syntax-highlighted changes…