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

micromark-factory-mdx-expression

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

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

Comparing version 2.0.1 to 2.0.2

70

dev/index.d.ts

@@ -28,24 +28,3 @@ /**

*/
export function factoryMdxExpression(
this: import('micromark-util-types').TokenizeContext,
effects: Effects,
ok: State,
type: TokenType,
markerType: TokenType,
chunkType: TokenType,
acorn?: Acorn | null | undefined,
acornOptions?: AcornOptions | null | undefined,
addResult?: boolean | null | undefined,
spread?: boolean | null | undefined,
allowEmpty?: boolean | null | undefined,
allowLazy?: boolean | null | undefined
): State
export type Program = import('estree').Program
export type Acorn = import('micromark-util-events-to-acorn').Acorn
export type AcornOptions = import('micromark-util-events-to-acorn').AcornOptions
export type Effects = import('micromark-util-types').Effects
export type Point = import('micromark-util-types').Point
export type State = import('micromark-util-types').State
export type TokenType = import('micromark-util-types').TokenType
export type TokenizeContext = import('micromark-util-types').TokenizeContext
export function factoryMdxExpression(this: TokenizeContext, effects: Effects, ok: State, type: TokenType, markerType: TokenType, chunkType: TokenType, acorn?: Acorn | null | undefined, acornOptions?: AcornOptions | null | undefined, addResult?: boolean | null | undefined, spread?: boolean | null | undefined, allowEmpty?: boolean | null | undefined, allowLazy?: boolean | null | undefined): State;
/**

@@ -55,11 +34,11 @@ * Good result.

export type MdxSignalOk = {
/**
* Type.
*/
type: 'ok'
/**
* Value.
*/
estree: Program | undefined
}
/**
* Type.
*/
type: "ok";
/**
* Value.
*/
estree: Program | undefined;
};
/**

@@ -69,12 +48,19 @@ * Bad result.

export type MdxSignalNok = {
/**
* Type.
*/
type: 'nok'
/**
* Value.
*/
message: VFileMessage
}
export type MdxSignal = MdxSignalNok | MdxSignalOk
import {VFileMessage} from 'vfile-message'
/**
* Type.
*/
type: "nok";
/**
* Value.
*/
message: VFileMessage;
};
export type MdxSignal = MdxSignalNok | MdxSignalOk;
import type { Effects } from 'micromark-util-types';
import type { State } from 'micromark-util-types';
import type { TokenType } from 'micromark-util-types';
import type { Acorn } from 'micromark-util-events-to-acorn';
import type { AcornOptions } from 'micromark-util-events-to-acorn';
import type { TokenizeContext } from 'micromark-util-types';
import type { Program } from 'estree';
import { VFileMessage } from 'vfile-message';

44

dev/index.js
/**
* @typedef {import('estree').Program} Program
* @typedef {import('micromark-util-events-to-acorn').Acorn} Acorn
* @typedef {import('micromark-util-events-to-acorn').AcornOptions} AcornOptions
* @typedef {import('micromark-util-types').Effects} Effects
* @typedef {import('micromark-util-types').Point} Point
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').TokenType} TokenType
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
* @import {Program} from 'estree'
* @import {Acorn, AcornOptions} from 'micromark-util-events-to-acorn'
* @import {Effects, Point, State, TokenType, TokenizeContext} from 'micromark-util-types'
*/

@@ -31,3 +26,4 @@

import {ok as assert} from 'devlop'
import {markdownLineEnding} from 'micromark-util-character'
import {factorySpace} from 'micromark-factory-space'
import {markdownLineEnding, markdownSpace} from 'micromark-util-character'
import {eventsToAcorn} from 'micromark-util-events-to-acorn'

@@ -38,6 +34,12 @@ import {codes, types} from 'micromark-util-symbol'

// Tab-size to eat has to be the same as what we serialize as.
// While in some places in markdown that’s 4, in JS it’s more common as 2.
// Which is what’s also in `mdast-util-mdx-jsx`:
// <https://github.com/syntax-tree/mdast-util-mdx-jsx/blob/40b951b/lib/index.js#L52>
const indentSize = 2
const trouble =
'https://github.com/micromark/micromark-extension-mdx-expression/tree/main/packages/micromark-extension-mdx-expression'
const unexpectedEofHash =
const unexpectedEndOfFileHash =
'#unexpected-end-of-file-in-expression-expected-a-corresponding-closing-brace-for-'

@@ -145,3 +147,3 @@ const unexpectedLazyHash =

)
error.url = trouble + unexpectedEofHash
error.url = trouble + unexpectedEndOfFileHash
throw error

