convert swc ast to babel
npm install swc-to-babel[NPMIMGURL]: https://img.shields.io/npm/v/swc-to-babel.svg?style=flat&longCache=true
[BuildStatusURL]: https://github.com/coderaiser/swc-to-babel/actions?query=workflow%3A%22Node+CI%22 "Build Status"
[BuildStatusIMGURL]: https://github.com/coderaiser/swc-to-babel/workflows/Node%20CI/badge.svg
[NPMURL]: https://npmjs.org/package/swc-to-babel "npm"
[BuildStatusURL]: https://travis-ci.org/coderaiser/swc-to-babel "Build Status"
[CoverageURL]: https://coveralls.io/github/coderaiser/swc-to-babel?branch=master
[CoverageIMGURL]: https://coveralls.io/repos/coderaiser/swc-to-babel/badge.svg?branch=master&service=github
Convert SWC JavaScript AST to Babel AST.
To use SWC parser with babel tools like:
- @babel/traverse
- @babel/types
- etc...
The thing is @babel/parser has a a little differences with swc standard:
- File node exists;
- Program instead of Module;
- loc with line and column instead of span;
- StringLiteral has no kind an hasEscape;
- Identifier has no optional and uses name instead of value;
- BlockStatement has body instead of stmts;
- VariableDeclarator has no optional and definite;
- CallExpression has no typeArguments, spread and expression properties in arguments;
- TemplateElement has value field with raw and cooked;
- TypeScript ast nodes has prefix TS instead of Ts;
- ExportNamedDeclaration instead of ExportDeclaration;
- ExportDefaultDeclaration instead of ExportDefaultExpression;
- VariableDeclaration has no declare field;
- Has no ParenthesisExpression;
- ClassDeclaration and ClassExpression uses id instead of identifier, has ClassBody;
- ClassMethod uses static instead of isStatic;
- MemberExpression has computed property instead of Computed node in property field;
- NewExpression has no untyped node with a spread property in arguments, always has arguments field, instead of null when absent;
- ArrayExpression has no untyped node with a spread property in elements;
- Function has no typeParameters;
- TSTypeReference has no typeParams field;
- TSTypeOperator has operator instead of op;
- TSTypeParameter has a field name which is string instead of Identifier;
- FunctionDeclaration instead of FunctionExpression with identifier field;
- ImportDeclaration has importKind instead of typeOnly and attributes fields;
- ObjectProperty instead of KeyValueProperty, KeyValuePatternProperty and AssignmentPatternProperty;
- ExportNamedDeclaration has exportKind, specifiers fields;
- ExportSpecifier has local which is never null instead of orig;
- ExportDefaultDeclaration has declaration instead of decl;
- TSAnyKeyword instead of TSKeywordType;
- TSAsExpression instead of TsConstAssertion;
- ObjectMethod with kind: get instead of GetterProperty;
- ObjectMethod with kind: set instead of SetterProperty;
- TSMappedType typeParameter.constraint, instead of typeParameter;
- etc...
swc-to-babel aims to smooth this differences.
```
npm i swc-to-babel
`js
import swc from '@swc/core';
import toBabel from 'swc-to-babel';
import traverse from 'babel/traverse';
const ast = toBabel(swc.parseSync(
const f = ({a}) => a;));
traverse({
ObjectProperty(path) {
console.log(path.value.name);
// output
'a';
},
});
`
`ts``
/**
* Convert an SWC ast to a babel ast
* @param ast {Module} SWC ast
* @param {string} [src=""] Source code
* @returns {ParseResult
*/
export default function toBabel(ast: Module, src?: string): ParseResult
MIT