Skip to content

Upgrade jsonforms and remove redux from spectrum package #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 5, 2021

Conversation

uudens
Copy link
Collaborator

@uudens uudens commented Feb 26, 2021

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
Copy link
Member

mburri commented Mar 2, 2021

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
Copy link
Collaborator Author

uudens commented Mar 5, 2021

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

@uudens uudens merged commit 1e5abdf into master Mar 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants