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

Converted npm-naming from TSLint to ESLint #681

Merged
merged 19 commits into from Dec 12, 2023

Conversation

JoshuaKGoldberg
Copy link
Contributor

@JoshuaKGoldberg JoshuaKGoldberg commented May 21, 2023

Continues #648 by migrating npm-naming from TSLint to ESLint.

...mostly. This removes the dependency on tslint but still operates on the TS tree instead of TSESTree tree. I ran out of energy overhauling these more TypeScript-intertwined rules... is this ok as an intermediary step? I ended up overhauling this to go all the way to using just ESLint/ESTree. ✔️

Also updates the dts-critic script's hardcoded naming rule modifiers to operate on .eslintrc.json instead of tslint.json.

Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the drawbacks of using the entire TS AST? Is it slower? Harder to maintain? I think of npm-naming as mostly delegating to dts-critic, but there is more code than I thought here.

packages/dtslint/src/rules/npm-naming.ts Outdated Show resolved Hide resolved
packages/dtslint/src/rules/npm-naming.ts Outdated Show resolved Hide resolved
@JoshuaKGoldberg JoshuaKGoldberg changed the title Converted npm-naming from TSLint to ESLint, ish Converted npm-naming from TSLint to ESLint Dec 8, 2023
let parent = path.dirname(fileName);
// May be a directory for an older version, e.g. `v0`.
// Note a types redirect `foo/ts3.1` should not have its own header.
if (allowNested && /^v(0\.)?\d+$/.test(path.basename(parent))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe not for this PR, but I suspect that it will be easier to just use the new util I have above which gives the package dir for any file within the workspace (or undefined if not). Then we don't need to use regexes, allowNested, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite confident enough in this repo to do that myself, but I do like the idea. 🙂

@jakebailey
Copy link
Member

One thing we'll need to do here is filter it out of the preset; after #843, what we'll do is have it disabled by default in the preset, then override it on when running eslint. That way, the editor doesn't run it but dtslint does (just like how I override ExpectType with the list of TS versions to test).


exports[`plugin should have the expected exports 1`] = `
{
"configs": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a new snapshot I added to the PR to baseline our exported plugin; you'll see in the commit that filtered out npm-naming that it was removed (but the rule remained as a rule).

@jakebailey
Copy link
Member

This PR should now be cleaned up and ready for review/merge (plus a change in DT itself to move the linting over).

@jakebailey
Copy link
Member

Running locally, I get:

4> alpha-shape failing:
4> TypeError: Error while loading rule '@definitelytyped/npm-naming': versions.forEach is not a function
4> Occurred while linting /home/jabaile/work/DefinitelyTyped/types/alpha-shape/index.d.ts
4>     at Object.maxSatisfying (/home/jabaile/work/DefinitelyTyped-tools/node_modules/.pnpm/semver@7.5.4/node_modules/semver/ranges/max-satisfying.js:13:12)
4>     at getMatchingVersion (/home/jabaile/work/DefinitelyTyped-tools/packages/dts-critic/dist/index.js:273:40)
4>     at checkNpm (/home/jabaile/work/DefinitelyTyped-tools/packages/dts-critic/dist/index.js:247:24)
4>     at dtsCritic (/home/jabaile/work/DefinitelyTyped-tools/packages/dts-critic/dist/index.js:110:28)
4>     at create (/home/jabaile/work/DefinitelyTyped-tools/packages/eslint-plugin/dist/rules/npm-naming.js:120:56)
4>     at Object.create (/home/jabaile/work/DefinitelyTyped-tools/node_modules/.pnpm/@typescript-eslint+utils@6.11.0_eslint@8.53.0_typescript@5.2.2/node_modules/@typescript-eslint/utils/dist/eslint-utils/RuleCreator.js:38:20)
4>     at createRuleListeners (/home/jabaile/work/DefinitelyTyped-tools/node_modules/.pnpm/eslint@8.53.0/node_modules/eslint/lib/linter/linter.js:910:21)
4>     at /home/jabaile/work/DefinitelyTyped-tools/node_modules/.pnpm/eslint@8.53.0/node_modules/eslint/lib/linter/linter.js:1081:110
4>     at Array.forEach (<anonymous>)
4>     at runRules (/home/jabaile/work/DefinitelyTyped-tools/node_modules/.pnpm/eslint@8.53.0/node_modules/eslint/lib/linter/linter.js:1018:34)

@jakebailey
Copy link
Member

Oh npm

$ npm info alpha-shape --json --silent versions dist-tags
{
  "versions": "1.0.0",
  "dist-tags": {
    "latest": "1.0.0"
  }
}

@@ -27,8 +27,8 @@ export async function lint(
// TODO: To remove tslint, replace this with a ts.createProgram (probably)
const lintProgram = Linter.createProgram(tsconfigPath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only use this program to build a list of files; not for this PR, but we're going to have to remove this when we remove tslint so I expect that we will just ask eslint to run on all non-ignored files (which is a lot better than having to find a list of all files here).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That being said, since we run type-using rules and also use project=true, it's likely that people will have random files that aren't in the program that may break via ts-eslint complaining about not being in a program, or via new lints that were previously uncaught.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cost reduction: Linter.createProgram is a standalone public static, so an equivalent could always be re-written.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, yeah, though it seems a bit overkill to build a Program to only use it to list files!

@jakebailey jakebailey merged commit 414ae48 into microsoft:master Dec 12, 2023
6 checks passed
@JoshuaKGoldberg JoshuaKGoldberg deleted the npm-naming branch December 12, 2023 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants