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

[Do Not Merge] The totality of the content-tag patch #6

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
push:
branches:
- main
- content-tag

env:
CI: 1
Expand Down
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@
"rust-analyzer",
"rkyv-impl",
"debug"
]
}
],
"editor.inlayHints.enabled": "off"
}
21 changes: 20 additions & 1 deletion crates/swc_ecma_ast/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use crate::{
Accessibility, TsExprWithTypeArgs, TsIndexSignature, TsTypeAnn, TsTypeParamDecl,
TsTypeParamInstantiation,
},
BigInt, ComputedPropName, EmptyStmt, Id, Ident, Number,
BigInt, ComputedPropName, ContentTagContent, ContentTagEnd, ContentTagStart, EmptyStmt, Id,
Ident, Number,
};

#[ast_node]
Expand Down Expand Up @@ -58,6 +59,21 @@ impl Take for Class {
}
}

#[ast_node("ContentTagMember")]
#[derive(Eq, Hash, EqIgnoreSpan)]
pub struct ContentTagMember {
pub span: Span,

#[cfg_attr(feature = "serde-impl", serde())]
pub opening: Box<ContentTagStart>,

#[cfg_attr(feature = "serde-impl", serde())]
pub contents: Box<ContentTagContent>,

#[cfg_attr(feature = "serde-impl", serde())]
pub closing: Box<ContentTagEnd>,
}

#[ast_node]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
Expand Down Expand Up @@ -86,6 +102,9 @@ pub enum ClassMember {
/// Stage 3
#[tag("AutoAccessor")]
AutoAccessor(AutoAccessor),

#[tag("ContentTag")]
ContentTagMember(ContentTagMember),
}

