Codeshift should be extended to support modules that have APIs that span across JS and CSS.
The idea is that a library maintainer can ship an API change to their library that affects both JS and CSS. This codemod would then be configured to run a transform for both environments.
Unfortunately JSCodehsift + Recast aren't designed to do that, however we can substitute other parsers like PostCSS as options.
https://twitter.com/danieldelcore/status/1496718163714576386?s=20&t=v4nUru6h12nyrnt6M88TKA
A config could look something like this:
{
module.exports = {
transforms: {
"10.0.0": {
"js,jsx,tsx,ts": require.resolve("./10.0.0/transform")
"css,sass,scss": require.resolve("./10.0.0/transform/css")
},
};
These extensions would be globs/regex that would match file extensions and apply a different codemod as a result.
These would be paired with custom parsers that would be "part" of the codeshift library.
import { FileInfo } from 'jscodeshift';
import postcss, { Node, Rule, Declaration, Plugin } from 'postcss';
import lessSyntax from 'postcss-less';
// https://github.com/postcss/postcss/blob/main/docs/writing-a-plugin.md
// https://astexplorer.net/#/2uBU1BLuJ1
const plugin = (): Plugin => {
const processed = Symbol('processed');
return {
postcssPlugin: 'UsingTokens',
Declaration: (decl) => {
// @ts-expect-error
if (decl[processed]) {
return;
}
// Transformation goes here
decl[processed] = true;
},
};
};
export default async function transformer(file: FileInfo) {
return await postcss([plugin()]).process(file.source, { syntax: lessSyntax })
.css;
}
facebook/jscodeshift#124