Skip to content

Commit

Permalink
add lexer error for invalid tags
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Apr 5, 2019
1 parent f29859f commit e3b9b04
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
8 changes: 7 additions & 1 deletion packages/moon/dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
*/

function lexError(message, input, index) {
var lexMessage = message + "\n\n";
var lexMessage = message + "\n\n"; // Show input characters surrounding the source of the error.

for (var i = Math.max(0, index - 16); i < Math.min(index + 16, input.length); i++) {
lexMessage += input[i];
Expand Down Expand Up @@ -189,6 +189,11 @@
// the match and captured groups.

var typeExec = typeRE.exec(input);

if ("development" === "development" && typeExec === null) {
lexError("Lexer expected a valid opening or closing tag.", input, i);
}

var typeMatch = typeExec[0];
var type = typeExec[1];
var attributesText = typeExec[2];
Expand Down Expand Up @@ -302,6 +307,7 @@


function parseErrorMessage(message) {
/* istanbul ignore next */
return "development" === "development" ? message : "";
}
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/moon/dist/moon.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/moon/src/compiler/lexer/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export function tokenString(token) {
function lexError(message, input, index) {
let lexMessage = `${message}\n\n`;

// Show input characters surrounding the source of the error.
for (
let i = Math.max(0, index - 16);
i < Math.min(index + 16, input.length);
Expand Down Expand Up @@ -152,6 +153,11 @@ export function lex(input) {
// Execute the tag type regular expression on the input and store
// the match and captured groups.
const typeExec = typeRE.exec(input);

if (process.env.MOON_ENV === "development" && typeExec === null) {
lexError("Lexer expected a valid opening or closing tag.", input, i);
}

const typeMatch = typeExec[0];
const type = typeExec[1];
const attributesText = typeExec[2];
Expand Down
14 changes: 14 additions & 0 deletions packages/moon/test/compiler/lexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ test("lex error from unclosed opening bracket", () => {
expect(console.error).toBeCalled();
});

test("lex error from invalid opening tag", () => {
console.error = jest.fn();

expect(Array.isArray(lex("<div"))).toBe(true);
expect(console.error).toBeCalled();
});

test("lex error from invalid closing tag", () => {
console.error = jest.fn();

expect(Array.isArray(lex("<div/"))).toBe(true);
expect(console.error).toBeCalled();
});

test("lex error from unclosed self-closing tag", () => {
console.error = jest.fn();

Expand Down

0 comments on commit e3b9b04

Please sign in to comment.