Skip to content

Commit

Permalink
use dep
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 23, 2022
1 parent d24a339 commit 9506240
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
45 changes: 16 additions & 29 deletions packages/mdx/lib/plugin/recma-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import {analyze} from 'periscopic'
import {stringifyPosition} from 'unist-util-stringify-position'
import {positionFromEstree} from 'unist-util-position-from-estree'
import {visit, SKIP} from 'estree-util-visit'
import {walk} from 'estree-walker'
import {create} from '../util/estree-util-create.js'
import {specifiersToDeclarations} from '../util/estree-util-specifiers-to-declarations.js'
import {declarationToExpression} from '../util/estree-util-declaration-to-expression.js'
Expand Down Expand Up @@ -299,36 +299,23 @@ export function recmaDocument(options = {}) {
tree.body = replacement

if (baseUrl) {
visit(tree, (node, field, index, parents) => {
if (
node.type === 'MemberExpression' &&
'object' in node &&
node.object.type === 'MetaProperty' &&
node.property.type === 'Identifier' &&
node.object.meta.name === 'import' &&
node.object.property.name === 'meta' &&
node.property.name === 'url'
) {
/** @type {string|number|null} */
let prop = field
let context = /** @type {Node|Array<Node>} */ (
parents[parents.length - 1]
)

// Remove non-standard `ParenthesizedExpression`.
if (context && prop) {
/* c8 ignore next 5 */
if (typeof index === 'number') {
// @ts-expect-error: indexable.
context = context[prop]
prop = index
}
walk(tree, {
enter(_node) {
const node = /** @type {Node} */ (_node)

// @ts-expect-error: indexable.
context[prop] = {type: 'Literal', value: baseUrl}
if (
node.type === 'MemberExpression' &&
'object' in node &&
node.object.type === 'MetaProperty' &&
node.property.type === 'Identifier' &&
node.object.meta.name === 'import' &&
node.object.property.name === 'meta' &&
node.property.name === 'url'
) {
/** @type {SimpleLiteral} */
const replacement = {type: 'Literal', value: baseUrl}
this.replace(replacement)
}

return SKIP
}
})
}
Expand Down
2 changes: 2 additions & 0 deletions packages/mdx/test/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ test('compile', async () => {
)
}

console.log('\nnote: the following warning is expected!\n')
assert.equal(
renderToStaticMarkup(
React.createElement(
Expand All @@ -545,6 +546,7 @@ test('compile', async () => {
'<a></a>',
'should render if a used member is defined locally (JSX in a function)'
)
console.log('\nnote: the preceding warning is expected!\n')

try {
renderToStaticMarkup(
Expand Down

0 comments on commit 9506240

Please sign in to comment.