npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

swc-to-babel

v3.0.1

Published

convert swc ast to babel

Downloads

254

Readme

SWC-to-babel NPM version Build Status Coverage Status

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:

  • 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
  • etc...

swc-to-babel aims to smooth this differences.

Install

npm i swc-to-babel

Example

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