impl Take for ClassMember {
Expand Down
38 changes: 38 additions & 0 deletions crates/swc_ecma_ast/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ pub enum Expr {
#[tag("JSXFragment")]
JSXFragment(JSXFragment),

#[tag("ContentTag")]
ContentTagExpression(ContentTagExpression),

#[tag("TsTypeAssertion")]
TsTypeAssertion(TsTypeAssertion),

Expand Down Expand Up @@ -273,6 +276,7 @@ impl Clone for Expr {
JSXEmpty(e) => JSXEmpty(e.clone()),
JSXElement(e) => JSXElement(e.clone()),
JSXFragment(e) => JSXFragment(e.clone()),
ContentTagExpression(e) => ContentTagExpression(e.clone()),
TsTypeAssertion(e) => TsTypeAssertion(e.clone()),
TsConstAssertion(e) => TsConstAssertion(e.clone()),
TsNonNull(e) => TsNonNull(e.clone()),
Expand Down Expand Up @@ -607,6 +611,40 @@ pub struct FnExpr {
pub function: Box<Function>,
}

#[ast_node("ContentTagExpression")]
#[derive(Eq, Hash, EqIgnoreSpan)]
pub struct ContentTagExpression {
pub span: Span,

#[cfg_attr(feature = "serde-impl", serde())]
pub opening: Box<ContentTagStart>,

#[cfg_attr(feature = "serde-impl", serde())]
pub contents: Box<ContentTagContent>,

#[cfg_attr(feature = "serde-impl", serde())]
pub closing: Box<ContentTagEnd>,
}

#[ast_node("ContentTagStart")]
#[derive(Eq, Hash, EqIgnoreSpan, Copy)]
pub struct ContentTagStart {
pub span: Span,
}

#[ast_node("ContentTagEnd")]
#[derive(Eq, Hash, EqIgnoreSpan, Copy)]
pub struct ContentTagEnd {
pub span: Span,
}

#[ast_node("ContentTagContent")]
#[derive(Eq, Hash, EqIgnoreSpan)]
pub struct ContentTagContent {
pub span: Span,
pub value: Atom,
}

impl Take for FnExpr {
fn dummy() -> Self {
FnExpr {
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span};

pub use self::{
class::{
AutoAccessor, Class, ClassMember, ClassMethod, ClassProp, Constructor, Decorator, Key,
MethodKind, PrivateMethod, PrivateProp, StaticBlock,
AutoAccessor, Class, ClassMember, ClassMethod, ClassProp, Constructor, ContentTagMember,
Decorator, Key, MethodKind, PrivateMethod, PrivateProp, StaticBlock,
},
decl::{ClassDecl, Decl, FnDecl, UsingDecl, VarDecl, VarDeclKind, VarDeclarator},
expr::*,
Expand Down
16 changes: 16 additions & 0 deletions crates/swc_ecma_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,7 @@ where
Expr::TsSatisfies(n) => {
emit!(n)
}
Expr::ContentTagExpression(ref n) => emit!(n),
}

if self.comments.is_some() {
Expand Down Expand Up @@ -943,6 +944,20 @@ where
self.wr.write_str_lit(n.span, "<invalid>")?;
}

#[emitter]
fn emit_content_tag_template_expr(&mut self, n: &ContentTagExpression) -> Result {
self.wr.write_str_lit(n.span, "<template>")?;
self.wr.write_str_lit(n.span, &n.contents.value)?;
self.wr.write_str_lit(n.span, "</template>")?;
}

#[emitter]
fn emit_content_tag_template_member(&mut self, n: &ContentTagMember) -> Result {
self.wr.write_str_lit(n.span, "<template>")?;
self.wr.write_str_lit(n.span, &n.contents.value)?;
self.wr.write_str_lit(n.span, "</template>")?;
}

#[emitter]
fn emit_call_expr(&mut self, node: &CallExpr) -> Result {
self.wr.commit_pending_semi()?;
Expand Down Expand Up @@ -1369,6 +1384,7 @@ where
ClassMember::Empty(ref n) => emit!(n),
ClassMember::StaticBlock(ref n) => emit!(n),
ClassMember::AutoAccessor(ref n) => emit!(n),
ClassMember::ContentTagMember(ref n) => emit!(n),
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/swc_ecma_codegen/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ impl StartsWithAlphaNum for Expr {

Expr::OptChain(e) => e.starts_with_alpha_num(),

Expr::ContentTagExpression(..) => false,

Expr::Invalid(..) => true,
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/swc_ecma_compat_es2015/src/classes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,9 @@ where
ClassMember::StaticBlock(..) => unreachable!(
"classes pass: static blocks\nstatic_blocks pass should remove this"
),
ClassMember::ContentTagMember(..) => {
unreachable!("classes pass: content tag member\ntranspiler should remove this")
}
ClassMember::AutoAccessor(..) => {
unreachable!(
"classes pass: auto accessor \nauto_accesssors pass should remove this"
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_compat_es2015/src/destructuring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,7 @@ fn can_be_null(e: &Expr) -> bool {
| Expr::MetaProp(..) => true,

Expr::Lit(..) => false,
Expr::ContentTagExpression(..) => false,

Expr::Array(..)
| Expr::Arrow(..)
Expand Down
5 changes: 5 additions & 0 deletions crates/swc_ecma_compat_es2022/src/class_properties/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ impl<C: Comments> ClassProperties<C> {
ClassMember::ClassProp(_)
| ClassMember::AutoAccessor(_)
| ClassMember::PrivateProp(_)
| ClassMember::ContentTagMember(_)
| ClassMember::StaticBlock(_) => true,
});

Expand Down Expand Up @@ -953,6 +954,10 @@ impl<C: Comments> ClassProperties<C> {
unreachable!("static_blocks pass should remove this")
}

ClassMember::ContentTagMember(..) => {
unreachable!("content tag: compiler pass should remove this")
}

ClassMember::AutoAccessor(..) => {
unreachable!("auto_accessor pass should remove this")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,7 @@ impl Optimizer<'_> {
Expr::Member(me) => self.is_member_expr_skippable_for_seq(a, me),

Expr::Lit(..) => true,
Expr::ContentTagExpression(..) => true,

Expr::Yield(..) | Expr::Await(..) => false,

Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_minifier/src/util/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ impl SizeWithCtxt for Expr {
Expr::TsAs(_) => TODO,
Expr::TsInstantiation(_) => TODO,
Expr::TsSatisfies(_) => TODO,
Expr::ContentTagExpression(_) => TODO,
}
}
}
Expand Down
48 changes: 47 additions & 1 deletion crates/swc_ecma_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ pub use self::{
};
use crate::{
error::{Error, SyntaxError},
token::{BinOpToken, IdentLike, Token, Word},
lexer::state::ContentTagState,
token::{
BinOpToken, ContentTagContent, ContentTagEnd, ContentTagStart, IdentLike, Token, Word,
},
Context, Syntax,
};

Expand Down Expand Up @@ -678,6 +681,40 @@ impl<'a> Lexer<'a> {
}))
}

fn read_content_tag_template(&mut self) -> LexResult<Token> {
let start = self.cur_pos();
loop {
if !self.cur().is_some() {
return self.error(start, SyntaxError::Eof);
}
if self.is_str("</template>") {
self.state.content_tag_template = ContentTagState::Ending;
let slice_end = self.cur_pos();
return Ok(ContentTagContent {
value: unsafe {
// safety: we know start and slice_end are valid cursor
// positions because that's where we got them from
self.input.slice(start, slice_end)
}
.into(),
});
}
self.bump();
}
}

fn end_content_tag_template(&mut self) -> LexResult<Token> {
let start = self.cur_pos();
if !self.cur().is_some() || !self.is_str("</template>") {
return self.error(start, SyntaxError::Eof);
}
for _ in 0..11 {
self.bump();
}
self.state.content_tag_template = ContentTagState::None;
Ok(ContentTagEnd)
}

#[inline(never)]
fn read_token_lt_gt(&mut self) -> LexResult<Option<Token>> {
debug_assert!(self.cur() == Some('<') || self.cur() == Some('>'));
Expand All @@ -687,6 +724,15 @@ impl<'a> Lexer<'a> {
let c = self.cur().unwrap();
self.bump();

if c == '<' && self.is_str("template>") {
// consume the rest of the opening <template> tag
for _ in 0..9 {
self.bump();
}
self.state.content_tag_template = ContentTagState::Reading;
return Ok(Some(ContentTagStart));
}

if self.syntax.typescript() && self.ctx.in_type && !self.ctx.should_not_lex_lt_or_gt_as_type
{
if c == '<' {
Expand Down
18 changes: 18 additions & 0 deletions crates/swc_ecma_parser/src/lexer/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ pub(super) struct State {
syntax: Syntax,

token_type: Option<TokenType>,

pub content_tag_template: ContentTagState,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ContentTagState {
None,
Reading,
Ending,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -198,6 +207,14 @@ impl<'a> Iterator for Lexer<'a> {
let mut start = self.cur_pos();

let res = (|| -> Result<Option<_>, _> {
if self.state.content_tag_template == ContentTagState::Reading {
return Ok(Some(self.read_content_tag_template()?));
}

if self.state.content_tag_template == ContentTagState::Ending {
return Ok(Some(self.end_content_tag_template()?));
}

if let Some(start) = self.state.next_regexp {
return Ok(Some(self.read_regexp(start)?));
}
Expand Down Expand Up @@ -383,6 +400,7 @@ impl State {
line_start: BytePos(0),
cur_line: 1,
syntax,
content_tag_template: ContentTagState::None,
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions crates/swc_ecma_parser/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ macro_rules! tok {
crate::token::Token::JSXTagEnd
};

(ContentTagStart) => {
crate::token::Token::ContentTagStart
};

(ContentTagEnd) => {
crate::token::Token::ContentTagEnd
};

($tt:tt) => {
crate::token::Token::Word(crate::token::Word::Ident(crate::token::IdentLike::Known(
known_ident!($tt),
Expand Down
14 changes: 14 additions & 0 deletions crates/swc_ecma_parser/src/parser/class_and_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,20 @@ impl<I: Tokens> Parser<I> {
trace_cur!(self, parse_class_member);

let start = cur_pos!(self);

if is!(self, ContentTagStart) {
return self
.parse_content_tag_template()
.map(|content_tag_template| {
ClassMember::ContentTagMember(ContentTagMember {
span: content_tag_template.span,
opening: content_tag_template.opening,
contents: content_tag_template.contents,
closing: content_tag_template.closing,
})
});
}

let decorators = self.parse_decorators(false)?;
let declare = self.syntax().typescript() && eat!(self, "declare");
let accessibility = if self.input.syntax().typescript() {
Expand Down
Loading
Loading