Skip to content

Commit fa0df7b

Browse files
authored
name-replacements: Handle TypeScript parameter properties (#3575)
1 parent 1961c6d commit fa0df7b

3 files changed

Lines changed: 76 additions & 1 deletion

File tree

docs/rules/name-replacements.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ You can find the default replacements [here](https://github.com/sindresorhus/esl
1717

1818
This rule is automatically fixable only for variable names with exactly one replacement defined. Ambiguous variable names and checked property names can be manually fixed with editor suggestions when the rename is local to source text. Filename reports and exported-name property reports do not provide editor suggestions.
1919

20-
Parameter names do not provide autofixes or editor suggestions when the function has an attached JSDoc `@param` comment, as those name references are not normal variable references and would otherwise be left stale. TypeScript type predicate and assertion signature parameter references are updated when the parameter is renamed.
20+
Parameter names do not provide autofixes or editor suggestions when the function has an attached JSDoc `@param` comment, as those name references are not normal variable references and would otherwise be left stale. TypeScript parameter properties also do not provide autofixes or editor suggestions, as the corresponding class property references are not part of the parameter variable's references. TypeScript type predicate and assertion signature parameter references are updated when the parameter is renamed.
2121

2222
## React
2323

rules/name-replacements.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,28 @@ const getParentFunctionLikeNode = identifier => {
320320
}
321321
};
322322

323+
const isTSParameterPropertyName = node => {
324+
if (node.parent.type === 'TSParameterProperty') {
325+
return node.parent.parameter === node;
326+
}
327+
328+
return (
329+
node.parent.type === 'AssignmentPattern'
330+
&& node.parent.left === node
331+
&& node.parent.parent.type === 'TSParameterProperty'
332+
&& node.parent.parent.parameter === node.parent
333+
);
334+
};
335+
323336
const shouldFixParameter = (definition, context) => {
324337
if (definition.type !== 'Parameter') {
325338
return true;
326339
}
327340

341+
if (isTSParameterPropertyName(definition.name)) {
342+
return false;
343+
}
344+
328345
const functionNode = getParentFunctionLikeNode(definition.name);
329346

330347
if (!functionNode) {

test/name-replacements.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,6 +2012,64 @@ test.typescript({
20122012
output: 'const foo = (extraParameters?: string) => {}',
20132013
errors: 1,
20142014
},
2015+
{
2016+
code: outdent`
2017+
class ValidationException {
2018+
constructor(private readonly err: JsonRpcErrorLocal) {}
2019+
2020+
getError(): JsonRpcErrorLocal {
2021+
return this.err;
2022+
}
2023+
}
2024+
`,
2025+
errors: [{
2026+
message: 'The variable `err` should be named `error`. A more descriptive name will do too.',
2027+
suggestions: [],
2028+
}],
2029+
},
2030+
{
2031+
code: outdent`
2032+
class ValidationException {
2033+
constructor(private err = 123) {}
2034+
2035+
getError() {
2036+
return this.err;
2037+
}
2038+
}
2039+
`,
2040+
errors: [{
2041+
message: 'The variable `err` should be named `error`. A more descriptive name will do too.',
2042+
suggestions: [],
2043+
}],
2044+
},
2045+
{
2046+
code: outdent`
2047+
class ValidationException {
2048+
constructor(private readonly e: Error) {}
2049+
2050+
getError(): Error {
2051+
return this.e;
2052+
}
2053+
}
2054+
`,
2055+
errors: [{
2056+
message: 'Please rename the variable `e`. Suggested names are: `error`, `event_`. A more descriptive name will do too.',
2057+
suggestions: [],
2058+
}],
2059+
},
2060+
{
2061+
code: outdent`
2062+
function getError(err = 123) {
2063+
return err;
2064+
}
2065+
`,
2066+
output: outdent`
2067+
function getError(error = 123) {
2068+
return error;
2069+
}
2070+
`,
2071+
errors: 1,
2072+
},
20152073
{
20162074
code: 'const foo = (extr\u{61}Params ? : string) => {}',
20172075
output: 'const foo = (extraParameters?: string) => {}',

0 commit comments

Comments
 (0)