Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Remove SourceCode#getComments() #17715

Merged
merged 1 commit into from Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/src/extend/custom-rules.md
Expand Up @@ -872,7 +872,6 @@ ESLint analyzes code paths while traversing AST. You can access code path object

Please note that the following `SourceCode` methods have been deprecated and will be removed in a future version of ESLint:

* `getComments()`: Replaced by `SourceCode#getCommentsBefore()`, `SourceCode#getCommentsAfter()`, and `SourceCode#getCommentsInside()`.
* `getTokenOrCommentBefore()`: Replaced by `SourceCode#getTokenBefore()` with the `{ includeComments: true }` option.
* `getTokenOrCommentAfter()`: Replaced by `SourceCode#getTokenAfter()` with the `{ includeComments: true }` option.
* `isSpaceBetweenTokens()`: Replaced by `SourceCode#isSpaceBetween()`
Expand Down
15 changes: 1 addition & 14 deletions lib/rule-tester/flat-rule-tester.js
Expand Up @@ -274,17 +274,6 @@ function wrapParser(parser) {
};
}

/**
* Function to replace `SourceCode.prototype.getComments`.
* @returns {void}
* @throws {Error} Deprecation message.
*/
function getCommentsDeprecation() {
throw new Error(
"`SourceCode#getComments()` is deprecated and will be removed in a future major version. Use `getCommentsBefore()`, `getCommentsAfter()`, and `getCommentsInside()` instead."
);
}

/**
* Emit a deprecation warning if rule uses CodePath#currentSegments.
* @param {string} ruleName Name of the rule.
Expand Down Expand Up @@ -727,12 +716,11 @@ class FlatRuleTester {
}

// Verify the code.
const { getComments, applyLanguageOptions, applyInlineConfig, finalize } = SourceCode.prototype;
const { applyLanguageOptions, applyInlineConfig, finalize } = SourceCode.prototype;
const originalCurrentSegments = Object.getOwnPropertyDescriptor(CodePath.prototype, "currentSegments");
let messages;

try {
SourceCode.prototype.getComments = getCommentsDeprecation;
Object.defineProperty(CodePath.prototype, "currentSegments", {
get() {
emitCodePathCurrentSegmentsWarning(ruleName);
Expand All @@ -746,7 +734,6 @@ class FlatRuleTester {

messages = linter.verify(code, configs, filename);
} finally {
SourceCode.prototype.getComments = getComments;
Object.defineProperty(CodePath.prototype, "currentSegments", originalCurrentSegments);
SourceCode.prototype.applyInlineConfig = applyInlineConfig;
SourceCode.prototype.applyLanguageOptions = applyLanguageOptions;
Expand Down
15 changes: 1 addition & 14 deletions lib/rule-tester/rule-tester.js
Expand Up @@ -330,17 +330,6 @@ function wrapParser(parser) {
};
}

/**
* Function to replace `SourceCode.prototype.getComments`.
* @returns {void}
* @throws {Error} Deprecation message.
*/
function getCommentsDeprecation() {
throw new Error(
"`SourceCode#getComments()` is deprecated and will be removed in a future major version. Use `getCommentsBefore()`, `getCommentsAfter()`, and `getCommentsInside()` instead."
);
}

