-
-
Save joshblack/8fae4757474422ad632f to your computer and use it in GitHub Desktop.
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
const pure = () => <div />; | |
const pureProps = props => <div {...props} />; | |
const pureBlock = () => { | |
return <div />; | |
}; | |
const modulePatternClassic = () => ({ | |
render: function render() { | |
return <div />; | |
} | |
}); | |
const modulePatternModern = () => ({ | |
render() { | |
return <div />; | |
return; | |
} | |
}); | |
const arrowInObjectLiteral = () => ({ | |
render: () => <div /> | |
}); | |
const exportNamed = () => <div />; | |
export { exportNamed }; | |
export default () => <div />; | |
export default () => { | |
return <div />; | |
}; |
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
"use strict"; | |
Object.defineProperty(exports, "__esModule", { | |
value: true | |
}); | |
var pure = "Direct JSX return statement"; | |
var pureProps = "Direct JSX return statement"; | |
var pureBlock = "Pure Block"; | |
var modulePatternClassic = function modulePatternClassic() { | |
return { | |
render: function render() { | |
return React.createElement("div", null); | |
} | |
}; | |
}; | |
var modulePatternModern = function modulePatternModern() { | |
return { | |
render: function render() { | |
return React.createElement("div", null); | |
return; | |
} | |
}; | |
}; | |
var arrowInObjectLiteral = function arrowInObjectLiteral() { | |
return { | |
render: function render() { | |
return React.createElement("div", null); | |
} | |
}; | |
}; | |
var exportNamed = "Direct JSX return statement"; | |
exports.exportNamed = exportNamed; | |
exports["default"] = "Export Default JSX Element"; | |
exports["default"] = "Export Default Pure Block"; |
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
export default function ({Plugin, types: t}) { | |
const formatters = new Map([ | |
['VariableDeclarator', formatVariableDeclarator], | |
['ExportDefaultDeclaration', formatExportDefaultDeclaration] | |
]); | |
function formatVariableDeclarator(node) { | |
if (t.isJSXElement(node.body)) { | |
return t.literal('Direct JSX return statement'); | |
} | |
if (t.isBlockStatement(node.body)) { | |
const [returnStatement] = node.body.body.filter(t.isReturnStatement); | |
if (t.isJSXElement(returnStatement.argument)) { | |
return t.literal('Pure Block'); | |
} | |
} | |
} | |
function formatExportDefaultDeclaration(node) { | |
if (t.isJSXElement(node.body)) { | |
return t.literal('Export Default JSX Element'); | |
} | |
if (t.isBlockStatement(node.body)) { | |
const [returnStatement] = node.body.body.filter(t.isReturnStatement); | |
if (t.isJSXElement(returnStatement.argument)) { | |
return t.literal('Export Default Pure Block'); | |
} | |
} | |
} | |
return new Plugin('ast-transform', { | |
visitor: { | |
ArrowFunctionExpression(node, parent) { | |
if (formatters.has(parent.type)) { | |
return formatters.get(parent.type)(node); | |
} | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment