oxc/no-this-in-exported-function 懸念
何を実行するか
エクスポートされた関数内で this を使用することを禁止します。
なぜ悪いのか
ほとんどのバンドラーでは、エクスポートされた関数に対して this の値は保持されません。 関数がエクスポートされ、別のモジュールでインポートされた場合、this は通常、モジュール名前空間オブジェクトではなく undefined になります。これは予期せぬランタイムエラーまたは誤った振る舞いを引き起こす可能性があります。
例
このルールに違反する不正なコードの例:
javascript
export function foo() {
console.log(this);
}
export default function bar() {
this.something();
}
function baz() {
const self = this;
}
export { baz };このルールに準拠する正しいコードの例:
javascript
function foo() {
console.log(this);
}
export const bar = () => {
console.log(this);
};使い方
設定ファイルまたは CLI でこのルールを有効化するには、以下のようにします:
json
{
"rules": {
"oxc/no-this-in-exported-function": "error"
}
}bash
oxlint --deny oxc/no-this-in-exported-function