Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

micromark-extension-mdx-expression

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

micromark-extension-mdx-expression - npm Package Compare versions

Comparing version 0.1.3 to 0.2.0

lib/util-events-to-acorn.js

165

lib/factory-expression.js

@@ -8,2 +8,3 @@ 'use strict'

var VMessage = require('vfile-message')
var eventsToAcorn = require('./util-events-to-acorn')

@@ -23,6 +24,5 @@ function factoryExpression(

var self = this
var lastEventIndex = this.events.length + 3 // Add main and marker token
var position = self.now()
var source = ''
var eventStart = this.events.length + 3 // Add main and marker token
var balance = 1
var startPosition
var lastCrash

@@ -38,2 +38,3 @@

effects.exit(expressionMarkerType)
startPosition = self.now()
return atBreak

@@ -84,8 +85,6 @@ }

// eslint-disable-next-line complexity
function atClosingBrace(code) {
var result
var exception
var reason
var token
var estree

@@ -110,31 +109,68 @@ balance--

// Gnostic mode: parse w/ acorn.
while (lastEventIndex < self.events.length) {
source += self.sliceSerialize(self.events[lastEventIndex][1])
lastEventIndex += 2 // Skip over `exit`.
}
result = eventsToAcorn(acorn, acornOptions, self.events.slice(eventStart), {
start: startPosition,
expression: true,
allowEmpty: !spread,
prefix: spread ? '({' : '',
suffix: spread ? '})' : ''
})
estree = result.estree
if (!empty(source)) {
try {
result = acorn.parseExpressionAt(
spread ? 'd={' + source + '}' : source,
0,
acornOptions
)
} catch (error) {
exception = error
// 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 (!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'
)
}
}
// Empty.
}
if (exception) {
reason = String(exception).replace(/ \(\d+:\d+\)$/, '')
if (result.error) {
lastCrash = new VMessage(
'Could not parse expression with acorn: ' + reason,
'Could not parse expression with acorn: ' + result.error.message,
{
line: position.line + exception.loc.line - 1,
column:
exception.loc.line === 1
? position.column + exception.loc.column + 1
: exception.loc.column + 1
line: result.error.loc.line,
column: result.error.loc.column + 1,
offset: result.error.pos
},

@@ -144,7 +180,3 @@ 'micromark-extension-mdx-expression:acorn'

if (
exception.raisedAt === source.length + (spread ? 4 : 0) ||
// Comments are `raisedAt` their start, instead of the EOF.
reason === 'SyntaxError: Unterminated comment'
) {
if (code !== null && result.swallow) {
effects.enter(expressionChunkType)

@@ -158,53 +190,2 @@ effects.consume(code)

if (result && !empty(source.slice(result.end))) {
throw new VMessage(
'Unexpected content after expression, expected `}`',
{
line: position.line + result.loc.end.line - 1,
column:
result.loc.end.line === 1
? position.column + result.loc.end.column + 1
: result.loc.end.column + 1
},
'micromark-extension-mdx-expression:unexpected-content'
)
}
// Get the spread value.
if (spread) {
if (
!result ||
!result.right ||
!result.right.properties ||
!result.right.properties[0]
) {
throw new VMessage(
'Unexpected empty spread expression: expected `...`',
{start: position, end: self.now()},
'micromark-extension-mdx-expression:non-spread'
)
}
result = result.right.properties[0]
if (result.type !== 'SpreadElement') {
throw new VMessage(
'Unexpected `' +
result.type +
'` in code: only spread elements are supported',
{
start: {
line: position.line + result.loc.start.line - 1,
column: result.loc.start.column + 1
},
end: {
line: position.line + result.loc.end.line - 1,
column: result.loc.end.column + 1
}
},
'micromark-extension-mdx-expression:non-spread'
)
}
}
effects.enter(expressionMarkerType)

@@ -214,17 +195,5 @@ effects.consume(code)

token = effects.exit(expressionType)
if (addResult) token.estree = result
if (addResult) token.estree = estree
return ok
}
}
function empty(value) {
return /^\s*$/.test(
value
// Multiline comments.
.replace(/\/\*[\s\S]*?\*\//g, '')
// Line comments.
// EOF instead of EOL is specifically not allowed, because that would
// mean the closing brace is on the commented-out line
.replace(/\/\/[^\r\n]*(\r\n|\n|\r)/g, '')
)
}

@@ -25,4 +25,3 @@ 'use strict'

{ecmaVersion: 2020, sourceType: 'module'},
options.acornOptions || {},
{locations: true}
options.acornOptions || {}
)

@@ -29,0 +28,0 @@

{
"name": "micromark-extension-mdx-expression",
"version": "0.1.3",
"version": "0.2.0",
"description": "micromark extension to support MDX or MDX JS expressions",

@@ -71,2 +71,3 @@ "license": "MIT",

"rules": {
"guard-for-in": "off",
"max-params": "off"

@@ -73,0 +74,0 @@ }

@@ -20,3 +20,3 @@ # micromark-extension-mdx-expression

support all of MDX (or MDX.js).
Or, use it through `remark-mdx` or `remark-mdxjs` (**[remark][]**).
Or, use it through [`remark-mdx`][remark-mdx] (**[remark][]**).

@@ -53,5 +53,6 @@ ## Install

Options to pass to acorn (`Object`, default: `{ecmaVersion: 2020, locations:
true, sourceType: 'module'}`).
All fields except for `locations` can be set.
Options to pass to acorn (`Object`, default: `{ecmaVersion: 2020, sourceType:
'module'}`).
All fields except can be set.
Position info (`loc`, `range`) is set on ES nodes regardless of acorn options.

@@ -140,20 +141,20 @@ ###### `options.addResult`

### Could not parse expression with acorn: $error
### Could not parse expression with acorn: Unexpected content after expression
This error occurs if acorn crashes (source: `micromark-extension-mdx-expression`,
rule id: `acorn`).
This error occurs when there is more content after a JS expression (source:
`micromark-extension-mdx-expression`, rule id: `acorn`).
For example:
```markdown
a {var b = "c"} d
a {"b" "c"} d
```
### Unexpected content after expression, expected `}`
### Could not parse expression with acorn: $error
This error occurs when there is more content after a JS expression (source:
`micromark-extension-mdx-expression`, rule id: `unexpected-content`).
This error occurs if acorn crashes (source: `micromark-extension-mdx-expression`,
rule id: `acorn`).
For example:
```markdown
a {"b" "c"} d
a {var b = "c"} d
```

@@ -268,1 +269,3 @@

[acorn]: https://github.com/acornjs/acorn
[remark-mdx]: https://github.com/mdx-js/mdx/tree/next/packages/remark-mdx
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc