Skip to content

Commit 6887ea9

Browse files
committed
Use unist-util-mdx-define
The `skip` conflict strategy is used, meaning the user can manually define `getStaticProps` if they desire.
1 parent 8dcd6fa commit 6887ea9

File tree

9 files changed

+99
-108
lines changed

9 files changed

+99
-108
lines changed

fixtures/custom-name/expected.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { Fragment as _Fragment, jsx as _jsx } from 'react/jsx-runtime'
22
export const exported = ''
3+
export const custom = () => ({
4+
props: JSON.parse(
5+
JSON.stringify({
6+
exported
7+
})
8+
)
9+
})
310
function _createMdxContent(props) {
411
return _jsx(_Fragment, {})
512
}
@@ -14,10 +21,3 @@ export default function MDXContent(props = {}) {
1421
})
1522
: _createMdxContent(props)
1623
}
17-
export const custom = () => ({
18-
props: JSON.parse(
19-
JSON.stringify({
20-
exported
21-
})
22-
)
23-
})

fixtures/exclude-exclude/expected.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ export const ignored = ''
33
export const string = ''
44
export const regex_foo = ''
55
export const fn = ''
6+
export const getStaticProps = () => ({
7+
props: JSON.parse(
8+
JSON.stringify({
9+
ignored
10+
})
11+
)
12+
})
613
function _createMdxContent(props) {
714
return _jsx(_Fragment, {})
815
}
@@ -17,10 +24,3 @@ export default function MDXContent(props = {}) {
1724
})
1825
: _createMdxContent(props)
1926
}
20-
export const getStaticProps = () => ({
21-
props: JSON.parse(
22-
JSON.stringify({
23-
ignored
24-
})
25-
)
26-
})
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { Fragment as _Fragment, jsx as _jsx } from 'react/jsx-runtime'
22
export const exported = ''
33
export function declared() {}
4+
export const getStaticProps = () => ({
5+
props: JSON.parse(
6+
JSON.stringify({
7+
exported
8+
})
9+
)
10+
})
411
function _createMdxContent(props) {
512
return _jsx(_Fragment, {})
613
}
@@ -15,10 +22,3 @@ export default function MDXContent(props = {}) {
1522
})
1623
: _createMdxContent(props)
1724
}
18-
export const getStaticProps = () => ({
19-
props: JSON.parse(
20-
JSON.stringify({
21-
exported
22-
})
23-
)
24-
})
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { Fragment as _Fragment, jsx as _jsx } from 'react/jsx-runtime'
22
export const exported = ''
3+
export const getStaticProps = () => ({
4+
props: JSON.parse(
5+
JSON.stringify({
6+
exported
7+
})
8+
)
9+
})
310
function _createMdxContent(props) {
411
return _jsx(_Fragment, {})
512
}
@@ -14,10 +21,3 @@ export default function MDXContent(props = {}) {
1421
})
1522
: _createMdxContent(props)
1623
}
17-
export const getStaticProps = () => ({
18-
props: JSON.parse(
19-
JSON.stringify({
20-
exported
21-
})
22-
)
23-
})
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { Fragment as _Fragment, jsx as _jsx } from 'react/jsx-runtime'
22
import defaultImported, { imported } from 'module'
3+
export const getStaticProps = () => ({
4+
props: JSON.parse(
5+
JSON.stringify({
6+
defaultImported,
7+
imported
8+
})
9+
)
10+
})
311
function _createMdxContent(props) {
412
return _jsx(_Fragment, {})
513
}
@@ -14,11 +22,3 @@ export default function MDXContent(props = {}) {
1422
})
1523
: _createMdxContent(props)
1624
}
17-
export const getStaticProps = () => ({
18-
props: JSON.parse(
19-
JSON.stringify({
20-
defaultImported,
21-
imported
22-
})
23-
)
24-
})

fixtures/include-include/expected.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ export const ignored = ''
33
export const string = ''
44
export const regex_foo = ''
55
export const fn = ''
6+
export const getStaticProps = () => ({
7+
props: JSON.parse(
8+
JSON.stringify({
9+
fn,
10+
regex_foo,
11+
string
12+
})
13+
)
14+
})
615
function _createMdxContent(props) {
716
return _jsx(_Fragment, {})
817
}
@@ -17,12 +26,3 @@ export default function MDXContent(props = {}) {
1726
})
1827
: _createMdxContent(props)
1928
}
20-
export const getStaticProps = () => ({
21-
props: JSON.parse(
22-
JSON.stringify({
23-
fn,
24-
regex_foo,
25-
string
26-
})
27-
)
28-
})

