Skip to content

Commit

Permalink
Fix crash on certain EOFs
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 21, 2022
1 parent bfdcc31 commit e84da9e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
14 changes: 11 additions & 3 deletions dev/lib/syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ export function mdxjsEsm(options) {
/** @type {State} */
function rest(code) {
if (code === codes.eof) {
return atEnd(code)
return atEndOfData(code)
}

if (markdownLineEnding(code)) {
return effects.check(nextBlankConstruct, atEnd, atEol)(code)
return effects.check(nextBlankConstruct, atEndOfData, atEol)(code)
}

effects.consume(code)
Expand All @@ -136,14 +136,22 @@ export function mdxjsEsm(options) {
return lineStart
}

if (code === codes.eof) {
return atEnd(code)
}

effects.enter('mdxjsEsmData')
return rest(code)
}

/** @type {State} */
function atEnd(code) {
function atEndOfData(code) {
effects.exit('mdxjsEsmData')
return atEnd(code)
}

/** @type {State} */
function atEnd(code) {
let index = -1
const result = eventsToAcorn(self.events.slice(eventStart), {
acorn,
Expand Down
11 changes: 11 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,17 @@ test('micromark-extension-mdxjs-esm', (t) => {
'should support JSX if an `acorn` instance supporting it is passed in'
)

t.throws(
() => {
micromark('export {a}\n', {
extensions: [syntax({acorn: acorn.Parser.extend(jsx())})],
htmlExtensions: [html]
})
},
/Export 'a' is not defined/,
'should support EOF after EOL'
)

t.throws(
() => {
micromark('export var a = () => {}\n\nb', {
Expand Down

0 comments on commit e84da9e

Please sign in to comment.