@@ -258,2 +260,4 @@ }

// Note: `markdown-rs` uses `4`, but we use `2`.
//
// Idea: investigate if we’d need to use more complex stripping.

@@ -268,8 +272,14 @@ // Take this example:

//
// The block quote takes one space from each line, the paragraph doesn’t.
// The intent above is *perhaps* for the split to be as `>␠␠|␠␠␠␠|d`,
// Currently, we *don’t* do anything at all, it’s `>␠|␠␠␠␠␠|d` instead.
//
// Note: we used to have some handling here, and `markdown-rs` still does,
// which should be removed.
// Currently, the “paragraph” starts at `> | aaa`, so for the next line
// here we split it into `>␠|␠␠|␠␠␠d` (prefix, this indent here,
// expression data).
if (markdownSpace(code)) {
return factorySpace(
effects,
before,
types.linePrefix,
indentSize + 1
)(code)
}
return before(code)

@@ -276,0 +286,0 @@ }

@@ -28,24 +28,3 @@ /**

*/
export function factoryMdxExpression(
this: import('micromark-util-types').TokenizeContext,
effects: Effects,
ok: State,
type: TokenType,
markerType: TokenType,
chunkType: TokenType,
acorn?: Acorn | null | undefined,
acornOptions?: AcornOptions | null | undefined,
addResult?: boolean | null | undefined,
spread?: boolean | null | undefined,
allowEmpty?: boolean | null | undefined,
allowLazy?: boolean | null | undefined
): State
export type Program = import('estree').Program
export type Acorn = import('micromark-util-events-to-acorn').Acorn
export type AcornOptions = import('micromark-util-events-to-acorn').AcornOptions
export type Effects = import('micromark-util-types').Effects
export type Point = import('micromark-util-types').Point
export type State = import('micromark-util-types').State
export type TokenType = import('micromark-util-types').TokenType
export type TokenizeContext = import('micromark-util-types').TokenizeContext
export function factoryMdxExpression(this: TokenizeContext, effects: Effects, ok: State, type: TokenType, markerType: TokenType, chunkType: TokenType, acorn?: Acorn | null | undefined, acornOptions?: AcornOptions | null | undefined, addResult?: boolean | null | undefined, spread?: boolean | null | undefined, allowEmpty?: boolean | null | undefined, allowLazy?: boolean | null | undefined): State;
/**

@@ -55,11 +34,11 @@ * Good result.

export type MdxSignalOk = {
/**
* Type.
*/
type: 'ok'
/**
* Value.
*/
estree: Program | undefined
}
/**
* Type.
*/
type: "ok";
/**
* Value.
*/
estree: Program | undefined;
};
/**

@@ -69,12 +48,19 @@ * Bad result.

export type MdxSignalNok = {
/**
* Type.
*/
type: 'nok'
/**
* Value.
*/
message: VFileMessage
}
export type MdxSignal = MdxSignalNok | MdxSignalOk
import {VFileMessage} from 'vfile-message'
/**
* Type.
*/
type: "nok";
/**
* Value.
*/
message: VFileMessage;
};
export type MdxSignal = MdxSignalNok | MdxSignalOk;
import type { Effects } from 'micromark-util-types';
import type { State } from 'micromark-util-types';
import type { TokenType } from 'micromark-util-types';
import type { Acorn } from 'micromark-util-events-to-acorn';
import type { AcornOptions } from 'micromark-util-events-to-acorn';
import type { TokenizeContext } from 'micromark-util-types';
import type { Program } from 'estree';
import { VFileMessage } from 'vfile-message';
/**
* @typedef {import('estree').Program} Program
* @typedef {import('micromark-util-events-to-acorn').Acorn} Acorn
* @typedef {import('micromark-util-events-to-acorn').AcornOptions} AcornOptions
* @typedef {import('micromark-util-types').Effects} Effects
* @typedef {import('micromark-util-types').Point} Point
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').TokenType} TokenType
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
* @import {Program} from 'estree'
* @import {Acorn, AcornOptions} from 'micromark-util-events-to-acorn'
* @import {Effects, Point, State, TokenType, TokenizeContext} from 'micromark-util-types'
*/

@@ -30,18 +25,20 @@

