Skip to content

Commit

Permalink
chore: add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Sep 7, 2023
1 parent 33616dc commit 2cf0b2e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ impl DeclareCollect {
/// These steps match tsc behavior:
/// 1. Remove all import bindings that are not used.
/// 2. Remove all export bindings that are defined as types.
/// 3. If a local value declaration shares a name with an imported binding,
/// treat the imported binding as a type import.
/// 4. If a local type declaration shares a name with an imported binding and
/// it's used in a value position, treat the imported binding as a value
/// import.
#[derive(Default)]
pub(crate) struct StripImportExport {
pub id_usage: AHashSet<Id>,
Expand Down
52 changes: 39 additions & 13 deletions crates/swc_ecma_transforms_typescript/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ impl Transform {
member_name: name.clone(),
};

// Never store TsEnumRecordValue::Opaque value in the record.
let value = if value.is_const() {
value.clone()
} else {
Expand Down Expand Up @@ -579,23 +580,47 @@ impl Transform {
}
}

/// Note:
/// Const exported declarations are transformed into immutable bindings.
/// `export` from `export let` / ` export var` will be stripped,
/// and a mutable binding will be created by `Object.defineProperty`.
///
/// Exported function and class will be treat as const exported which is in
/// line with how the TypeScript compiler handles exports.
///
/// Inline exported syntax should not be used with function which will lead
/// to issues with function hoisting.
///
/// Input:
/// ```TypeScript
/// export const foo = init, { bar: baz } = init;
///
/// export function a() {}
///
/// export let b = init;
/// ```
///
/// Output:
/// ```TypeScript
/// const foo = NS.foo = init, { bar: baz } = { bar: NS.baz } = init;
///
/// function a() {}
/// NS.a = a;
///
/// let b = init;
/// Object.defineProperty(NS, "b", {
/// enumerable: true,
/// get () { return b; },
/// set (v) { b = v; }
/// });
/// ```
fn transform_export_var_decl_in_ts_module_block(
mut var_decl: VarDecl,
id: &Id,
) -> (Option<Stmt>, Stmt) {
debug_assert!(!var_decl.declare);

if var_decl.kind == VarDeclKind::Const {
// Input:
// ```TypeScript
// export const foo = init;
// export const { foo: bar } = init;
// ```
// Output:
// ```TypeScript
// const foo = Foo.foo = init;
// const { foo: bar } = { foo: Foo.bar } = init;
// ```
var_decl.decls.iter_mut().for_each(|var_declarator| {
Self::export_const_var_with_init(var_declarator, id);
});
Expand Down Expand Up @@ -863,8 +888,9 @@ impl Transform {
let mut should_inject = false;
let create_require = private_ident!("_createRequire");
let require = private_ident!("__require");
let cjs_require = quote_ident!(DUMMY_SP.apply_mark(self.unresolved_mark), "require");
let cjs_exports = quote_ident!(DUMMY_SP.apply_mark(self.unresolved_mark), "exports");
let global_span = DUMMY_SP.apply_mark(self.unresolved_mark);
let cjs_require = quote_ident!(global_span, "require");
let cjs_exports = quote_ident!(global_span, "exports");

let mut cjs_export_assign = None;

Expand Down Expand Up @@ -982,7 +1008,7 @@ impl Transform {
}

if should_inject {
n.inject_after_directive(vec![
n.inject_after_directive([
// import { createRequire } from "module";
ModuleDecl::Import(ImportDecl {
span: DUMMY_SP,
Expand Down

0 comments on commit 2cf0b2e

Please sign in to comment.