Skip to content

Commit

Permalink
Code readability tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertBroersma authored and Justin Reidy committed May 19, 2020
1 parent 560494f commit de323db
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions packages/core/config/babel-plugin-redwood-cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
// The end result of this plugin is something like:
// ```js
// import { withCell } from '@redwoodjs/web'
//
// export default withCell({ QUERY, Loading, Succes, Failure, Empty, beforeQuery, afterQuery })
// ```
module.exports = function ({ types: t }) {
// These are the expected named exports from a Cell file.
const EXPORTS_FROM_CELL = [
const EXPECTED_EXPORTS_FROM_CELL = [
'QUERY',
'Loading',
'Success',
Expand All @@ -17,10 +18,30 @@ module.exports = function ({ types: t }) {
'afterQuery',
]

// This array will
// - collect exports from the Cell file during ExportNamedDeclaration
// - collected exports will then be passed to `withCell`
// - be cleared after Program exit to prepare for the next file
let exportNames = []

return {
visitor: {
ExportNamedDeclaration(path) {
const { declaration } = path.node

let name
if (declaration.type === 'VariableDeclaration') {
name = declaration.declarations[0].id.name
}

if (declaration.type === 'FunctionDeclaration') {
name = declaration.id.name
}

if (EXPECTED_EXPORTS_FROM_CELL.includes(name)) {
exportNames.push(name)
}
},
Program: {
exit(path) {
// import { withCell } from '@redwoodjs/web'
Expand Down Expand Up @@ -57,22 +78,6 @@ module.exports = function ({ types: t }) {
exportNames = []
},
},
ExportNamedDeclaration(path) {
const { declaration } = path.node

let name
if (declaration.type === 'VariableDeclaration') {
name = declaration.declarations[0].id.name
}

if (declaration.type === 'FunctionDeclaration') {
name = declaration.id.name
}

if (EXPORTS_FROM_CELL.includes(name)) {
exportNames.push(name)
}
},
},
}
}

0 comments on commit de323db

Please sign in to comment.