Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -7,6 +7,10 @@ import { | ||
| nullThrows, | ||
| NullThrowsReasons, | ||
| } from '../util'; | ||
| import { | ||
| getMemberHeadLoc, | ||
| getParameterPropertyHeadLoc, | ||
| } from '../util/getMemberHeadLoc'; | ||
| type AccessibilityLevel = | ||
| | 'explicit' // require an accessor (including public) | ||
| @@ -166,7 +170,7 @@ export default createRule<Options, MessageIds>({ | ||
| }); | ||
| } else if (check === 'explicit' && !methodDefinition.accessibility) { | ||
| context.report({ | ||
| loc:getMemberHeadLoc(context.sourceCode,methodDefinition), | ||
| messageId: 'missingAccessibility', | ||
| data: { | ||
| type: nodeType, | ||
| @@ -221,87 +225,6 @@ export default createRule<Options, MessageIds>({ | ||
| return { range: keywordRange, rangeToRemove }; | ||
| } | ||
MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| /** | ||
| * Creates a fixer that adds an accessibility modifier keyword | ||
| */ | ||
| @@ -385,7 +308,7 @@ export default createRule<Options, MessageIds>({ | ||
| !propertyDefinition.accessibility | ||
| ) { | ||
| context.report({ | ||
| loc:getMemberHeadLoc(context.sourceCode,propertyDefinition), | ||
| messageId: 'missingAccessibility', | ||
| data: { | ||
| type: nodeType, | ||
| @@ -422,7 +345,8 @@ export default createRule<Options, MessageIds>({ | ||
| case 'explicit': { | ||
| if (!node.accessibility) { | ||
| context.report({ | ||
| loc: getParameterPropertyHeadLoc( | ||
| context.sourceCode, | ||
| node, | ||
| nodeName, | ||
| ), | ||
| Original file line number | Diff line number | Diff 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, | ||
| }; | ||
| } |
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.