mirror of
https://github.com/swc-project/swc.git
synced 2024-12-25 06:36:08 +03:00
fix(es): Fix bugs for the type checker (#1823)
swc_ecma_codegen: - Fix codegen of constructor signatures. swc_ecma_parser: - Fix lexing of long numeric literals.
This commit is contained in:
parent
001af8637d
commit
7fa4e1bea5
@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs"]
|
||||
license = "Apache-2.0/MIT"
|
||||
name = "swc_ecma_codegen"
|
||||
repository = "https://github.com/swc-project/swc.git"
|
||||
version = "0.57.0"
|
||||
version = "0.57.1"
|
||||
|
||||
[dependencies]
|
||||
bitflags = "1"
|
||||
|
@ -82,11 +82,17 @@ impl<'a> Emitter<'a> {
|
||||
fn emit_ts_constructor_signature_decl(&mut self, n: &TsConstructSignatureDecl) -> Result {
|
||||
self.emit_leading_comments_of_span(n.span(), false)?;
|
||||
|
||||
keyword!("constructor");
|
||||
keyword!("new");
|
||||
|
||||
punct!("(");
|
||||
self.emit_list(n.span, Some(&n.params), ListFormat::Parameters)?;
|
||||
punct!(")");
|
||||
|
||||
if let Some(type_ann) = &n.type_ann {
|
||||
punct!(":");
|
||||
space!();
|
||||
emit!(type_ann);
|
||||
}
|
||||
}
|
||||
|
||||
#[emitter]
|
||||
|
51
ecmascript/codegen/tests/fixture.rs
Normal file
51
ecmascript/codegen/tests/fixture.rs
Normal file
@ -0,0 +1,51 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use swc_common::input::SourceFileInput;
|
||||
use swc_ecma_ast::EsVersion;
|
||||
use swc_ecma_codegen::{text_writer::JsWriter, Emitter};
|
||||
use swc_ecma_parser::{lexer::Lexer, Parser, Syntax};
|
||||
use testing::{run_test2, NormalizedOutput};
|
||||
|
||||
#[testing::fixture("fixture/**/input.ts")]
|
||||
fn test_fixture(input: PathBuf) {
|
||||
let dir = input.parent().unwrap();
|
||||
let output = dir.join(format!(
|
||||
"output.{}",
|
||||
input.extension().unwrap().to_string_lossy()
|
||||
));
|
||||
|
||||
run_test2(false, |cm, _| {
|
||||
let fm = cm.load_file(&input).unwrap();
|
||||
|
||||
let lexer = Lexer::new(
|
||||
Syntax::Typescript(Default::default()),
|
||||
EsVersion::latest(),
|
||||
SourceFileInput::from(&*fm),
|
||||
None,
|
||||
);
|
||||
let mut parser = Parser::new_from(lexer);
|
||||
let m = parser
|
||||
.parse_module()
|
||||
.expect("failed to parse input as a module");
|
||||
|
||||
let mut buf = vec![];
|
||||
|
||||
{
|
||||
let mut emitter = Emitter {
|
||||
cfg: swc_ecma_codegen::Config { minify: false },
|
||||
cm: cm.clone(),
|
||||
comments: None,
|
||||
wr: Box::new(JsWriter::new(cm.clone(), "\n", &mut buf, None)),
|
||||
};
|
||||
|
||||
emitter.emit_module(&m).unwrap();
|
||||
}
|
||||
|
||||
NormalizedOutput::from(String::from_utf8(buf).unwrap())
|
||||
.compare_to_file(&output)
|
||||
.unwrap();
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
14
ecmascript/codegen/tests/fixture/stc/0001/input.ts
Normal file
14
ecmascript/codegen/tests/fixture/stc/0001/input.ts
Normal file
@ -0,0 +1,14 @@
|
||||
interface I {
|
||||
(strs: TemplateStringsArray, ...subs: number[]): I;
|
||||
member: {
|
||||
new(s: string): {
|
||||
new(n: number): {
|
||||
new(): boolean;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
var f: I;
|
||||
|
||||
var x = new new new f`abc${0}def`.member("hello")(42) === true;
|
||||
|
12
ecmascript/codegen/tests/fixture/stc/0001/output.ts
Normal file
12
ecmascript/codegen/tests/fixture/stc/0001/output.ts
Normal file
@ -0,0 +1,12 @@
|
||||
interface I {
|
||||
(strs: TemplateStringsArray, ...subs: number[]) : I;
|
||||
member: {
|
||||
new(s: string): {
|
||||
new(n: number): {
|
||||
new(): boolean;
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
var f: I;
|
||||
var x = new new new f`abc${0}def`.member("hello")(42) === true;
|
@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs", "examples/**/*.rs"]
|
||||
license = "Apache-2.0/MIT"
|
||||
name = "swc_ecma_parser"
|
||||
repository = "https://github.com/swc-project/swc.git"
|
||||
version = "0.59.1"
|
||||
version = "0.59.2"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
|
@ -26,6 +26,7 @@ impl<'a, I: Input> Lexer<'a, I> {
|
||||
let start = self.cur_pos();
|
||||
|
||||
let starts_with_zero = self.cur().unwrap() == '0';
|
||||
let mut raw_val = String::new();
|
||||
|
||||
let val = if starts_with_dot {
|
||||
// first char is '.'
|
||||
@ -37,6 +38,7 @@ impl<'a, I: Input> Lexer<'a, I> {
|
||||
self.input.bump();
|
||||
return Ok(Either::Right(s));
|
||||
}
|
||||
write!(raw_val, "{}", s).unwrap();
|
||||
if starts_with_zero {
|
||||
// TODO: I guess it would be okay if I don't use -ffast-math
|
||||
// (or something like that), but needs review.
|
||||
@ -88,6 +90,7 @@ impl<'a, I: Input> Lexer<'a, I> {
|
||||
//
|
||||
// `.1.a`, `.1e-4.a` are valid,
|
||||
if self.cur() == Some('.') {
|
||||
raw_val.push('.');
|
||||
self.bump();
|
||||
if starts_with_dot {
|
||||
debug_assert!(self.cur().is_some());
|
||||
@ -97,15 +100,15 @@ impl<'a, I: Input> Lexer<'a, I> {
|
||||
let mut raw = Raw(Some(String::new()));
|
||||
// Read numbers after dot
|
||||
let dec_val = self.read_int(10, 0, &mut raw)?;
|
||||
val = self.with_buf(|_, s| {
|
||||
write!(s, "{}.", val).unwrap();
|
||||
|
||||
val = {
|
||||
if let Some(..) = dec_val {
|
||||
s.push_str(&raw.0.as_ref().unwrap());
|
||||
raw_val.push_str(&raw.0.as_ref().unwrap());
|
||||
}
|
||||
|
||||
Ok(s.parse().expect("failed to parse float using rust's impl"))
|
||||
})?;
|
||||
raw_val
|
||||
.parse()
|
||||
.expect("failed to parse float using rust's impl")
|
||||
};
|
||||
}
|
||||
|
||||
// Handle 'e' and 'E'
|
||||
@ -123,6 +126,8 @@ impl<'a, I: Input> Lexer<'a, I> {
|
||||
}
|
||||
};
|
||||
|
||||
raw_val.push('e');
|
||||
|
||||
let positive = if next == '+' || next == '-' {
|
||||
self.bump(); // remove '+', '-'
|
||||
next == '+'
|
||||
@ -132,10 +137,12 @@ impl<'a, I: Input> Lexer<'a, I> {
|
||||
|
||||
let exp = self.read_number_no_dot(10)?;
|
||||
let flag = if positive { '+' } else { '-' };
|
||||
|
||||
raw_val.push(flag);
|
||||
write!(raw_val, "{}", exp).unwrap();
|
||||
|
||||
// TODO:
|
||||
val = format!("{}e{}{}", val, flag, exp)
|
||||
.parse()
|
||||
.expect("failed to parse float literal");
|
||||
val = raw_val.parse().expect("failed to parse float literal");
|
||||
}
|
||||
|
||||
self.ensure_not_ident()?;
|
||||
@ -519,7 +526,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn large_float() {
|
||||
fn large_bin_number() {
|
||||
const LONG: &str =
|
||||
"0B11111111111111111111111111111111111111111111111101001010100000010111110001111111111";
|
||||
|
||||
@ -529,6 +536,13 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn large_float_number() {
|
||||
const LONG: &str = "9.671406556917009e+24";
|
||||
|
||||
assert_eq!(num(LONG), 9.671406556917009e+24);
|
||||
}
|
||||
|
||||
/// Valid even on strict mode.
|
||||
const VALID_CASES: &[&str] = &[".0", "0.e-1", "0e8", ".8e1", "0.8e1", "1.18e1"];
|
||||
const INVALID_CASES_ON_STRICT: &[&str] = &["08e1", "08.1", "08.8e1", "08", "01"];
|
||||
|
1
ecmascript/parser/tests/typescript/stc/0002/input.ts
Normal file
1
ecmascript/parser/tests/typescript/stc/0002/input.ts
Normal file
@ -0,0 +1 @@
|
||||
obj2[9.671406556917009e+24];
|
47
ecmascript/parser/tests/typescript/stc/0002/input.ts.json
Normal file
47
ecmascript/parser/tests/typescript/stc/0002/input.ts.json
Normal file
@ -0,0 +1,47 @@
|
||||
{
|
||||
"type": "Script",
|
||||
"span": {
|
||||
"start": 0,
|
||||
"end": 28,
|
||||
"ctxt": 0
|
||||
},
|
||||
"body": [
|
||||
{
|
||||
"type": "ExpressionStatement",
|
||||
"span": {
|
||||
"start": 0,
|
||||
"end": 28,
|
||||
"ctxt": 0
|
||||
},
|
||||
"expression": {
|
||||
"type": "MemberExpression",
|
||||
"span": {
|
||||
"start": 0,
|
||||
"end": 27,
|
||||
"ctxt": 0
|
||||
},
|
||||
"object": {
|
||||
"type": "Identifier",
|
||||
"span": {
|
||||
"start": 0,
|
||||
"end": 4,
|
||||
"ctxt": 0
|
||||
},
|
||||
"value": "obj2",
|
||||
"optional": false
|
||||
},
|
||||
"property": {
|
||||
"type": "NumericLiteral",
|
||||
"span": {
|
||||
"start": 5,
|
||||
"end": 26,
|
||||
"ctxt": 0
|
||||
},
|
||||
"value": 9.671406556917009e24
|
||||
},
|
||||
"computed": true
|
||||
}
|
||||
}
|
||||
],
|
||||
"interpreter": null
|
||||
}
|
@ -1084,7 +1084,7 @@
|
||||
"end": 8920,
|
||||
"ctxt": 0
|
||||
},
|
||||
"value": 9.67140655691701e24
|
||||
"value": 9.671406556917009e24
|
||||
},
|
||||
"computed": true
|
||||
}
|
||||
|
@ -1084,7 +1084,7 @@
|
||||
"end": 8918,
|
||||
"ctxt": 0
|
||||
},
|
||||
"value": 9.67140655691701e24
|
||||
"value": 9.671406556917009e24
|
||||
},
|
||||
"computed": true
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user