swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
31.02k stars 1.22k forks source link

Possible to use swc with / for eslint? #246

Open rvolgers opened 5 years ago

rvolgers commented 5 years ago

eslint is a big part of our largest project's build times and it's currently configured to use babel-parser.

To get rid of babel entirely it'd be nice to be able to get eslint to use swc somehow: https://eslint.org/docs/user-guide/configuring#specifying-parser

On an unrelated note, with swc 1.0.21 the project finally builds and seems to mostly work (I haven't looked to closely yet). Thank you for your hard work and the quick responses to bug reports, it's much appreciated!

kdy1 commented 5 years ago

It's blocked by #230 (because we can't emit esprima-compatible ast)

ForsakenHarmony commented 5 years ago

Would be pretty nice if the parser could be turned into a standalone project you can use in other rust projects 👀

kdy1 commented 5 years ago

@ForsakenHarmony Parser is uploaded on crates.io, and it can be built with stable rust.

kapouer commented 4 years ago

I just tried but it seems eslint is expecting something a little bit different than the format returned by

exports.parse = function(str, opts) {
  return {
    ast: require('@swc/core').parseSync(str.toString(), {})
  };
};

EDIT2: this is what eslint expects: https://eslint.org/docs/developer-guide/working-with-custom-parsers

To test with eslint, just create a swcparser.js file at the root of your project, then put this into your project's .eslintrc:

{"parser": "./swcparser.js"}

Then to debug,

DEBUG=eslint:* eslint somefile.js

Using @swc/core 1.1.6 and eslint 6.7.2:

echo "var a = 1;" | eslint --stdin
  eslint:linter Parsing error: AST is missing the tokens array.
  eslint:linter Error: AST is missing the tokens array.

as expected, since the tokens array is indeed not where it is supposed to be.

pbadenski commented 3 years ago

@kdy1 I'm a bit confused about the status of this, hope you can clarify. (As you might be guessing I'm also super excited about swc use with eslint because of speed).

A comment above (https://github.com/swc-project/swc/issues/246#issuecomment-465403756) is mentioning this having been blocked by #230. That issue is closed and merged. Considering the comment above "AST is missing the tokens array." I'm guessing there is more missing?

kdy1 commented 3 years ago

It still requires lots of work

axyz commented 3 years ago

If I got it right #230 would allow to have the needed AST format as an output.

as https://github.com/swc-project/swc/pull/274 is merged, does this mean this issue is unblocked? What would be the needed next steps?

In case it is not unblocked, would something like https://github.com/babel/babel-eslint/tree/master/lib/babylon-to-espree be an intermediate solution? Adapting it to work with the output from swc? Or is there some issue with the format that would make that solution used by babel impossible with swc?

kdy1 commented 3 years ago

It's possible, but I'm not sure if it worths

axyz commented 3 years ago

Can you elaborate more?

Do you mean that with #274 it is now natively possible to use swc as a eslint (espree) parser? And if not, what is missing to be done?

or do you mean that converting the format in js as it is done with babel is "possible, but not worth it"? Would that be too much work for a possibly small speed improvement?

In general I think this is a topic of interest to speed up eslint end if the remaining work to be done is clear maybe easier to get it contributed.

kdy1 commented 3 years ago

Do you mean that with #274 it is now natively possible to use swc as a eslint (espree) parser? And if not, what is missing to be done?

No, because eslint does not use babel ast. It can be used if something like https://github.com/babel/babel-eslint/tree/master/lib/babylon-to-espree is used.

swc-to-espree is not done.

or do you mean that converting the format in js as it is done with babel is "possible, but not worth it"? Would that be too much work for a possibly small speed improvement?

Is eslint slow because of the parser? I know parsing in javsacript is very slow, but optimizations have only meaning if it was the bottleneck.

Note: It can be a huge difference if eslint custom parser api supports asynchronous parser. If it's the case, performance will be improved a lot even if the bottleneck wasn't the parser.

kdy1 commented 3 years ago

I started a discussion on eslint about asynchronous parser api. https://github.com/eslint/eslint/discussions/14733

pbadenski commented 3 years ago

It takes over 90 seconds to lint our project on my Mac. Here's a screenshot of a CPU profile (Top-down view) - hope it helps.

Screenshot 2021-06-22 at 15 40 38

FYI The missing 20% is GC.

kdy1 commented 3 years ago

It may worth then, but I expect conversion of swc ast -> babel ast -> espree ast to be slow. Need some profiling I guess.

Extroonie commented 2 years ago

Is there some insight on this? Is there a working solution that could make ESLint work with SWC yet?

kdy1 commented 2 years ago

@extroonie swc has code to produce ESTree AST/ acorn AST, but it's not exposed to js world currently. It will be nice if you can patch it to expose it.

Extroonie commented 2 years ago

@kdy1 sorry but I don't quite get that. What do you mean by patching it? Could you walk me through it please?

kdy1 commented 2 years ago

@extroonie I mean adding a method that is callable from js world, like https://github.com/swc-project/swc/blob/a4e93eb2dff0614e7a39c4301c84654412cf68dc/crates/node/src/transform.rs#L140-L157

Extroonie commented 2 years ago

@kdy1 thanks. Just want to double-check, the problem I'm having with ESLint and SWC is that some of the code with decorators and other ECMAScript proposals are throwing parser errors with ESLint. Now, I'm not sure whether SWC or ESLint has to deal with it.

kdy1 commented 2 years ago

You need to patch both

manuschillerdev commented 2 years ago

@extroonie swc has code to produce ESTree AST/ acorn AST, but it's not exposed to js world currently. It will be nice if you can patch it to expose it.

@kdy1 I'd love to give it a shot!

Which methods of swc_estree_compat would you expose for that?

kdy1 commented 2 years ago

@manuschillerdev There's babelify()

SrShark commented 2 years ago

In my case, I have just deleted the attribute "parser": "@babel/eslint-parser" in the .eslintrc config fie.

Then I set up the following config in vscode.

  "editor.defaultFormatter": "dbaeumer.vscode-eslint",
  "[javascript]": {
    "editor.defaultFormatter": "dbaeumer.vscode-eslint"
  },
  "[javascriptreact]": {
    "editor.defaultFormatter": "dbaeumer.vscode-eslint"
  }

It works well for me.

doox911-opensource commented 1 year ago

How are things going?

matheusmichels commented 3 months ago

@Extroonie swc has code to produce ESTree AST/ acorn AST, but it's not exposed to js world currently. It will be nice if you can patch it to expose it.

Hey everyone! Is anyone still looking into this? I see the issue has been stale for a while and was wondering if we already have it exposed in JS? Really excited about the idea of moving off of @typescript-eslint/parser (as this is the main bottleneck of my current eslint setup) to SWC!

cc @kdy1 👀

kdy1 commented 3 months ago

Nope. The linter should be written in Rust to make it performant enough.