Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(eslint-plugin): [prefer-readonly] report if a member's property i…
…s reassigned (#6043)

* fix(eslint-plugin): [prefer-readonly] report if a member's property is reassigned

* format

* Add test case

* fix test case

* fix test case

Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
  • Loading branch information
islandryu and JoshuaKGoldberg committed Nov 26, 2022
1 parent d69fdf4 commit 6e079eb
Show file tree
Hide file tree
Showing 2 changed files with 349 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/eslint-plugin/src/rules/prefer-readonly.ts
Expand Up @@ -119,7 +119,10 @@ export default util.createRule<Options, MessageIds>({
ts.isArrayLiteralExpression(parent.parent))
) {
current = parent;
} else if (ts.isBinaryExpression(parent)) {
} else if (
ts.isBinaryExpression(parent) &&
!ts.isPropertyAccessExpression(current)
) {
return (
parent.left === current &&
parent.operatorToken.kind === ts.SyntaxKind.EqualsToken
Expand Down
345 changes: 345 additions & 0 deletions packages/eslint-plugin/tests/rules/prefer-readonly.test.ts
Expand Up @@ -339,6 +339,34 @@ class Foo {
}
`,
},
{
code: `
class Test {
private testObj = {
prop: '',
};
public test(): void {
this.testObj = '';
}
}
`,
},
{
code: `
class TestObject {
public prop: number;
}
class Test {
private testObj = new TestObject();
public test(): void {
this.testObj = new TestObject();
}
}
`,
},
],
invalid: [
{
Expand Down Expand Up @@ -742,5 +770,322 @@ function ClassWithName<TBase extends new (...args: any[]) => {}>(Base: TBase) {
},
],
},
{
code: `
class Test {
private testObj = {
prop: '',
};
public test(): void {
this.testObj.prop = '';
}
}
`,
output: `
class Test {
private readonly testObj = {
prop: '',
};
public test(): void {
this.testObj.prop = '';
}
}
`,
errors: [
{
data: {
name: 'testObj',
},
line: 3,
messageId: 'preferReadonly',
},
],
},
{
code: `
class TestObject {
public prop: number;
}
class Test {
private testObj = new TestObject();
public test(): void {
this.testObj.prop = 10;
}
}
`,
output: `
class TestObject {
public prop: number;
}
class Test {
private readonly testObj = new TestObject();
public test(): void {
this.testObj.prop = 10;
}
}
`,
errors: [
{
data: {
name: 'testObj',
},
line: 7,
messageId: 'preferReadonly',
},
],
},
{
code: `
class Test {
private testObj = {
prop: '',
};
public test(): void {
this.testObj.prop;
}
}
`,
output: `
class Test {
private readonly testObj = {
prop: '',
};
public test(): void {
this.testObj.prop;
}
}
`,
errors: [
{
data: {
name: 'testObj',
},
line: 3,
messageId: 'preferReadonly',
},
],
},
{
code: `
class Test {
private testObj = {};
public test(): void {
this.testObj?.prop;
}
}
`,
output: `
class Test {
private readonly testObj = {};
public test(): void {
this.testObj?.prop;
}
}
`,
errors: [
{
data: {
name: 'testObj',
},
line: 3,
messageId: 'preferReadonly',
},
],
},
{
code: `
class Test {
private testObj = {};
public test(): void {
this.testObj!.prop;
}
}
`,
output: `
class Test {
private readonly testObj = {};
public test(): void {
this.testObj!.prop;
}
}
`,
errors: [
{
data: {
name: 'testObj',
},
line: 3,
messageId: 'preferReadonly',
},
],
},
{
code: `
class Test {
private testObj = {};
public test(): void {
this.testObj.prop.prop = '';
}
}
`,
output: `
class Test {
private readonly testObj = {};
public test(): void {
this.testObj.prop.prop = '';
}
}
`,
errors: [
{
data: {
name: 'testObj',
},
line: 3,
messageId: 'preferReadonly',
},
],
},
{
code: `
class Test {
private testObj = {};
public test(): void {
this.testObj.prop.doesSomething();
}
}
`,
output: `
class Test {
private readonly testObj = {};
public test(): void {
this.testObj.prop.doesSomething();
}
}
`,
errors: [
{
data: {
name: 'testObj',
},
line: 3,
messageId: 'preferReadonly',
},
],
},
{
code: `
class Test {
private testObj = {};
public test(): void {
this.testObj?.prop.prop;
}
}
`,
output: `
class Test {
private readonly testObj = {};
public test(): void {
this.testObj?.prop.prop;
}
}
`,
errors: [
{
data: {
name: 'testObj',
},
line: 3,
messageId: 'preferReadonly',
},
],
},
{
code: `
class Test {
private testObj = {};
public test(): void {
this.testObj?.prop?.prop;
}
}
`,
output: `
class Test {
private readonly testObj = {};
public test(): void {
this.testObj?.prop?.prop;
}
}
`,
errors: [
{
data: {
name: 'testObj',
},
line: 3,
messageId: 'preferReadonly',
},
],
},
{
code: `
class Test {
private testObj = {};
public test(): void {
this.testObj.prop?.prop;
}
}
`,
output: `
class Test {
private readonly testObj = {};
public test(): void {
this.testObj.prop?.prop;
}
}
`,
errors: [
{
data: {
name: 'testObj',
},
line: 3,
messageId: 'preferReadonly',
},
],
},
{
code: `
class Test {
private testObj = {};
public test(): void {
this.testObj!.prop?.prop;
}
}
`,
output: `
class Test {
private readonly testObj = {};
public test(): void {
this.testObj!.prop?.prop;
}
}
`,
errors: [
{
data: {
name: 'testObj',
},
line: 3,
messageId: 'preferReadonly',
},
],
},
],
});

0 comments on commit 6e079eb

Please sign in to comment.