Implement SkuConfigUpdater
for codemodding sku config
#1086
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
As part of the upcoming eslint v9 breaking change, the need has arisen to modify consumer's sku config to enable a smoother migration. This PR implements a
SkuConfigUpdater
class that provides a simple interface for performing imperative updates to a sku config, abstracting away the AST parsing, modification and serialization.Reviewer notes
AST transformation code can be hard to understand just via visual inspection. The majority of it is just narrowing types to keep the typechecker happy. I wouldn't waste much time reviewing the AST code, the test cases showcase the API much more clearly.
API Example
Below is an example of how the future
eslintIgnore
option will be inserted into a user's sku config:See the test cases for more examples.
Implementation
SkuConfigUpdater
primarily abstracts overmagicast
, which itself is an abstraction overrecast
, but uses babel to parse ASTs rather than acorn, and provides a nice Proxy-based API for modifying module imports/exports.While
magicast
's API is nice, it doesn't handle CJS exports at all, so I went through a bit of effort implementing logic to deal with sku configs that usemodule.exports
. It's not as clean as I'd like, but I think future effort could be put into automatically uplifting non-TS and/or non-ESM sku configs to TS + ESM, potentially enablingsku
to drop support for CJS config entirely.Dependencies
All dependencies of
magicast
are already insku
's dependency tree, andmagicast
itself isn't that large (~120kb), so IMO there's no cause for concern.Testing
Used this new API to test out
fs-fixture
, a simple library for creating disposable test fixtures on disk. I think it's pretty neat, and has enabled a relatively simple way to write full integration tests forSkuConfigUpdater
solutions that involve mocking the file system.Misc.
SkuConfigUpdater
isn't a great name, definitely open to better suggestions.