Skip to content

Commit 2b2754c

Browse files
authored
Add positional info to attributes
Closes GH-8. Closes GH-11. Related-to: mdx-js/mdx#2034. Reviewed-by: Remco Haszing <remcohaszing@gmail.com> Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent fe46427 commit 2b2754c

File tree

2 files changed

+268
-19
lines changed

2 files changed

+268
-19
lines changed

lib/index.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* @typedef {import('../index.js').MdxJsxExpressionAttribute} MdxJsxExpressionAttribute
1717
* @typedef {import('../index.js').MdxJsxFlowElement} MdxJsxFlowElement
1818
* @typedef {import('../index.js').MdxJsxTextElement} MdxJsxTextElement
19+
*
20+
* @typedef {import('unist').Point} Point
1921
*/
2022

2123
/**
@@ -136,6 +138,18 @@ export function mdxJsxFromMarkdown() {
136138
this.buffer()
137139
}
138140

141+
/**
142+
* Copy a point-like value.
143+
*
144+
* @param {Point} d
145+
* Point-like value.
146+
* @returns {Point}
147+
* unist point.
148+
*/
149+
function point(d) {
150+
return {line: d.line, column: d.column, offset: d.offset}
151+
}
152+
139153
/**
140154
* @this {CompileContext}
141155
* @type {FromMarkdownHandle}
@@ -263,7 +277,16 @@ export function mdxJsxFromMarkdown() {
263277
const tag = this.data.mdxJsxTag
264278
assert(tag, 'expected `mdxJsxTag`')
265279
enterMdxJsxTagAnyAttribute.call(this, token)
266-
tag.attributes.push({type: 'mdxJsxAttribute', name: '', value: null})
280+
tag.attributes.push({
281+
type: 'mdxJsxAttribute',
282+
name: '',
283+
value: null,
284+
position: {
285+
start: point(token.start),
286+
// @ts-expect-error: `end` will be patched later.
287+
end: undefined
288+
}
289+
})
267290
}
268291

269292
/**
@@ -306,6 +329,8 @@ export function mdxJsxFromMarkdown() {
306329
const node = tag.attributes[tag.attributes.length - 1]
307330
assert(node.type === 'mdxJsxAttribute')
308331
node.name = this.sliceSerialize(token)
332+
assert(node.position !== undefined)
333+
node.position.end = point(token.end)
309334
}
310335

311336
/**
@@ -318,19 +343,21 @@ export function mdxJsxFromMarkdown() {
318343
const node = tag.attributes[tag.attributes.length - 1]
319344
assert(node.type === 'mdxJsxAttribute')
320345
node.name += ':' + this.sliceSerialize(token)
346+
assert(node.position !== undefined)
347+
node.position.end = point(token.end)
321348
}
322349

323350
/**
324351
* @this {CompileContext}
325352
* @type {FromMarkdownHandle}
326353
*/
327-
function exitMdxJsxTagAttributeValueLiteral() {
354+
function exitMdxJsxTagAttributeValueLiteral(token) {
328355
const tag = this.data.mdxJsxTag
329356
assert(tag, 'expected `mdxJsxTag`')
330-
tag.attributes[tag.attributes.length - 1].value = parseEntities(
331-
this.resume(),
332-
{nonTerminated: false}
333-
)
357+
const node = tag.attributes[tag.attributes.length - 1]
358+
node.value = parseEntities(this.resume(), {nonTerminated: false})
359+
assert(node.position !== undefined)
360+
node.position.end = point(token.end)
334361
}
335362

336363
/**
@@ -351,6 +378,8 @@ export function mdxJsxFromMarkdown() {
351378
}
352379

353380
tail.value = node
381+
assert(tail.position !== undefined)
382+
tail.position.end = point(token.end)
354383
}
355384

356385
/**

0 commit comments

Comments
 (0)