Skip to main content

dot-notation

Enforce dot notation whenever possible.

🔧

此规则报告的一些问题可通过 --fix ESLint 命令行选项自动修复

💭

该规则需要 类型信息 才能运行。


此规则扩展了基本 eslint/dot-notation 规则。 它补充道:

英:This rule extends the base eslint/dot-notation rule. It adds:

  • 支持选择性地忽略计算的 private 和/或 protected 成员访问。
  • 与 TypeScript 的 noPropertyAccessFromIndexSignature 选项兼容。

选项

该规则添加了以下选项:

英:This rule adds the following options:

interface Options extends BaseDotNotationOptions {
allowPrivateClassPropertyAccess?: boolean;
allowProtectedClassPropertyAccess?: boolean;
allowIndexSignaturePropertyAccess?: boolean;
}

const defaultOptions: Options = {
...baseDotNotationDefaultOptions,
allowPrivateClassPropertyAccess: false,
allowProtectedClassPropertyAccess: false,
allowIndexSignaturePropertyAccess: false,
};

如果 TypeScript 编译器选项 noPropertyAccessFromIndexSignature 设置为 true,则此规则始终允许使用方括号表示法来访问具有 string 索引签名的类型的属性,即使 allowIndexSignaturePropertyAccessfalse

英:If the TypeScript compiler option noPropertyAccessFromIndexSignature is set to true, then this rule always allows the use of square bracket notation to access properties of types that have a string index signature, even if allowIndexSignaturePropertyAccess is false.

allowPrivateClassPropertyAccess

allowPrivateClassPropertyAccess 设置为 true 时的正确代码示例:

英:Example of a correct code when allowPrivateClassPropertyAccess is set to true:

class X {
private priv_prop = 123;
}

const x = new X();
x['priv_prop'] = 123;
Open in Playground

allowProtectedClassPropertyAccess

allowProtectedClassPropertyAccess 设置为 true 时的正确代码示例:

英:Example of a correct code when allowProtectedClassPropertyAccess is set to true:

class X {
protected protected_prop = 123;
}

const x = new X();
x['protected_prop'] = 123;
Open in Playground

allowIndexSignaturePropertyAccess

allowIndexSignaturePropertyAccess 设置为 true 时的正确代码示例:

英:Example of correct code when allowIndexSignaturePropertyAccess is set to true:

class X {
[key: string]: number;
}

const x = new X();
x['hello'] = 123;
Open in Playground

如果 TypeScript 编译器选项 noPropertyAccessFromIndexSignature 设置为 true,则始终允许上述代码,即使 allowIndexSignaturePropertyAccessfalse

英:If the TypeScript compiler option noPropertyAccessFromIndexSignature is set to true, then the above code is always allowed, even if allowIndexSignaturePropertyAccess is false.

何时不使用它

如果你出于风格原因特别想要使用两种成员访问类型,或者不希望强制使用一种风格而不是另一种风格,则可以避免此规则。

英:If you specifically want to use both member access kinds for stylistic reasons, or don't wish to enforce one style over the other, you can avoid this rule.

但是,请记住,不一致的风格可能会损害项目的可读性。 我们建议为此规则选择一个最适合你的项目的选项。

英:However, keep in mind that inconsistent style can harm readability in a project. We recommend picking a single option for this rule that works best for your project.

如何使用

.eslintrc.cjs
module.exports = {
"rules": {
// Note: you must disable the base rule as it can report incorrect errors
"dot-notation": "off",
"@typescript-eslint/dot-notation": "error"
}
};
在线运行试试这个规则 ↗

选项

参见 eslint/dot-notation 选项

资源

摘自 ❤️ ESLint 内核