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] report if a member's property is reassigned#6043

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
5 changes: 4 additions & 1 deletionpackages/eslint-plugin/src/rules/prefer-readonly.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
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 deletionspackages/eslint-plugin/tests/rules/prefer-readonly.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
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 DownExpand 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',
},
],
},
Copy link
Member

@JoshuaKGoldbergJoshuaKGoldbergNov 19, 2022
edited
Loading

Choose a reason for hiding this comment

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

A few more test cases to consider...

this.testObj.prop;this.testObj?.prop;this.testObj!.prop;this.testObj!.prop='';this.testObj.prop.prop='';this.testObj.prop.doesSomething();this.testObj?.prop.prop;this.testObj?.prop?.prop;this.testObj.prop?.prop;this.testObj!.prop?.prop;

For any change to rule logic, it's generally good to try to add bothvalid andinvalid test cases.

Choose a reason for hiding this comment

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

@islandryu by "test cases" I do mean cases plural - we generally try to keep these separate, so that each test case tests one case. It's easy to miss incorrect or mismatched errors when they're all lumped together. Could you split them up please?

{
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',
},
],
},
],
});

[8]ページ先頭

©2009-2025 Movatter.jp