Skip to content

Commit

Permalink
feat: rename experimental-utils to utils and make `experimental-u…
Browse files Browse the repository at this point in the history
…tils` an alias to the new package (#4172)
  • Loading branch information
Josh Goldberg committed Jan 12, 2022
1 parent e61e388 commit 1d55a75
Show file tree
Hide file tree
Showing 287 changed files with 541 additions and 718 deletions.
20 changes: 10 additions & 10 deletions .github/ISSUE_TEMPLATE.md
Expand Up @@ -67,13 +67,13 @@ i.e. eslint --ext ".ts,.js" src --debug

**Versions**

| package | version |
| --------------------------------------- | ------- |
| `@typescript-eslint/eslint-plugin` | `X.Y.Z` |
| `@typescript-eslint/parser` | `X.Y.Z` |
| `@typescript-eslint/typescript-estree` | `X.Y.Z` |
| `@typescript-eslint/experimental-utils` | `X.Y.Z` |
| `@typescript-eslint/type-utils` | `X.Y.Z` |
| `TypeScript` | `X.Y.Z` |
| `node` | `X.Y.Z` |
| `npm` | `X.Y.Z` |
| package | version |
| -------------------------------------- | ------- |
| `@typescript-eslint/eslint-plugin` | `X.Y.Z` |
| `@typescript-eslint/parser` | `X.Y.Z` |
| `@typescript-eslint/typescript-estree` | `X.Y.Z` |
| `@typescript-eslint/type-utils` | `X.Y.Z` |
| `@typescript-eslint/utils` | `X.Y.Z` |
| `TypeScript` | `X.Y.Z` |
| `node` | `X.Y.Z` |
| `npm` | `X.Y.Z` |
25 changes: 12 additions & 13 deletions .github/ISSUE_TEMPLATE/typescript-eslint-utils.md
@@ -1,9 +1,8 @@
---
name: '@typescript-eslint/experimental-utils'
about: Report an issue with the '@typescript-eslint/experimental-utils' package
title: ''
labels: 'package: utils, triage'
assignees: ''
name: '@typescript-eslint/utils'
about: Report an issue with the 'typescript-eslint/utils' package
title: '': 'package: utils, triage'
: ''
---

<!--
Expand All @@ -16,10 +15,10 @@ The more relevant information you can include, the faster we can find the issue
<!--
🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨
This issue template is only for problems specifically with the `@typescript-eslint/experimental-utils` package.
This issue template is only for problems specifically with the `@typescript-eslint/utils` package.
If you have a problem with a specific lint rule, please back out and select the `@typescript-eslint/eslint-plugin` template.
If you have a problem with the parser, please back out and select the `@typescript-eslint/parser` template.
you have a problem with a specific lint rule, please back out and select the `@typescript-eslint/eslint-plugin` template.
you have a problem with the parser, please back out and select the `@typescript-eslint/parser` template.
-->

- [ ] I have tried restarting my IDE and the issue persists.
Expand Down Expand Up @@ -64,8 +63,8 @@ i.e. eslint --ext ".ts,.js" src --debug

**Versions**

| package | version |
| --------------------------------------- | ------- |
| `@typescript-eslint/experimental-utils` | `X.Y.Z` |
| `TypeScript` | `X.Y.Z` |
| `node` | `X.Y.Z` |
| package | version |
| ---------------------------| ------- |
| `@typescript-eslint/utils` | `X.Y.Z` |
| `TypeScript` | `X.Y.Z` |
| `node` | `X.Y.Z` |
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Expand Up @@ -111,8 +111,8 @@ jobs:
env:
CI: true

- name: Run unit tests for experimental-utils
run: npx nx test @typescript-eslint/experimental-utils
- name: Run unit tests for utils
run: npx nx test @typescript-eslint/utils
env:
CI: true

Expand Down Expand Up @@ -283,8 +283,8 @@ jobs:
env:
CI: true

- name: Run unit tests for experimental-utils
run: npx nx test @typescript-eslint/experimental-utils
- name: Run unit tests for utils
run: npx nx test @typescript-eslint/utils
env:
CI: true

Expand Down
60 changes: 30 additions & 30 deletions .vscode/launch.json
Expand Up @@ -20,10 +20,10 @@
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"skipFiles": [
"${workspaceFolder}/packages/experimental-utils/src/index.ts",
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
"${workspaceFolder}/packages/utils/src/index.ts",
"${workspaceFolder}/packages/utils/dist/index.js",
"${workspaceFolder}/packages/utils/src/ts-estree.ts",
"${workspaceFolder}/packages/utils/dist/ts-estree.js",
"${workspaceFolder}/packages/type-utils/src/index.ts",
"${workspaceFolder}/packages/type-utils/dist/index.js",
"${workspaceFolder}/packages/parser/src/index.ts",
Expand Down Expand Up @@ -54,10 +54,10 @@
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"skipFiles": [
"${workspaceFolder}/packages/experimental-utils/src/index.ts",
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
"${workspaceFolder}/packages/utils/src/index.ts",
"${workspaceFolder}/packages/utils/dist/index.js",
"${workspaceFolder}/packages/utils/src/ts-estree.ts",
"${workspaceFolder}/packages/utils/dist/ts-estree.js",
"${workspaceFolder}/packages/type-utils/src/index.ts",
"${workspaceFolder}/packages/type-utils/dist/index.js",
"${workspaceFolder}/packages/parser/src/index.ts",
Expand Down Expand Up @@ -88,10 +88,10 @@
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"skipFiles": [
"${workspaceFolder}/packages/experimental-utils/src/index.ts",
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
"${workspaceFolder}/packages/utils/src/index.ts",
"${workspaceFolder}/packages/utils/dist/index.js",
"${workspaceFolder}/packages/utils/src/ts-estree.ts",
"${workspaceFolder}/packages/utils/dist/ts-estree.js",
"${workspaceFolder}/packages/type-utils/src/ts-estree.ts",
"${workspaceFolder}/packages/type-utils/dist/ts-estree.js",
"${workspaceFolder}/packages/parser/src/index.ts",
Expand Down Expand Up @@ -122,10 +122,10 @@
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"skipFiles": [
"${workspaceFolder}/packages/experimental-utils/src/index.ts",
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
"${workspaceFolder}/packages/utils/src/index.ts",
"${workspaceFolder}/packages/utils/dist/index.js",
"${workspaceFolder}/packages/utils/src/ts-estree.ts",
"${workspaceFolder}/packages/utils/dist/ts-estree.js",
"${workspaceFolder}/packages/type-utils/src/index.ts",
"${workspaceFolder}/packages/type-utils/dist/index.js",
"${workspaceFolder}/packages/parser/src/index.ts",
Expand All @@ -143,8 +143,8 @@
{
"type": "node",
"request": "launch",
"name": "Run currently opened experimental-utils test",
"cwd": "${workspaceFolder}/packages/experimental-utils/",
"name": "Run currently opened utils test",
"cwd": "${workspaceFolder}/packages/utils/",
"program": "${workspaceFolder}/node_modules/jest/bin/jest.js",
"args": [
"--runInBand",
Expand All @@ -156,10 +156,10 @@
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"skipFiles": [
"${workspaceFolder}/packages/experimental-utils/src/index.ts",
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
"${workspaceFolder}/packages/utils/src/index.ts",
"${workspaceFolder}/packages/utils/dist/index.js",
"${workspaceFolder}/packages/utils/src/ts-estree.ts",
"${workspaceFolder}/packages/utils/dist/ts-estree.js",
"${workspaceFolder}/packages/type-utils/src/index.ts",
"${workspaceFolder}/packages/type-utils/dist/index.js",
"${workspaceFolder}/packages/parser/src/index.ts",
Expand Down Expand Up @@ -190,10 +190,10 @@
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"skipFiles": [
"${workspaceFolder}/packages/experimental-utils/src/index.ts",
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
"${workspaceFolder}/packages/utils/src/index.ts",
"${workspaceFolder}/packages/utils/dist/index.js",
"${workspaceFolder}/packages/utils/src/ts-estree.ts",
"${workspaceFolder}/packages/utils/dist/ts-estree.js",
"${workspaceFolder}/packages/type-utils/src/index.ts",
"${workspaceFolder}/packages/type-utils/dist/index.js",
"${workspaceFolder}/packages/parser/src/index.ts",
Expand Down Expand Up @@ -224,10 +224,10 @@
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"skipFiles": [
"${workspaceFolder}/packages/experimental-utils/src/index.ts",
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
"${workspaceFolder}/packages/utils/src/index.ts",
"${workspaceFolder}/packages/utils/dist/index.js",
"${workspaceFolder}/packages/utils/src/ts-estree.ts",
"${workspaceFolder}/packages/utils/dist/ts-estree.js",
"${workspaceFolder}/packages/type-utils/src/index.ts",
"${workspaceFolder}/packages/type-utils/dist/index.js",
"${workspaceFolder}/packages/parser/src/index.ts",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -27,7 +27,7 @@ See https://typescript-eslint.io/docs/development/architecture/packages for more
- [`@typescript-eslint/eslint-plugin`](./packages/eslint-plugin)
- [`@typescript-eslint/parser`](./packages/parser)
- [`@typescript-eslint/eslint-plugin-tslint`](./packages/eslint-plugin-tslint)
- [`@typescript-eslint/experimental-utils`](./packages/experimental-utils)
- [`@typescript-eslint/utils`](./packages/utils)
- [`@typescript-eslint/typescript-estree`](./packages/typescript-estree)
- [`@typescript-eslint/scope-manager`](./packages/scope-manager)

Expand Down
38 changes: 16 additions & 22 deletions docs/development/CUSTOM_RULES.md
Expand Up @@ -11,13 +11,13 @@ You should be familiar with [ESLint's developer guide](https://eslint.org/docs/d
As long as you are using `@typescript-eslint/parser` as the `parser` in your ESLint configuration, custom ESLint rules generally work the same way for JavaScript and TypeScript code.
The main three changes to custom rules writing are:

- [Utils Package](#utils-package): we recommend using `@typescript-eslint/experimental-utils` to create custom rules
- [Utils Package](#utils-package): we recommend using `@typescript-eslint/utils` to create custom rules
- [AST Extensions](#ast-extensions): targeting TypeScript-specific syntax in your rule selectors
- [Typed Rules](#typed-rules): using the TypeScript type checker to inform rule logic

## Utils Package

The `@typescript-eslint/experimental-utils` package acts as a replacement package for `eslint` that exports all the same objects and types, but with typescript-eslint support.
The `@typescript-eslint/utils` package acts as a replacement package for `eslint` that exports all the same objects and types, but with typescript-eslint support.
It also exports common utility functions and constants most custom typescript-eslint rules tend to use.

:::caution
Expand All @@ -27,15 +27,15 @@ You should generally not need to import from `eslint` when writing custom typesc

### `RuleCreator`

The recommended way to create custom ESLint rules that make use of typescript-eslint features and/or syntax is with the `ESLintUtils.RuleCreator` function exported by `@typescript-eslint/experimental-utils`.
The recommended way to create custom ESLint rules that make use of typescript-eslint features and/or syntax is with the `ESLintUtils.RuleCreator` function exported by `@typescript-eslint/utils`.

It takes in a function that transforms a rule name into its documentation URL, then returns a function that takes in a rule module object.
`RuleCreator` will infer the allowed message IDs the rule is allowed to emit from the provided `meta.messages` object.

This rule bans function declarations that start with a lower-case letter:

```ts
import { ESLintUtils } from '@typescript-eslint/experimental-utils';
import { ESLintUtils } from '@typescript-eslint/utils';

const createRule = ESLintUtils.RuleCreator(
name => `https://example.com/rule/${name}`,
Expand Down Expand Up @@ -70,7 +70,7 @@ export const rule = createRule({
});
```

`RuleCreator` rule creator functions return rules typed as the `RuleModule` interface exported by `@typescript-eslint/experimental-utils`.
`RuleCreator` rule creator functions return rules typed as the `RuleModule` interface exported by `@typescript-eslint/utils`.
It allows specifying generics for:

- `MessageIds`: a union of string literal message IDs that may be reported
Expand All @@ -79,7 +79,7 @@ It allows specifying generics for:
If the rule is able to take in rule options, declare them as a tuple type containing a single object of rule options:

```ts
import { ESLintUtils } from '@typescript-eslint/experimental-utils';
import { ESLintUtils } from '@typescript-eslint/utils';

type MessageIds = 'lowercase' | 'uppercase';

Expand All @@ -101,7 +101,7 @@ Although it is generally not recommended to create custom rules without document
It applies the same type inference as the `createRule`s above without enforcing a documentation URL.

```ts
import { ESLintUtils } from '@typescript-eslint/experimental-utils';
import { ESLintUtils } from '@typescript-eslint/utils';

export const rule = ESLintUtils.RuleCreator.withoutDocs({
create(context) {
Expand All @@ -126,7 +126,7 @@ You can query for them in your rule selectors.
This version of the above rule instead bans interface declaration names that start with a lower-case letter:

```ts
import { ESLintUtils } from '@typescript-eslint/experimental-utils';
import { ESLintUtils } from '@typescript-eslint/utils';

export const rule = createRule({
create(context) {
Expand All @@ -144,7 +144,7 @@ export const rule = createRule({

### Node Types

TypeScript types for nodes exist in a `TSESTree` namespace exported by `@typescript-eslint/experimental-utils`.
TypeScript types for nodes exist in a `TSESTree` namespace exported by `@typescript-eslint/utils`.
The above rule body could be better written in TypeScript with a type annotation on the `node`:

An `AST_NODE_TYPES` enum is exported as well to hold the values for AST node `type` properties.
Expand All @@ -153,10 +153,7 @@ An `AST_NODE_TYPES` enum is exported as well to hold the values for AST node `ty
For example, checking `node.type` can narrow down the type of the `node`:

```ts
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';

export function describeNode(node: TSESTree.Node): string {
switch (node.type) {
Expand All @@ -180,10 +177,7 @@ In that case, it is best to add an explicit type declaration.
This rule snippet targets name nodes of both function and interface declarations:

```ts
import {
AST_NODE_TYPES,
ESLintUtils,
} from '@typescript-eslint/experimental-utils';
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils';

export const rule = createRule({
create(context) {
Expand Down Expand Up @@ -211,7 +205,7 @@ Read TypeScript's [Compiler APIs > Using the Type Checker](https://github.com/mi

The biggest addition typescript-eslint brings to ESLint rules is the ability to use TypeScript's type checker APIs.

`@typescript-eslint/experimental-utils` exports an `ESLintUtils` namespace containing a `getParserServices` function that takes in an ESLint context and returns a `parserServices` object.
`@typescript-eslint/utils` exports an `ESLintUtils` namespace containing a `getParserServices` function that takes in an ESLint context and returns a `parserServices` object.

That `parserServices` object contains:

Expand All @@ -224,7 +218,7 @@ By mapping from ESTree nodes to TypeScript nodes and retrieving the TypeScript p
This rule bans for-of looping over an enum by using the type-checker via typescript-eslint and TypeScript APIs:

```ts
import { ESLintUtils } from '@typescript-eslint/experimental-utils';
import { ESLintUtils } from '@typescript-eslint/utils';
import * as ts from 'typescript';
import * as tsutils from 'tsutils';

Expand Down Expand Up @@ -268,15 +262,15 @@ export const rule: eslint.Rule.RuleModule = {

## Testing

`@typescript-eslint/experimental-utils` exports a `RuleTester` with a similar API to the built-in [ESLint `RuleTester`](https://eslint.org/docs/developer-guide/nodejs-api#ruletester).
`@typescript-eslint/utils` exports a `RuleTester` with a similar API to the built-in [ESLint `RuleTester`](https://eslint.org/docs/developer-guide/nodejs-api#ruletester).
It should be provided with the same `parser` and `parserOptions` you would use in your ESLint configuration.

### Testing Untyped Rules

For rules that don't need type information, passing just the `parser` will do:

```ts
import { ESLintUtils } from '@typescript-eslint/experimental-utils';
import { ESLintUtils } from '@typescript-eslint/utils';
import rule from './my-rule';

const ruleTester = new ESLintUtils.RuleTester({
Expand All @@ -295,7 +289,7 @@ For rules that do need type information, `parserOptions` must be passed in as we
Tests must have at least an absolute `tsconfigRootDir` path provided as well as a relative `project` path from that directory:

```ts
import { ESLintUtils } from '@typescript-eslint/experimental-utils';
import { ESLintUtils } from '@typescript-eslint/utils';
import rule from './my-typed-rule';

const ruleTester = new ESLintUtils.RuleTester({
Expand Down
6 changes: 3 additions & 3 deletions docs/development/architecture/PACKAGES.md
Expand Up @@ -55,9 +55,9 @@ It works by:
A "scope analyser" traverses an AST and builds a model of how variables (and in our case, types) are defined and consumed by the source code.
This form of static analysis allows you to understand and trace variables throughout the program, allowing you to access powerful information about a program without needing to drop into the much, much heavier type information.

## `@typescript-eslint/experimental-utils`
## `@typescript-eslint/utils`

[`@typescript-eslint/experimental-utils`] contains public utilities for writing custom rules and plugins in TypeScript.
[`@typescript-eslint/utils`] contains public utilities for writing custom rules and plugins in TypeScript.
Rules declared in `@typescript-eslint/eslint-plugin` are created using its utility functions.
Any custom rules you write generally will be as well.

Expand All @@ -71,7 +71,7 @@ Any custom rules you write generally will be as well.

[`@typescript-eslint/eslint-plugin-tslint`]: https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/eslint-plugin-tslint
[`@typescript-eslint/eslint-plugin`]: https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/eslint-plugin
[`@typescript-eslint/experimental-utils`]: https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/experimental-utils
[`@typescript-eslint/utils`]: https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/utils
[`@typescript-eslint/parser`]: https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/parser
[`@typescript-eslint/scope-manager`]: https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/scope-manager
[`@typescript-eslint/typescript-estree`]: https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/typescript-estree
Expand Down

0 comments on commit 1d55a75

Please sign in to comment.