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

feat(es/parser): Support stripping flow with ES parser #9114

Draft
wants to merge 234 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
234 commits
Select commit Hold shift + click to select a range
44e58fa
Add tests
kdy1 Jun 29, 2024
b44c0c2
cargo feature
kdy1 Jun 29, 2024
241a7a2
Syntax::Flow
kdy1 Jun 29, 2024
8add23d
Rename cargo feature
kdy1 Jun 29, 2024
6bdd34a
Syntax::FLow
kdy1 Jun 29, 2024
22095ce
test for flow
kdy1 Jun 29, 2024
c5dc173
path
kdy1 Jun 29, 2024
84224d5
Update test refs
kdy1 Jun 29, 2024
e86dfdd
Syntax#flow()
kdy1 Jun 29, 2024
a383e05
Add flow.rs
kdy1 Jun 29, 2024
5d0083f
consume_flow_type
kdy1 Jun 29, 2024
2758bc4
`consume_flow_type_ann`
kdy1 Jun 29, 2024
4aac107
consume_flow_type_ann
kdy1 Jun 29, 2024
ffff84a
Flow patterns
kdy1 Jun 29, 2024
333d019
Update test refs
kdy1 Jun 29, 2024
e242928
enable jsx for flow
kdy1 Jun 29, 2024
1643f31
Update test refs
kdy1 Jun 29, 2024
e31f5c3
generic
kdy1 Jun 29, 2024
2046903
Suport more flow types
kdy1 Jun 29, 2024
f26bfa4
Update test refs
kdy1 Jun 29, 2024
e4e6eee
function return type
kdy1 Jun 29, 2024
9405149
Update test refs
kdy1 Jun 29, 2024
310c171
Type params
kdy1 Jun 29, 2024
cc694c9
Update test refs
kdy1 Jun 29, 2024
dd41590
`this` as param
kdy1 Jun 29, 2024
0a0a075
Update test refs
kdy1 Jun 29, 2024
948bf1e
unexpected!(self, "flow type")
kdy1 Jun 29, 2024
8901556
fn type
kdy1 Jun 29, 2024
502620d
Update test refs
kdy1 Jun 29, 2024
8dee107
tuple
kdy1 Jun 29, 2024
30b5865
var decl
kdy1 Jun 29, 2024
bf2eaf1
var decl
kdy1 Jun 29, 2024
dad7ce7
object type
kdy1 Jun 29, 2024
5f3164c
Update test refs
kdy1 Jun 29, 2024
e3e6733
Some work for `consume_flow_object_property`
kdy1 Jun 29, 2024
e44fc91
Update test refs
kdy1 Jun 29, 2024
7c4d1fd
comma
kdy1 Jun 29, 2024
a5ee7b8
Update test refs
kdy1 Jun 29, 2024
52255f6
generic
kdy1 Jun 29, 2024
8a7a9b9
Update test refs
kdy1 Jun 29, 2024
ce35726
Improve `consume_flow_fn_type`
kdy1 Jun 29, 2024
5819ab2
More work for flow types
kdy1 Jun 29, 2024
75dc0d6
tuple
kdy1 Jun 29, 2024
9be635e
More
kdy1 Jun 29, 2024
d3a28b1
`consume_flow_type_init`
kdy1 Jun 29, 2024
4c2c5f7
More work
kdy1 Jun 29, 2024
0d0acfe
allow(unused)
kdy1 Jun 29, 2024
d880367
Comment out some
kdy1 Jun 29, 2024
8421f2f
More work
kdy1 Jun 29, 2024
22c409e
Update test refs
kdy1 Jun 29, 2024
28fb21a
morgan
kdy1 Jun 29, 2024
4bd6570
`token_is_identifier`
kdy1 Jun 29, 2024
3a2b84e
more work
kdy1 Jun 29, 2024
6d9731f
More work for `consume_flow_primary_type`
kdy1 Jun 29, 2024
8aafad2
token_is_keyword
kdy1 Jun 29, 2024
7c8d2ec
consume_flow_inteface_type
kdy1 Jun 29, 2024
df0f9dd
Did more work
kdy1 Jun 29, 2024
1e27828
Update test refs
kdy1 Jun 29, 2024
b30b366
`may`
kdy1 Jun 29, 2024
c61c1d1
is_one_of
kdy1 Jun 29, 2024
8597967
use may
kdy1 Jun 29, 2024
016a978
Update test refs
kdy1 Jun 29, 2024
d263509
Fix `consume_flow_type_param_decls`
kdy1 Jun 29, 2024
60798b7
Update test refs
kdy1 Jun 29, 2024
b9d611c
Use `may_consume_flow_type_param_decls` for super class
kdy1 Jun 29, 2024
ac7744d
Update test refs
kdy1 Jun 29, 2024
e6504d6
Class type params
kdy1 Jun 29, 2024
7d1dcbf
Update test refs
kdy1 Jun 29, 2024
5efe80d
fix `consume_flow_type_param_decls`
kdy1 Jun 29, 2024
3d848a5
Update test refs
kdy1 Jun 29, 2024
fb376d3
rest pat
kdy1 Jun 29, 2024
fe1d226
flow declare/opaque
kdy1 Jun 29, 2024
5479897
Remove wrong assert
kdy1 Jun 29, 2024
e4db3ae
Update test refs
kdy1 Jun 29, 2024
d443c0c
`consume_flow_type_alias`
kdy1 Jun 29, 2024
6da0e27
known_ident
kdy1 Jun 29, 2024
6e88f0f
declare export
kdy1 Jun 29, 2024
7ce6f9c
Update test refs
kdy1 Jun 29, 2024
961465a
type alias
kdy1 Jun 29, 2024
96913a8
generic instat
kdy1 Jun 29, 2024
98bafbd
Update test refs
kdy1 Jun 29, 2024
dbecfbd
cmt
kdy1 Jun 29, 2024
73c13fb
fixup
kdy1 Jun 29, 2024
e301939
More port
kdy1 Jun 29, 2024
3e16b91
Remove usage of cur!(false)
kdy1 Jun 29, 2024
da5f0c4
Doc
kdy1 Jun 29, 2024
17bd388
Fix `consume_flow_type_init`
kdy1 Jun 29, 2024
87cc834
Update test refs
kdy1 Jun 29, 2024
d07923e
more
kdy1 Jun 29, 2024
3f2ba01
consume_flow_function_type_param
kdy1 Jun 30, 2024
c6fe366
Update test refs
kdy1 Jun 30, 2024
052e021
flowParseTupleType
kdy1 Jun 30, 2024
b8c8d82
flowParseTypeofType
kdy1 Jun 30, 2024
9a743b7
test: Invalid should fail
kdy1 Jun 30, 2024
b497f48
Use parse_ident_name
kdy1 Jun 30, 2024
795d8fe
Update test refs
kdy1 Jun 30, 2024
93d7c84
+- literals
kdy1 Jun 30, 2024
81e60d1
Update test refs
kdy1 Jun 30, 2024
88d7530
`may_consume_flow_type_ann`
kdy1 Jun 30, 2024
ccf0ef1
is_class_property
kdy1 Jun 30, 2024
b606dd8
Update test refs
kdy1 Jun 30, 2024
5032fce
Fix `consume_flow_tuple_type`
kdy1 Jun 30, 2024
33d341a
known ident: "renders"
kdy1 Jun 30, 2024
6c21c4c
`renders` type
kdy1 Jun 30, 2024
c29b384
TODO
kdy1 Jun 30, 2024
ebf50da
eat
kdy1 Jun 30, 2024
6825a4d
export
kdy1 Jun 30, 2024
5a48ffd
fix opaque
kdy1 Jun 30, 2024
d0c6e73
Update test refs
kdy1 Jun 30, 2024
5b328f8
do while
kdy1 Jun 30, 2024
f934e3d
`parse_flow_restricted_ident`
kdy1 Jun 30, 2024
9dd1be0
this param
kdy1 Jun 30, 2024
6d22856
trace_cur
kdy1 Jun 30, 2024
ccdb1bd
trace_cur
kdy1 Jun 30, 2024
e076a9f
trace_cur
kdy1 Jun 30, 2024
8c12200
trace_cur
kdy1 Jun 30, 2024
b6ee58e
trace_cur
kdy1 Jun 30, 2024
a1b5e88
Fix `consume_flow_generic_type`
kdy1 Jun 30, 2024
32ae5e5
Update test refs
kdy1 Jun 30, 2024
8f52075
parse var stmt
kdy1 Jun 30, 2024
46ff87f
Update test refs
kdy1 Jun 30, 2024
3b25e56
WIP: consume_flow_object_type
kdy1 Jun 30, 2024
f6c66d6
consume_flow_object_type_interanl_slot
kdy1 Jun 30, 2024
5f39a06
() for node
kdy1 Jun 30, 2024
18fc592
`parse_flow_object_type_indexer`
kdy1 Jun 30, 2024
c5ed90a
`consume_flow_object_property_key`
kdy1 Jun 30, 2024
64e9983
`consume_flow_object_type_call_property`
kdy1 Jun 30, 2024
7ec8524
`consume_flow_object_type_methodish`
kdy1 Jun 30, 2024
1c1b78d
`consume_flow_object_type_semicolon`
kdy1 Jun 30, 2024
ed4a21f
Fix compilation
kdy1 Jun 30, 2024
037c951
Update test refs
kdy1 Jun 30, 2024
38ba014
`is_class_method`
kdy1 Jun 30, 2024
50b5385
Update test refs
kdy1 Jun 30, 2024
c3aecea
Use consume_flow_export
kdy1 Jun 30, 2024
6221c37
Update test refs
kdy1 Jun 30, 2024
b211804
trace_cur
kdy1 Jun 30, 2024
44c33d4
`consume_flow_type_annotatable_identifier`
kdy1 Jun 30, 2024
eeae9c2
Enable decorator
kdy1 Jun 30, 2024
863546c
Update test refs
kdy1 Jun 30, 2024
ec8f262
Revert
kdy1 Jun 30, 2024
fba716c
fix test suite
kdy1 Jun 30, 2024
e472c51
Update test refs
kdy1 Jun 30, 2024
474645b
fixup for test suite
kdy1 Jun 30, 2024
479bdba
implements
kdy1 Jun 30, 2024
9b4cd4d
Update test refs
kdy1 Jun 30, 2024
953a92a
eat!(self, ?)
kdy1 Jun 30, 2024
b33c1d4
empty body
kdy1 Jun 30, 2024
043c1db
Update test refs
kdy1 Jun 30, 2024
34ea6d8
label for tuple
kdy1 Jun 30, 2024
347a259
Update test refs
kdy1 Jun 30, 2024
91df1a6
renders type
kdy1 Jun 30, 2024
c19816b
Update test refs
kdy1 Jun 30, 2024
af9ff93
ctx
kdy1 Jun 30, 2024
b8049e9
eat!(self, "opaque");
kdy1 Jun 30, 2024
3e6e243
Update test refs
kdy1 Jun 30, 2024
8510e97
type-only imports
kdy1 Jun 30, 2024
f9fa097
Update test refs
kdy1 Jun 30, 2024
d37d88b
type-only imports
kdy1 Jun 30, 2024
0137a86
Update test refs
kdy1 Jun 30, 2024
e13c72a
`consume_flow_object_type_property`
kdy1 Jun 30, 2024
f28aad8
fixup
kdy1 Jun 30, 2024
b6287c3
Fix build
kdy1 Jun 30, 2024
0309671
Update test refs
kdy1 Jun 30, 2024
92dc4b4
`consume_flow_type_parameter_instantiation_call_or_new`
kdy1 Jun 30, 2024
a0f1648
Use it
kdy1 Jun 30, 2024
afaa2de
Update test refs
kdy1 Jun 30, 2024
fe023c2
tracing-spans
kdy1 Jun 30, 2024
b7d732e
tracing
kdy1 Jun 30, 2024
2795bc0
More work for flow
kdy1 Jun 30, 2024
7946dbc
Update test refs
kdy1 Jun 30, 2024
ba41d6b
Update test refs
kdy1 Jun 30, 2024
0ef6f01
Remove dbg
kdy1 Jun 30, 2024
c751db9
type alias in declare opaque
kdy1 Jun 30, 2024
26e31d0
Update test refs
kdy1 Jun 30, 2024
92b901d
Remove some wrong code
kdy1 Jun 30, 2024
f2063c2
Update test refs
kdy1 Jun 30, 2024
e5e1d02
Update test refs
kdy1 Jun 30, 2024
670819e
declare
kdy1 Jun 30, 2024
afa15df
Update test refs
kdy1 Jun 30, 2024
16cee22
Option<()>
kdy1 Jul 1, 2024
07e00b6
consume_flow_anon_function_without_parens
kdy1 Jul 1, 2024
dfc9266
Update test refs
kdy1 Jul 1, 2024
dc96a3d
type ann
kdy1 Jul 1, 2024
c8c5cfe
Update test refs
kdy1 Jul 1, 2024
102123c
Check for line break
kdy1 Jul 1, 2024
c8f48f2
parse_reexports
kdy1 Jul 2, 2024
4f5ce83
USe it
kdy1 Jul 2, 2024
9673854
consume `export` token
kdy1 Jul 2, 2024
7611d4c
Update test refs
kdy1 Jul 2, 2024
a394572
"module" token
kdy1 Jul 2, 2024
4bf3c59
`consume_flow_module_declaration`
kdy1 Jul 2, 2024
b4c8b88
Update test refs
kdy1 Jul 2, 2024
04614ee
`consume_flow_interfaceish`
kdy1 Jul 2, 2024
b42c592
"mixins" is now a token
kdy1 Jul 2, 2024
dabdb54
Use it
kdy1 Jul 2, 2024
58e552c
Update test refs
kdy1 Jul 2, 2024
032930d
eat!(self, "interface");
kdy1 Jul 2, 2024
cb0c2b4
Update test refs
kdy1 Jul 2, 2024
cf85c69
export type
kdy1 Jul 2, 2024
a0eec6c
`: type` for opaque
kdy1 Jul 2, 2024
f09372f
Update test refs
kdy1 Jul 2, 2024
c584a34
fixup: `: type` should be optional
kdy1 Jul 2, 2024
1a4a0db
Improve `consume_flow_type_alias`
kdy1 Jul 2, 2024
1e45f5e
Update test refs
kdy1 Jul 2, 2024
0ee4c25
`export Var`
kdy1 Jul 2, 2024
d57a0e0
Update test refs
kdy1 Jul 2, 2024
e173c86
ignore some errors
kdy1 Jul 2, 2024
6e700c1
Ignore tests for `reserved word`
kdy1 Jul 2, 2024
116ed33
Ignore some ECMA spec tests
kdy1 Jul 2, 2024
43516bb
`eval` / `arguments`
kdy1 Jul 2, 2024
6940341
`IGNORED`
kdy1 Jul 2, 2024
98cefd1
fix lints
kdy1 Jul 2, 2024
78036af
negate
kdy1 Jul 2, 2024
878e4f9
jsx
kdy1 Jul 2, 2024
90d29fb
jsx
kdy1 Jul 2, 2024
6ee6860
Update test refs
kdy1 Jul 2, 2024
efddbaf
`consume_flow_type_args`
kdy1 Jul 2, 2024
df035e0
`consume_flow_enum_declaration`
kdy1 Jul 2, 2024
3fe4d8b
`consume_flow_enum_body`
kdy1 Jul 3, 2024
9baf7b9
WIP
kdy1 Jul 4, 2024
49aa6ce
`consume_flow_enum_member_init`
kdy1 Jul 4, 2024
1c4eff5
`consume_flow_enum_member_init`
kdy1 Jul 4, 2024
478762a
lint
kdy1 Jul 4, 2024
3768e14
consume comma
kdy1 Jul 4, 2024
66eff34
Update test refs
kdy1 Jul 4, 2024
003b81b
`export enum`
kdy1 Jul 4, 2024
fd45a86
eat!("enum")
kdy1 Jul 4, 2024
89970bc
Update test refs
kdy1 Jul 4, 2024
7da7a18
empty enum
kdy1 Jul 4, 2024
3ee4c22
enum declaration
kdy1 Jul 4, 2024
31629d7
Update test refs
kdy1 Jul 4, 2024
193275a
interface
kdy1 Jul 4, 2024
cb117a7
Update test refs
kdy1 Jul 4, 2024
b69558e
fixup for rebase
kdy1 Jul 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
6 changes: 4 additions & 2 deletions crates/swc_ecma_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ bench = false

