micromark-extension-mdx-expression
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -20,3 +20,4 @@ 'use strict' | ||
expressionChunkType, | ||
spread | ||
spread, | ||
forbidEmpty | ||
) { | ||
@@ -71,8 +72,6 @@ var self = this | ||
if (code === 123) { | ||
if (!acorn) { | ||
effects.consume(code) | ||
balance++ | ||
return inside | ||
} | ||
if (code === 123 && !acorn) { | ||
effects.consume(code) | ||
balance++ | ||
return inside | ||
} | ||
@@ -110,3 +109,5 @@ | ||
expression: true, | ||
allowEmpty: !spread, | ||
// To do next major: remove double meaning of `spread` and only accept | ||
// `forbidEmpty` here. | ||
allowEmpty: !spread && !forbidEmpty, | ||
prefix: spread ? '({' : '', | ||
@@ -118,48 +119,37 @@ suffix: spread ? '})' : '' | ||
// Get the spread value. | ||
if (spread) { | ||
if ( | ||
estree && | ||
// The next checks should always be the case, as we wrap in `d={}` | ||
estree.type === 'Program' && | ||
estree.body[0] && | ||
estree.body[0].type === 'ExpressionStatement' && | ||
estree.body[0].expression.type === 'ObjectExpression' | ||
if ( | ||
spread && | ||
estree && | ||
// The next checks should always be the case, as we wrap in `d={}` | ||
estree.type === 'Program' && | ||
estree.body[0] && | ||
estree.body[0].type === 'ExpressionStatement' && | ||
estree.body[0].expression.type === 'ObjectExpression' | ||
) { | ||
if (estree.body[0].expression.properties[1]) { | ||
throw new VMessage( | ||
'Unexpected extra content in spread: only a single spread is supported', | ||
{ | ||
line: estree.body[0].expression.properties[1].loc.start.line, | ||
column: | ||
estree.body[0].expression.properties[1].loc.start.column + 1, | ||
offset: estree.body[0].expression.properties[1].start | ||
}, | ||
'micromark-extension-mdx-expression:spread-extra' | ||
) | ||
} else if ( | ||
estree.body[0].expression.properties[0].type !== 'SpreadElement' | ||
) { | ||
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]) { | ||
throw new VMessage( | ||
'Unexpected extra content in spread: only a single spread is supported', | ||
{ | ||
line: estree.body[0].expression.properties[1].loc.start.line, | ||
column: | ||
estree.body[0].expression.properties[1].loc.start.column + 1, | ||
offset: estree.body[0].expression.properties[1].start | ||
}, | ||
'micromark-extension-mdx-expression:spread-extra' | ||
) | ||
} else if ( | ||
estree.body[0].expression.properties[0].type !== 'SpreadElement' | ||
) { | ||
throw new VMessage( | ||
'Unexpected `' + | ||
estree.body[0].expression.properties[0].type + | ||
'` in code: only spread elements are supported', | ||
{ | ||
line: estree.body[0].expression.properties[0].loc.start.line, | ||
column: | ||
estree.body[0].expression.properties[0].loc.start.column + 1, | ||
offset: estree.body[0].expression.properties[0].start | ||
}, | ||
'micromark-extension-mdx-expression:non-spread' | ||
) | ||
} | ||
throw new VMessage( | ||
'Unexpected `' + | ||
estree.body[0].expression.properties[0].type + | ||
'` in code: only spread elements are supported', | ||
{ | ||
line: estree.body[0].expression.properties[0].loc.start.line, | ||
column: | ||
estree.body[0].expression.properties[0].loc.start.column + 1, | ||
offset: estree.body[0].expression.properties[0].start | ||
}, | ||
'micromark-extension-mdx-expression:non-spread' | ||
) | ||
} | ||
@@ -166,0 +156,0 @@ } |
@@ -14,2 +14,3 @@ 'use strict' | ||
var spread | ||
var forbidEmpty | ||
var acornOptions | ||
@@ -31,5 +32,7 @@ | ||
// 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) { | ||
@@ -64,3 +67,4 @@ throw new Error('Expected an `acorn` instance passed in as `options.acorn`') | ||
'mdxFlowExpressionChunk', | ||
spread | ||
spread, | ||
forbidEmpty | ||
)(code) | ||
@@ -94,3 +98,4 @@ } | ||
'mdxTextExpressionChunk', | ||
spread | ||
spread, | ||
forbidEmpty | ||
)(code) | ||
@@ -97,0 +102,0 @@ } |
'use strict' | ||
var VMessage = require('vfile-message') | ||
module.exports = eventsToAcorn | ||
@@ -54,4 +56,13 @@ | ||
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 { | ||
@@ -74,26 +85,24 @@ estree = | ||
if (estree) { | ||
if (config.expression && !isEmptyExpression) { | ||
if (empty(value.slice(estree.end, value.length - after.length))) { | ||
estree = { | ||
type: 'Program', | ||
start: 0, | ||
end: before.length + source.length, | ||
body: [ | ||
{ | ||
type: 'ExpressionStatement', | ||
expression: estree, | ||
start: 0, | ||
end: before.length + source.length | ||
} | ||
], | ||
sourceType: 'module' | ||
} | ||
} else { | ||
point = parseOffsetToUnistPoint(estree.end) | ||
exception = new Error('Unexpected content after expression') | ||
exception.pos = point.offset | ||
exception.loc = {line: point.line, column: point.column - 1} | ||
estree = undefined | ||
if (estree && config.expression && !isEmptyExpression) { | ||
if (empty(value.slice(estree.end, value.length - after.length))) { | ||
estree = { | ||
type: 'Program', | ||
start: 0, | ||
end: before.length + source.length, | ||
body: [ | ||
{ | ||
type: 'ExpressionStatement', | ||
expression: estree, | ||
start: 0, | ||
end: before.length + source.length | ||
} | ||
], | ||
sourceType: 'module' | ||
} | ||
} else { | ||
point = parseOffsetToUnistPoint(estree.end) | ||
exception = new Error('Unexpected content after expression') | ||
exception.pos = point.offset | ||
exception.loc = {line: point.line, column: point.column - 1} | ||
estree = undefined | ||
} | ||
@@ -100,0 +109,0 @@ } |
{ | ||
"name": "micromark-extension-mdx-expression", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "micromark extension to support MDX or MDX JS expressions", | ||
@@ -45,3 +45,3 @@ "license": "MIT", | ||
"tape": "^5.0.0", | ||
"xo": "^0.36.0" | ||
"xo": "^0.38.0" | ||
}, | ||
@@ -72,4 +72,6 @@ "scripts": { | ||
"rules": { | ||
"complexity": "off", | ||
"guard-for-in": "off", | ||
"max-params": "off" | ||
"max-params": "off", | ||
"unicorn/no-this-assignment": "off" | ||
} | ||
@@ -76,0 +78,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23804
421