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