/**
* Function to replace forbidden `SourceCode` methods.
* @param {string} methodName The name of the method to forbid.
Expand Down Expand Up @@ -812,12 +801,11 @@ class RuleTester {
validate(config, "rule-tester", id => (id === ruleName ? rule : null));

// Verify the code.
const { getComments, applyLanguageOptions, applyInlineConfig, finalize } = SourceCode.prototype;
const { applyLanguageOptions, applyInlineConfig, finalize } = SourceCode.prototype;
const originalCurrentSegments = Object.getOwnPropertyDescriptor(CodePath.prototype, "currentSegments");
let messages;

try {
SourceCode.prototype.getComments = getCommentsDeprecation;
Object.defineProperty(CodePath.prototype, "currentSegments", {
get() {
emitCodePathCurrentSegmentsWarning(ruleName);
Expand All @@ -831,7 +819,6 @@ class RuleTester {

messages = linter.verify(code, config, filename);
} finally {
SourceCode.prototype.getComments = getComments;
Object.defineProperty(CodePath.prototype, "currentSegments", originalCurrentSegments);
SourceCode.prototype.applyInlineConfig = applyInlineConfig;
SourceCode.prototype.applyLanguageOptions = applyLanguageOptions;
Expand Down
78 changes: 0 additions & 78 deletions lib/source-code/source-code.js
Expand Up @@ -438,9 +438,6 @@ class SourceCode extends TokenStore {
}
this.lines.push(this.text.slice(this.lineStartIndices[this.lineStartIndices.length - 1]));

// Cache for comments found using getComments().
this._commentCache = new WeakMap();

// don't allow further modification of this object
Object.freeze(this);
Object.freeze(this.lines);
Expand Down Expand Up @@ -490,81 +487,6 @@ class SourceCode extends TokenStore {
return this.ast.comments;
}

/**
* Gets all comments for the given node.
* @param {ASTNode} node The AST node to get the comments for.
* @returns {Object} An object containing a leading and trailing array
* of comments indexed by their position.
* @public
* @deprecated replaced by getCommentsBefore(), getCommentsAfter(), and getCommentsInside().
*/
getComments(node) {
if (this._commentCache.has(node)) {
return this._commentCache.get(node);
}

const comments = {
leading: [],
trailing: []
};

/*
* Return all comments as leading comments of the Program node when
* there is no executable code.
*/
if (node.type === "Program") {
if (node.body.length === 0) {
comments.leading = node.comments;
}
} else {

/*
* Return comments as trailing comments of nodes that only contain
* comments (to mimic the comment attachment behavior present in Espree).
*/
if ((node.type === "BlockStatement" || node.type === "ClassBody") && node.body.length === 0 ||
node.type === "ObjectExpression" && node.properties.length === 0 ||
node.type === "ArrayExpression" && node.elements.length === 0 ||
node.type === "SwitchStatement" && node.cases.length === 0
) {
comments.trailing = this.getTokens(node, {
includeComments: true,
filter: isCommentToken
});
}

/*
* Iterate over tokens before and after node and collect comment tokens.
* Do not include comments that exist outside of the parent node
* to avoid duplication.
*/
let currentToken = this.getTokenBefore(node, { includeComments: true });

while (currentToken && isCommentToken(currentToken)) {
if (node.parent && node.parent.type !== "Program" && (currentToken.start < node.parent.start)) {
break;
}
comments.leading.push(currentToken);
currentToken = this.getTokenBefore(currentToken, { includeComments: true });
}

comments.leading.reverse();

currentToken = this.getTokenAfter(node, { includeComments: true });

while (currentToken && isCommentToken(currentToken)) {
if (node.parent && node.parent.type !== "Program" && (currentToken.end > node.parent.end)) {
break;
}
comments.trailing.push(currentToken);
currentToken = this.getTokenAfter(currentToken, { includeComments: true });
}
}

this._commentCache.set(node, comments);
return comments;
}

/**
* Retrieves the JSDoc comment for a given node.
* @param {ASTNode} node The AST node to get the comment for.
Expand Down
33 changes: 0 additions & 33 deletions tests/lib/rule-tester/flat-rule-tester.js
Expand Up @@ -2665,39 +2665,6 @@ describe("FlatRuleTester", () => {

});

describe("SourceCode#getComments()", () => {
const useGetCommentsRule = {
create: context => ({
Program(node) {
const sourceCode = context.sourceCode;

sourceCode.getComments(node);
}
})
};

it("should throw if called from a valid test case", () => {
assert.throws(() => {
ruleTester.run("use-get-comments", useGetCommentsRule, {
valid: [""],
invalid: []
});
}, /`SourceCode#getComments\(\)` is deprecated/u);
});

it("should throw if called from an invalid test case", () => {
assert.throws(() => {
ruleTester.run("use-get-comments", useGetCommentsRule, {
valid: [],
invalid: [{
code: "",
errors: [{}]
}]
});
}, /`SourceCode#getComments\(\)` is deprecated/u);
});
});

describe("SourceCode forbidden methods", () => {

[
Expand Down
34 changes: 0 additions & 34 deletions tests/lib/rule-tester/rule-tester.js
Expand Up @@ -2895,40 +2895,6 @@ describe("RuleTester", () => {

});

describe("SourceCode#getComments()", () => {
const useGetCommentsRule = {
create: context => ({
Program(node) {
const sourceCode = context.sourceCode;

sourceCode.getComments(node);
}
})
};

it("should throw if called from a valid test case", () => {
assert.throws(() => {
ruleTester.run("use-get-comments", useGetCommentsRule, {
valid: [""],
invalid: []
});
}, /`SourceCode#getComments\(\)` is deprecated/u);
});

it("should throw if called from an invalid test case", () => {
assert.throws(() => {
ruleTester.run("use-get-comments", useGetCommentsRule, {
valid: [],
invalid: [{
code: "",
errors: [{}]
}]
});
}, /`SourceCode#getComments\(\)` is deprecated/u);
});
});


describe("SourceCode forbidden methods", () => {

[
Expand Down