The `Linter` object does the actual evaluation of the JavaScript code. It doesn't do any filesystem operations, it simply parses and reports on the code. In particular, the `Linter` object does not process configuration objects or files. You can retrieve instances of `Linter` like this:
The `Linter` object does the actual evaluation of the JavaScript code. It doesn't do any filesystem operations, it simply parses and reports on the code. In particular, the `Linter` object does not process configuration objects or files.
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:
*`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](./working-with-rules.md#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.
In this example, rules run on `linter1` will get `path/to/project` when calling `context.getCwd()`.
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).
###Linter#verify
The most important method on `Linter` is `verify()`, which initiates linting of the given text. This method accepts three arguments:
@@ -126,6 +126,7 @@ The `context` object contains additional functionality that is helpful for rules
Additionally, the `context` object has the following methods:
*`getAncestors()` - 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.
*`getCwd()` - returns the `cwd` passed to [Linter](./nodejs-api.md#Linter). It is a path to a directory that should be considered as the current working directory.
*`getDeclaredVariables(node)` - returns a list of [variables](./scope-manager-interface.md#variable-interface) declared by the given node. This information can be used to track references to variables.
* If the node is a `VariableDeclaration`, all variables declared in the declaration are returned.
* If the node is a `VariableDeclarator`, all variables declared in the declarator are returned.
@@ -985,6 +987,24 @@ function getRule(slots, ruleId) {
);
}
/**
* Normalize the value of the cwd
* @param {string | undefined} cwd raw value of the cwd, path to a directory that should be considered as the current working directory, can be undefined.
* @returns {string | undefined} normalized cwd
*/
functionnormalizeCwd(cwd){
if(cwd){
returncwd;
}
if(typeofprocess==="object"){
returnprocess.cwd();
}
// It's more explicit to assign the undefined
// eslint-disable-next-line no-undefined
returnundefined;
}
/**
* The map to store private data.
* @type {WeakMap<Linter, LinterInternalSlots>}
@@ -1001,8 +1021,14 @@ const internalSlotsMap = new WeakMap();
*/
classLinter{
constructor(){
/**
* Initialize the Linter.
* @param {Object} [config] the config object
* @param {string} [config.cwd] path to a directory that should be considered as the current working directory, can be undefined.
*/
constructor({ cwd }={}){
internalSlotsMap.set(this,{
cwd: normalizeCwd(cwd),
lastConfigArray: null,
lastSourceCode: null,
parserMap: newMap([["espree",espree]]),
@@ -1134,7 +1160,8 @@ class Linter {
parserName,
settings,
options.filename,
options.disableFixes
options.disableFixes,
slots.cwd
);
}catch(err){
err.message+=`\nOccurred while linting ${options.filename}`;