WIP - add extractor, generate snippet_data

This commit is contained in:
Stefan Fejes
2019-08-20 15:52:05 +02:00
parent 88084d3d30
commit cc8f1d8a7a
37396 changed files with 4588842 additions and 133 deletions

View File

@ -0,0 +1,6 @@
module.exports = {
configurableMacro: {
fileConfig: true,
someConfig: true,
},
}

View File

@ -0,0 +1,4 @@
const configured = require('./configurable.macro')
// eslint-disable-next-line babel/no-unused-expressions
configured`stuff`

View File

@ -0,0 +1,4 @@
import configured from './configurable.macro'
// eslint-disable-next-line babel/no-unused-expressions
configured`stuff`

View File

@ -0,0 +1,10 @@
const {createMacro} = require('../../../../')
const configName = 'configurableMacro'
const realMacro = jest.fn()
module.exports = createMacro(realMacro, {configName})
// for testing purposes only
Object.assign(module.exports, {
realMacro,
configName,
})

View File

@ -0,0 +1,8 @@
const {createMacro} = require('../../')
export default createMacro(evalMacro)
function evalMacro() {
// we're lazy right now
// we don't want to eval
}

View File

@ -0,0 +1,29 @@
// this is a fake version of emotion
// const printAST = require('ast-pretty-print')
const {createMacro} = require('../../')
module.exports = createMacro(emotionMacro)
function emotionMacro({references, babel}) {
const {types: t} = babel
references.css.forEach(cssRef => {
if (cssRef.parentPath.type === 'TaggedTemplateExpression') {
cssRef.parentPath.replaceWith(
t.stringLiteral(
cssRef.parentPath
.get('quasi')
.evaluate()
.value.trim(),
),
)
}
})
references.styled.forEach(styledRef => {
if (styledRef.parentPath.parentPath.type === 'TaggedTemplateExpression') {
const quasi = styledRef.parentPath.parentPath.get('quasi')
const val = quasi.evaluate().value.trim()
const replacement = t.templateLiteral([t.templateElement(val)], [])
quasi.replaceWith(replacement)
}
})
}

View File

@ -0,0 +1,8 @@
// const printAST = require('ast-pretty-print')
const {createMacro} = require('../../')
module.exports = createMacro(evalMacro)
function evalMacro() {
throw new Error('very unhelpful')
}

View File

@ -0,0 +1,54 @@
const {parse} = require('@babel/parser')
// const printAST = require('ast-pretty-print')
const {createMacro} = require('../../')
module.exports = createMacro(evalMacro)
function evalMacro({references, state}) {
references.default.forEach(referencePath => {
if (referencePath.parentPath.type === 'TaggedTemplateExpression') {
asTag(referencePath.parentPath.get('quasi'), state)
} else if (referencePath.parentPath.type === 'CallExpression') {
asFunction(referencePath.parentPath.get('arguments'), state)
} else if (referencePath.parentPath.type === 'JSXOpeningElement') {
asJSX(
{
attributes: referencePath.parentPath.get('attributes'),
children: referencePath.parentPath.parentPath.get('children'),
},
state,
)
} else {
// TODO: throw a helpful error message
}
})
}
function asTag(quasiPath) {
const value = quasiPath.parentPath.get('quasi').evaluate().value
quasiPath.parentPath.replaceWith(evalToAST(value))
}
function asFunction(argumentsPaths) {
const value = argumentsPaths[0].evaluate().value
argumentsPaths[0].parentPath.replaceWith(evalToAST(value))
}
// eslint-disable-next-line no-unused-vars
function asJSX({attributes, children}) {
// It's a shame you cannot use evaluate() with JSX
const value = children[0].node.value
children[0].parentPath.replaceWith(evalToAST(value))
}
function evalToAST(value) {
let x
// eslint-disable-next-line
eval(`x = ${value}`)
return thingToAST(x)
}
function thingToAST(object) {
const fileNode = parse(`var x = ${JSON.stringify(object)}`)
return fileNode.program.body[0].declarations[0].init
}

View File

@ -0,0 +1,20 @@
// adds "prefix-" to each `id` attribute
const {createMacro} = require('../../')
module.exports = createMacro(wrapWidget)
function wrapWidget({references, babel}) {
const {types: t} = babel
references.default.forEach(wrap => {
wrap.parentPath.traverse({
JSXAttribute(path) {
const name = path.get('name')
if (t.isJSXIdentifier(name) && name.node.name === 'id') {
const value = path.get('value')
if (t.isStringLiteral(value))
value.replaceWith(t.stringLiteral(`macro-${value.node.value}`))
}
},
})
})
}

View File

@ -0,0 +1,23 @@
// babel-plugin adding `plugin-` prefix to each "id" JSX attribute
module.exports = main
function main({types: t}) {
return {
visitor: {
// intentionally traversing from Program,
// if it matches JSXAttribute here the issue won't be reproduced
Program(progPath) {
progPath.traverse({
JSXAttribute(path) {
const name = path.get('name')
if (t.isJSXIdentifier(name) && name.node.name === 'id') {
const value = path.get('value')
if (t.isStringLiteral(value))
value.replaceWith(t.stringLiteral(`plugin-${value.node.value}`))
}
},
})
},
},
}
}

View File

@ -0,0 +1,7 @@
const {createMacro} = require('../../')
module.exports = createMacro(keepImportMacro)
function keepImportMacro() {
return {keepImports: true}
}

View File

@ -0,0 +1,8 @@
// const printAST = require('ast-pretty-print')
const {createMacro, MacroError} = require('../../')
module.exports = createMacro(evalMacro)
function evalMacro() {
throw new MacroError('very helpful')
}

View File

@ -0,0 +1 @@
module.exports = () => {}

View File

@ -0,0 +1,5 @@
import myEval from '../eval.macro'
const result = myEval`+('4' + '2')`
global.result = result

View File

@ -0,0 +1,3 @@
module.exports = {
configurableMacro: 4,
}

View File

@ -0,0 +1,4 @@
import configured from './configurable.macro'
// eslint-disable-next-line babel/no-unused-expressions
configured`stuff`

View File

@ -0,0 +1,10 @@
const {createMacro} = require('../../../../')
const configName = 'configurableMacro'
const realMacro = jest.fn()
module.exports = createMacro(realMacro, {configName})
// for testing purposes only
Object.assign(module.exports, {
realMacro,
configName,
})