Skip to content

Commit

Permalink
prevent-abbreviations: Fix crash with TypeScript parser (#913)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Dec 7, 2020
1 parent dfc1ce0 commit 9d5eca6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion rules/utils/rename-identifier.js
Expand Up @@ -28,7 +28,10 @@ function renameIdentifier(identifier, name, fixer, sourceCode) {

// `typeAnnotation`
if (identifier.typeAnnotation) {
return fixer.replaceText(identifier, `${name}${identifier.optional ? '?' : ''}${sourceCode.getText(identifier.typeAnnotation)}`);
return fixer.replaceTextRange(
[identifier.range[0], identifier.typeAnnotation.range[0]],
`${name}${identifier.optional ? '?' : ''}`
);
}

return fixer.replaceText(identifier, name);
Expand Down
28 changes: 28 additions & 0 deletions test/prevent-abbreviations.js
Expand Up @@ -1793,6 +1793,34 @@ runTest.typescript({
code: 'const foo = (extraParams?: string) => {}',
output: 'const foo = (extraParameters?: string) => {}',
errors: createErrors()
},
{
code: 'const foo = (extr\u0061Params ? : string) => {}',
output: 'const foo = (extraParameters?: string) => {}',
errors: 1
},

// #912
{
code: outdent`
interface Prop {
id: number;
}
const Prop: Prop = { id: 1 };
export default Prop;
`,
output: outdent`
interface Property {
id: number;
}
const Property: Property = { id: 1 };
export default Property;
`,
errors: 1
}
]
});

0 comments on commit 9d5eca6

Please sign in to comment.