swc-to-babel
v5.0.2
Published
convert swc ast to babel
Downloads
263
Maintainers
Readme
SWC-to-babel

Convert SWC JavaScript AST to Babel AST.
To use SWC parser with babel tools like:
The thing is @babel/parser has a a little differences with swc standard:
Filenode exists;Programinstead ofModule;locwithlineandcolumninstead ofspan;StringLiteralhas nokindanhasEscape;Identifierhas nooptionaland usesnameinstead ofvalue;BlockStatementhasbodyinstead ofstmts;VariableDeclaratorhas nooptionalanddefinite;CallExpressionhas notypeArguments,spreadandexpressionproperties inarguments;TemplateElementhasvaluefield withrawandcooked;- TypeScript ast nodes has prefix
TSinstead ofTs; ExportNamedDeclarationinstead ofExportDeclaration;ExportDefaultDeclarationinstead ofExportDefaultExpression;VariableDeclarationhas nodeclarefield;- Has no
ParenthesisExpression; ClassDeclarationandClassExpressionusesidinstead ofidentifier, hasClassBody;ClassMethodusesstaticinstead ofisStatic;MemberExpressionhascomputedproperty instead ofComputednode inpropertyfield;NewExpressionhas no untyped node with aspreadproperty inarguments, always hasargumentsfield, instead ofnullwhen absent;ArrayExpressionhas no untyped node with aspreadproperty inelements;Functionhas notypeParameters;TSTypeReferencehas notypeParamsfield;TSTypeOperatorhasoperatorinstead ofop;TSTypeParameterhas a fieldnamewhich isstringinstead ofIdentifier;FunctionDeclarationinstead ofFunctionExpressionwithidentifierfield;ImportDeclarationhasimportKindinstead oftypeOnlyandattributesfields;ObjectPropertyinstead ofKeyValueProperty,KeyValuePatternPropertyandAssignmentPatternProperty;ExportNamedDeclarationhasexportKind,specifiersfields;ExportSpecifierhaslocalwhich is nevernullinstead oforig;ExportDefaultDeclarationhasdeclarationinstead ofdecl;TSAnyKeywordinstead ofTSKeywordType;TSAsExpressioninstead ofTsConstAssertion;ObjectMethodwithkind: getinstead ofGetterProperty;ObjectMethodwithkind: setinstead ofSetterProperty;TSMappedTypetypeParameter.constraint, instead oftypeParameter;- etc...
swc-to-babel aims to smooth this differences.
Install
npm i swc-to-babelExample
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';
},
});API reference
/**
* Convert an SWC ast to a babel ast
* @param ast {Module} SWC ast
* @param {string} [src=""] Source code
* @returns {ParseResult<File>} Babel ast
*/
export default function toBabel(ast: Module, src?: string): ParseResult<File>;License
MIT
