3.0.1 • Published 2 years ago
swc-to-babel v3.0.1
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 ofGetterPropertyObjectMethodwithkind: setinstead ofSetterProperty- etc...
swc-to-babel aims to smooth this differences.
Install
npm i swc-to-babelExample
const swc = require('@swc/core');
const toBabel = require('swc-to-babel');
const traverse = require('@babel/traverse').default;
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
3.0.1
2 years ago
3.0.0
2 years ago
2.2.0
2 years ago
2.1.0
2 years ago
2.0.0
3 years ago
1.26.0
3 years ago
1.25.0
3 years ago
1.25.1
3 years ago
1.23.0
3 years ago
1.24.0
3 years ago
1.15.0
3 years ago
1.2.0
3 years ago
1.1.1
3 years ago
1.14.0
3 years ago
1.1.0
3 years ago
1.13.0
3 years ago
1.11.2
3 years ago
1.12.0
3 years ago
1.11.1
3 years ago
1.19.0
3 years ago
1.18.0
3 years ago
1.17.1
3 years ago
1.17.0
3 years ago
1.16.1
3 years ago
1.16.0
3 years ago
1.9.0
3 years ago
1.8.0
3 years ago
1.7.1
3 years ago
1.7.0
3 years ago
1.6.0
3 years ago
1.5.0
3 years ago
1.4.0
3 years ago
1.3.1
3 years ago
1.3.0
3 years ago
1.21.0
3 years ago
1.20.1
3 years ago
1.22.0
3 years ago
1.20.0
3 years ago
1.11.0
3 years ago
1.10.0
3 years ago
1.0.2
4 years ago
1.0.1
4 years ago
1.0.0
4 years ago