Skip to content

Commit

Permalink
Add nicer error on empty expression when forbidden
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Mar 7, 2021
1 parent 083e0de commit 6e6d434
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
19 changes: 6 additions & 13 deletions lib/factory-expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ function factoryExpression(
expressionType,
expressionMarkerType,
expressionChunkType,
spread
spread,
forbidEmpty
) {
var self = this
var eventStart = this.events.length + 3 // Add main and marker token
Expand Down Expand Up @@ -105,7 +106,9 @@ function factoryExpression(
result = eventsToAcorn(acorn, acornOptions, self.events.slice(eventStart), {
start: startPosition,
expression: true,
allowEmpty: !spread,
// To do next major: remove double meaning of `spread` and only accept
// `forbidEmpty` here.
allowEmpty: !spread && !forbidEmpty,
prefix: spread ? '({' : '',
suffix: spread ? '})' : ''
})
Expand All @@ -121,17 +124,7 @@ function factoryExpression(
estree.body[0].type === 'ExpressionStatement' &&
estree.body[0].expression.type === 'ObjectExpression'
) {
if (!estree.body[0].expression.properties[0]) {
throw new VMessage(
'Unexpected empty spread expression: expected `...`',
{
line: estree.loc.start.line,
column: estree.loc.start.column + 1,
offset: estree.start
},
'micromark-extension-mdx-expression:non-spread'
)
} else if (estree.body[0].expression.properties[1]) {
if (estree.body[0].expression.properties[1]) {
throw new VMessage(
'Unexpected extra content in spread: only a single spread is supported',
{
Expand Down
13 changes: 9 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function mdxExpression(options) {
var addResult = settings.addResult
var acorn = settings.acorn
var spread
var forbidEmpty
var acornOptions

if (acorn) {
Expand All @@ -27,9 +28,11 @@ function mdxExpression(options) {

// Hidden: `micromark-extension-mdx-jsx` supports expressions in tags,
// and one of them is only “spread” elements.
// Instead of duplicating code there is a small hidden feature here to
// support that.
// It also has expressions that are not allowed to be empty (`<x y={}/>`).
// Instead of duplicating code there, this are two small hidden feature here
// to test that behavior.
spread = settings.spread
forbidEmpty = settings.forbidEmpty
} else if (settings.acornOptions || settings.addResult) {
throw new Error('Expected an `acorn` instance passed in as `options.acorn`')
}
Expand Down Expand Up @@ -59,7 +62,8 @@ function mdxExpression(options) {
'mdxFlowExpression',
'mdxFlowExpressionMarker',
'mdxFlowExpressionChunk',
spread
spread,
forbidEmpty
)(code)
}

Expand Down Expand Up @@ -88,7 +92,8 @@ function mdxExpression(options) {
'mdxTextExpression',
'mdxTextExpressionMarker',
'mdxTextExpressionChunk',
spread
spread,
forbidEmpty
)(code)
}
}
Expand Down
13 changes: 12 additions & 1 deletion lib/util-events-to-acorn.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

var VMessage = require('vfile-message')

module.exports = eventsToAcorn

// Parse a list of micromark events with acorn.
Expand Down Expand Up @@ -51,7 +53,16 @@ function eventsToAcorn(acorn, acornOptions, events, config) {

source = chunks.join('')
value = before + source + after
isEmptyExpression = config.expression && config.allowEmpty && empty(source)

isEmptyExpression = config.expression && empty(source)

if (isEmptyExpression && !config.allowEmpty) {
throw new VMessage(
'Unexpected empty expression',
parseOffsetToUnistPoint(0),
'micromark-extension-mdx-expression:unexpected-empty-expression'
)
}

try {
estree =
Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ test('micromark-extension-mdx-expression', function (t) {
extensions: [syntax({acorn: acorn, spread: true})]
})
},
/Unexpected empty spread expression: expected `\.\.\.`/,
/Unexpected empty expression/,
'should crash on an empty spread'
)

Expand All @@ -701,7 +701,7 @@ test('micromark-extension-mdx-expression', function (t) {
extensions: [syntax({acorn: acorn, spread: true})]
})
},
/Unexpected empty spread expression: expected `\.\.\.`/,
/Unexpected empty expression/,
'should crash on a comment spread'
)

Expand Down

0 comments on commit 6e6d434

Please sign in to comment.