From 9b75b4133fa78eb9bef0108adaf33f190e279d4e Mon Sep 17 00:00:00 2001 From: Alan Pierce Date: Fri, 6 Jul 2018 08:39:17 -0700 Subject: [PATCH] Fix infinite loop on incomplete JSX (#296) Fixes #294 --- src/parser/plugins/jsx/index.ts | 3 +++ test/jsx-test.ts | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/parser/plugins/jsx/index.ts b/src/parser/plugins/jsx/index.ts index 2166b834..579498d8 100644 --- a/src/parser/plugins/jsx/index.ts +++ b/src/parser/plugins/jsx/index.ts @@ -73,6 +73,9 @@ function jsxReadString(quote: number): void { function jsxReadWord(): void { let ch: number; do { + if (state.pos > input.length) { + unexpected(null, "Unexpectedly reached the end of input."); + } ch = input.charCodeAt(++state.pos); } while (isIdentifierChar(ch) || ch === charCodes.dash); finishToken(tt.jsxName); diff --git a/test/jsx-test.ts b/test/jsx-test.ts index 2b6fc247..d8f237ba 100644 --- a/test/jsx-test.ts +++ b/test/jsx-test.ts @@ -1,4 +1,6 @@ -import {Transform} from "../src"; +import {throws} from "assert"; + +import {transform, Transform} from "../src"; import {IMPORT_DEFAULT_PREFIX, JSX_PREFIX} from "./prefixes"; import * as util from "./util"; @@ -466,6 +468,10 @@ describe("transform JSX", () => { ); }); + it("does not infinite loop on incomplete JSX", () => { + throws(() => transform("const x = <", {transforms: ["jsx"]})); + }); + describe("with production true", () => { it("handles no props", () => { assertResult(