Unverified Commit 22ef969b authored by Justin Ridgewell's avatar Justin Ridgewell Committed by GitHub

Remove hook_optimizer transform (#41203)

Fixes an issue where `const […foo] = useState(0)` is improperly transformed into `const { 0: …foo } = useState(0)`.

The hook optimizer was useful when hooks first came out, primarily because browsers had never optimized array destructuring. But current browsers have, and the optimization doesn't help anymore.



## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent e8c760f7
use swc_core::{
common::DUMMY_SP,
ecma::ast::{
ArrayPat, Callee, Decl, Expr, Ident, ImportDecl, ImportSpecifier, KeyValuePatProp, Number,
ObjectPat, ObjectPatProp, Pat, PropName, VarDecl, VarDeclarator,
},
ecma::atoms::JsWord,
ecma::visit::{Fold, FoldWith},
};
pub fn hook_optimizer() -> impl Fold {
HookOptimizer::default()
}
#[derive(Debug, Default)]
struct HookOptimizer {
hooks: Vec<JsWord>,
}
impl Fold for HookOptimizer {
// Find hooks imported from react/preact
fn fold_import_decl(&mut self, decl: ImportDecl) -> ImportDecl {
let ImportDecl {
ref src,
ref specifiers,
..
} = decl;
if &src.value == "react" || &src.value == "preact/hooks" {
for specifier in specifiers {
if let ImportSpecifier::Named(named_specifier) = specifier {
if named_specifier.local.sym.starts_with("use") {
self.hooks.push(named_specifier.local.sym.clone())
}
}
}
}
decl
}
// Transform array desctructing to object destructuring for relevant hooks
fn fold_decl(&mut self, node: Decl) -> Decl {
let node = node.fold_children_with(self);
match node {
Decl::Var(box VarDecl {
decls,
span,
kind,
declare,
}) => {
let mut new_decls = Vec::with_capacity(decls.len());
for decl in decls {
new_decls.push(self.get_decl(decl));
}
Decl::Var(Box::new(VarDecl {
decls: new_decls,
span,
kind,
declare,
}))
}
_ => node,
}
}
}
impl HookOptimizer {
fn get_decl(&mut self, decl: VarDeclarator) -> VarDeclarator {
let VarDeclarator {
name,
init,
span,
definite,
} = &decl;
let init_clone = init.clone();
if let Pat::Array(a) = name {
if let Expr::Call(c) = init.as_deref().unwrap() {
if let Callee::Expr(i) = &c.callee {
if let Expr::Ident(Ident { sym, .. }) = &**i {
if self.hooks.contains(sym) {
let name = get_object_pattern(a);
return VarDeclarator {
name,
init: init_clone,
span: *span,
definite: *definite,
};
}
}
}
}
}
decl
}
}
fn get_object_pattern(array_pattern: &ArrayPat) -> Pat {
let props: Vec<ObjectPatProp> = array_pattern
.elems
.iter()
.enumerate()
.filter_map(|(i, elem)| {
elem.as_ref().map(|elem| {
ObjectPatProp::KeyValue(KeyValuePatProp {
key: PropName::Num(Number {
value: i as f64,
span: DUMMY_SP,
raw: None,
}),
value: Box::new(elem.clone()),
})
})
})
.collect();
Pat::Object(ObjectPat {
props,
span: DUMMY_SP,
optional: false,
type_ann: None,
})
}
...@@ -50,7 +50,6 @@ use swc_core::{ ...@@ -50,7 +50,6 @@ use swc_core::{
pub mod amp_attributes; pub mod amp_attributes;
mod auto_cjs; mod auto_cjs;
pub mod disallow_re_export_all_in_page; pub mod disallow_re_export_all_in_page;
pub mod hook_optimizer;
pub mod next_dynamic; pub mod next_dynamic;
pub mod next_font_loaders; pub mod next_font_loaders;
pub mod next_ssg; pub mod next_ssg;
...@@ -167,7 +166,6 @@ where ...@@ -167,7 +166,6 @@ where
} else { } else {
Either::Right(noop()) Either::Right(noop())
}, },
hook_optimizer::hook_optimizer(),
match &opts.styled_components { match &opts.styled_components {
Some(config) => Either::Left(styled_components::styled_components( Some(config) => Either::Left(styled_components::styled_components(
file.name.clone(), file.name.clone(),
......
...@@ -10,7 +10,7 @@ const trim = (s) => s.join('\n').trim().replace(/^\s+/gm, '') ...@@ -10,7 +10,7 @@ const trim = (s) => s.join('\n').trim().replace(/^\s+/gm, '')
describe('next/swc', () => { describe('next/swc', () => {
describe('hook_optimizer', () => { describe('hook_optimizer', () => {
it('should transform Array-destructured hook return values use object destructuring', async () => { it('should leave alone array destructuring of hooks', async () => {
const output = await swc( const output = await swc(
trim` trim`
import { useState } from 'react'; import { useState } from 'react';
...@@ -18,32 +18,95 @@ describe('next/swc', () => { ...@@ -18,32 +18,95 @@ describe('next/swc', () => {
` `
) )
expect(output).toMatch(trim`
var ref = useState(0), count = ref[0], setCount = ref[1];
`)
expect(output).toMatchInlineSnapshot(` expect(output).toMatchInlineSnapshot(`
"import { useState } from \\"react\\"; "function _arrayLikeToArray(arr, len) {
var ref = useState(0), count = ref[0], setCount = ref[1]; if (len == null || len > arr.length) len = arr.length;
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
return arr2;
}
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArrayLimit(arr, i) {
var _i = arr == null ? null : typeof Symbol !== \\"undefined\\" && arr[Symbol.iterator] || arr[\\"@@iterator\\"];
if (_i == null) return;
var _arr = [];
var _n = true;
var _d = false;
var _s, _e;
try {
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally{
try {
if (!_n && _i[\\"return\\"] != null) _i[\\"return\\"]();
} finally{
if (_d) throw _e;
}
}
return _arr;
}
function _nonIterableRest() {
throw new TypeError(\\"Invalid attempt to destructure non-iterable instance.\\\\\\\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.\\");
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === \\"string\\") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === \\"Object\\" && o.constructor) n = o.constructor.name;
if (n === \\"Map\\" || n === \\"Set\\") return Array.from(n);
if (n === \\"Arguments\\" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
import { useState } from \\"react\\";
var ref = _slicedToArray(useState(0), 2), count = ref[0], setCount = ref[1];
" "
`) `)
}) })
it('should be able to ignore some Array-destructured hook return values', async () => { it('should leave alone array spread of hooks', async () => {
const output = await swc( const output = await swc(
trim` trim`
import { useState } from 'react'; import { useState } from 'react';
const [, setCount] = useState(0); const [...copy] = useState(0);
` `
) )
expect(output).toMatch(trim`
var ref = useState(0), setCount = ref[1];
`)
expect(output).toMatchInlineSnapshot(` expect(output).toMatchInlineSnapshot(`
"import { useState } from \\"react\\"; "function _arrayLikeToArray(arr, len) {
var ref = useState(0), setCount = ref[1]; if (len == null || len > arr.length) len = arr.length;
for(var i1 = 0, arr2 = new Array(len); i1 < len; i1++)arr2[i1] = arr[i1];
return arr2;
}
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArray(iter) {
if (typeof Symbol !== \\"undefined\\" && iter[Symbol.iterator] != null || iter[\\"@@iterator\\"] != null) return Array.from(iter);
}
function _nonIterableRest() {
throw new TypeError(\\"Invalid attempt to destructure non-iterable instance.\\\\\\\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.\\");
}
function _toArray(arr) {
return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === \\"string\\") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === \\"Object\\" && o.constructor) n = o.constructor.name;
if (n === \\"Map\\" || n === \\"Set\\") return Array.from(n);
if (n === \\"Arguments\\" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
import { useState } from \\"react\\";
var ref = _toArray(useState(0)), copy = ref.slice(0);
" "
`) `)
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment