Skip to content

Commit

Permalink
fix: handle other decl pat
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Feb 2, 2022
1 parent 55dc9a7 commit bdff6b9
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 31 deletions.
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issue-3235/9/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jsc": {
"target": "es5"
}
}
43 changes: 43 additions & 0 deletions crates/swc/tests/fixture/issue-3235/9/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function foo() {
{
let { a, b: c, d = e, f: g = h, ...i } = {};
// all should be renamed
use(a, c, d, g, i);
}

var { a, b: c, d = e, f: g = h, ...i } = {};
use(a, c, d, g, i);
}

function bar() {
var { a, b: c, d = e, f: g = h, ...i } = {};
use(a, c, d, g, i);

{
let { a, b: c, d = e, f: g = h, ...i } = {};
// all should be renamed
use(a, c, d, g, i);
}
}

{
let b = 1; // keep name
let e = 1; // should rename. conflict with unresoved
let f = 1; // keep name
let h = 1; // should rename. conflict with unresoved
}

{
let { a, b: c, d = e, f: g = h, ...i } = {};
// all should be renamed
use(a, c, d, g, i);
}

// should not touch following
var a = 1;
var c = 1;
var d = 1;
var g = 1;
var i = 1;
var _ref = 1;
var tmp = 1;
87 changes: 87 additions & 0 deletions crates/swc/tests/fixture/issue-3235/9/output/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
function _objectWithoutProperties(source, excluded) {
if (source == null) return {};
var target = _objectWithoutPropertiesLoose(source, excluded);
var key, i;
if (Object.getOwnPropertySymbols) {
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
for(i = 0; i < sourceSymbolKeys.length; i++){
key = sourceSymbolKeys[i];
if (excluded.indexOf(key) >= 0) continue;
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
target[key] = source[key];
}
}
return target;
}
function _objectWithoutPropertiesLoose(source, excluded) {
if (source == null) return {};
var target = {};
var sourceKeys = Object.keys(source);
var key, i;
for(i = 0; i < sourceKeys.length; i++){
key = sourceKeys[i];
if (excluded.indexOf(key) >= 0) continue;
target[key] = source[key];
}
return target;
}
function foo() {
{
var _ref = {}, a2 = _ref.a, c2 = _ref.b, _d2 = _ref.d, d2 = _d2 === void 0 ? e : _d2, tmp = _ref.f, g2 = tmp === void 0 ? h : tmp, i2 = _objectWithoutProperties(_ref, [
"a",
"b",
"d",
"f"
]);
// all should be renamed
use(a2, c2, d2, g2, i2);
}
var _ref2 = {}, a = _ref2.a, c = _ref2.b, _d1 = _ref2.d, d = _d1 === void 0 ? e : _d1, tmp2 = _ref2.f, g = tmp2 === void 0 ? h : tmp2, i = _objectWithoutProperties(_ref2, [
"a",
"b",
"d",
"f"
]);
use(a, c, d, g, i);
}
function bar() {
var _ref = {}, a = _ref.a, c = _ref.b, _d4 = _ref.d, d = _d4 === void 0 ? e : _d4, tmp = _ref.f, g = tmp === void 0 ? h : tmp, i = _objectWithoutProperties(_ref, [
"a",
"b",
"d",
"f"
]);
use(a, c, d, g, i);
{
var _ref3 = {}, a3 = _ref3.a, c3 = _ref3.b, _d3 = _ref3.d, d3 = _d3 === void 0 ? e : _d3, tmp3 = _ref3.f, g3 = tmp3 === void 0 ? h : tmp3, i3 = _objectWithoutProperties(_ref3, [
"a",
"b",
"d",
"f"
]);
// all should be renamed
use(a3, c3, d3, g3, i3);
}
}
{
var b = 1; // keep name
var e1 = 1; // should rename. conflict with unresoved
var f = 1; // keep name
var h1 = 1; // should rename. conflict with unresoved
}{
var _ref1 = {}, a1 = _ref1.a, c1 = _ref1.b, _d = _ref1.d, d1 = _d === void 0 ? e : _d, tmp1 = _ref1.f, g1 = tmp1 === void 0 ? h : tmp1, i1 = _objectWithoutProperties(_ref1, [
"a",
"b",
"d",
"f"
]);
// all should be renamed
use(a1, c1, d1, g1, i1);
}// should not touch following
var a = 1;
var c = 1;
var d = 1;
var g = 1;
var i = 1;
var _ref = 1;
var tmp = 1;
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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];
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, i1) {
function _iterableToArrayLimit(arr, i) {
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
if (_i == null) return;
var _arr = [];
Expand All @@ -16,7 +16,7 @@ function _iterableToArrayLimit(arr, i1) {
try {
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
_arr.push(_s.value);
if (i1 && _arr.length === i1) break;
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
Expand All @@ -33,8 +33,8 @@ function _iterableToArrayLimit(arr, i1) {
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, i1) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i1) || _unsupportedIterableToArray(arr, i1) || _nonIterableRest();
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ t2.z.toString(); // Should error
y.toFixed(); // Should OK
z.toFixed(); // Should error
}{
const { x: x1 } = numMapPoint, q = _objectWithoutProperties(numMapPoint, [
const { x } = numMapPoint, q = _objectWithoutProperties(numMapPoint, [
"x"
]);
x1.toFixed(); // Should OK
x.toFixed(); // Should OK
q.y.toFixed(); // Should OK
q.z.toFixed(); // Should error
}// Assignment forms
[target_string] = strArray; // Should error
[target_string_undef] = strArray; // Should OK
[, , , ...target_string_arr] = strArray; // Should OK
{
let x2, y1, z1;
({ x: x2 , y: y1 , z: z1 } = numMapPoint); // Should OK
let q1;
({ q: q1 } = numMapPoint); // Should error
let x, y, z;
({ x , y , z } = numMapPoint); // Should OK
let q;
({ q } = numMapPoint); // Should error
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ t2.z.toString();
const { x , y , z } = numMapPoint;
x.toFixed(), y.toFixed(), z.toFixed();
}{
const { x: x1 } = numMapPoint, q = function(source, excluded) {
const { x } = numMapPoint, q = function(source, excluded) {
if (null == source) return {};
var key, i, target = function(source, excluded) {
if (null == source) return {};
Expand All @@ -37,11 +37,11 @@ t2.z.toString();
}(numMapPoint, [
"x"
]);
x1.toFixed(), q.y.toFixed(), q.z.toFixed();
x.toFixed(), q.y.toFixed(), q.z.toFixed();
}[target_string] = strArray, [target_string_undef] = strArray, [, , , ...target_string_arr] = strArray;
{
let x2, y1, z1;
({ x: x2 , y: y1 , z: z1 } = numMapPoint);
let q1;
({ q: q1 } = numMapPoint);
let x, y, z;
({ x , y , z } = numMapPoint);
let q;
({ q } = numMapPoint);
}
62 changes: 48 additions & 14 deletions crates/swc_ecma_transforms_base/src/ident_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use std::{cell::RefCell, rc::Rc};
use is_macro::Is;
use swc_atoms::JsWord;
use swc_common::{collections::AHashMap, SyntaxContext};
use swc_ecma_ast::{Pat, VarDecl, VarDeclKind};
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};
use swc_ecma_ast::{AssignPatProp, Expr, Ident, KeyValuePatProp, PropName, VarDecl, VarDeclKind};
use swc_ecma_visit::{
as_folder, noop_visit_mut_type, noop_visit_type, Fold, Visit, VisitMut, VisitMutWith, VisitWith,
};

#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
pub struct IdentScope {
Expand Down Expand Up @@ -48,29 +50,61 @@ impl From<VarDeclKind> for IdentScopeKind {
pub type IdentScopeRecord = Rc<RefCell<AHashMap<IdentScope, IdentScopeKind>>>;

pub fn ident_scope_collector(ident_scope_record: IdentScopeRecord) -> impl Fold {
as_folder(UnblockIdentCollector { ident_scope_record })
as_folder(IdentScopeCollector { ident_scope_record })
}

struct UnblockIdentCollector {
struct IdentScopeCollector {
ident_scope_record: IdentScopeRecord,
}

impl VisitMut for UnblockIdentCollector {
impl VisitMut for IdentScopeCollector {
noop_visit_mut_type!();

fn visit_mut_var_decl(&mut self, var: &mut VarDecl) {
var.visit_mut_children_with(self);

let mut pat_visitor = PatVisitor {
ident_scope_record: self.ident_scope_record.clone(),
ident_scope_kind: var.kind.into(),
};

for decl in var.decls.iter() {
if let Pat::Ident(name) = &decl.name {
self.ident_scope_record
.borrow_mut()
.entry(IdentScope {
sym: name.id.sym.clone(),
ctxt: name.id.span.ctxt(),
})
.or_insert_with(|| var.kind.into());
}
decl.name.visit_with(&mut pat_visitor);
}
}
}

struct PatVisitor {
ident_scope_record: IdentScopeRecord,
ident_scope_kind: IdentScopeKind,
}

impl Visit for PatVisitor {
noop_visit_type!();

/// No-op (we don't care about expressions)
fn visit_expr(&mut self, _: &Expr) {}

fn visit_ident(&mut self, ident: &Ident) {
self.ident_scope_record
.borrow_mut()
.entry(IdentScope {
sym: ident.sym.clone(),
ctxt: ident.span.ctxt(),
})
.or_insert_with(|| self.ident_scope_kind);
}

/// No-op (we don't care about expressions)
fn visit_prop_name(&mut self, _: &PropName) {}

/// let { x: y } = z;
fn visit_key_value_pat_prop(&mut self, n: &KeyValuePatProp) {
n.value.visit_with(self);
}

/// let { x = y } = z;
fn visit_assign_pat_prop(&mut self, n: &AssignPatProp) {
n.key.visit_with(self);
}
}

0 comments on commit bdff6b9

Please sign in to comment.