mysticatea / eslint4b

ESLint which works in browsers.
MIT License
37 stars 19 forks source link

Support for ES6 Import and Typescript Definition File #3

Closed eyedean closed 4 years ago

eyedean commented 4 years ago

Thanks for making linting possible in the browser!

In my typescript project I am using ES6 style import, and it's very challenging for me to get eslint4b to work.

Ideally I want to have:

import { Linter } from "eslint4b"; 

...
this.linter = new Linter();
this.linter.verify(...);
...

But apparently import("eslint4b") is returning a promise and it messes up all the Type Definitions I manually copy/pasted from the original eslint's .d.ts file.

Thanks. :)

PS. How can I get other classes like SourceCode?

mysticatea commented 4 years ago

Thank you for this issue.

ES2020 import() expression returns a promise by the language design. You can use it with await.

async function load() {
    const { Linter } = await import("eslint4b")
    const linter = new Linter();
    linter.verify(...);
}

I have a fence to add type definitions into this package for now...


PS. How can I get other classes like SourceCode?

You can get source code instance by linter.getSourceCode() method after linter.verify(). But this package doesn't include the other classes (CLIEngine, RuleTester).

I'd like to recommend to avoid the use of SourceCode constructor. To instantiate SourceCode instance correctly requires several additional steps and ESLint doesn't explain/provide it. The only sane way to instantiate SourceCode is linter.verify(code, config, options); linter.getSourceCode().

eyedean commented 4 years ago

Thanks for quick response Tory.

When I am using direct await import("eslint4b"), my Typescript compiler complains about not having the types available. The workaround I have come up is getting the types from the "eslint" package (which is in fact @types/eslint), and converting what I get from "eslint4b" into that. i.e.

import * as Eslint from "eslint";

const Linter4b = require("eslint4b");

const linter = new Linter4b() as Eslint.Linter;
linter.verify(...)

I'm working to add types into ESLint core (eslint/eslint#12082), but it's in progress.

Adding type definitions into the package would be wonderful! thanks for having that on the roadmap!

In fact, I am just stepping into the realm of espree directly and it seems like it doesn't even have types in "@types" and supporting types wasn't on the roadmap at least in 2017. :-/ It's good to know that maybe sometime soon it will come there too!


I'd like to recommend to avoid the use of SourceCode constructor... The only sane way ...

Ah, that's good to know.

Thanks again for your support!