Skip to content

Commit

Permalink
fix(swc): Fix bugs (#1300)
Browse files Browse the repository at this point in the history
swc_ecma_codegen:
 - Handle unicode escape sequences when target is es5. (#1227, #1326)

swc_ecma_transforms_compat:
 - Handle template literals correctly. (#1314)
 - Handle private class properties correctly. (#1306)

swc:
 - Don't panic on `export * as foo from 'foo'`. (#1307)
  • Loading branch information
kdy1 authored Jan 14, 2021
1 parent 6984217 commit a9bf9bb
Show file tree
Hide file tree
Showing 29 changed files with 368 additions and 99 deletions.
2 changes: 2 additions & 0 deletions bundler/tests/deno-exec/issue-8302/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import axiod from 'https://deno.land/x/axiod/mod.ts';
console.log(axiod)
3 changes: 2 additions & 1 deletion bundler/tests/fixture/dynamic/input/entry.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const a = import('./dep')

console.log(a)
console.log(a)

4 changes: 4 additions & 0 deletions ecmascript/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2493,6 +2493,10 @@ fn escape_with_source<'s>(
s: &'s str,
single_quote: Option<bool>,
) -> String {
if target <= JscTarget::Es5 {
return escape_without_source(s, target, single_quote.unwrap_or(false));
}

if span.is_dummy() {
return escape_without_source(s, target, single_quote.unwrap_or(false));
}
Expand Down
4 changes: 4 additions & 0 deletions ecmascript/codegen/src/text_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,8 @@ where
fn write_punct(&mut self, s: &'static str) -> Result {
(**self).write_punct(s)
}

fn target(&self) -> JscTarget {
(**self).target()
}
}
17 changes: 17 additions & 0 deletions ecmascript/codegen/src/text_writer/basic_impl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{Result, WriteJs};
use std::io::{self, Write};
use swc_common::{sync::Lrc, BytePos, LineCol, SourceMap, Span};
use swc_ecma_parser::JscTarget;

///
/// -----
Expand All @@ -19,6 +20,7 @@ pub struct JsWriter<'a, W: Write> {
srcmap: Option<&'a mut Vec<(BytePos, LineCol)>>,
wr: W,
written_bytes: usize,
target: JscTarget,
}

impl<'a, W: Write> JsWriter<'a, W> {
Expand All @@ -27,6 +29,16 @@ impl<'a, W: Write> JsWriter<'a, W> {
new_line: &'a str,
wr: W,
srcmap: Option<&'a mut Vec<(BytePos, LineCol)>>,
) -> Self {
Self::with_target(cm, new_line, wr, srcmap, JscTarget::Es2020)
}

pub fn with_target(
cm: Lrc<SourceMap>,
new_line: &'a str,
wr: W,
srcmap: Option<&'a mut Vec<(BytePos, LineCol)>>,
target: JscTarget,
) -> Self {
JsWriter {
_cm: cm,
Expand All @@ -38,6 +50,7 @@ impl<'a, W: Write> JsWriter<'a, W> {
srcmap,
wr,
written_bytes: 0,
target,
}
}

Expand Down Expand Up @@ -187,6 +200,10 @@ impl<'a, W: Write> WriteJs for JsWriter<'a, W> {
self.write(None, s)?;
Ok(())
}

fn target(&self) -> JscTarget {
self.target
}
}

fn compute_line_starts(s: &str) -> Vec<usize> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,22 @@ impl<'a> Fold for FieldAccessFolder<'a> {
.fold_children_with(self)
}
}
Expr::Member(e) => self.fold_private_get(e, None).0,
Expr::Member(e) => {
let e = e.fold_with(self);
self.fold_private_get(e, None).0
}
_ => e.fold_children_with(self),
}
}

fn fold_member_expr(&mut self, mut e: MemberExpr) -> MemberExpr {
e.obj = e.obj.fold_with(self);
if e.computed {
e.prop = e.prop.fold_with(self);
}
e
}

fn fold_pat(&mut self, p: Pat) -> Pat {
if let Pat::Expr(expr) = &p {
if let Expr::Member(me) = &**expr {
Expand Down
68 changes: 68 additions & 0 deletions ecmascript/transforms/compat/tests/es2020_class_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4840,3 +4840,71 @@ export class HygieneTest {
}",
ok_if_code_eq
);

test!(
syntax(),
|_| class_properties(),
issue_1306_1,
r#"
class Animal {
#name;
constructor(name) {
this.#name = name
}
noise() {
return this.#name
}
}
"#,
"
class Animal {
noise() {
return _classPrivateFieldGet(this, _name);
}
constructor(name){
_name.set(this, {
writable: true,
value: void 0
});
_classPrivateFieldSet(this, _name, name);
}
}
var _name = new WeakMap();
"
);

test!(
syntax(),
|_| class_properties(),
issue_1306_2,
r#"
class Animal {
#name;
constructor(name) {
this.#name = name
}
noise() {
return this.#name.toUpperCase()
}
}
"#,
"
class Animal {
noise() {
return _classPrivateFieldGet(this, _name).toUpperCase();
}
constructor(name){
_name.set(this, {
writable: true,
value: void 0
});
_classPrivateFieldSet(this, _name, name);
}
}
var _name = new WeakMap();
"
);
2 changes: 2 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::config::{GlobalPassOption, JscTarget, ModuleConfig};
use compat::es2020::export_namespace_from;
use either::Either;
use std::{collections::HashMap, sync::Arc};
use swc_atoms::JsWord;
Expand Down Expand Up @@ -176,6 +177,7 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
self.pass,
compat_pass,
compat::reserved_words::reserved_words(),
Optional::new(export_namespace_from(), need_interop_analysis),
// module / helper
Optional::new(
modules::import_analysis::import_analyzer(),
Expand Down
80 changes: 0 additions & 80 deletions src/codegen.rs

This file was deleted.

23 changes: 10 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use swc_ecma_transforms::{
use swc_ecma_visit::FoldWith;

mod builder;
mod codegen;
pub mod config;

pub struct Compiler {
Expand Down Expand Up @@ -207,19 +206,17 @@ impl Compiler {
cfg: swc_ecma_codegen::Config { minify },
comments: if minify { None } else { Some(&self.comments) },
cm: self.cm.clone(),
wr: Box::new(self::codegen::WriterWapper {
wr: Box::new(swc_ecma_codegen::text_writer::JsWriter::with_target(
self.cm.clone(),
"\n",
&mut buf,
if source_map.enabled() {
Some(&mut src_map_buf)
} else {
None
},
target,
inner: swc_ecma_codegen::text_writer::JsWriter::new(
self.cm.clone(),
"\n",
&mut buf,
if source_map.enabled() {
Some(&mut src_map_buf)
} else {
None
},
),
}),
)),
};

node.emit_with(&mut emitter)
Expand Down
2 changes: 1 addition & 1 deletion tests/fixture/codegen/escape/case-1/output/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _default = "\nvoid main() {\n gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}\n";
var _default = "\nvoid main() {\n\tgl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}\n";
exports.default = _default;
Loading

0 comments on commit a9bf9bb

Please sign in to comment.