Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hook_optimizer transform #41203

Merged
merged 2 commits into from Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -50,7 +50,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_font_loaders;
pub mod next_ssg;
Expand Down Expand Up @@ -167,7 +166,6 @@ where
} else {
Either::Right(noop())
},
hook_optimizer::hook_optimizer(),
match &opts.styled_components {
Some(config) => Either::Left(styled_components::styled_components(
file.name.clone(),
Expand Down
93 changes: 78 additions & 15 deletions test/unit/next-swc.test.ts
Expand Up @@ -10,40 +10,103 @@ 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];
"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];
"
`)
})

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(
trim`
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(`
"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 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);
"
`)
})
Expand Down