[features]
# Used for debugging
debug = ["tracing-spans"]
default = ["typescript", "stacker"]
debug = ["tracing-spans"]
default = ["typescript", "stacker"]
# Strip flow types
flow = []
tracing-spans = []
typescript = []
verify = ["swc_ecma_visit"]
Expand Down
51 changes: 51 additions & 0 deletions crates/swc_ecma_parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,25 @@ pub enum SyntaxError {

ReservedTypeAssertion,
ReservedArrowTypeParam,

FlowThisParamMustBeFirst,
FlowThisParamCannotBeOptional,
FlowReservedWordInType {
word: JsWord,
},
FlowInexactInsideNonObject,
FlowInexactInsideExact,
FlowInexactVariance,
FlowUnexpectedSpreadType,
FlowUnexpectedProtoSpread,
FlowSpreadVariance,
FlowUnexpectedProtoMethod,
FlowMethodVariance,
FlowUnexpectedGetterSetter,
FlowDeclareOpaqueType,
FlowEnumExplicitTypeMustBeIdent,
FlowEnumInitMustBeLiteral,
FlowEnumShouldHaveMember,
}

impl SyntaxError {
Expand Down Expand Up @@ -758,6 +777,38 @@ impl SyntaxError {
as in `<T,>() => ...`."
.into(),
SyntaxError::InvalidAssignTarget => "Invalid assignment target".into(),
SyntaxError::FlowThisParamMustBeFirst => {
"The 'this' parameter must be the first parameter".into()
}
SyntaxError::FlowThisParamCannotBeOptional => {
"The 'this' parameter cannot be optional".into()
}
SyntaxError::FlowReservedWordInType { ref word } => {
format!("Cannot use a reserved word as a type name: '{}'", word).into()
}
SyntaxError::FlowInexactInsideNonObject => {
"Inexact type can only be used with object type".into()
}
SyntaxError::FlowInexactInsideExact => {
"Exact type can only be used with object type".into()
}
SyntaxError::FlowInexactVariance => "Inexact type cannot have variance".into(),
SyntaxError::FlowUnexpectedSpreadType => "Unexpected spread type".into(),
SyntaxError::FlowUnexpectedProtoSpread => {
"Unexpected spread of prototype property".into()
}
SyntaxError::FlowSpreadVariance => "Spread type cannot have variance".into(),
SyntaxError::FlowUnexpectedProtoMethod => "Unexpected prototype method".into(),
SyntaxError::FlowMethodVariance => "Method cannot have variance".into(),
SyntaxError::FlowUnexpectedGetterSetter => {
"Unexpected getter or setter in object type".into()
}
SyntaxError::FlowDeclareOpaqueType => "Opaque type cannot be declared".into(),
SyntaxError::FlowEnumExplicitTypeMustBeIdent => {
"Explicit type for enum must be an identifier".into()
}
SyntaxError::FlowEnumInitMustBeLiteral => "Enum initializer must be a literal".into(),
SyntaxError::FlowEnumShouldHaveMember => "Enum must have at least one member".into(),
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions crates/swc_ecma_parser/src/lexer/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ const L_L: ByteHandler = Some(|lexer| {
const L_M: ByteHandler = Some(|lexer| {
lexer.read_word_with(&|s| match s {
"meta" => Some(Word::Ident(IdentLike::Known(KnownIdent::Meta))),
"mixins" => Some(Word::Ident(IdentLike::Known(KnownIdent::Mixins))),
"module" => Some(Word::Ident(IdentLike::Known(KnownIdent::Module))),
_ => None,
})
});
Expand All @@ -191,6 +193,7 @@ const L_O: ByteHandler = Some(|lexer| {
lexer.read_word_with(&|s| match s {
"of" => Some(Word::Ident(IdentLike::Known(KnownIdent::Of))),
"object" => Some(Word::Ident(IdentLike::Known(KnownIdent::Object))),
"opaque" => Some(Word::Ident(IdentLike::Known(KnownIdent::Opaque))),
_ => None,
})
});
Expand All @@ -201,6 +204,7 @@ const L_P: ByteHandler = Some(|lexer| {
"package" => Some(Word::Ident(IdentLike::Known(KnownIdent::Package))),
"protected" => Some(Word::Ident(IdentLike::Known(KnownIdent::Protected))),
"private" => Some(Word::Ident(IdentLike::Known(KnownIdent::Private))),
"proto" => Some(Word::Ident(IdentLike::Known(KnownIdent::Proto))),
_ => None,
})
});
Expand All @@ -212,6 +216,7 @@ const L_R: ByteHandler = Some(|lexer| {
"return" => Some(Word::Keyword(Keyword::Return)),
"readonly" => Some(Word::Ident(IdentLike::Known(KnownIdent::Readonly))),
"require" => Some(Word::Ident(IdentLike::Known(KnownIdent::Require))),
"renders" => Some(Word::Ident(IdentLike::Known(KnownIdent::Renders))),
_ => None,
})
});
Expand Down
38 changes: 37 additions & 1 deletion crates/swc_ecma_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(test, feature(test))]
#![deny(clippy::all)]
#![deny(unused)]
#![allow(clippy::nonminimal_bool)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::unnecessary_unwrap)]
Expand Down Expand Up @@ -156,6 +155,11 @@ pub enum Syntax {
#[cfg_attr(docsrs, doc(cfg(feature = "typescript")))]
#[serde(rename = "typescript")]
Typescript(TsSyntax),
/// This variant requires the cargo feature `flow` to be enabled.
#[cfg(feature = "flow")]
#[cfg_attr(docsrs, doc(cfg(feature = "flow")))]
#[serde(rename = "flow")]
Flow(FlowSyntax),
}

