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

fix(es/typescript): Analyze import chain #9369

Merged
merged 5 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ define([
//// [foo_1.ts]
define([
"require",
"exports",
"./foo_0"
], function(require, exports, _foo_0) {
"exports"
], function(require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ define([
//// [foo_1.ts]
define([
"require",
"exports",
"./foo_0"
], function(require, exports, _foo_0) {
"exports"
], function(require, exports) {
Object.defineProperty(exports, "__esModule", {
value: !0
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ var E1;
Object.defineProperty(exports, "__esModule", {
value: true
});
var foo = require("./foo_0");
var i;
var x = {};
var y = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ C1.s1 = !0, (E1 = E11 || (E11 = {}))[E1.A = 0] = "A", E1[E1.B = 1] = "B", E1[E1.
//// [foo_1.ts]
Object.defineProperty(exports, "__esModule", {
value: !0
}), require("./foo_0");
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
var C;
// no code gen expected
(function(C) {
var a = A;
var m;
var p;
var p = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//// [importStatementsInterfaces.ts]
var C, D, E;
C || (C = {}), A, D || (D = {}), (E || (E = {})).xDist = function(x) {
C || (C = {}), D || (D = {}), (E || (E = {})).xDist = function(x) {
return 0 - x.x;
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::mem;

use swc_common::collections::AHashSet;
use swc_common::collections::{AHashMap, AHashSet};
use swc_ecma_ast::*;
use swc_ecma_utils::stack_size::maybe_grow_default;
use swc_ecma_visit::{noop_visit_type, Visit, VisitMut, VisitMutWith, VisitWith};
Expand All @@ -10,11 +10,15 @@ use crate::{strip_type::IsConcrete, ImportsNotUsedAsValues};
#[derive(Debug, Default)]
struct UsageCollect {
id_usage: AHashSet<Id>,
import_chain: AHashMap<Id, Id>,
}

impl From<AHashSet<Id>> for UsageCollect {
fn from(id_usage: AHashSet<Id>) -> Self {
Self { id_usage }
Self {
id_usage,
import_chain: Default::default(),
}
}
}

Expand Down Expand Up @@ -68,7 +72,15 @@ impl Visit for UsageCollect {
return;
};

get_module_ident(ts_entity_name).visit_with(self);
let id = get_module_ident(ts_entity_name);

if n.is_export {
id.visit_with(self);
n.id.visit_with(self);
return;
}

self.import_chain.insert(n.id.to_id(), id.to_id());
}

fn visit_export_named_specifier(&mut self, n: &ExportNamedSpecifier) {
Expand Down Expand Up @@ -101,6 +113,25 @@ impl UsageCollect {
fn has_usage(&self, id: &Id) -> bool {
self.id_usage.contains(id)
}

fn analyze_import_chain(&mut self) {
if self.import_chain.is_empty() {
return;
}

let mut new_usage = AHashSet::default();
for id in &self.id_usage {
let mut id = id.clone();
magic-akari marked this conversation as resolved.
Show resolved Hide resolved
while let Some(next) = self.import_chain.remove(&id) {
new_usage.insert(next.clone());
id = next;
}
if self.import_chain.is_empty() {
break;
}
}
self.id_usage.extend(new_usage);
}
}

fn get_module_ident(ts_entity_name: &TsEntityName) -> &Ident {
Expand Down Expand Up @@ -264,6 +295,8 @@ impl VisitMut for StripImportExport {
n.visit_with(&mut usage_info);
n.visit_with(&mut declare_info);

usage_info.analyze_import_chain();

let mut strip_ts_import_equals = StripTsImportEquals;

n.retain_mut(|module_item| match module_item {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
import * as mongo from 'https://deno.land/x/mongo@v0.27.0/mod.ts';
const mongoClient = {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Schemas } from './types.d';
import AddressResource = Schemas.AddressResource;

// type usage
const x: AddressResource = {};


import { foo, bar } from "mod";

// value usage
import y = foo.y;

// type usage
import z = bar.z;

console.log(y as z);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// type usage
const x = {};
import { foo } from "mod";
// value usage
const y = foo.y;
console.log(y);
Loading