Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to use swc with / for eslint? #246

Open
rvolgers opened this issue Feb 19, 2019 · 25 comments
Open

Possible to use swc with / for eslint? #246

rvolgers opened this issue Feb 19, 2019 · 25 comments
Labels

Comments

@rvolgers
Copy link

rvolgers commented Feb 19, 2019

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
Copy link
Member

kdy1 commented Feb 20, 2019

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

@ForsakenHarmony
Copy link
Contributor

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

@kdy1
Copy link
Member

kdy1 commented Jun 14, 2019

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

@kapouer
Copy link

kapouer commented Dec 3, 2019

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.

@kdy1 kdy1 added the E-hard label Mar 30, 2020
@pbadenski
Copy link

@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 (#246 (comment)) 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
Copy link
Member

kdy1 commented Mar 31, 2021

It still requires lots of work

@axyz
Copy link

axyz commented Jun 21, 2021

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

as #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
Copy link
Member

kdy1 commented Jun 22, 2021

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

@axyz
Copy link

axyz commented Jun 22, 2021

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
Copy link
Member

kdy1 commented Jun 22, 2021

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
Copy link
Member

kdy1 commented Jun 22, 2021

I started a discussion on eslint about asynchronous parser api.
eslint/eslint#14733

@pbadenski
Copy link

pbadenski commented Jun 22, 2021

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
Copy link
Member

kdy1 commented Jun 22, 2021

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

@Extroonie
Copy link

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

@kdy1
Copy link
Member

kdy1 commented Jan 13, 2022

@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
Copy link

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

@kdy1
Copy link
Member

kdy1 commented Jan 15, 2022

@Extroonie I mean adding a method that is callable from js world, like

#[napi]
pub fn transform_file(
src: String,
_is_module: bool,
options: Buffer,
signal: Option<AbortSignal>,
) -> napi::Result<AsyncTask<TransformTask>> {
let c = get_compiler();
let options = String::from_utf8_lossy(options.as_ref()).to_string();
let path = clean(&src);
let task = TransformTask {
c,
input: Input::File(path.into()),
options,
};
Ok(AsyncTask::with_optional_signal(task, signal))
}

@Extroonie
Copy link

@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
Copy link
Member

kdy1 commented Jan 15, 2022

You need to patch both

@manuschillerdev
Copy link

@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
Copy link
Member

kdy1 commented Mar 12, 2022

@manuschillerdev There's babelify()

@SrShark
Copy link

SrShark commented Jul 7, 2022

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.

marckassay added a commit to marckassay/MyList that referenced this issue Aug 17, 2022
- Due to swc/jest not supporting lint at this time

- See: swc-project/swc#246
@doox911-opensource

This comment was marked as spam.

@matheusmichels
Copy link

matheusmichels commented Jul 1, 2024

@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
Copy link
Member

kdy1 commented Jul 3, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests