Skip to content

Commit 9858528

Browse files
authored
fix(gatsby-plugin-mdx): ignore anything after an import in mdx (#25639)
1 parent 1399426 commit 9858528

File tree

3 files changed

+92
-7
lines changed

3 files changed

+92
-7
lines changed

packages/gatsby-plugin-mdx/utils/__tests__/__snapshots__/import-parser.js.snap

+72
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,78 @@ Object {
913913
`;
914914

915915
exports[`regex import scanner syntactic coverage should parse brute force regular case 57 1`] = `
916+
Object {
917+
"input": "import {x, import as y} from 'bar';",
918+
"result": Object {
919+
"bindings": Array [
920+
"x",
921+
"y",
922+
],
923+
"segments": Array [
924+
"x",
925+
"import as y",
926+
],
927+
},
928+
}
929+
`;
930+
931+
exports[`regex import scanner syntactic coverage should parse brute force regular case 58 1`] = `
932+
Object {
933+
"input": "import foo from 'bar' // commment",
934+
"result": Object {
935+
"bindings": Array [
936+
"foo",
937+
],
938+
"segments": Array [
939+
"foo",
940+
],
941+
},
942+
}
943+
`;
944+
945+
exports[`regex import scanner syntactic coverage should parse brute force regular case 59 1`] = `
946+
Object {
947+
"input": "import foo from 'bar' // commment containing confusing from \\"chars\\"",
948+
"result": Object {
949+
"bindings": Array [
950+
"foo",
951+
],
952+
"segments": Array [
953+
"foo",
954+
],
955+
},
956+
}
957+
`;
958+
959+
exports[`regex import scanner syntactic coverage should parse brute force regular case 60 1`] = `
960+
Object {
961+
"input": "import foo from 'bar' // import bad from 'imp'",
962+
"result": Object {
963+
"bindings": Array [
964+
"foo",
965+
],
966+
"segments": Array [
967+
"foo",
968+
],
969+
},
970+
}
971+
`;
972+
973+
exports[`regex import scanner syntactic coverage should parse brute force regular case 61 1`] = `
974+
Object {
975+
"input": "import foo from 'bar'//next to it",
976+
"result": Object {
977+
"bindings": Array [
978+
"foo",
979+
],
980+
"segments": Array [
981+
"foo",
982+
],
983+
},
984+
}
985+
`;
986+
987+
exports[`regex import scanner syntactic coverage should parse brute force regular case 62 1`] = `
916988
Object {
917989
"input": "import multi as dong, {foo} from 'bar'
918990
import as as x, {from as y} from 'bar'",

packages/gatsby-plugin-mdx/utils/__tests__/import-parser.js

+19-6
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ function getBruteForceCases() {
6767
import {import as x, y} from 'bar'
6868
import {x, import as y} from 'bar'
6969
import Events from "@components/events/events"
70+
import {x, import as y} from 'bar';
71+
import foo from 'bar' // commment
72+
import foo from 'bar' // commment containing confusing from "chars"
73+
import foo from 'bar' // import bad from 'imp'
74+
import foo from 'bar'//next to it
7075
`
7176
.trim()
7277
.split(/\n/g)
@@ -99,19 +104,27 @@ describe(`regex import scanner`, () => {
99104
)
100105
).toBe(true)
101106

107+
// For the next tests, don't mangle the comment and make sure it doesn't
108+
// end up on a newline by itself. Test cases only have `//` for comment.
109+
let commentStart = input.indexOf(`//`) - 1
110+
if (commentStart < 0) commentStart = input.length
111+
const commentLess = input.slice(0, commentStart)
112+
const commentPart = input.slice(commentStart)
113+
102114
// Confirm that the parser works when all spaces become newlines
103-
const newlined = input.replace(/ /g, `\n`)
115+
const newlined = commentLess.replace(/ /g, `\n`) + commentPart
104116
expect(parseImportBindings(newlined)).toEqual(bindings)
105117

106118
// Confirm that the parser works with a minimal amount of spacing
107-
const minified = input.replace(
108-
/(?<=[_\w$]) (?![_\w$])|(?<![_\w$]) (?=[_\w$])|(?<![_\w$]) (?![_\w$])/g,
109-
``
110-
)
119+
const minified =
120+
commentLess.replace(
121+
/(?<=[_\w$]) (?![_\w$])|(?<![_\w$]) (?=[_\w$])|(?<![_\w$]) (?![_\w$])/g,
122+
``
123+
) + commentPart
111124
expect(parseImportBindings(minified)).toEqual(bindings)
112125

113126
// Confirm that the parser works with an excessive amount of spacing
114-
const blown = input.replace(/ /g, ` `)
127+
const blown = commentLess.replace(/ /g, ` `) + commentPart
115128
expect(parseImportBindings(blown)).toEqual(bindings)
116129
})
117130
})

packages/gatsby-plugin-mdx/utils/import-parser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
function parseImportBindings(importCode, returnSegments = false) {
2121
const str = importCode.replace(
22-
/^\s*import|[{},]|\s*from\s*['"][^'"]*?['"]\s*$/gm,
22+
/^\s*import|[{},]|\s*from\s*['"][^'"]*?['"].*?$/gm,
2323
` , `
2424
)
2525
const segments = str

0 commit comments

Comments
 (0)