Implement more methods for codegen (#902)

swc_ecma_codegen:
 - implement emit_private_property (Closes #903)
This commit is contained in:
강동윤 2020-07-27 22:58:42 +09:00 committed by GitHub
parent ea885df521
commit ecd7b4decc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 593 additions and 657 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "swc_ecma_codegen"
version = "0.28.0"
version = "0.28.1"
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
license = "Apache-2.0/MIT"
repository = "https://github.com/swc-project/swc.git"

View File

@ -894,7 +894,28 @@ impl<'a> Emitter<'a> {
fn emit_private_prop(&mut self, n: &PrivateProp) -> Result {
self.emit_leading_comments_of_pos(n.span().lo())?;
unimplemented!("emit_private_prop")
self.emit_list(n.span, Some(&n.decorators), ListFormat::Decorators);
self.emit_accesibility(n.accessibility)?;
if n.readonly {
keyword!("readonly");
space!();
}
emit!(n.key);
if let Some(type_ann) = &n.type_ann {
punct!(":");
space!();
emit!(type_ann);
}
if let Some(value) = &n.value {
punct!("=");
emit!(value);
}
semi!();
}
#[emitter]

View File

@ -1,6 +1,6 @@
{
"devDependencies": {
"regenerator-runtime": "^0.11.0",
"jest": "^23.6.0"
"jest": "^23.6.0",
"regenerator-runtime": "^0.11.0"
}
}

View File

@ -1,5 +1,6 @@
#![feature(test)]
use swc_common::{chain, Mark};
use swc_ecma_parser::{Syntax, TsConfig};
use swc_ecma_transforms::{
compat,
compat::es2020::class_properties,
@ -20,9 +21,14 @@ use swc_ecma_visit::Fold;
#[macro_use]
mod common;
fn syntax() -> ::swc_ecma_parser::Syntax {
fn syntax() -> Syntax {
Default::default()
}
fn ts_syntax() -> Syntax {
Syntax::Typescript(TsConfig {
..Default::default()
})
}
fn tr(config: Config) -> impl Fold {
let mark = Mark::fresh(Mark::root());
@ -4153,3 +4159,29 @@ const resources = [
}
];"
);
test!(
ts_syntax(),
|_| tr(Config {
..Default::default()
}),
issue_895,
"import { queryString } from './url'
export function setup(url: string, obj: any) {
const _queryString = queryString(obj)
const _url = url + '?' + _queryString
return _url
}",
"'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.setup = setup;
var _url = require('./url');
function setup(url: string, obj: any) {
const _queryString = _url.queryString(obj);
const _url1 = url + '?' + _queryString;
return _url1;
}"
);

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,8 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": true
}
}
}

View File

@ -0,0 +1,4 @@
/**
* Don't ask why it is named as divider.
*/
export const Divider = <div/>

View File

@ -0,0 +1,4 @@
/**
* Don't ask why it is named as divider.
*/
export var Divider = React.createElement('div', null);

View File

@ -547,3 +547,16 @@ fn issue_879() {
"swc should not emit `function X` twice"
);
}
#[test]
fn issue_895() {
let f = file("tests/projects/issue-895/input.ts").unwrap();
println!("{}", f);
assert!(f.contains("_url ="));
assert!(f.contains("_url.queryString"));
let s = f.replace("_url =", "").replace("_url.queryString", "");
assert!(!s.contains("_url."));
assert!(!s.contains("_url,"));
}

View File

@ -0,0 +1,10 @@
{
"jsc": {
"parser": {
"syntax": "typescript"
}
},
"module": {
"type": "commonjs"
}
}

View File

@ -0,0 +1,7 @@
import { queryString } from './url'
export function setup(url: string, obj: any) {
const _queryString = queryString(obj)
const _url = url + '?' + _queryString
return _url
}