Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve export name extraction for shortcode generation (#1160)
* Improve export name extraction Many different types of export syntax wasn't supported, primarily destructuring for objects and arrays. This adds export extraction to the import name extraction step so we can more accurately determine what shortcodes to generate. * Place gatsby-plugin-mdx in the root for now, seems to not be resolving properly * Fix linting * Make null handling more clear * Fix handling of null specifiers * Move dep to proper place
- Loading branch information
Showing
10 changed files
with
256 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const {declare} = require('@babel/helper-plugin-utils') | ||
|
||
class BabelPluginExtractExportNames { | ||
constructor() { | ||
const names = [] | ||
this.state = {names} | ||
|
||
this.plugin = declare(api => { | ||
api.assertVersion(7) | ||
const {types: t} = api | ||
|
||
const handleDeclarations = node => { | ||
if (!node.declaration) { | ||
return | ||
} | ||
|
||
const {declarations} = node.declaration | ||
if (!declarations) { | ||
return | ||
} | ||
|
||
declarations.forEach(declaration => { | ||
if (t.isIdentifier(declaration.id)) { | ||
// Export const foo = 'bar' | ||
names.push(declaration.id.name) | ||
} else if (t.isArrayPattern(declaration.id)) { | ||
// Export const [ a, b ] = [] | ||
declaration.id.elements.forEach(decl => { | ||
names.push(decl.name) | ||
}) | ||
} else if (t.isObjectPattern(declaration.id)) { | ||
// Export const { a, b } = {} | ||
declaration.id.properties.forEach(decl => { | ||
names.push(decl.key.name) | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
const handleSpecifiers = node => { | ||
const {specifiers} = node | ||
|
||
if (!specifiers) { | ||
return | ||
} | ||
|
||
specifiers.forEach(specifier => { | ||
if (t.isExportDefaultSpecifier(specifier)) { | ||
names.push(specifier.exported.name) | ||
} else { | ||
names.push(specifier.local.name) | ||
} | ||
}) | ||
} | ||
|
||
return { | ||
visitor: { | ||
ExportNamedDeclaration(path) { | ||
handleDeclarations(path.node) | ||
handleSpecifiers(path.node) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
module.exports = BabelPluginExtractExportNames |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "babel-plugin-extract-export-names", | ||
"version": "2.0.0-next.1", | ||
"description": "Extract export names", | ||
"repository": "mdx-js/mdx", | ||
"homepage": "https://mdxjs.com", | ||
"bugs": "https://github.com/mdx-js/mdx/issues", | ||
"funding": { | ||
"type": "opencollective", | ||
"url": "https://opencollective.com/unified" | ||
}, | ||
"author": "John Otander <johnotander@gmail.com> (http://johnotander.com)", | ||
"license": "MIT", | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"mdx", | ||
"markdown", | ||
"react", | ||
"jsx", | ||
"remark", | ||
"babel" | ||
], | ||
"dependencies": { | ||
"@babel/helper-plugin-utils": "7.10.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# `babel-plugin-extract-export-names` | ||
|
||
Babel plugin that extracts all variable names from | ||
export statements.Used by the [MDX](https://mdxjs.com) | ||
pragma. | ||
|
||
## Installation | ||
|
||
```sh | ||
yarn add babel-plugin-extract-export-names | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
const babel = require('@babel/core') | ||
|
||
const BabelPluginExtractExportNames = require('babel-plugin-extract-export-names') | ||
|
||
const jsx = ` | ||
export const foo = 'bar' | ||
export const [A] = [1] | ||
` | ||
|
||
const plugin = new BabelPluginExtractExportNames() | ||
|
||
const result = babel.transform(jsx, { | ||
configFile: false, | ||
plugins: [plugin.plugin] | ||
}) | ||
|
||
console.log(plugin.state.names) | ||
``` | ||
|
||
## License | ||
|
||
MIT |
56 changes: 56 additions & 0 deletions
56
packages/babel-plugin-extract-export-names/test/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const babel = require('@babel/core') | ||
|
||
const BabelPluginExtractExportNames = require('..') | ||
|
||
const FIXTURE = ` | ||
export const foo = 'bar' | ||
export const foo2 = { | ||
bar: 'baze', | ||
hi: \`Hello! \${foo.toJSON() || 'hrm'}\`, | ||
abc: { | ||
def: 123 | ||
} | ||
} | ||
export const foo3 = [1, 2, 3, { a: 'b' }] | ||
export const A = 'baz' | ||
export const [B] = [1] | ||
export const [C, D, E] = [a, { b: 'c' }] | ||
export const { F } = { foo: 'bar' } | ||
export const { G, H } = {} | ||
export { Super } from './super' | ||
` | ||
|
||
const transform = str => { | ||
const plugin = new BabelPluginExtractExportNames() | ||
|
||
const result = babel.transform(str, { | ||
configFile: false, | ||
plugins: [plugin.plugin] | ||
}) | ||
|
||
return { | ||
...result, | ||
state: plugin.state | ||
} | ||
} | ||
|
||
describe('babel-plugin-extract-export-names', () => { | ||
test('adds export names to state', () => { | ||
const result = transform(FIXTURE) | ||
|
||
expect(result.state.names).toEqual([ | ||
'foo', | ||
'foo2', | ||
'foo3', | ||
'A', | ||
'B', | ||
'C', | ||
'D', | ||
'E', | ||
'F', | ||
'G', | ||
'H', | ||
'Super' | ||
]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters