Skip to content

Commit 8f3b292

Browse files
committedOct 22, 2023
Add a use strict directive to function bodies
1 parent e525db9 commit 8f3b292

File tree

4 files changed

+78
-17
lines changed

4 files changed

+78
-17
lines changed
 

‎package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/mdx/lib/plugin/recma-jsx-build.js

+31-14
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,37 @@ export function recmaJsxBuild(options) {
4646
// When compiling to a function body, replace the import that was just
4747
// generated, and get `jsx`, `jsxs`, and `Fragment` from `arguments[0]`
4848
// instead.
49-
if (
50-
outputFormat === 'function-body' &&
51-
tree.body[0] &&
52-
tree.body[0].type === 'ImportDeclaration' &&
53-
typeof tree.body[0].source.value === 'string' &&
54-
/\/jsx-(dev-)?runtime$/.test(tree.body[0].source.value)
55-
) {
56-
tree.body[0] = {
57-
type: 'VariableDeclaration',
58-
kind: 'const',
59-
declarations: specifiersToDeclarations(
60-
tree.body[0].specifiers,
61-
toIdOrMemberExpression(['arguments', 0])
62-
)
49+
if (outputFormat === 'function-body') {
50+
let index = 0
51+
52+
// Skip directives: JS currently only has `use strict`, but Acorn allows
53+
// arbitrary ones.
54+
// Practically things like `use client` could be used?
55+
while (index < tree.body.length) {
56+
const child = tree.body[index]
57+
if ('directive' in child && child.directive) {
58+
index++
59+
} else {
60+
break
61+
}
62+
}
63+
64+
const declaration = tree.body[index]
65+
66+
if (
67+
declaration &&
68+
declaration.type === 'ImportDeclaration' &&
69+
typeof declaration.source.value === 'string' &&
70+
/\/jsx-(dev-)?runtime$/.test(declaration.source.value)
71+
) {
72+
tree.body[index] = {
73+
type: 'VariableDeclaration',
74+
kind: 'const',
75+
declarations: specifiersToDeclarations(
76+
declaration.specifiers,
77+
toIdOrMemberExpression(['arguments', 0])
78+
)
79+
}
6380
}
6481
}
6582
}

‎packages/mdx/lib/plugin/recma-jsx-rewrite.js

+8
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,14 @@ export function recmaJsxRewrite(options) {
560560
}
561561
})
562562
}
563+
564+
if (outputFormat === 'function-body') {
565+
tree.body.unshift({
566+
type: 'ExpressionStatement',
567+
expression: {type: 'Literal', value: 'use strict'},
568+
directive: 'use strict'
569+
})
570+
}
563571
}
564572
}
565573

‎packages/mdx/test/syntax.js

+36
Original file line numberDiff line numberDiff line change
@@ -899,4 +899,40 @@ test('@mdx-js/mdx: syntax: MDX (ESM)', async function (t) {
899899
].join('\n')
900900
)
901901
})
902+
903+
await t.test(
904+
"should add a 'use strict' when generating a `function-body`",
905+
async function () {
906+
assert.equal(
907+
String(await compile('# hi', {outputFormat: 'function-body'})),
908+
[
909+
'/*@jsxRuntime automatic @jsxImportSource react*/',
910+
'"use strict";',
911+
'const {jsx: _jsx} = arguments[0];',
912+
'function _createMdxContent(props) {',
913+
' const _components = {',
914+
' h1: "h1",',
915+
' ...props.components',
916+
' };',
917+
' return _jsx(_components.h1, {',
918+
' children: "hi"',
919+
' });',
920+
'}',
921+
'function MDXContent(props = {}) {',
922+
' const {wrapper: MDXLayout} = props.components || ({});',
923+
' return MDXLayout ? _jsx(MDXLayout, {',
924+
' ...props,',
925+
' children: _jsx(_createMdxContent, {',
926+
' ...props',
927+
' })',
928+
' }) : _createMdxContent(props);',
929+
'}',
930+
'return {',
931+
' default: MDXContent',
932+
'};',
933+
''
934+
].join('\n')
935+
)
936+
}
937+
)
902938
})

0 commit comments

Comments
 (0)
Please sign in to comment.