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

test(es/typescript): Verify TypeScript stripped output #9398

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
104 changes: 103 additions & 1 deletion crates/swc_fast_ts_strip/tests/fixture.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::path::PathBuf;

use swc_ecma_parser::TsSyntax;
use swc_common::{
comments::SingleThreadedComments, errors::Handler, sync::Lrc, FileName, SourceMap,
};
use swc_ecma_ast::EsVersion;
use swc_ecma_parser::{lexer::Lexer, Capturing, EsSyntax, Parser, StringInput, Syntax, TsSyntax};
use swc_fast_ts_strip::{operate, Mode, Options};
use testing::NormalizedOutput;

Expand Down Expand Up @@ -43,6 +47,55 @@ fn test(input: PathBuf) {
.expect("should not fail");
}

#[testing::fixture("../swc_ecma_parser/tests/tsc/**/*.ts")]
fn verify(input: PathBuf) {
let input_code = std::fs::read_to_string(&input).unwrap();

let output = PathBuf::from(
input
.to_string_lossy()
.replace("swc_ecma_parser", "swc_fast_ts_strip"),
);

let output_file = output.with_extension("strip.js");
let output_stderr = output.with_extension("strip.broken");

let err = testing::run_test(false, |cm, handler| {
let code = match operate(&cm, handler, input_code.clone(), opts(Mode::StripOnly)) {
Ok(v) => v.code,
Err(..) => {
// Skip if the input is invalid
return Ok(());
}
};

if handler.has_errors() {
// Skip if the input is invalid

return Ok(());
}

reparse(&cm, handler, &output_file, code.clone());

if handler.has_errors() {
NormalizedOutput::new_raw(code)
.compare_to_file(&output_file)
.unwrap();

return Err(());
}

Ok(())
});

if let Err(err) = err {
err.compare_to_file(output_stderr).unwrap();
} else {
let _ = std::fs::remove_file(&output_file);
let _ = std::fs::remove_file(&output_stderr);
}
}

#[testing::fixture("tests/errors/**/*.ts")]
fn error(input: PathBuf) {
let input_code = std::fs::read_to_string(&input).unwrap();
Expand Down Expand Up @@ -70,3 +123,52 @@ fn opts(mode: Mode) -> Options {
..Default::default()
}
}

fn reparse(cm: &Lrc<SourceMap>, handler: &Handler, filename: &PathBuf, input: String) {
let filename = FileName::Real(filename.into());

let fm = cm.new_source_file(filename.into(), input);

let syntax = Syntax::Es(EsSyntax {
auto_accessors: true,
decorators: true,
decorators_before_export: true,
explicit_resource_management: true,
import_attributes: true,
..Default::default()
});
let target = EsVersion::latest();

let comments = SingleThreadedComments::default();

let lexer = Capturing::new(Lexer::new(
syntax,
target,
StringInput::from(&*fm),
Some(&comments),
));

let mut parser = Parser::new_from(lexer);

let program = parser.parse_program();
let errors = parser.take_errors();

match program {
Ok(program) => program,
Err(err) => {
err.into_diagnostic(handler).emit();

for e in errors {
e.into_diagnostic(handler).emit();
}

return;
}
};

if !errors.is_empty() {
for e in errors {
e.into_diagnostic(handler).emit();
}
}
}
7 changes: 7 additions & 0 deletions crates/swc_fast_ts_strip/tests/tsc/Protected3.strip.broken
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
x Unexpected token `constructor`. Expected * for generator, private key, identifier or async
,-[$DIR/tests/tsc/Protected3.strip.js:2:1]
1 | class C {
2 | protected constructor() { }
: ^^^^^^^^^^^
3 | }
`----
3 changes: 3 additions & 0 deletions crates/swc_fast_ts_strip/tests/tsc/Protected3.strip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class C {
protected constructor() { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
x Unexpected token `constructor`. Expected * for generator, private key, identifier or async
,-[$DIR/tests/tsc/accessorsOverrideProperty9.strip.js:31:1]
30 | class MixedClass extends baseClass {
31 | public constructor(...args ) {
: ^^^^^^^^^^^
32 | super(...args);
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// @strict: true
// @target: es2017
// #41347, based on microsoft/rushstack

// Mixin utilities





// Base class
class ApiItem {
get members() {
return [];
}
}

// Normal subclass
class ApiEnumMember extends ApiItem {
}

// Mixin base class




function ApiItemContainerMixin (
baseClass
) {
class MixedClass extends baseClass {
public constructor(...args ) {
super(...args);
}

get members() {
return [];
}
}

return MixedClass;
}

// Subclass inheriting from mixin
export class ApiEnum extends ApiItemContainerMixin(ApiItem) {
// This worked prior to TypeScript 4.0:
get members() {
return [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
x Unexpected token `accessor`. Expected * for generator, private key, identifier or async
,-[$DIR/tests/tsc/autoAccessor7.strip.js:5:1]
4 | class C1 {
5 | abstract accessor a ;
: ^^^^^^^^
6 | }
`----
14 changes: 14 additions & 0 deletions crates/swc_fast_ts_strip/tests/tsc/autoAccessor7.strip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @target: esnext, es2022, es2015
// @useDefineForClassFields: *

