Skip to content

Commit

Permalink
refactor: use error message template in jsx plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Mar 3, 2020
1 parent 5ededb5 commit d3c067e
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions packages/babel-parser/src/plugins/jsx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,23 @@ import * as N from "../../types";
import { isIdentifierChar, isIdentifierStart } from "../../util/identifier";
import type { Position } from "../../util/location";
import { isNewLine } from "../../util/whitespace";
import { Errors } from "../../parser/location";

const HEX_NUMBER = /^[\da-fA-F]+$/;
const DECIMAL_NUMBER = /^\d+$/;

const jsxErrors = Object.freeze({
AttributeIsEmpty:
"JSX attributes must only be assigned a non-empty expression",
MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>",
MissingClosingTagElement: "Expected corresponding JSX closing tag for <%0>",
UnsupportedJsxValue:
"JSX value should be either an expression or a quoted JSX text",
UnterminatedJsxContent: "Unterminated JSX contents",
UnwrappedAdjacentJSXElements:
"Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?",
});

// Be aware that this file is always executed and not only when the plugin is enabled.
// Therefore this contexts and tokens do always exist.
tc.j_oTag = new TokContext("<tag", false);
Expand Down Expand Up @@ -83,7 +96,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
let chunkStart = this.state.pos;
for (;;) {
if (this.state.pos >= this.length) {
throw this.raise(this.state.start, "Unterminated JSX contents");
throw this.raise(this.state.start, jsxErrors.UnterminatedJsxContent);
}

const ch = this.input.charCodeAt(this.state.pos);
Expand Down Expand Up @@ -143,7 +156,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
let chunkStart = ++this.state.pos;
for (;;) {
if (this.state.pos >= this.length) {
throw this.raise(this.state.start, "Unterminated string constant");
throw this.raise(this.state.start, Errors.UnterminatedString);
}

const ch = this.input.charCodeAt(this.state.pos);
Expand Down Expand Up @@ -280,10 +293,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.next();
node = this.jsxParseExpressionContainer(node);
if (node.expression.type === "JSXEmptyExpression") {
this.raise(
node.start,
"JSX attributes must only be assigned a non-empty expression",
);
this.raise(node.start, jsxErrors.AttributeIsEmpty);
}
return node;

Expand All @@ -292,10 +302,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.parseExprAtom();

default:
throw this.raise(
this.state.start,
"JSX value should be either an expression or a quoted JSX text",
);
throw this.raise(this.state.start, jsxErrors.UnsupportedJsxValue);
}
}

Expand Down Expand Up @@ -450,15 +457,14 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.raise(
// $FlowIgnore
closingElement.start,
"Expected corresponding JSX closing tag for <>",
jsxErrors.MissingClosingTagFragment,
);
} else if (!isFragment(openingElement) && isFragment(closingElement)) {
this.raise(
// $FlowIgnore
closingElement.start,
"Expected corresponding JSX closing tag for <" +
getQualifiedJSXName(openingElement.name) +
">",
jsxErrors.MissingClosingTagElement,
getQualifiedJSXName(openingElement.name),
);
} else if (!isFragment(openingElement) && !isFragment(closingElement)) {
if (
Expand All @@ -469,9 +475,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.raise(
// $FlowIgnore
closingElement.start,
"Expected corresponding JSX closing tag for <" +
getQualifiedJSXName(openingElement.name) +
">",
jsxErrors.MissingClosingTagElement,
getQualifiedJSXName(openingElement.name),
);
}
}
Expand All @@ -488,8 +493,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (this.isRelational("<")) {
throw this.raise(
this.state.start,
"Adjacent JSX elements must be wrapped in an enclosing tag. " +
"Did you want a JSX fragment <>...</>?",
jsxErrors.UnwrappedAdjacentJSXElements,
);
}

Expand Down

0 comments on commit d3c067e

Please sign in to comment.