swc/babel/compat/tests
강동윤 104be9837b
fix(es/transforms): Fix transforms (#1909)
swc_ecma_transforms_compat:
 - `regenerator`: Use es6 import while folding module. (#1641)
 - `typeof_symbol`: Handle `undefined` specially. (#1843)
 - `regenerator`: Do not create useless codes. (#1687)
 - `typeof_symbol`: Migrate to `VisitMut`.

swc_ecma_transforms_module:
 - Add `import_hoister`.
 - Improve import analyzer. (#1682)
 - Allow overriding `export *` wth named exports. (#1714)

swc_ecma_transforms_testing:
 - Add a hack for `regenerator-runtime`.

swc:
 - Run import analyzer ahead of time. (#1682)

misc:
 - Downgrade rustc to the version rust-analyzer supports.
2021-07-10 11:18:28 +00:00
..
fixtures feat(babel/compat): Improve performance of babelify (#1626) 2021-05-06 14:56:54 +09:00
babelgen.js feat(babel/compat): Improve performance of babelify (#1626) 2021-05-06 14:56:54 +09:00
compare.sh feat(babel/compat): Improve performance of babelify (#1626) 2021-05-06 14:56:54 +09:00
convert.rs fix(es/transforms): Fix transforms (#1909) 2021-07-10 11:18:28 +00:00
package.json feat(babel/compat): Improve performance of babelify (#1626) 2021-05-06 14:56:54 +09:00
README.md feat(babel/compat): Improve performance of babelify (#1626) 2021-05-06 14:56:54 +09:00
swcgen.js feat(babel/compat): Improve performance of babelify (#1626) 2021-05-06 14:56:54 +09:00

How tests work

The babel-compat tests are mostly written as fixtures, similar to the @babel/parser tests. The src/convert.rs test runner looks in the fixtures/ directory for input and expected output files. Input files are parsed into an swc AST and converted to a Babel AST in Rust. Output files are parsed directly into a Babel AST. The two ASTs are then compared, with any differences causing the test to fail.

How to write a test

Step 1: Create a new fixture dir and input file.

mkdir fixtures/my-test
echo "var a = true;" > fixtures/my-test/input.js

Step 2: Generate an output file with the expected Babel AST as JSON. There's a utility script available to help, but you can do this however you like.

# If using the babelgen.js utility, run `npm install` first to get @babel/parser dependency.
node babelgen.js fixtures/my-test/input.js > fixtures/my-test/output.json

Step 3: cargo test should now pick up your new test automatically.

There's a small, insignificant different between the default Babel AST and the converted one causing my test to fail.

This happens a lot with None and Some(false). You'll probably want to add a normalizer function to the Normalizer visitor in src/normalize/mod.rs.

Other random utlities

  • swcgen.js: Prints the swc AST as JSON.
  • compare.sh: prints the Babel and swc ASTs side-by-side.