class C1 {
abstract accessor a ;
}

class C2 extends C1 {
accessor a = 1;
}

class C3 extends C1 {
get a() { return 1; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
x Unexpected token `accessor`. Expected * for generator, private key, identifier or async
,-[$DIR/tests/tsc/autoAccessorAllowedModifiers.strip.js:6:1]
5 | accessor a ;
6 | public accessor b ;
: ^^^^^^^^
7 | private accessor c ;
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// @target: esnext
// @noTypesAndSymbols: true

class C1 {
accessor a ;
public accessor b ;
private accessor c ;
protected accessor d ;
abstract accessor e ;
static accessor f ;
public static accessor g ;
private static accessor h ;
protected static accessor i ;
accessor #j ;
accessor "k" ;
accessor 108 ;
accessor ["m"] ;
accessor n! ;
}

class C2 extends C1 {
override accessor e ;
static override accessor i ;
}





Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
x Unexpected token `?`. Expected * for generator, private key, identifier or async
,-[$DIR/tests/tsc/callChainWithSuper.strip.js:6:1]
5 | // GH#34952
6 | class Base { method?() {} }
: ^
7 | class Derived extends Base {
`----
10 changes: 10 additions & 0 deletions crates/swc_fast_ts_strip/tests/tsc/callChainWithSuper.strip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @target: *,-es3
// @strict: true
// @noTypesAndSymbols: true

// GH#34952
class Base { method?() {} }
class Derived extends Base {
method1() { return super.method?.(); }
method2() { return super["method"]?.(); }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
x Unexpected token `constructor`. Expected * for generator, private key, identifier or async
,-[$DIR/tests/tsc/classConstructorAccessibility4.strip.js:4:1]
3 | class A {
4 | private constructor() { }
: ^^^^^^^^^^^
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// @declaration: true

class A {
private constructor() { }

method() {
class B {
method() {
new A(); // OK
}
}

class C extends A { // OK
}
}
}

class D {
protected constructor() { }

method() {
class E {
method() {
new D(); // OK
}
}

class F extends D { // OK
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
x Unexpected token `constructor`. Expected * for generator, private key, identifier or async
,-[$DIR/tests/tsc/classConstructorAccessibility5.strip.js:2:1]
1 | class Base {
2 | protected constructor() { }
: ^^^^^^^^^^^
3 | }
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Base {
protected constructor() { }
}
class Derived extends Base {
static make() { new Base() } // ok
}

class Unrelated {
static fake() { new Base() } // error
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
x Unexpected token `constructor`. Expected * for generator, private key, identifier or async
,-[$DIR/tests/tsc/classConstructorOverloadsAccessibility.strip.js:4:1]
3 | class A {
4 | public constructor(a ) // error
: ^^^^^^^^^^^
5 | protected constructor(a ) // error
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// @declaration: true

class A {
public constructor(a ) // error
protected constructor(a ) // error
private constructor(a )
private constructor() {

}
}

class B {
protected constructor(a ) // error
constructor(a )
constructor() {

}
}

class C {
protected constructor(a )
protected constructor(a )
protected constructor() {

}
}

class D {
constructor(a )
constructor(a )
public constructor() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
x Unexpected token `?`. Expected * for generator, private key, identifier or async
,-[$DIR/tests/tsc/classWithOptionalParameter.strip.js:5:1]
4 | x ;
5 | f?() {}
: ^
6 | }
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// classes do not permit optional parameters, these are errors

class C {
x ;
f?() {}
}

class C2 {
x ;
f?(x ) {}
}
Loading
Loading