Does jscodeshift work with css/scss/etc..?
See original GitHub issueHey there,
First off, thanks for the awesome looking tool. I thought I’d write a simple codemod to change px to em’s and wanted to use jscodeshift to do it.
Here’s the transform I’m using: http://astexplorer.net/#/usDeoLTPFE/2
And here’s the jscodeshift command I was running:
jscodeshift . -t ./codemods/scss-px-to-em.js -v 1 --extensions scss
I must be doing something wrong because the output is totally wrong. So, is it possible to use jscodeshift to do postcss transforms?
Thanks very much!
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Write Code to Rewrite Your Code: jscodeshift - Toptal
The jscodeshift toolkit is great for working with codemods. Think of codemods as a scripted find and replace functionality that can read and...
Read more >Automatic refactoring with jscodeshift/codemods | by Juntao Qiu
How jscodeshift works is: firstly you need to define a transform script, this script need to respect the protocol so it can be...
Read more >jscodeshift example | Better world by better software
Take a simple calc.js that exports a single function that adds two numbers. ... Any client can load the function directly using require('./calc') ......
Read more >Writing your very first codemod with jscodeshift - Medium
AST: A tree representation of a piece of source code. When working with JavaScript tools that allow you to inspect/modify an AST, this...
Read more >An Introduction to jscodeshift - The Blog of Feifan Zhou
The “real” AST is more pedantic, but has the same shape: jscodeshift provides a way to work with AST data structures: finding and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The techniques that
recast
uses are generic enough to work for any language that has an AST, though you’d have to implement pretty-printer cases for all those AST types.That said, CSS formatting tends to be much more regular than JS formatting, so you could more easily get away with just parsing, transforming, and pretty-printing your CSS, without so much effort to preserve idiomatic formatting.
Hey folks, it is possible to do as suggested above via PostCSS.
I’ve written a guide here, hope it helps: www.codeshiftcommunity.com/docs/css-codemods
Here’s an example:
The main downside with this approach is that you aren’t able to leverage the “API” & AST of jscodeshift and recast. So you will need to rely solely on PostCSS. Also since jscodeshift uses dependency injection to expose it’s API, distributing codemods that use dependencies may be problematic.
An idea for jscodeshift future architectural direction could be to refactor the “Runner” to be generic and allow authors of transforms to import the AST/Transformation library of choice.