-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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: Automatically generate cooked
for templateElement
#14757
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/52573/ |
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.
For reviewers: please also review https://github.com/iansan5653/unraw/blob/master/src/index.ts
packages/babel-types/test/builders/es2015/__snapshots__/templateElement.js.snap
Outdated
Show resolved
Hide resolved
|
||
let cooked = null; | ||
try { | ||
cooked = unraw(raw); |
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.
Q: should we throw if user provided inconsistent cooked
? Currently we mute such errors and trust raw
, but in this case either the raw
or cooked
could be incorrect.
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.
should we throw if user provided inconsistent
cooked
?
I'm not sure.
Currently we mute such errors and trust
raw
, but in this case either theraw
orcooked
could be incorrect.
Can you give an example?
I test that fn \u
is legal and \u
is not.
Looks like it should be checked in TemplateLiteral
.
Then we can do it later.😂
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.
For example, when user provided both raw
and cooked
:
t.templateElement({
raw: "face",
cooked: "faec" // which one is correct? face or faec?
})
The current main behaviour is leave it as-is. The PR behaviour is to ignore cooked
and reassign cooked
to be raw
. Now thanks to unraw
we know that this is not a valid template element. Should we throw a validation error in this case?
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.
Make this a behavior in babel8?
But I don't know if jest snap supports 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.
Now we ignore the cooked
passed in by the user.
As for raw
exceptions I think it can be done in the future.
We already have the logic to "unraw" template literals in our parser, I wonder if we could extract it from there to have a single implementation. |
@nicolo-ribaudo |
@nicolo-ribaudo readStringContents("template", raw, 0, 0, 0, {
unterminated() {
unterminatedCalled = true;
},
strictNumericEscape: error,
invalidEscapeSequence: error,
numericSeparatorInEscapeSequence: error,
unexpectedNumericSeparator: error,
invalidDigit: error,
invalidCodePoint: error,
}) |
I think it's not
(which is actually an invalid |
Try this: let unterminatedCalled = false;
let str, containsInvalid;
try {
const error = () => {
throw new Error();
};
({ str, containsInvalid } = readStringContents(
"template",
"abc\\u{1000_0000}",
0,
0,
0,
{
unterminated() {
unterminatedCalled = true;
},
strictNumericEscape: error,
invalidEscapeSequence: error,
numericSeparatorInEscapeSequence: error,
unexpectedNumericSeparator: error,
invalidDigit: error,
invalidCodePoint: error,
}
));
} catch {
// TODO: When https://github.com/babel/babel/issues/14775 is fixed
// we can remove the try/catch block.
unterminatedCalled = true;
containsInvalid = true;
}
if (!unterminatedCalled) throw new Error("Invalid raw");
cooked = containsInvalid ? null : str; some tests: "abcdef"; // ok
"abc\u{123}def"; // ok
"abc\u{}def"; // null cooked
"abc\u{10000_0000}def"; // null cooked
"abc`def"; // Invalid raw, error
"abc`"; // Invalid raw, error
"`abc"; // Invalid raw, error
"abc${"; // Invalid raw, error
"${abc"; // Invalid raw, error
"abc\\`"; // ok |
Whops sorry, it's a bug in |
95bb3bb
to
6f0d90f
Compare
Now it works fine! |
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! I just added two new tests.
As a Babel 8 follow-up, we could always try to generate .cooked
and throw if the generated version is different from the one passed as an argument.
cooked
for templateElement
.cooked
for templateElement
@liuxingbaoyu do you know when this is going to be released? |
I'm not sure, generally released by @nicolo-ribaudo and @JLHwung . |
I can release later today! |
unraw
I'm not sure if I need to check the
cooked
parameter with a value, please give your opinion.Also I don't think we need to do the same in
@babel/plugin-transform-template-literals
, just updatetypes
.