Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(eslint-plugin): [prefer-readonly] refine report locations#8894

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,10 @@ import {
nullThrows,
NullThrowsReasons,
} from '../util';
import {
getMemberHeadLoc,
getParameterPropertyHeadLoc,
} from '../util/getMemberHeadLoc';

type AccessibilityLevel =
| 'explicit' // require an accessor (including public)
Expand DownExpand Up@@ -166,7 +170,7 @@ export default createRule<Options, MessageIds>({
});
} else if (check === 'explicit' && !methodDefinition.accessibility) {
context.report({
loc:getMissingAccessibilityReportLoc(methodDefinition),
loc:getMemberHeadLoc(context.sourceCode,methodDefinition),
messageId: 'missingAccessibility',
data: {
type: nodeType,
Expand DownExpand Up@@ -221,87 +225,6 @@ export default createRule<Options, MessageIds>({
return { range: keywordRange, rangeToRemove };
}

/**
* For missing accessibility modifiers, we want to report any keywords
* out in front of the key, and the key itself, but not anything afterwards,
* i.e. parens, type annotations, method bodies, or `?`.
*/
function getMissingAccessibilityReportLoc(
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

These functions are just moved to their own file so they can be shared

JoshuaKGoldberg reacted with thumbs up emoji
node:
| TSESTree.MethodDefinition
| TSESTree.TSAbstractMethodDefinition
| TSESTree.PropertyDefinition
| TSESTree.TSAbstractPropertyDefinition,
): TSESTree.SourceLocation {
let start: TSESTree.Position;

if (node.decorators.length === 0) {
start = node.loc.start;
} else {
const lastDecorator = node.decorators[node.decorators.length - 1];
const nextToken = nullThrows(
context.sourceCode.getTokenAfter(lastDecorator),
NullThrowsReasons.MissingToken('token', 'last decorator'),
);
start = nextToken.loc.start;
}

let end: TSESTree.Position;

if (!node.computed) {
end = node.key.loc.end;
} else {
const closingBracket = nullThrows(
context.sourceCode.getTokenAfter(
node.key,
token => token.value === ']',
),
NullThrowsReasons.MissingToken(']', node.type),
);
end = closingBracket.loc.end;
}

return {
start: structuredClone(start),
end: structuredClone(end),
};
}

/**
* For missing accessibility modifiers, we want to report any keywords
* out in front of the key, and the key itself, but not anything afterwards,
* i.e. parens, type annotations, method bodies, or `?`.
*/
function getMissingAccessibilityReportLocForParameterProperty(
node: TSESTree.TSParameterProperty,
nodeName: string,
): TSESTree.SourceLocation {
// Parameter properties have a weirdly different AST structure
// than other class members.

let start: TSESTree.Position;

if (node.decorators.length === 0) {
start = structuredClone(node.loc.start);
} else {
const lastDecorator = node.decorators[node.decorators.length - 1];
const nextToken = nullThrows(
context.sourceCode.getTokenAfter(lastDecorator),
NullThrowsReasons.MissingToken('token', 'last decorator'),
);
start = structuredClone(nextToken.loc.start);
}

const end = context.sourceCode.getLocFromIndex(
node.parameter.range[0] + nodeName.length,
);

return {
start,
end,
};
}

/**
* Creates a fixer that adds an accessibility modifier keyword
*/
Expand DownExpand Up@@ -385,7 +308,7 @@ export default createRule<Options, MessageIds>({
!propertyDefinition.accessibility
) {
context.report({
loc:getMissingAccessibilityReportLoc(propertyDefinition),
loc:getMemberHeadLoc(context.sourceCode,propertyDefinition),
messageId: 'missingAccessibility',
data: {
type: nodeType,
Expand DownExpand Up@@ -422,7 +345,8 @@ export default createRule<Options, MessageIds>({
case 'explicit': {
if (!node.accessibility) {
context.report({
loc: getMissingAccessibilityReportLocForParameterProperty(
loc: getParameterPropertyHeadLoc(
context.sourceCode,
node,
nodeName,
),
Expand Down
37 changes: 27 additions & 10 deletionspackages/eslint-plugin/src/rules/prefer-readonly.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,10 @@ import {
nullThrows,
typeIsOrHasBaseType,
} from '../util';
import {
getMemberHeadLoc,
getParameterPropertyHeadLoc,
} from '../util/getMemberHeadLoc';

type MessageIds = 'preferReadonly';
type Options = [
Expand DownExpand Up@@ -160,15 +164,6 @@ export default createRule<Options, MessageIds>({
function getEsNodesFromViolatingNode(
violatingNode: ParameterOrPropertyDeclaration,
): { esNode: TSESTree.Node; nameNode: TSESTree.Node } {
if (
ts.isParameterPropertyDeclaration(violatingNode, violatingNode.parent)
) {
return {
esNode: services.tsNodeToESTreeNodeMap.get(violatingNode.name),
nameNode: services.tsNodeToESTreeNodeMap.get(violatingNode.name),
};
}

return {
esNode: services.tsNodeToESTreeNodeMap.get(violatingNode),
nameNode: services.tsNodeToESTreeNodeMap.get(violatingNode.name),
Expand DownExpand Up@@ -196,13 +191,35 @@ export default createRule<Options, MessageIds>({
for (const violatingNode of finalizedClassScope.finalizeUnmodifiedPrivateNonReadonlys()) {
const { esNode, nameNode } =
getEsNodesFromViolatingNode(violatingNode);

const reportNodeOrLoc:
| { node: TSESTree.Node }
| { loc: TSESTree.SourceLocation } = (() => {
switch (esNode.type) {
case AST_NODE_TYPES.MethodDefinition:
case AST_NODE_TYPES.PropertyDefinition:
case AST_NODE_TYPES.TSAbstractMethodDefinition:
return { loc: getMemberHeadLoc(context.sourceCode, esNode) };
case AST_NODE_TYPES.TSParameterProperty:
return {
loc: getParameterPropertyHeadLoc(
context.sourceCode,
esNode,
(nameNode as TSESTree.Identifier).name,
),
};
default:
return { node: esNode };
}
})();

context.report({
...reportNodeOrLoc,
data: {
name: context.sourceCode.getText(nameNode),
},
fix: fixer => fixer.insertTextBefore(nameNode, 'readonly '),
messageId: 'preferReadonly',
node: esNode,
});
}
},
Expand Down
108 changes: 108 additions & 0 deletionspackages/eslint-plugin/src/util/getMemberHeadLoc.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import {
nullThrows,
NullThrowsReasons,
} from '@typescript-eslint/utils/eslint-utils';

/**
* Generates report loc suitable for reporting on how a class member is
* declared, rather than how it's implemented.
*
* ```ts
* class A {
* abstract method(): void;
* ~~~~~~~~~~~~~~~
*
* concreteMethod(): void {
* ~~~~~~~~~~~~~~
* // code
* }
*
* abstract private property?: string;
* ~~~~~~~~~~~~~~~~~~~~~~~~~
*
* @decorator override concreteProperty = 'value';
* ~~~~~~~~~~~~~~~~~~~~~~~~~
* }
* ```
*/
export function getMemberHeadLoc(
sourceCode: Readonly<TSESLint.SourceCode>,
node:
| TSESTree.MethodDefinition
| TSESTree.TSAbstractMethodDefinition
| TSESTree.PropertyDefinition
| TSESTree.TSAbstractPropertyDefinition,
): TSESTree.SourceLocation {
let start: TSESTree.Position;

if (node.decorators.length === 0) {
start = node.loc.start;
} else {
const lastDecorator = node.decorators[node.decorators.length - 1];
const nextToken = nullThrows(
sourceCode.getTokenAfter(lastDecorator),
NullThrowsReasons.MissingToken('token', 'last decorator'),
);
start = nextToken.loc.start;
}

let end: TSESTree.Position;

if (!node.computed) {
end = node.key.loc.end;
} else {
const closingBracket = nullThrows(
sourceCode.getTokenAfter(node.key, token => token.value === ']'),
NullThrowsReasons.MissingToken(']', node.type),
);
end = closingBracket.loc.end;
}

return {
start: structuredClone(start),
end: structuredClone(end),
};
}

/**
* Generates report loc suitable for reporting on how a parameter property is
* declared.
*
* ```ts
* class A {
* constructor(private property: string = 'value') {
* ~~~~~~~~~~~~~~~~
* }
* ```
*/
export function getParameterPropertyHeadLoc(
sourceCode: Readonly<TSESLint.SourceCode>,
node: TSESTree.TSParameterProperty,
nodeName: string,
): TSESTree.SourceLocation {
// Parameter properties have a weirdly different AST structure
// than other class members.

let start: TSESTree.Position;

if (node.decorators.length === 0) {
start = structuredClone(node.loc.start);
} else {
const lastDecorator = node.decorators[node.decorators.length - 1];
const nextToken = nullThrows(
sourceCode.getTokenAfter(lastDecorator),
NullThrowsReasons.MissingToken('token', 'last decorator'),
);
start = structuredClone(nextToken.loc.start);
}

const end = sourceCode.getLocFromIndex(
node.parameter.range[0] + nodeName.length,
);

return {
start,
end,
};
}
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Loading

[8]ページ先頭

©2009-2025 Movatter.jp