Skip to content

Commit 3db18b0

Browse files
authored
refactor: Extract FileContext into class (#18831)
* refactor: Extract FileContext into class refs #18787 * Fix JSDoc comments
1 parent 931d650 commit 3db18b0

2 files changed

Lines changed: 147 additions & 18 deletions

File tree

lib/linter/file-context.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/**
2+
* @fileoverview The FileContext class.
3+
* @author Nicholas C. Zakas
4+
*/
5+
6+
"use strict";
7+
8+
/**
9+
* Represents a file context that the linter can use to lint a file.
10+
*/
11+
class FileContext {
12+
13+
/**
14+
* The current working directory.
15+
* @type {string}
16+
*/
17+
cwd;
18+
19+
/**
20+
* The filename of the file being linted.
21+
* @type {string}
22+
*/
23+
filename;
24+
25+
/**
26+
* The physical filename of the file being linted.
27+
* @type {string}
28+
*/
29+
physicalFilename;
30+
31+
/**
32+
* The source code of the file being linted.
33+
* @type {SourceCode}
34+
*/
35+
sourceCode;
36+
37+
/**
38+
* The parser options for the file being linted.
39+
* @type {Record<string, unknown>}
40+
* @deprecated Use `languageOptions` instead.
41+
*/
42+
parserOptions;
43+
44+
/**
45+
* The path to the parser used to parse this file.
46+
* @type {string}
47+
* @deprecated No longer supported.
48+
*/
49+
parserPath;
50+
51+
/**
52+
* The language options used when parsing this file.
53+
* @type {Record<string, unknown>}
54+
*/
55+
languageOptions;
56+
57+
/**
58+
* The settings for the file being linted.
59+
* @type {Record<string, unknown>}
60+
*/
61+
settings;
62+
63+
/**
64+
* Creates a new instance.
65+
* @param {Object} config The configuration object for the file context.
66+
* @param {string} config.cwd The current working directory.
67+
* @param {string} config.filename The filename of the file being linted.
68+
* @param {string} config.physicalFilename The physical filename of the file being linted.
69+
* @param {SourceCode} config.sourceCode The source code of the file being linted.
70+
* @param {Record<string, unknown>} config.parserOptions The parser options for the file being linted.
71+
* @param {string} config.parserPath The path to the parser used to parse this file.
72+
* @param {Record<string, unknown>} config.languageOptions The language options used when parsing this file.
73+
* @param {Record<string, unknown>} config.settings The settings for the file being linted.
74+
*/
75+
constructor({
76+
cwd,
77+
filename,
78+
physicalFilename,
79+
sourceCode,
80+
parserOptions,
81+
parserPath,
82+
languageOptions,
83+
settings
84+
}) {
85+
this.cwd = cwd;
86+
this.filename = filename;
87+
this.physicalFilename = physicalFilename;
88+
this.sourceCode = sourceCode;
89+
this.parserOptions = parserOptions;
90+
this.parserPath = parserPath;
91+
this.languageOptions = languageOptions;
92+
this.settings = settings;
93+
94+
Object.freeze(this);
95+
}
96+
97+
/**
98+
* Gets the current working directory.
99+
* @returns {string} The current working directory.
100+
* @deprecated Use `cwd` instead.
101+
*/
102+
getCwd() {
103+
return this.cwd;
104+
}
105+
106+
/**
107+
* Gets the filename of the file being linted.
108+
* @returns {string} The filename of the file being linted.
109+
* @deprecated Use `filename` instead.
110+
*/
111+
getFilename() {
112+
return this.filename;
113+
}
114+
115+
/**
116+
* Gets the physical filename of the file being linted.
117+
* @returns {string} The physical filename of the file being linted.
118+
* @deprecated Use `physicalFilename` instead.
119+
*/
120+
getPhysicalFilename() {
121+
return this.physicalFilename;
122+
}
123+
124+
/**
125+
* Gets the source code of the file being linted.
126+
* @returns {SourceCode} The source code of the file being linted.
127+
* @deprecated Use `sourceCode` instead.
128+
*/
129+
getSourceCode() {
130+
return this.sourceCode;
131+
}
132+
}
133+
134+
exports.FileContext = FileContext;

lib/linter/linter.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const parserSymbol = Symbol.for("eslint.RuleTester.parser");
5353
const { LATEST_ECMA_VERSION } = require("../../conf/ecma-version");
5454
const { VFile } = require("./vfile");
5555
const { ParserService } = require("../services/parser-service");
56+
const { FileContext } = require("./file-context");
5657
const STEP_KIND_VISIT = 1;
5758
const STEP_KIND_CALL = 2;
5859

@@ -979,24 +980,18 @@ function runRules(
979980
* All rule contexts will inherit from this object. This avoids the performance penalty of copying all the
980981
* properties once for each rule.
981982
*/
982-
const sharedTraversalContext = Object.freeze(
983-
{
984-
getCwd: () => cwd,
985-
cwd,
986-
getFilename: () => filename,
987-
filename,
988-
getPhysicalFilename: () => physicalFilename || filename,
989-
physicalFilename: physicalFilename || filename,
990-
getSourceCode: () => sourceCode,
991-
sourceCode,
992-
parserOptions: {
993-
...languageOptions.parserOptions
994-
},
995-
parserPath: parserName,
996-
languageOptions,
997-
settings
998-
}
999-
);
983+
const sharedTraversalContext = new FileContext({
984+
cwd,
985+
filename,
986+
physicalFilename: physicalFilename || filename,
987+
sourceCode,
988+
parserOptions: {
989+
...languageOptions.parserOptions
990+
},
991+
parserPath: parserName,
992+
languageOptions,
993+
settings
994+
});
1000995

1001996
const lintingProblems = [];
1002997

0 commit comments

Comments
 (0)