import {markdownLineEnding} from 'micromark-util-character'
import {eventsToAcorn} from 'micromark-util-events-to-acorn'
import {positionFromEstree} from 'unist-util-position-from-estree'
import {VFileMessage} from 'vfile-message'
const trouble =
'https://github.com/micromark/micromark-extension-mdx-expression/tree/main/packages/micromark-extension-mdx-expression'
const unexpectedEofHash =
'#unexpected-end-of-file-in-expression-expected-a-corresponding-closing-brace-for-'
const unexpectedLazyHash =
'#unexpected-lazy-line-in-expression-in-container-expected-line-to-be-prefixed'
const nonSpreadHash =
'#unexpected-type-in-code-expected-an-object-spread-spread'
const spreadExtraHash =
'#unexpected-extra-content-in-spread-only-a-single-spread-is-supported'
const acornHash = '#could-not-parse-expression-with-acorn'
import { factorySpace } from 'micromark-factory-space';
import { markdownLineEnding, markdownSpace } from 'micromark-util-character';
import { eventsToAcorn } from 'micromark-util-events-to-acorn';
import { positionFromEstree } from 'unist-util-position-from-estree';
import { VFileMessage } from 'vfile-message';
// Tab-size to eat has to be the same as what we serialize as.
// While in some places in markdown that’s 4, in JS it’s more common as 2.
// Which is what’s also in `mdast-util-mdx-jsx`:
// <https://github.com/syntax-tree/mdast-util-mdx-jsx/blob/40b951b/lib/index.js#L52>
const indentSize = 2;
const trouble = 'https://github.com/micromark/micromark-extension-mdx-expression/tree/main/packages/micromark-extension-mdx-expression';
const unexpectedEndOfFileHash = '#unexpected-end-of-file-in-expression-expected-a-corresponding-closing-brace-for-';
const unexpectedLazyHash = '#unexpected-lazy-line-in-expression-in-container-expected-line-to-be-prefixed';
const nonSpreadHash = '#unexpected-type-in-code-expected-an-object-spread-spread';
const spreadExtraHash = '#unexpected-extra-content-in-spread-only-a-single-spread-is-supported';
const acornHash = '#could-not-parse-expression-with-acorn';
/**

@@ -75,23 +72,11 @@ * @this {TokenizeContext}

// eslint-disable-next-line max-params
export function factoryMdxExpression(
effects,
ok,
type,
markerType,
chunkType,
acorn,
acornOptions,
addResult,
spread,
allowEmpty,
allowLazy
) {
const self = this
const eventStart = this.events.length + 3 // Add main and marker token
let size = 0
export function factoryMdxExpression(effects, ok, type, markerType, chunkType, acorn, acornOptions, addResult, spread, allowEmpty, allowLazy) {
const self = this;
const eventStart = this.events.length + 3; // Add main and marker token
let size = 0;
/** @type {Point} */
let pointStart
let pointStart;
/** @type {Error} */
let lastCrash
return start
let lastCrash;
return start;

@@ -109,8 +94,8 @@ /**

function start(code) {
effects.enter(type)
effects.enter(markerType)
effects.consume(code)
effects.exit(markerType)
pointStart = self.now()
return before
effects.enter(type);
effects.enter(markerType);
effects.consume(code);
effects.exit(markerType);
pointStart = self.now();
return before;
}

@@ -130,56 +115,42 @@

if (code === null) {
if (lastCrash) throw lastCrash
const error = new VFileMessage(
'Unexpected end of file in expression, expected a corresponding closing brace for `{`',
{
place: self.now(),
ruleId: 'unexpected-eof',
source: 'micromark-extension-mdx-expression'
}
)
error.url = trouble + unexpectedEofHash
throw error
if (lastCrash) throw lastCrash;
const error = new VFileMessage('Unexpected end of file in expression, expected a corresponding closing brace for `{`', {
place: self.now(),
ruleId: 'unexpected-eof',
source: 'micromark-extension-mdx-expression'
});
error.url = trouble + unexpectedEndOfFileHash;
throw error;
}
if (markdownLineEnding(code)) {
effects.enter('lineEnding')
effects.consume(code)
effects.exit('lineEnding')
return eolAfter
effects.enter("lineEnding");
effects.consume(code);
effects.exit("lineEnding");
return eolAfter;
}
if (code === 125 && size === 0) {
/** @type {MdxSignal} */
const next = acorn
? mdxExpressionParse.call(
self,
acorn,
acornOptions,
chunkType,
eventStart,
pointStart,
allowEmpty || false,
spread || false
)
: {
type: 'ok',
estree: undefined
}
const next = acorn ? mdxExpressionParse.call(self, acorn, acornOptions, chunkType, eventStart, pointStart, allowEmpty || false, spread || false) : {
type: 'ok',
estree: undefined
};
if (next.type === 'ok') {
effects.enter(markerType)
effects.consume(code)
effects.exit(markerType)
const token = effects.exit(type)
effects.enter(markerType);
effects.consume(code);
effects.exit(markerType);
const token = effects.exit(type);
if (addResult && next.estree) {
Object.assign(token, {
estree: next.estree
})
});
}
return ok
return ok;
}
lastCrash = next.message
effects.enter(chunkType)
effects.consume(code)
return inside
lastCrash = next.message;
effects.enter(chunkType);
effects.consume(code);
return inside;
}
effects.enter(chunkType)
return inside(code)
effects.enter(chunkType);
return inside(code);
}

