0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

fix(cli): inject helpers when transpiling via swc (#8221)

Fixes #8212
This commit is contained in:
Kitson Kelly 2020-11-02 22:33:43 +11:00 committed by GitHub
parent fdcc78500c
commit 272e9b82eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 64 additions and 5 deletions

View file

@ -298,6 +298,7 @@ impl ParsedModule {
legacy: true,
emit_metadata: options.emit_metadata
}),
helpers::inject_helpers(),
typescript::strip(),
fixer(Some(&self.comments)),
);
@ -413,6 +414,7 @@ pub fn transpile_module(
src: &str,
media_type: &MediaType,
emit_options: &EmitOptions,
globals: &Globals,
cm: Rc<SourceMap>,
) -> Result<(Rc<SourceFile>, Module), AnyError> {
// TODO(@kitsonk) DRY-up with ::parse()
@ -464,10 +466,11 @@ pub fn transpile_module(
legacy: true,
emit_metadata: emit_options.emit_metadata
}),
helpers::inject_helpers(),
typescript::strip(),
fixer(Some(&comments)),
);
let module = swc_common::GLOBALS.set(&Globals::new(), || {
let module = swc_common::GLOBALS.set(globals, || {
helpers::HELPERS.set(&helpers::Helpers::new(false), || {
module.fold_with(&mut passes)
})

View file

@ -120,20 +120,23 @@ impl Error for GraphError {}
/// avoid a circular dependency with `ast`.
struct BundleLoader<'a> {
cm: Rc<swc_common::SourceMap>,
graph: &'a Graph2,
emit_options: &'a ast::EmitOptions,
globals: &'a swc_common::Globals,
graph: &'a Graph2,
}
impl<'a> BundleLoader<'a> {
pub fn new(
graph: &'a Graph2,
emit_options: &'a ast::EmitOptions,
globals: &'a swc_common::Globals,
cm: Rc<swc_common::SourceMap>,
) -> Self {
BundleLoader {
cm,
graph,
emit_options,
globals,
graph,
}
}
}
@ -158,6 +161,7 @@ impl swc_bundler::Load for BundleLoader<'_> {
&src,
&media_type,
self.emit_options,
self.globals,
self.cm.clone(),
)
} else {
@ -965,9 +969,9 @@ impl Graph2 {
let cm = Rc::new(swc_common::SourceMap::new(
swc_common::FilePathMapping::empty(),
));
let loader = BundleLoader::new(self, emit_options, cm.clone());
let hook = Box::new(BundleHook);
let globals = swc_common::Globals::new();
let loader = BundleLoader::new(self, emit_options, &globals, cm.clone());
let hook = Box::new(BundleHook);
let bundler = swc_bundler::Bundler::new(
&globals,
cm.clone(),

View file

@ -2570,6 +2570,11 @@ itest!(no_check {
http_server: true,
});
itest!(no_check_decorators {
args: "run --quiet --reload --no-check no_check_decorators.ts",
output: "no_check_decorators.ts.out",
});
itest!(lib_ref {
args: "run --quiet --unstable --reload lib_ref.ts",
output: "lib_ref.ts.out",

View file

@ -0,0 +1,21 @@
/* eslint-disable */
function a() {
console.log("a(): evaluated");
return (
_target: any,
_propertyKey: string,
_descriptor: PropertyDescriptor,
) => {
console.log("a(): called");
};
}
class B {
@a()
method() {
console.log("method");
}
}
const b = new B();
b.method();

View file

@ -0,0 +1,3 @@
a(): evaluated
a(): called
method

View file

@ -0,0 +1,18 @@
/* eslint-disable */
function a() {
console.log("a(): evaluated");
return (
_target: any,
_propertyKey: string,
_descriptor: PropertyDescriptor,
) => {
console.log("a(): called");
};
}
export class B {
@a()
method() {
console.log("method");
}
}

View file

@ -1,3 +1,5 @@
[WILDCARD]
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
[WILDCARD]
new SomeClass().test();
[WILDCARD]

View file

@ -1,5 +1,7 @@
/* eslint-disable */
import { B } from "./subdir/more_decorators.ts";
function Decorator() {
return function (
target: Record<string, any>,
@ -20,3 +22,4 @@ class SomeClass {
}
new SomeClass().test();
new B().method();