Skip to content

Commit

Permalink
Remove hook_optimizer transform
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jridgewell committed Oct 5, 2022
1 parent d53e2f2 commit 4dde236
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 151 deletions.
123 changes: 0 additions & 123 deletions packages/next-swc/crates/core/src/hook_optimizer.rs

This file was deleted.

2 changes: 0 additions & 2 deletions packages/next-swc/crates/core/src/lib.rs
Expand Up @@ -49,7 +49,6 @@ use swc_core::{
pub mod amp_attributes;
mod auto_cjs;
pub mod disallow_re_export_all_in_page;
pub mod hook_optimizer;
pub mod next_dynamic;
pub mod next_ssg;
pub mod page_config;
Expand Down Expand Up @@ -133,7 +132,6 @@ pub fn custom_before_pass<'a, C: Comments + 'a>(
chain!(
disallow_re_export_all_in_page::disallow_re_export_all_in_page(opts.is_page_file),
styled_jsx::styled_jsx(cm.clone(), file.name.clone()),
hook_optimizer::hook_optimizer(),
match &opts.styled_components {
Some(config) => Either::Left(styled_components::styled_components(
file.name.clone(),
Expand Down
75 changes: 49 additions & 26 deletions test/unit/next-swc.test.ts
Expand Up @@ -10,40 +10,63 @@ const trim = (s) => s.join('\n').trim().replace(/^\s+/gm, '')

describe('next/swc', () => {
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(
trim`
import { useState } from 'react';
const [count, setCount] = useState(0);
`
)

expect(output).toMatch(trim`
var ref = useState(0), count = ref[0], setCount = ref[1];
`)

expect(output).toMatchInlineSnapshot(`
"import { useState } from \\"react\\";
var ref = useState(0), count = ref[0], setCount = ref[1];
"
`)
})

it('should be able to ignore some Array-destructured hook return values', async () => {
const output = await swc(
trim`
import { useState } from 'react';
const [, setCount] = useState(0);
`
)

expect(output).toMatch(trim`
var ref = useState(0), setCount = ref[1];
`)

expect(output).toMatchInlineSnapshot(`
"import { useState } from \\"react\\";
var ref = useState(0), setCount = ref[1];
"function _arrayLikeToArray(arr, len) {
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];
"
`)
})
Expand Down

0 comments on commit 4dde236

Please sign in to comment.