@@ -198,9 +169,5 @@

function inside(code) {
if (
(code === 125 && size === 0) ||
code === null ||
markdownLineEnding(code)
) {
effects.exit(chunkType)
return before(code)
if (code === 125 && size === 0 || code === null || markdownLineEnding(code)) {
effects.exit(chunkType);
return before(code);
}

@@ -210,8 +177,8 @@

if (code === 123 && !acorn) {
size += 1
size += 1;
} else if (code === 125) {
size -= 1
size -= 1;
}
effects.consume(code)
return inside
effects.consume(code);
return inside;
}

@@ -231,22 +198,17 @@

function eolAfter(code) {
const now = self.now()
const now = self.now();
// Lazy continuation in a flow expression (or flow tag) is a syntax error.
if (
now.line !== pointStart.line &&
!allowLazy &&
self.parser.lazy[now.line]
) {
const error = new VFileMessage(
'Unexpected lazy line in expression in container, expected line to be prefixed with `>` when in a block quote, whitespace when in a list, etc',
{
place: self.now(),
ruleId: 'unexpected-lazy',
source: 'micromark-extension-mdx-expression'
}
)
error.url = trouble + unexpectedLazyHash
throw error
if (now.line !== pointStart.line && !allowLazy && self.parser.lazy[now.line]) {
const error = new VFileMessage('Unexpected lazy line in expression in container, expected line to be prefixed with `>` when in a block quote, whitespace when in a list, etc', {
place: self.now(),
ruleId: 'unexpected-lazy',
source: 'micromark-extension-mdx-expression'
});
error.url = trouble + unexpectedLazyHash;
throw error;
}
// Note: `markdown-rs` uses `4`, but we use `2`.
//
// Idea: investigate if we’d need to use more complex stripping.

@@ -261,9 +223,9 @@ // Take this example:

//
// The block quote takes one space from each line, the paragraph doesn’t.
// The intent above is *perhaps* for the split to be as `>␠␠|␠␠␠␠|d`,
// Currently, we *don’t* do anything at all, it’s `>␠|␠␠␠␠␠|d` instead.
//
// Note: we used to have some handling here, and `markdown-rs` still does,
// which should be removed.
return before(code)
// Currently, the “paragraph” starts at `> | aaa`, so for the next line
// here we split it into `>␠|␠␠|␠␠␠d` (prefix, this indent here,
// expression data).
if (markdownSpace(code)) {
return factorySpace(effects, before, "linePrefix", indentSize + 1)(code);
}
return before(code);
}

@@ -290,11 +252,3 @@ }

// eslint-disable-next-line max-params
function mdxExpressionParse(
acorn,
acornOptions,
chunkType,
eventStart,
pointStart,
allowEmpty,
spread
) {
function mdxExpressionParse(acorn, acornOptions, chunkType, eventStart, pointStart, allowEmpty, spread) {
// Gnostic mode: parse w/ acorn.

@@ -310,4 +264,4 @@ const result = eventsToAcorn(this.events.slice(eventStart), {

suffix: spread ? '})' : ''
})
const estree = result.estree
});
const estree = result.estree;

@@ -318,51 +272,32 @@ // Get the spread value.

const head = estree.body[0]
if (
head.type !== 'ExpressionStatement' ||
head.expression.type !== 'ObjectExpression'
) {
const place = positionFromEstree(head)
const error = new VFileMessage(
'Unexpected `' +
head.type +
'` in code: expected an object spread (`{...spread}`)',
{
place: place.start,
ruleId: 'non-spread',
source: 'micromark-extension-mdx-expression'
}
)
error.url = trouble + nonSpreadHash
throw error
const head = estree.body[0];
if (head.type !== 'ExpressionStatement' || head.expression.type !== 'ObjectExpression') {
const place = positionFromEstree(head);
const error = new VFileMessage('Unexpected `' + head.type + '` in code: expected an object spread (`{...spread}`)', {
place: place.start,
ruleId: 'non-spread',
source: 'micromark-extension-mdx-expression'
});
error.url = trouble + nonSpreadHash;
throw error;
}
if (head.expression.properties[1]) {
const place = positionFromEstree(head.expression.properties[1])
const error = new VFileMessage(
'Unexpected extra content in spread: only a single spread is supported',
{
place: place.start,
ruleId: 'spread-extra',
source: 'micromark-extension-mdx-expression'
}
)
error.url = trouble + spreadExtraHash
throw error
const place = positionFromEstree(head.expression.properties[1]);
const error = new VFileMessage('Unexpected extra content in spread: only a single spread is supported', {
place: place.start,
ruleId: 'spread-extra',
source: 'micromark-extension-mdx-expression'
});
error.url = trouble + spreadExtraHash;
throw error;
}
if (
head.expression.properties[0] &&
head.expression.properties[0].type !== 'SpreadElement'
) {
const place = positionFromEstree(head.expression.properties[0])
const error = new VFileMessage(
'Unexpected `' +
head.expression.properties[0].type +
'` in code: only spread elements are supported',
{
place: place.start,
ruleId: 'non-spread',
source: 'micromark-extension-mdx-expression'
}
)
error.url = trouble + nonSpreadHash
throw error
if (head.expression.properties[0] && head.expression.properties[0].type !== 'SpreadElement') {
const place = positionFromEstree(head.expression.properties[0]);
const error = new VFileMessage('Unexpected `' + head.expression.properties[0].type + '` in code: only spread elements are supported', {
place: place.start,
ruleId: 'non-spread',
source: 'micromark-extension-mdx-expression'
});
error.url = trouble + nonSpreadHash;
throw error;
}

@@ -380,8 +315,8 @@ }

source: 'micromark-extension-mdx-expression'
})
error.url = trouble + acornHash
});
error.url = trouble + acornHash;
return {
type: 'nok',
message: error
}
};
}

