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/ |
JLHwung
left a comment
There was a problem hiding this comment.
For reviewers: please also review https://github.com/iansan5653/unraw/blob/master/src/index.ts
|
|
||
| let cooked = null; | ||
| try { | ||
| cooked = unraw(raw); |
There was a problem hiding this comment.
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.
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 theraworcookedcould 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.
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.
Make this a behavior in babel8?
But I don't know if jest snap supports this.
There was a problem hiding this comment.
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! |
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! |


unrawI'm not sure if I need to check the
cookedparameter 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.