From d67e3076a9a54515bb84ed1f2de2183b1b19bfcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Thu, 15 Jun 2023 19:29:37 +0800 Subject: [PATCH] docs: resubmit pr 17115 doc changes credits: https://github.com/snitin315 Refs: * https://github.com/eslint/eslint/issues/17225 * https://github.com/eslint/eslint/pull/17107 --- docs/src/extend/code-path-analysis.md | 3 +-- docs/src/extend/custom-rules.md | 10 ++++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/docs/src/extend/code-path-analysis.md b/docs/src/extend/code-path-analysis.md index a2caeff8ea9..7344f8647ad 100644 --- a/docs/src/extend/code-path-analysis.md +++ b/docs/src/extend/code-path-analysis.md @@ -259,8 +259,7 @@ Please use a map of information instead. ```js function hasCb(node, context) { if (node.type.indexOf("Function") !== -1) { - const sourceCode = context.getSourceCode(); - + const sourceCode = context.sourceCode; return sourceCode.getDeclaredVariables(node).some(function(v) { return v.type === "Parameter" && v.name === "cb"; }); diff --git a/docs/src/extend/custom-rules.md b/docs/src/extend/custom-rules.md index 1fe23d97ce1..f5508e82ee4 100644 --- a/docs/src/extend/custom-rules.md +++ b/docs/src/extend/custom-rules.md @@ -147,7 +147,7 @@ Additionally, the `context` object has the following methods: * `getFilename()`: Returns the filename associated with the source. * `getPhysicalFilename()`: 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 `` if not specified. * `getScope()`: (**Deprecated:** Use `SourceCode#getScope(node)` instead.) Returns the [scope](./scope-manager-interface#scope-interface) of the currently-traversed node. This information can be used to track references to variables. -* `getSourceCode()`: 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)). +* `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)). * `markVariableAsUsed(name)`: (**Deprecated:** Use `SourceCode#markVariableAsUsed(name, node)` instead.) Marks a variable with the given name in the current scope as used. This affects the [no-unused-vars](../rules/no-unused-vars) rule. Returns `true` if a variable with the given name was found and marked as used, otherwise `false`. * `report(descriptor)`. Reports a problem in the code (see the [dedicated section](#reporting-problems)). @@ -506,18 +506,20 @@ When using options, make sure that your rule has some logical defaults in case t ### Accessing the Source Code -The `SourceCode` object is the main object for getting more information about the source code being linted. You can retrieve the `SourceCode` object at any time by using the `context.getSourceCode()` method: +The `SourceCode` object is the main object for getting more information about the source code being linted. You can retrieve the `SourceCode` object at any time by using the `context.sourceCode` property: ```js module.exports = { create: function(context) { - var sourceCode = context.getSourceCode(); + var sourceCode = context.sourceCode; // ... } }; ``` +**Deprecated:** The `context.getSourceCode()` method is deprecated; make sure to use `context.sourceCode` property instead. + Once you have an instance of `SourceCode`, you can use the following methods on it to work with the code: * `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)). @@ -706,7 +708,7 @@ To help with this, you can use the `sourceCode.markVariableAsUsed()` method. Thi ```js module.exports = { create: function(context) { - var sourceCode = context.getSourceCode(); + var sourceCode = context.sourceCode; return { ReturnStatement(node) {