impl Default for Syntax {
Expand Down Expand Up @@ -184,6 +188,8 @@ impl Syntax {
}) => import_attributes,
#[cfg(feature = "typescript")]
Syntax::Typescript(_) => true,
#[cfg(feature = "flow")]
Syntax::Flow(..) => true,
}
}

Expand All @@ -193,6 +199,8 @@ impl Syntax {
Syntax::Es(EsSyntax { jsx: true, .. }) => true,
#[cfg(feature = "typescript")]
Syntax::Typescript(TsSyntax { tsx: true, .. }) => true,
#[cfg(feature = "flow")]
Syntax::Flow(..) => true,
_ => false,
}
}
Expand All @@ -210,6 +218,8 @@ impl Syntax {
Syntax::Typescript(TsSyntax {
decorators: true, ..
}) => true,
#[cfg(feature = "flow")]
Syntax::Flow(..) => true,
_ => false,
}
}
Expand Down Expand Up @@ -238,6 +248,18 @@ impl Syntax {
matches!(self, Syntax::Typescript(..))
}

/// Should we parse flow?
#[cfg(not(feature = "flow"))]
pub const fn flow(self) -> bool {
false
}

/// Should we parse flow?
#[cfg(feature = "flow")]
pub const fn flow(self) -> bool {
matches!(self, Syntax::Flow(..))
}

