Skip to content

Commit 96512a6

Browse files
nzakasfasttime
andauthored
fix!: Remove deprecated rule context methods (#20086)
* fix!: Remove deprecated rule context methods closes #16999 * Update tools/internal-rules/multiline-comment-style.js Co-authored-by: Francesco Trotta <github@fasttime.org> * Remove docs for deprecated methods * Update docs * Update docs * Add issue to migration guide --------- Co-authored-by: Francesco Trotta <github@fasttime.org>
1 parent 4b5dbcd commit 96512a6

14 files changed

Lines changed: 76 additions & 368 deletions

File tree

docs/src/extend/custom-rules.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ The source file for a rule exports an object with the following properties. Both
6565

6666
- `defaultOptions`: (`array`) Specifies [default options](#option-defaults) for the rule. If present, any user-provided options in their config will be merged on top of them recursively.
6767

68-
- `deprecated`: (`boolean | DeprecatedInfo`) Indicates whether the rule has been deprecated. You may omit the `deprecated` property if the rule has not been deprecated.
68+
- `deprecated`: (`boolean | DeprecatedInfo`) Indicates whether the rule has been deprecated. You may omit the `deprecated` property if the rule has not been deprecated.
6969
There is a dedicated page for the [DeprecatedInfo](./rule-deprecation)
7070

7171
- `replacedBy`: (`array`, **Deprecated** Use `meta.deprecated.replacedBy` instead.) In the case of a deprecated rule, specify replacement rule(s).
@@ -144,15 +144,9 @@ The `context` object has the following properties:
144144
- `parser`: (`object`): The parser used to parse the current file.
145145
- `parserOptions`: (`object`) The parser options configured for this file.
146146
- `globals`: (`object`) The specified globals.
147-
- `parserPath`: (`string`, **Removed** Use `context.languageOptions.parser` instead.) The name of the `parser` from the configuration.
148-
- `parserOptions`: (**Deprecated** Use `context.languageOptions.parserOptions` instead.) The parser options configured for this run (more details [here](../use/configure/language-options#specifying-parser-options)).
149147

150148
Additionally, the `context` object has the following methods:
151149

152-
- `getCwd()`: (**Deprecated:** Use `context.cwd` instead.) Returns the `cwd` option passed to the [Linter](../integrate/nodejs-api#linter). It is a path to a directory that should be considered the current working directory.
153-
- `getFilename()`: (**Deprecated:** Use `context.filename` instead.) Returns the filename associated with the source.
154-
- `getPhysicalFilename()`: (**Deprecated:** Use `context.physicalFilename` instead.) When linting a file, it returns the full path of the file on disk without any code block information. When linting text, it returns the value passed to `—stdin-filename` or `<text>` if not specified.
155-
- `getSourceCode()`: (**Deprecated:** Use `context.sourceCode` instead.) Returns a `SourceCode` object that you can use to work with the source that was passed to ESLint (see [Accessing the Source Code](#accessing-the-source-code)).
156150
- `report(descriptor)`. Reports a problem in the code (see the [dedicated section](#reporting-problems)).
157151

158152
**Note:** Earlier versions of ESLint supported additional methods on the `context` object. Those methods were removed in the new format and should not be relied upon.
@@ -533,8 +527,6 @@ module.exports = {
533527
};
534528
```
535529

536-
**Deprecated:** The `context.getSourceCode()` method is deprecated; make sure to use `context.sourceCode` property instead.
537-
538530
Once you have an instance of `SourceCode`, you can use the following methods on it to work with the code:
539531

540532
- `getText(node)`: Returns the source code for the given node. Omit `node` to get the whole source (see the [dedicated section](#accessing-the-source-text)).
@@ -890,7 +882,7 @@ The following table contains a list of AST node types and the scope type that th
890882
| `CatchClause` | `catch` |
891883
| others | ※3 ※4 |
892884

893-
**※1** Only if the configured parser provided the block-scope feature. The default parser provides the block-scope feature if `parserOptions.ecmaVersion` is not less than `6`.<br>
885+
**※1** Only if the configured parser provided the block-scope feature. The default parser provides the block-scope feature if `languageOptions.ecmaVersion` is not less than `6`.<br>
894886
**※2** Only if the `for` statement defines the iteration variable as a block-scoped variable (E.g., `for (let i = 0;;) {}`).<br>
895887
**※3** The scope of the closest ancestor node which has own scope. If the closest ancestor node has multiple scopes then it chooses the innermost scope (E.g., the `Program` node has a `global` scope and a `module` scope if `Program#sourceType` is `"module"`. The innermost scope is the `module` scope.).<br>
896888
**※4** Each `PropertyDefinition#value` node (it can be any expression node type), has a `class-field-initializer` scope. For example, in `class C { field = 1 }`, the `Literal` node that represents `1` has a `class-field-initializer` scope. If the node has other scopes, the `class-field-initializer` scope will be the outermost one. For example, in `class C { field = () => {} }`, the `ArrowFunctionExpression` node has two scopes: `class-field-initializer` and `function`.

docs/src/integrate/nodejs-api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ The `Linter` object does the actual evaluation of the JavaScript code. It doesn'
641641
642642
The `Linter` is a constructor, and you can create a new instance by passing in the options you want to use. The available options are:
643643
644-
- `cwd` - Path to a directory that should be considered as the current working directory. It is accessible to rules from `context.cwd` or by calling `context.getCwd()` (see [The Context Object](../extend/custom-rules#the-context-object)). If `cwd` is `undefined`, it will be normalized to `process.cwd()` if the global `process` object is defined (for example, in the Node.js runtime) , or `undefined` otherwise.
644+
- `cwd` - Path to a directory that should be considered as the current working directory. It is accessible to rules from `context.cwd` (see [The Context Object](../extend/custom-rules#the-context-object)). If `cwd` is `undefined`, it will be normalized to `process.cwd()` if the global `process` object is defined (for example, in the Node.js runtime) , or `undefined` otherwise.
645645
646646
For example:
647647
@@ -651,7 +651,7 @@ const linter1 = new Linter({ cwd: "path/to/project" });
651651
const linter2 = new Linter();
652652
```
653653
654-
In this example, rules run on `linter1` will get `path/to/project` from `context.cwd` or when calling `context.getCwd()`.
654+
In this example, rules run on `linter1` will get `path/to/project` from `context.cwd`.
655655
Those run on `linter2` will get `process.cwd()` if the global `process` object is defined or `undefined` otherwise (e.g. on the browser <https://eslint.org/demo>).
656656
657657
### Linter#verify

docs/src/rules/no-implicit-globals.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ window.bar = function() {};
7777

7878
:::
7979

80-
Examples of **correct** code for this rule with `"parserOptions": { "sourceType": "module" }` in the ESLint configuration:
80+
Examples of **correct** code for this rule with `"languageOptions": { "sourceType": "module" }` in the ESLint configuration:
8181

8282
::: correct { "sourceType": "module" }
8383

docs/src/rules/no-invalid-this.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ And this rule always allows `this` keywords in the following contexts:
4545
Otherwise are considered problems.
4646

4747
This rule applies **only** in strict mode.
48-
With `"parserOptions": { "sourceType": "module" }` in the ESLint configuration, your code is in strict mode even without a `"use strict"` directive.
48+
With `"languageOptions": { "sourceType": "module" }` in the ESLint configuration, your code is in strict mode even without a `"use strict"` directive.
4949

5050
Examples of **incorrect** code for this rule in strict mode:
5151

docs/src/rules/rest-spread-spacing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ This rule aims to enforce consistent spacing between rest and spread operators a
4949

5050
```json
5151
{
52-
"parserOptions": {
52+
"languageOptions": {
5353
"ecmaVersion": 2018
5454
}
5555
}

docs/src/use/migrate-to-10.0.0.md

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ The lists below are ordered roughly by the number of users each change is expect
3434
- [`Program` AST node range spans entire source text](#program-node-range)
3535
- [Fixer methods now require string `text` arguments](#fixer-text-must-be-string)
3636
- [New requirements for `ScopeManager` implementations](#scope-manager)
37+
- [Removal of deprecated `context` members](#rule-context)
3738

3839
### Breaking changes for integration developers
3940

@@ -70,7 +71,7 @@ Three new rules have been enabled in `eslint:recommended`:
7071

7172
## <a name="config-lookup-from-file"></a> New configuration file lookup algorithm
7273

73-
In ESLint v9, the alternate config lookup behavior could be enabled with the `v10_config_lookup_from_file` feature flag. This behavior made ESLint locate `eslint.config.*` by starting from the directory of each linted file and searching up towards the filesystem root. In ESLint v10, this behavior is now the default and the `v10_config_lookup_from_file` flag has been removed. Attempting to use this flag will now result in an error.
74+
In ESLint v9, the alternate config lookup behavior could be enabled with the `v10_config_lookup_from_file` feature flag. This behavior made ESLint locate `eslint.config.*` by starting from the directory of each linted file and searching up towards the filesystem root. In ESLint v10.0.0, this behavior is now the default and the `v10_config_lookup_from_file` flag has been removed. Attempting to use this flag will now result in an error.
7475

7576
**To address:**
7677

@@ -134,7 +135,7 @@ The default behavior of this rule has not been changed.
134135

135136
## <a name="no-shadow-restricted-names"></a> `no-shadow-restricted-names` now reports `globalThis` by default
136137

137-
In ESLint v10, the [`no-shadow-restricted-names`](../rules/no-shadow-restricted-names) rule now treats `globalThis` as a restricted name by default. Consequently, the `reportGlobalThis` option now defaults to `true` (previously `false`). As a result, declarations such as `const globalThis = "foo";` or `function globalThis() {}` will now be reported by default.
138+
In ESLint v10.0.0, the [`no-shadow-restricted-names`](../rules/no-shadow-restricted-names) rule now treats `globalThis` as a restricted name by default. Consequently, the `reportGlobalThis` option now defaults to `true` (previously `false`). As a result, declarations such as `const globalThis = "foo";` or `function globalThis() {}` will now be reported by default.
138139

139140
**To address:**
140141

@@ -153,7 +154,7 @@ In ESLint v10, the [`no-shadow-restricted-names`](../rules/no-shadow-restricted-
153154

154155
## <a name="func-names"></a> `func-names` schema is stricter
155156

156-
In ESLint v10, the [`func-names`](../rules/func-names) rule schema now disallows extra items in the options array. Previously, configurations that included additional array elements beyond the allowed options were accepted but ignored. Such configurations are now considered invalid.
157+
In ESLint v10.0.0, the [`func-names`](../rules/func-names) rule schema now disallows extra items in the options array. Previously, configurations that included additional array elements beyond the allowed options were accepted but ignored. Such configurations are now considered invalid.
157158

158159
For example, this configuration is now invalid due to the extra element `"foo"`:
159160

@@ -171,7 +172,7 @@ For example, this configuration is now invalid due to the extra element `"foo"`:
171172

172173
## <a name="no-invalid-regexp"></a> `allowConstructorFlags` option of `no-invalid-regexp` now accepts only unique items
173174

174-
In ESLint v10, the `allowConstructorFlags` option of `no-invalid-regexp` no longer accepts duplicate flags as input. Previously, configurations with duplicate flags in the array were accepted but treated the same as having unique flags. Such configurations are now considered invalid and will result in a configuration error.
175+
In ESLint v10.0.0, the `allowConstructorFlags` option of `no-invalid-regexp` no longer accepts duplicate flags as input. Previously, configurations with duplicate flags in the array were accepted but treated the same as having unique flags. Such configurations are now considered invalid and will result in a configuration error.
175176

176177
For example, this configuration is now invalid due to the duplicate `"u"` flag:
177178

@@ -185,7 +186,7 @@ For example, this configuration is now invalid due to the duplicate `"u"` flag:
185186

186187
## <a name="ruletester-type-removed"></a> Removal of `type` property in errors of invalid `RuleTester` cases
187188

188-
In ESLint v10, the deprecated `type` property in errors of invalid test cases for rules has been removed. Using the `type` property in test cases now throws an error.
189+
In ESLint v10.0.0, the deprecated `type` property in errors of invalid test cases for rules has been removed. Using the `type` property in test cases now throws an error.
189190

190191
**To address:** Remove the `type` property from error objects in invalid test cases.
191192

@@ -216,7 +217,7 @@ Starting with ESLint v10, `Program.range` covers the entire source text, includi
216217

217218
## <a name="fixer-text-must-be-string"></a> Fixer methods now require string `text` arguments
218219

219-
In ESLint v10, all rule fixer methods that accept a `text` argument now require that it be a string. Providing a non-string value will throw a `TypeError`.
220+
In ESLint v10.0.0, all rule fixer methods that accept a `text` argument now require that it be a string. Providing a non-string value will throw a `TypeError`.
220221

221222
Affected methods:
222223

@@ -233,7 +234,7 @@ Affected methods:
233234

234235
## <a name="scope-manager"></a> New requirements for `ScopeManager` implementations
235236

236-
As of ESLint v10.0.0, custom `ScopeManager` implementations must automatically resolve references to global variables declared in the code, including `var` and `function` declarations, and provide an instance method `addGlobals(names: string[])` that creates variables with the given names in the global scope and resolves references to them.
237+
In ESLint v10.0.0, custom `ScopeManager` implementations must automatically resolve references to global variables declared in the code, including `var` and `function` declarations, and provide an instance method `addGlobals(names: string[])` that creates variables with the given names in the global scope and resolves references to them.
237238

238239
The default `ScopeManager` implementation [`eslint-scope`](https://www.npmjs.com/package/eslint-scope) has already been updated.
239240

@@ -243,9 +244,50 @@ This change does not affect custom rules.
243244

244245
**Related issue(s):** [eslint/js#665](https://github.com/eslint/js/issues/665)
245246

247+
## <a name="rule-context"></a> Removal of deprecated `context` members
248+
249+
In ESLint v9.x, we deprecated the following [methods](https://eslint.org/blog/2023/09/preparing-custom-rules-eslint-v9/#context-methods-becoming-properties) and [properties](https://eslint.org/blog/2023/09/preparing-custom-rules-eslint-v9/#context-properties%3A-parseroptions-and-parserpath-being-removed):
250+
251+
- `context.getCwd()`
252+
- `context.getFilename()`
253+
- `context.getPhysicalFilename()`
254+
- `context.getSourceCode()`
255+
- `context.parserOptions`
256+
- `context.parserPath`
257+
258+
In ESLint v10.0.0, all of these members have been removed.
259+
260+
**To address:** In your custom rules, make the following changes:
261+
262+
| **Removed on `context`** | **Replacement on `context`** |
263+
| ------------------------------- | -------------------------------------------------------------------- |
264+
| `context.getCwd()` | `context.cwd` |
265+
| `context.getFilename()` | `context.filename` |
266+
| `context.getPhysicalFilename()` | `context.physicalFilename` |
267+
| `context.getSourceCode()` | `context.sourceCode` |
268+
| `context.parserOptions` | `context.languageOptions` or `context.languageOptions.parserOptions` |
269+
| `context.parserPath` | No replacement. |
270+
271+
You can make changes for the removed `context` methods using the [`eslint-transforms`](https://www.npmjs.com/package/eslint-transforms) utility. To use the utility, first install it and then run the `v9-rule-migration` transform, like this:
272+
273+
```shell
274+
# install the utility
275+
npm install eslint-transforms -g
276+
277+
# apply the transform to one file
278+
eslint-transforms v9-rule-migration rule.js
279+
280+
# apply the transform to all files in a directory
281+
eslint-transforms v9-rule-migration rules/
282+
```
283+
284+
The removed `context` properties must be done manually as there may not be a direct one-to-one replacement.
285+
286+
**Related issue(s):** [eslint/eslint#16999](https://github.com/eslint/eslint/issues/16999)
287+
246288
## <a name="lintmessage-nodetype-removed"></a> Removal of `nodeType` property in `LintMessage` objects
247289

248-
In ESLint v10, the deprecated `nodeType` property on `LintMessage` objects has been removed. This affects consumers of the Node.js API (for example, custom formatters and editor/tool integrations) that previously relied on `message.nodeType`.
290+
In ESLint v10.0.0, the deprecated `nodeType` property on `LintMessage` objects has been removed. This affects consumers of the Node.js API (for example, custom formatters and editor/tool integrations) that previously relied on `message.nodeType`.
249291

250292
**To address:** Remove all usages of `message.nodeType` in your integrations and formatters.
251293

lib/linter/file-context.js

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -73,42 +73,6 @@ class FileContext {
7373
Object.freeze(this);
7474
}
7575

76-
/**
77-
* Gets the current working directory.
78-
* @returns {string} The current working directory.
79-
* @deprecated Use `cwd` instead.
80-
*/
81-
getCwd() {
82-
return this.cwd;
83-
}
84-
85-
/**
86-
* Gets the filename of the file being linted.
87-
* @returns {string} The filename of the file being linted.
88-
* @deprecated Use `filename` instead.
89-
*/
90-
getFilename() {
91-
return this.filename;
92-
}
93-
94-
/**
95-
* Gets the physical filename of the file being linted.
96-
* @returns {string} The physical filename of the file being linted.
97-
* @deprecated Use `physicalFilename` instead.
98-
*/
99-
getPhysicalFilename() {
100-
return this.physicalFilename;
101-
}
102-
103-
/**
104-
* Gets the source code of the file being linted.
105-
* @returns {SourceCode} The source code of the file being linted.
106-
* @deprecated Use `sourceCode` instead.
107-
*/
108-
getSourceCode() {
109-
return this.sourceCode;
110-
}
111-
11276
/**
11377
* Creates a new object with the current object as the prototype and
11478
* the specified properties as its own properties.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@
163163
"eslint-plugin-expect-type": "^0.6.0",
164164
"eslint-plugin-yml": "^1.14.0",
165165
"eslint-release": "^3.3.0",
166-
"eslint-rule-composer": "^0.3.0",
166+
"eslint-rule-extender": "^0.0.1",
167167
"eslump": "^3.0.0",
168168
"esprima": "^4.0.1",
169169
"fs-teardown": "^0.1.3",

tests/fixtures/plugins/node_modules/eslint-plugin-with-function-style-rules.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/fixtures/plugins/node_modules/eslint-plugin-with-rules.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)