Skip to content

Commit 67e7af3

Browse files
authored
1 parent 5338b56 commit 67e7af3

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

docs/src/extend/code-path-analysis.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,7 @@ Please use a map of information instead.
259259
```js
260260
function hasCb(node, context) {
261261
if (node.type.indexOf("Function") !== -1) {
262-
const sourceCode = context.getSourceCode();
263-
262+
const sourceCode = context.sourceCode;
264263
return sourceCode.getDeclaredVariables(node).some(function(v) {
265264
return v.type === "Parameter" && v.name === "cb";
266265
});

docs/src/extend/custom-rules.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ As the name implies, the `context` object contains information that is relevant
124124
The `context` object has the following properties:
125125

126126
* `id`: (`string`) The rule ID.
127+
* `filename`: (`string`) The filename associated with the source.
128+
* `physicalFilename`: (`string`) When linting a file, it provides the full path of the file on disk without any code block information. When linting text, it provides the value passed to `—stdin-filename` or `<text>` if not specified.
129+
* `cwd`: (`string`) 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.
127130
* `options`: (`array`) An array of the [configured options](../use/configure/rules) for this rule. This array does not include the rule severity (see the [dedicated section](#accessing-options-passed-to-a-rule)).
131+
* `sourceCode`: (`object`) 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)).
128132
* `settings`: (`object`) The [shared settings](../use/configure/configuration-files#adding-shared-settings) from the configuration.
129133
* `parserPath`: (`string`) The name of the `parser` from the configuration.
130134
* `parserServices`: (`object`) Contains parser-provided services for rules. The default parser does not provide any services. However, if a rule is intended to be used with a custom parser, it could use `parserServices` to access anything provided by that parser. (For example, a TypeScript parser could provide the ability to get the computed type of a given node.)
@@ -133,7 +137,7 @@ The `context` object has the following properties:
133137
Additionally, the `context` object has the following methods:
134138

135139
* `getAncestors()`: (**Deprecated:** Use `SourceCode#getAncestors(node)` instead.) Returns an array of the ancestors of the currently-traversed node, starting at the root of the AST and continuing through the direct parent of the current node. This array does not include the currently-traversed node itself.
136-
* `getCwd()`: 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.
140+
* `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.
137141
* `getDeclaredVariables(node)`: (**Deprecated:** Use `SourceCode#getDeclaredVariables(node)` instead.) Returns a list of [variables](./scope-manager-interface#variable-interface) declared by the given node. This information can be used to track references to variables.
138142
* If the node is a `VariableDeclaration`, all variables declared in the declaration are returned.
139143
* If the node is a `VariableDeclarator`, all variables declared in the declarator are returned.
@@ -144,10 +148,10 @@ Additionally, the `context` object has the following methods:
144148
* If the node is an `ImportDeclaration`, variables for all of its specifiers are returned.
145149
* If the node is an `ImportSpecifier`, `ImportDefaultSpecifier`, or `ImportNamespaceSpecifier`, the declared variable is returned.
146150
* Otherwise, if the node does not declare any variables, an empty array is returned.
147-
* `getFilename()`: Returns the filename associated with the source.
148-
* `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 `<text>` if not specified.
151+
* `getFilename()`: (**Deprecated:** Use `context.filename` instead.) Returns the filename associated with the source.
152+
* `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.
149153
* `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.
150-
* `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)).
154+
* `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)).
151155
* `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`.
152156
* `report(descriptor)`. Reports a problem in the code (see the [dedicated section](#reporting-problems)).
153157

@@ -506,18 +510,20 @@ When using options, make sure that your rule has some logical defaults in case t
506510

507511
### Accessing the Source Code
508512

509-
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:
513+
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:
510514

511515
```js
512516
module.exports = {
513517
create: function(context) {
514-
var sourceCode = context.getSourceCode();
518+
var sourceCode = context.sourceCode;
515519

516520
// ...
517521
}
518522
};
519523
```
520524

525+
**Deprecated:** The `context.getSourceCode()` method is deprecated; make sure to use `context.sourceCode` property instead.
526+
521527
Once you have an instance of `SourceCode`, you can use the following methods on it to work with the code:
522528

523529
* `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 +712,7 @@ To help with this, you can use the `sourceCode.markVariableAsUsed()` method. Thi
706712
```js
707713
module.exports = {
708714
create: function(context) {
709-
var sourceCode = context.getSourceCode();
715+
var sourceCode = context.sourceCode;
710716

711717
return {
712718
ReturnStatement(node) {

docs/src/integrate/nodejs-api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ The `Linter` object does the actual evaluation of the JavaScript code. It doesn'
510510

511511
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:
512512

513-
* `cwd` - Path to a directory that should be considered as the current working directory. It is accessible to rules 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.
513+
* `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.
514514

515515
For example:
516516

@@ -520,7 +520,7 @@ const linter1 = new Linter({ cwd: 'path/to/project' });
520520
const linter2 = new Linter();
521521
```
522522

523-
In this example, rules run on `linter1` will get `path/to/project` when calling `context.getCwd()`.
523+
In this example, rules run on `linter1` will get `path/to/project` from `context.cwd` or when calling `context.getCwd()`.
524524
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>).
525525

526526
### Linter#verify

0 commit comments

Comments
 (0)