pub fn export_default_from(self) -> bool {
matches!(
self,
Expand All @@ -264,6 +286,8 @@ impl Syntax {
}) => allow_super_outside_method,
#[cfg(feature = "typescript")]
Syntax::Typescript(_) => true,
#[cfg(feature = "flow")]
Syntax::Flow(..) => false,
}
}

Expand All @@ -275,6 +299,8 @@ impl Syntax {
}) => allow_return_outside_function,
#[cfg(feature = "typescript")]
Syntax::Typescript(_) => false,
#[cfg(feature = "flow")]
Syntax::Flow(..) => false,
}
}

Expand All @@ -283,6 +309,8 @@ impl Syntax {
#[cfg(feature = "typescript")]
Syntax::Typescript(t) => !t.no_early_errors,
Syntax::Es(..) => true,
#[cfg(feature = "flow")]
Syntax::Flow(..) => true,
}
}

Expand All @@ -302,6 +330,8 @@ impl Syntax {
}) => *using_decl,
#[cfg(feature = "typescript")]
Syntax::Typescript(_) => true,
#[cfg(feature = "flow")]
Syntax::Flow(..) => true,
}
}
}
Expand Down Expand Up @@ -379,6 +409,10 @@ pub struct EsSyntax {
#[deprecated(note = "Use 'EsSyntax' instead")]
pub type EsConfig = EsSyntax;

#[cfg(feature = "flow")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct FlowSyntax {}

/// Syntactic context.
#[derive(Debug, Clone, Copy, Default)]
pub struct Context {
Expand Down Expand Up @@ -442,6 +476,8 @@ pub struct Context {
disallow_conditional_types: bool,

allow_using_decl: bool,

flow_no_anon_function_type: bool,
}

#[derive(Debug, Clone, Copy, Default)]
Expand Down
39 changes: 34 additions & 5 deletions crates/swc_ecma_parser/src/parser/class_and_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ impl<I: Tokens> Parser<I> {

let type_params = if p.input.syntax().typescript() {
p.try_parse_ts_type_params(true, true)?
} else if p.syntax().flow() {
p.may_consume_flow_type_param_decls()?;
None
} else {
None
};
Expand Down Expand Up @@ -121,6 +124,9 @@ impl<I: Tokens> Parser<I> {

let implements = if p.input.syntax().typescript() && eat!(p, "implements") {
p.parse_ts_heritage_clause()?
} else if p.input.syntax().flow() && eat!(p, "implements") {
p.consume_flow_interface_extends()?;
vec![]
} else {
vec![]
};
Expand Down Expand Up @@ -239,6 +245,10 @@ impl<I: Tokens> Parser<I> {
// but it's a super class with type params, for example, in JSX.
if self.syntax().typescript() && is!(self, '<') {
Ok((super_class, self.parse_ts_type_args().map(Some)?))
} else if self.syntax().flow() && is!(self, '<') {
self.may_consume_flow_type_param_decls()?;

Ok((super_class, None))
} else {
Ok((super_class, None))
}
Expand Down Expand Up @@ -382,7 +392,7 @@ impl<I: Tokens> Parser<I> {

let start = cur_pos!(self);
let decorators = self.parse_decorators(false)?;
let declare = self.syntax().typescript() && eat!(self, "declare");
let declare = (self.syntax().typescript() || self.syntax().flow()) && eat!(self, "declare");
let accessibility = if self.input.syntax().typescript() {
self.parse_access_modifier()?
} else {
Expand Down Expand Up @@ -1066,7 +1076,14 @@ impl<I: Tokens> Parser<I> {
}
let definite = self.input.syntax().typescript() && !is_optional && eat!(self, '!');

let type_ann = self.try_parse_ts_type_ann()?;
let type_ann = if self.input.syntax().typescript() {
self.try_parse_ts_type_ann()?
} else if self.input.syntax().flow() {
self.may_consume_flow_type_ann()?;
None
} else {
None
};

let ctx = Context {
include_in_expr: true,
Expand Down Expand Up @@ -1150,12 +1167,14 @@ impl<I: Tokens> Parser<I> {

fn is_class_method(&mut self) -> bool {
is!(self, '(')
|| (self.input.syntax().typescript() && is!(self, '<'))
|| (self.input.syntax().typescript() && is!(self, JSXTagStart))
|| ((self.input.syntax().typescript() || self.input.syntax().flow()) && is!(self, '<'))
|| ((self.input.syntax().typescript() || self.input.syntax().flow())
&& is!(self, JSXTagStart))
}

fn is_class_property(&mut self, asi: bool) -> bool {
(self.input.syntax().typescript() && is!(self, '!' | ':'))
|| (self.input.syntax().flow() && is!(self, ':'))
|| is!(self, '=' | '}')
|| if asi {
is!(self, ';')
Expand Down Expand Up @@ -1298,6 +1317,9 @@ impl<I: Tokens> Parser<I> {
None
})
})?
} else if p.syntax().flow() {
p.may_consume_flow_type_param_decls()?;
None
} else {
None
};
Expand All @@ -1319,6 +1341,10 @@ impl<I: Tokens> Parser<I> {
let return_type = if p.syntax().typescript() && is!(p, ':') {
p.parse_ts_type_or_type_predicate_ann(&tok!(':'))
.map(Some)?
} else if p.input.syntax().flow() && is!(p, ':') {
p.consume_flow_type_ann()?;

None
} else {
None
};
Expand Down Expand Up @@ -1675,7 +1701,10 @@ impl<I: Tokens> FnBodyParser<Option<BlockStmt>> for Parser<I> {
is_simple_parameter_list: bool,
) -> PResult<Option<BlockStmt>> {
// allow omitting body and allow placing `{` on next line
if self.input.syntax().typescript() && !is!(self, '{') && eat!(self, ';') {
if (self.input.syntax().typescript() || self.input.syntax().flow())
&& !is!(self, '{')
&& eat!(self, ';')
{
return Ok(None);
}
let block = self.include_in_expr(true).parse_block(true);
Expand Down
17 changes: 17 additions & 0 deletions crates/swc_ecma_parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,10 @@ impl<I: Tokens> Parser<I> {
}
Ok(Some(args))
})
} else if self.input.syntax().flow() && is!(self, '<') {
self.consume_flow_type_param_decl()?;

None
} else {
None
};
Expand Down Expand Up @@ -659,6 +663,9 @@ impl<I: Tokens> Parser<I> {

let type_args = if self.syntax().typescript() && is!(self, '<') {
self.try_parse_ts_type_args()
} else if self.syntax().flow() && is!(self, '<') {
self.consume_flow_type_parameter_instantiation_call_or_new()?;
None
} else {
None
};
Expand Down Expand Up @@ -842,6 +849,12 @@ impl<I: Tokens> Parser<I> {

Ok(Some(return_type))
})
} else if !self.ctx().will_expect_colon_for_cond
&& self.input.syntax().flow()
&& is!(self, ':')
{
self.consume_flow_type_ann()?;
None
} else {
None
};
Expand Down Expand Up @@ -1886,6 +1899,10 @@ impl<I: Tokens> Parser<I> {

items.push(AssignTargetOrSpread::Pat(pat))
} else {
if self.input.syntax().flow() && is!(self, ':') {
self.may_consume_flow_type_ann()?;
}

if has_modifier {
self.emit_err(span!(self, modifier_start), SyntaxError::TS2369);
}
Expand Down
Loading
Loading