Skip to content

Commit

Permalink
Pass filename to Babel transform for better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
silvenon committed Jul 1, 2020
1 parent b3b751a commit 5b1dda3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/mdx/mdx-hast-to-jsx.js
Expand Up @@ -121,6 +121,7 @@ MDXContent.isMDXComponent = true`
// Check JSX nodes against imports
const babelPluginExtractImportNamesInstance = new BabelPluginExtractImportNames()
transformSync(importStatements, {
filename: options.file.path,
configFile: false,
babelrc: false,
plugins: [
Expand All @@ -135,6 +136,7 @@ MDXContent.isMDXComponent = true`
const babelPluginApplyMdxPropToExportsInstance = new BabelPluginApplyMdxProp()

const fnPostMdxTypeProp = transformSync(fn, {
filename: options.file.path,
configFile: false,
babelrc: false,
plugins: [
Expand All @@ -145,6 +147,7 @@ MDXContent.isMDXComponent = true`
}).code

const exportStatementsPostMdxTypeProps = transformSync(exportStatements, {
filename: options.file.path,
configFile: false,
babelrc: false,
plugins: [
Expand Down Expand Up @@ -250,8 +253,8 @@ export default ${fnPostMdxTypeProp}`
}

function compile(options = {}) {
this.Compiler = function (tree) {
return toJSX(tree, {}, options)
this.Compiler = function (tree, file) {
return toJSX(tree, {}, { file, ...options })
}
}

Expand Down
16 changes: 16 additions & 0 deletions packages/mdx/test/index.test.js
Expand Up @@ -419,3 +419,19 @@ test('Should handle layout props', () => {
MDXContent.isMDXComponent = true;"
`)
})

it('Should include file name in Babel error', async () => {
expect(async () => {
await mdx(`<br>`, {filepath: '/path/to/file.mdx'})
}).rejects.toThrowErrorMatchingInlineSnapshot(`
"/path/to/file.mdx: Unterminated JSX contents (8:16)
  6 |  components={components}>
  7 | <br>
> 8 |  </MDXLayout>
  |  ^
  9 |  )
  10 | };
  11 | MDXContent.isMDXComponent = true"
`)
})

0 comments on commit 5b1dda3

Please sign in to comment.