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

tagged template literal can contain invalid escape sequence #78

Closed
kdy1 opened this issue Nov 25, 2018 · 1 comment · Fixed by #1175
Closed

tagged template literal can contain invalid escape sequence #78

kdy1 opened this issue Nov 25, 2018 · 1 comment · Fixed by #1175

Comments

@kdy1
Copy link
Member

kdy1 commented Nov 25, 2018

parser currently does not support parsing code like

latex`\unicode`

Note: this is valid only for es2018+
Implementor should check for the JscTarget.

This can be done by modifying code at

fn read_tmpl_token(&mut self, start_of_tpl: BytePos) -> LexResult<Token> {
let start = self.cur_pos();
// TODO: Optimize
let mut has_escape = false;
let mut cooked = String::new();
let mut raw = String::new();
while let Some(c) = self.cur() {
if c == '`' || (c == '$' && self.peek() == Some('{')) {
if start == self.cur_pos() && self.state.last_was_tpl_element() {
if c == '$' {
self.bump();
self.bump();
return Ok(tok!("${"));
} else {
self.bump();
return Ok(tok!('`'));
}
}
// TODO: Handle error
return Ok(Template {
cooked: cooked.into(),
raw: raw.into(),
has_escape,
});
}
if c == '\\' {
has_escape = true;
raw.push('\\');
let mut wrapped = Raw(Some(raw));
let ch = self.read_escaped_char(&mut wrapped)?;
raw = wrapped.0.unwrap();
if let Some(s) = ch {
cooked.extend(s);
}
} else if c.is_line_break() {
self.state.had_line_break = true;
let c = if c == '\r' && self.peek() == Some('\n') {
raw.push_str("\\r\\n");
self.bump(); // '\r'
'\n'
} else {
match c {
'\n' => raw.push_str("\n"),
'\r' => raw.push_str("\r"),
'\u{2028}' => raw.push_str("\u{2028}"),
'\u{2029}' => raw.push_str("\u{2029}"),
_ => unreachable!(),
}
c
};
self.bump();
cooked.push(c);
} else {
self.bump();
cooked.push(c);
raw.push(c);
}
}
self.error(start_of_tpl, SyntaxError::UnterminatedTpl)?
}

@swc-bot
Copy link
Collaborator

swc-bot commented Oct 26, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging a pull request may close this issue.

2 participants