@@ -391,3 +326,3 @@ return {

estree
}
}
};
}
{
"name": "micromark-factory-mdx-expression",
"version": "2.0.1",
"version": "2.0.2",
"description": "micromark factory to parse MDX expressions (found in JSX attributes, flow, text)",

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

"devlop": "^1.0.0",
"micromark-factory-space": "^2.0.0",
"micromark-util-character": "^2.0.0",

@@ -59,3 +60,8 @@ "micromark-util-events-to-acorn": "^2.0.0",

},
"xo": false
"xo": {
"prettier": true,
"rules": {
"unicorn/no-this-assignment": "off"
}
}
}

@@ -16,11 +16,11 @@ # micromark-factory-mdx-expression

* [Install](#install)
* [Use](#use)
* [API](#api)
* [`factoryMdxExpression(…)`](#factorymdxexpression)
* [Types](#types)
* [Compatibility](#compatibility)
* [Security](#security)
* [Contribute](#contribute)
* [License](#license)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`factoryMdxExpression(…)`](#factorymdxexpression)
* [Types](#types)
* [Compatibility](#compatibility)
* [Security](#security)
* [Contribute](#contribute)
* [License](#license)

@@ -100,24 +100,24 @@ ## Install

* `effects` (`Effects`)
— context
* `ok` (`State`)
— state switched to when successful
* `type` (`string`)
— token type for whole (`{}`)
* `markerType` (`string`)
— token type for the markers (`{`, `}`)
* `chunkType` (`string`)
— token type for the value (`1`)
* `acorn` (`Acorn`)
— object with `acorn.parse` and `acorn.parseExpressionAt`
* `acornOptions` ([`AcornOptions`][acorn-options])
— configuration for acorn
* `boolean` (`addResult`, default: `false`)
— add `estree` to token
* `boolean` (`spread`, default: `false`)
— support a spread (`{...a}`) only
* `boolean` (`allowEmpty`, default: `false`)
— support an empty expression
* `boolean` (`allowLazy`, default: `false`)
— support lazy continuation of an expression
* `effects` (`Effects`)
— context
* `ok` (`State`)
— state switched to when successful
* `type` (`string`)
— token type for whole (`{}`)
* `markerType` (`string`)
— token type for the markers (`{`, `}`)
* `chunkType` (`string`)
— token type for the value (`1`)
* `acorn` (`Acorn`)
— object with `acorn.parse` and `acorn.parseExpressionAt`
* `acornOptions` ([`AcornOptions`][acorn-options])
— configuration for acorn
* `boolean` (`addResult`, default: `false`)
— add `estree` to token
* `boolean` (`spread`, default: `false`)
— support a spread (`{...a}`) only
* `boolean` (`allowEmpty`, default: `false`)
— support an empty expression
* `boolean` (`allowLazy`, default: `false`)
— support lazy continuation of an expression

@@ -124,0 +124,0 @@ ###### Returns

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