lib/recma-nextjs-static-props.js

Lines changed: 50 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import { analyze } from 'periscopic'
7+
import { define } from 'unist-util-mdx-define'
78

89
/**
910
* @typedef {RegExp | string | ((value: string) => boolean)} Test
@@ -51,7 +52,7 @@ function passTest(test, value) {
5152
*/
5253
const recmaNextjsStaticProps =
5354
({ exclude, include, name = 'getStaticProps' } = {}) =>
54-
(ast) => {
55+
(ast, file) => {
5556
/**
5657
* @type {string[]}
5758
*/
@@ -90,78 +91,71 @@ const recmaNextjsStaticProps =
9091
return
9192
}
9293

93-
ast.body.push({
94-
type: 'ExportNamedDeclaration',
95-
specifiers: [],
96-
declaration: {
97-
type: 'VariableDeclaration',
98-
kind: 'const',
99-
declarations: [
100-
{
101-
type: 'VariableDeclarator',
102-
id: { type: 'Identifier', name },
103-
init: {
104-
type: 'ArrowFunctionExpression',
105-
expression: true,
106-
generator: false,
107-
async: false,
108-
params: [],
109-
body: {
110-
type: 'ObjectExpression',
111-
properties: [
112-
{
113-
type: 'Property',
114-
method: false,
115-
shorthand: false,
94+
define(
95+
ast,
96+
file,
97+
{
98+
[name]: {
99+
type: 'ArrowFunctionExpression',
100+
expression: true,
101+
generator: false,
102+
async: false,
103+
params: [],
104+
body: {
105+
type: 'ObjectExpression',
106+
properties: [
107+
{
108+
type: 'Property',
109+
method: false,
110+
shorthand: false,
111+
computed: false,
112+
kind: 'init',
113+
key: { type: 'Identifier', name: 'props' },
114+
value: {
115+
type: 'CallExpression',
116+
optional: false,
117+
callee: {
118+
type: 'MemberExpression',
116119
computed: false,
117-
kind: 'init',
118-
key: { type: 'Identifier', name: 'props' },
119-
value: {
120+
optional: false,
121+
object: { type: 'Identifier', name: 'JSON' },
122+
property: { type: 'Identifier', name: 'parse' }
123+
},
124+
arguments: [
125+
{
120126
type: 'CallExpression',
121127
optional: false,
122128
callee: {
123129
type: 'MemberExpression',
124130
computed: false,
125131
optional: false,
126132
object: { type: 'Identifier', name: 'JSON' },
127-
property: { type: 'Identifier', name: 'parse' }
133+
property: { type: 'Identifier', name: 'stringify' }
128134
},
129135
arguments: [
130136
{
131-
type: 'CallExpression',
132-
optional: false,
133-
callee: {
134-
type: 'MemberExpression',
137+
type: 'ObjectExpression',
138+
properties: identifiers.sort().map((id) => ({
139+
type: 'Property',
140+
method: false,
141+
shorthand: true,
135142
computed: false,
136-
optional: false,
137-
object: { type: 'Identifier', name: 'JSON' },
138-
property: { type: 'Identifier', name: 'stringify' }
139-
},
140-
arguments: [
141-
{
142-
type: 'ObjectExpression',
143-
properties: identifiers.sort().map((id) => ({
144-
type: 'Property',
145-
method: false,
146-
shorthand: true,
147-
computed: false,
148-
kind: 'init',
149-
key: { type: 'Identifier', name: id },
150-
value: { type: 'Identifier', name: id }
151-
}))
152-
}
153-
]
143+
kind: 'init',
144+
key: { type: 'Identifier', name: id },
145+
value: { type: 'Identifier', name: id }
146+
}))
154147
}
155148
]
156149
}
157-
}
158-
]
150+
]
151+
}
159152
}
160-
}
153+
]
161154
}
162-
]
163-
}
164-
})
155+
}
156+
},
157+
{ conflict: 'skip' }
158+
)
165159
}
166160

167161
export default recmaNextjsStaticProps

package-lock.json

Lines changed: 2 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"dependencies": {
3636
"@types/estree": "^1.0.0",
3737
"periscopic": "^3.0.0",
38-
"unified": "^11.0.0"
38+
"unified": "^11.0.0",
39+
"unist-util-mdx-define": "^1.0.0"
3940
},
4041
"devDependencies": {
4142
"@mdx-js/loader": "^3.0.0",

0 commit comments

Comments
 (0)