🌐 AI搜索 & 代理 主页
Skip to content

Repo: Write lint rule to prefer tsutils methods over bitwise checks #11625

@JoshuaKGoldberg

Description

@JoshuaKGoldberg

Suggestion

Coming over from #11617: it's not well-known that ts-api-utils provides a handful of shortcut/wrapper methods around TypeScript bitwise enums/flags. For example, this method:

function hasBaseTypes(type: ts.Type): type is ts.InterfaceType {
  return (
    (type.flags & ts.TypeFlags.Object) !== 0 &&
    (((type as ts.ObjectType).objectFlags & ts.ObjectFlags.Interface) !==
      0 ||
      ((type as ts.ObjectType).objectFlags & ts.ObjectFlags.Class) !== 0)
  );
}

...can be rewritten as:

function hasBaseTypes(type: ts.Type): type is ts.InterfaceType {
  return (
    tsutils.isObjectType(type) &&
    tsutils.isObjectFlagSet(
      type,
      ts.ObjectFlags.Interface | ts.ObjectFlags.Class,
    )
  );
}

Proposal: let's write a general-purpose lint rule that enforces the latter when possible?

Additional Info

Eventually this might make sense to move to https://github.com/eslint-community/eslint-plugin-eslint-plugin, perhaps.

💖

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuerepo maintenancethings to do with maintenance of the repo, and not with code/docs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions