Skip to content

Commit

Permalink
Update error messages, and make them more helpful
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed May 19, 2020
1 parent 6022db0 commit 98cded9
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
26 changes: 19 additions & 7 deletions packages/babel-plugin-transform-unicode-escapes/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ export default declare(api => {
function escape(code) {
let str = code.toString(16);
// Sigh, node 6 doesn't have padStart
// TODO: Remove in Babel 8, when we drop node 6.
while (str.length < 4) str = "0" + str;
return "\\u" + str;
}

function replacer(match, backslashes, code) {
if (backslashes.length % 2 === 0) {
return match;
Expand All @@ -23,10 +25,21 @@ export default declare(api => {

return char.length === 1 ? escaped : escaped + escape(char.charCodeAt(1));
}

function replaceUnicodeEscapes(str) {
return str.replace(unicodeEscape, replacer);
}

function getUnicodeEscape(str) {
let match;
while ((match = unicodeEscape.exec(str))) {
if (match[1].length % 2 === 0) continue;
unicodeEscape.lastIndex = 0;
return match[0];
}
return null;
}

return {
name: "transform-unicode-escapes",
visitor: {
Expand Down Expand Up @@ -54,7 +67,7 @@ export default declare(api => {
}

throw path.buildCodeFrameError(
`Can't represent "${name}" as a bare identifier`,
`Can't represent '${name}' as a bare identifier`,
);
},

Expand All @@ -69,18 +82,17 @@ export default declare(api => {
const { node, parentPath } = path;
const { value } = node;

value.cooked = replaceUnicodeEscapes(value.cooked);

const raw = replaceUnicodeEscapes(value.raw);
if (raw === value.raw) return;
const firstEscape = getUnicodeEscape(value.raw);
if (!firstEscape) return;

const grandParent = parentPath.parentPath;
if (grandParent.isTaggedTemplateExpression()) {
throw path.buildCodeFrameError(
`Can't replace Unicode escape inside tagged template literal`,
`Can't replace Unicode escape '${firstEscape}' inside tagged template literals. You can enable '@babel/plugin-transform-template-literals' to compile them to classic strings.`,
);
}
value.raw = raw;

value.raw = replaceUnicodeEscapes(value.raw);
},
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Can't represent \"饾挏\" as a bare identifier"
"throws": "Can't represent '饾挏' as a bare identifier"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Can't represent \"饾挏\" as a bare identifier"
"throws": "Can't represent '饾挏' as a bare identifier"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Can't represent \"饾挏\" as a bare identifier"
"throws": "Can't represent '饾挏' as a bare identifier"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Can't represent \"饾挏\" as a bare identifier"
"throws": "Can't represent '饾挏' as a bare identifier"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Can't replace Unicode escape inside tagged template literal"
"throws": "Can't replace Unicode escape '\\u{1d49c}' inside tagged template literals. You can enable '@babel/plugin-transform-template-literals' to compile them to classic strings."
}

0 comments on commit 98cded9

Please sign in to comment.