Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7ec047b

Browse files
committed
Add deprecation notices to RuleTester/FlatRuleTester
1 parent 5544ba2 commit 7ec047b

File tree

5 files changed

+122
-2
lines changed

5 files changed

+122
-2
lines changed

lib/linter/code-path-analysis/code-path.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class CodePath {
117117
/**
118118
* Current code path segments.
119119
* @type {CodePathSegment[]}
120+
* @deprecated
120121
*/
121122
get currentSegments() {
122123
return this.internal.currentSegments;

lib/rule-tester/flat-rule-tester.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ const
1616
equal = require("fast-deep-equal"),
1717
Traverser = require("../shared/traverser"),
1818
{ getRuleOptionsSchema } = require("../config/flat-config-helpers"),
19-
{ Linter, SourceCodeFixer, interpolate } = require("../linter");
19+
{ Linter, SourceCodeFixer, interpolate } = require("../linter"),
20+
CodePath = require("../linter/code-path-analysis/code-path");
21+
2022
const { FlatConfigArray } = require("../config/flat-config-array");
2123
const { defaultConfig } = require("../config/default-config");
2224

@@ -274,6 +276,21 @@ function getCommentsDeprecation() {
274276
);
275277
}
276278

279+
/**
280+
* Emit a deprecation warning if rule uses CodePath#currentSegments.
281+
* @param {string} ruleName Name of the rule.
282+
* @returns {void}
283+
*/
284+
function emitCodePathCurrentSegmentsWarning(ruleName) {
285+
if (!emitCodePathCurrentSegmentsWarning[`warned-${ruleName}`]) {
286+
emitCodePathCurrentSegmentsWarning[`warned-${ruleName}`] = true;
287+
process.emitWarning(
288+
`"${ruleName}" rule uses CodePath#currentSegments and will stop working in ESLint v9. Please read the documentation for how to update your code: https://eslint.org/docs/latest/extend/code-path-analysis#usage-examples`,
289+
"DeprecationWarning"
290+
);
291+
}
292+
}
293+
277294
//------------------------------------------------------------------------------
278295
// Public Interface
279296
//------------------------------------------------------------------------------
@@ -664,6 +681,7 @@ class FlatRuleTester {
664681

665682
// Verify the code.
666683
const { getComments } = SourceCode.prototype;
684+
const originalCurrentSegments = Object.getOwnPropertyDescriptor(CodePath.prototype, "currentSegments");
667685
let messages;
668686

669687
// check for validation errors
@@ -677,11 +695,20 @@ class FlatRuleTester {
677695

678696
try {
679697
SourceCode.prototype.getComments = getCommentsDeprecation;
698+
Object.defineProperty(CodePath.prototype, "currentSegments", {
699+
get() {
700+
emitCodePathCurrentSegmentsWarning(ruleName);
701+
return originalCurrentSegments.get.call(this);
702+
}
703+
});
704+
680705
messages = linter.verify(code, configs, filename);
681706
} finally {
682707
SourceCode.prototype.getComments = getComments;
708+
Object.defineProperty(CodePath.prototype, "currentSegments", originalCurrentSegments);
683709
}
684710

711+
685712
const fatalErrorMessage = messages.find(m => m.fatal);
686713

687714
assert(!fatalErrorMessage, `A fatal parsing error occurred: ${fatalErrorMessage && fatalErrorMessage.message}`);

lib/rule-tester/rule-tester.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ const
4848
equal = require("fast-deep-equal"),
4949
Traverser = require("../../lib/shared/traverser"),
5050
{ getRuleOptionsSchema, validate } = require("../shared/config-validator"),
51-
{ Linter, SourceCodeFixer, interpolate } = require("../linter");
51+
{ Linter, SourceCodeFixer, interpolate } = require("../linter"),
52+
CodePath = require("../linter/code-path-analysis/code-path");
5253

5354
const ajv = require("../shared/ajv")({ strictDefaults: true });
5455

@@ -335,6 +336,21 @@ function emitMissingSchemaWarning(ruleName) {
335336
}
336337
}
337338

339+
/**
340+
* Emit a deprecation warning if rule uses CodePath#currentSegments.
341+
* @param {string} ruleName Name of the rule.
342+
* @returns {void}
343+
*/
344+
function emitCodePathCurrentSegmentsWarning(ruleName) {
345+
if (!emitCodePathCurrentSegmentsWarning[`warned-${ruleName}`]) {
346+
emitCodePathCurrentSegmentsWarning[`warned-${ruleName}`] = true;
347+
process.emitWarning(
348+
`"${ruleName}" rule uses CodePath#currentSegments and will stop working in ESLint v9. Please read the documentation for how to update your code: https://eslint.org/docs/latest/extend/code-path-analysis#usage-examples`,
349+
"DeprecationWarning"
350+
);
351+
}
352+
}
353+
338354
//------------------------------------------------------------------------------
339355
// Public Interface
340356
//------------------------------------------------------------------------------
@@ -686,13 +702,22 @@ class RuleTester {
686702

687703
// Verify the code.
688704
const { getComments } = SourceCode.prototype;
705+
const originalCurrentSegments = Object.getOwnPropertyDescriptor(CodePath.prototype, "currentSegments");
689706
let messages;
690707

691708
try {
692709
SourceCode.prototype.getComments = getCommentsDeprecation;
710+
Object.defineProperty(CodePath.prototype, "currentSegments", {
711+
get() {
712+
emitCodePathCurrentSegmentsWarning(ruleName);
713+
return originalCurrentSegments.get.call(this);
714+
}
715+
});
716+
693717
messages = linter.verify(code, config, filename);
694718
} finally {
695719
SourceCode.prototype.getComments = getComments;
720+
Object.defineProperty(CodePath.prototype, "currentSegments", originalCurrentSegments);
696721
}
697722

698723
const fatalErrorMessage = messages.find(m => m.fatal);

tests/lib/rule-tester/flat-rule-tester.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2248,6 +2248,45 @@ describe("FlatRuleTester", () => {
22482248
});
22492249
});
22502250

2251+
describe("deprecations", () => {
2252+
let processStub;
2253+
2254+
beforeEach(() => {
2255+
processStub = sinon.stub(process, "emitWarning");
2256+
});
2257+
2258+
afterEach(() => {
2259+
processStub.restore();
2260+
});
2261+
2262+
it("should emit a deprecation warning when CodePath#currentSegments is accessed", () => {
2263+
2264+
const useCurrentSegmentsRule = {
2265+
create: () => ({
2266+
onCodePathStart(codePath) {
2267+
codePath.currentSegments.forEach(() => { });
2268+
}
2269+
})
2270+
};
2271+
2272+
ruleTester.run("use-current-segments", useCurrentSegmentsRule, {
2273+
valid: ["foo"],
2274+
invalid: []
2275+
});
2276+
2277+
assert.strictEqual(processStub.callCount, 1, "calls `process.emitWarning()` once");
2278+
assert.deepStrictEqual(
2279+
processStub.getCall(0).args,
2280+
[
2281+
"\"use-current-segments\" rule uses CodePath#currentSegments and will stop working in ESLint v9. Please read the documentation for how to update your code: https://eslint.org/docs/latest/extend/code-path-analysis#usage-examples",
2282+
"DeprecationWarning"
2283+
]
2284+
);
2285+
2286+
});
2287+
2288+
});
2289+
22512290
/**
22522291
* Asserts that a particular value will be emitted from an EventEmitter.
22532292
* @param {EventEmitter} emitter The emitter that should emit a value

tests/lib/rule-tester/rule-tester.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2489,6 +2489,34 @@ describe("RuleTester", () => {
24892489

24902490
assert.strictEqual(processStub.callCount, 0, "never calls `process.emitWarning()`");
24912491
});
2492+
2493+
2494+
it("should emit a deprecation warning when CodePath#currentSegments is accessed", () => {
2495+
2496+
const useCurrentSegmentsRule = {
2497+
create: () => ({
2498+
onCodePathStart(codePath) {
2499+
codePath.currentSegments.forEach(() => {});
2500+
}
2501+
})
2502+
};
2503+
2504+
ruleTester.run("use-current-segments", useCurrentSegmentsRule, {
2505+
valid: ["foo"],
2506+
invalid: []
2507+
});
2508+
2509+
assert.strictEqual(processStub.callCount, 1, "calls `process.emitWarning()` once");
2510+
assert.deepStrictEqual(
2511+
processStub.getCall(0).args,
2512+
[
2513+
"\"use-current-segments\" rule uses CodePath#currentSegments and will stop working in ESLint v9. Please read the documentation for how to update your code: https://eslint.org/docs/latest/extend/code-path-analysis#usage-examples",
2514+
"DeprecationWarning"
2515+
]
2516+
);
2517+
2518+
});
2519+
24922520
});
24932521

24942522
/**

0 commit comments

Comments
 (0)