facebook / jscodeshift

A JavaScript codemod toolkit.
https://jscodeshift.com
MIT License
9.22k stars 477 forks source link

fix: Use transform's exported parser in testUtils #528

Closed CrispyBacon12 closed 1 year ago

CrispyBacon12 commented 1 year ago

Motivation

The defineTest and defineSnapshotTestFromFixture functions currently require an explicit testOptions when working with Typescript textfixtures, such as in this example.

const transform: Transform = (file, api) => {
  // ...
}
export default transform;
export const parser = 'ts';
defineTest(__dirname, 'MyTransform', null, null, { parser: 'ts' });

However, when a transform file exports a ts or tsx parser it would be more convenient if these functions could use that, and avoid needing to write 'ts' twice across files.

Changes

When testOptions.parser is not explicitly defined, fallback to reading module.parser. This mirrors the behaviour seen in applyTransform.

const transform: Transform = (file, api) => {
  // ...
}
export default transform;
export const parser = 'ts';
defineTest(__dirname, 'MyTransform');

Haven't updated docs, since they already describes these testUtils as making assumptions to reduce configuration. This is how I expected it to work at first reading.

Daniel15 commented 1 year ago

This sounds reasonable to me. Thanks for your contribution :)