headwirecom / jsonforms-react-spectrum-renderers

Other
8 stars 4 forks source link

Upgrade jsonforms and remove redux from spectrum package #97

Closed uudens closed 3 years ago

uudens commented 3 years ago

I ended up writing a jscodeshift transform to help get rid of redux in unit tests. Posting my transform here just in case it becomes useful for somebody in the future

export default function transformer(file, api, options) {
    const j = api.jscodeshift;
    const root = j(file.source);
    root.findJSXElements('Provider').forEach(path => {
        const store = path.node.openingElement.attributes.find(attr => attr.name.name === 'store');
        const block = findBlock(path);
        let data = null
        let storeDeclaratorPath = null
        const storeDeclarator = j(block).find(j.VariableDeclarator).forEach(p => {
            if (p.value.id.name === 'store' && p.value.init.callee.name === 'initJsonFormsSpectrumStore') {
                data = p.value.init.arguments[0].properties.find(p => p.key.name === 'data').value
                storeDeclaratorPath = p
            }
        })

        const JsonFormsReduxContext = path.node.children.find(n => n.type === 'JSXElement' && n.openingElement.name.name === 'JsonFormsReduxContext')
        if (!JsonFormsReduxContext) {
            console.log('encountered Provider without JsonFormsReduxContext, skipping')
            return;
        }
        j(storeDeclaratorPath).remove()

        const cell = JsonFormsReduxContext.children.find(n => n.type === 'JSXElement');
        if (!cell) {
            console.log(JsonFormsReduxContext.children.map(n => n.type === 'JSXElement' && n.openingElement.name.name))
            console.error('could not find cell')
            return;
        }
        const schema = cell.openingElement.attributes.find(attr => attr.name.name === 'schema')
        const uischema = cell.openingElement.attributes.find(attr => attr.name.name === 'uischema')
        const newNode = j.jsxElement(
            j.jsxOpeningElement(
                j.jsxIdentifier('JsonForms'),
                [
                    schema,
                    uischema,
                    ...(data ? [j.jsxAttribute(j.jsxIdentifier('data'), j.jsxExpressionContainer(data))] : []),
                    j.jsxAttribute(j.jsxIdentifier('renderers'), j.jsxExpressionContainer(j.identifier('spectrumRenderers'))),
                    j.jsxAttribute(j.jsxIdentifier('cells'), j.jsxExpressionContainer(j.identifier('cells'))),
                ],
                true,
            )
        )
        j(path).replaceWith(newNode)
    });
    return root.toSource(options)
}

function findBlock(path) {
    let parent = path.parent
    return parent.value.type === 'BlockStatement' ? parent : findBlock(parent)
}
mburri commented 3 years ago

Very nice! This makes me think about our version number - we should probably bump it too, since our package no longer works with jsonforms 2.4.x

uudens commented 3 years ago

Yes, thanks! Bumped version to 0.0.1-beta.4