-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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/ast): Add raw
to Str
#4071
feat(es/ast): Add raw
to Str
#4071
Conversation
crates/swc_ecma_utils/src/macros.rs
Outdated
@@ -31,8 +31,7 @@ macro_rules! quote_str { | |||
$crate::swc_ecma_ast::Str { | |||
span: $span, | |||
value: $s.into(), | |||
has_escape: false, | |||
kind: Default::default(), | |||
raw: $s.into(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't we need '
or "
?
73238f9
to
34c53c2
Compare
@@ -1 +1 @@ | |||
var foo = "\xa0"; | |||
var foo = "\u{a0}"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrong as the target is es5.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much!
kind: StrKind::Normal { | ||
contains_quote: false, | ||
}, | ||
raw: Some(("\"".to_owned() + &v.to_string() + "\"").into()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would be bad for performance.
I think
raw: Some(format!("\"{}\"", v)),
would work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can manually use String
for better performance, but I don' think it will have a huge didfference.
(I mean String::push
& String::push_str
)
crates/swc_ecma_ast/src/lit.rs
Outdated
} | ||
|
||
impl Take for Str { | ||
fn dummy() -> Self { | ||
Str { | ||
span: DUMMY_SP, | ||
value: js_word!(""), | ||
has_escape: Default::default(), | ||
kind: Default::default(), | ||
raw: Some("\"\"".into()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be None
crates/swc_ecma_ast/src/expr.rs
Outdated
} | ||
|
||
impl Take for TplElement { | ||
fn dummy() -> Self { | ||
TplElement { | ||
span: DUMMY_SP, | ||
tail: Default::default(), | ||
cooked: Take::dummy(), | ||
raw: Take::dummy(), | ||
cooked: Some("".into()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
c77ed1c
to
629d8bc
Compare
kind: StrKind::Normal { | ||
contains_quote: false, | ||
}, | ||
raw: Some(format!("\"{}\"", v).into()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you missed this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is having this better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kdy1 Hm, do you mean
You can manually use String for better performance, but I don' think it will have a huge didfference.
(I mean String::push & String::push_str)
Just create String::new()
? I missed that because it is recovery mode
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in real code this will not happen... but I can refactor to using String
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean something like
{
let mut buf = String::new();
// ...
}
(I'm afk now)
@@ -1 +1 @@ | |||
console.log("\ud83d\ude00", "\ud83d@\ude00"); | |||
console.log("\\\0ud83d\\\0ude00", "\\\0ud83d@\\\0ude00"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is wrong, as the input was
console.log("\ud83d" + "\ude00", "\ud83d" + "@" + "\ude00");
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kdy1 I see... They are surrogates pairs... Looking for workaround
condition(), (x = 20); | ||
z || condition(), (x = "fuji"); | ||
x = (condition(), "foobar"); | ||
x = y ? "foo" : "foo"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be modified
crates/swc_ecma_minifier/tests/terser/compress/concat-strings/concat_1/output.js
Show resolved
Hide resolved
@kdy1 Fixed 👍 |
...s/compress/fixture/next/feedback-util-promisify/chunks/pages/_app-72ad41192608e93a/output.js
Show resolved
Hide resolved
@@ -1,7 +1,7 @@ | |||
import { m } from "../index.f66dda46.js"; | |||
import { u as useLang, a as useTitleTemplate, b as useTitle, c as useMeta, d as useLink } from "./hoofd.module.6c5395cb.js"; | |||
function MetaTags() { | |||
return useLang("nl"), useTitleTemplate("%s | \u{1F4AD}"), useTitle("Welcome to hoofd"), useMeta({ | |||
return useLang("nl"), useTitleTemplate("%s | 💭"), useTitle("Welcome to hoofd"), useMeta({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be problematic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm, it was original code, so we keep them as is, original code:
useTitleTemplate("%s | 💭")
Because target is not es5, it will not be problem
crates/swc_ecma_minifier/tests/terser/compress/object/property_with_unprintable/output.js
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Thank you so much for the hard work.
swc-bump:
- swc_ecma_ast --breaking
f98d571
to
93d76b2
Compare
Description:
feat - add
raw
property forStringLiteral
Also:
\n
in propsTemplateLiteral
- do not storeStringLiteral
(useJsWord
), we just duplicate AST node, but it is unnecessaryNote - there is another improvement and simplification inWe need do it here to better rewrite codeTemplateLiteral
, but I will do it in another PRAlso we should add
raw
for numbers too, so we will support linting1_000_000
ad also will print numbers as they were written by developer@ArturAralin after this we can simplify
quotes
rule, no need to look at original code (it is slow)BREAKING CHANGE:
Yes
Related issue (if exists):
No
Also:
IsDirective
in parser andswc_ecma_utils