# @droppedcode/eslint-plugin-jasmine-memoryleak-linter 中文文档

规范地址：https://npmcn.com/package/%40droppedcode/eslint-plugin-jasmine-memoryleak-linter/

版本：1\.0\.13
npm 元数据更新时间：2024-11-17T08:34:15.141Z
中文译文更新时间：2026-11-12T23:32:07.095Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40droppedcode/eslint-plugin-jasmine-memoryleak-linter
原文对照：https://npmcn.com/package/%40droppedcode/eslint-plugin-jasmine-memoryleak-linter/?lang=original
项目主页：https://github.com/droppedcode/eslint-plugin-jasmine-memoryleak-linter
源码仓库：https://github.com/droppedcode/eslint-plugin-jasmine-memoryleak-linter

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：199 kB，51 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- peer 依赖：@typescript-eslint/utils ^8\.14\.0
- 首次发布：2023-07-09T10:35:51.901Z
- 最新版本发布：2024-11-17T08:34:14.966Z
- 近 30 天下载量：3026（2026-10-13 至 2026-11-11，npm 官方下载统计）
- 已发布版本数：14（近 12 个月 0 个）

## 中文简介

一个ESLint扩展以检测并修复茉莉花/摩卡相關内存出漏.

## 安装

```sh
npm install @droppedcode/eslint-plugin-jasmine-memoryleak-linter
```

也可以使用：`pnpm add @droppedcode/eslint-plugin-jasmine-memoryleak-linter`、`yarn add @droppedcode/eslint-plugin-jasmine-memoryleak-linter`、`bun add @droppedcode/eslint-plugin-jasmine-memoryleak-linter`

## README

# 寻找和修复茉莉花内存出漏的 ESLint 插件

这个 Eslint 插件在使用茉莉花时查找出错(或者摩卡,但这仅仅是基于相同命名惯例的巧合) .

## 配置

要使用通常的方式配置 ESLint :

-   添加为插件
-   添加要使用的规则( 或使用推荐的规则)

```json
{
  // ...
  "plugins": [
    // ...
    "@droppedcode/eslint-plugin-jasmine-memoryleak-linter"
  ],
  "rules": {
    // ...
    "@droppedcode/jasmine-memoryleak-linter/describe-declarations": "error",
    "@droppedcode/jasmine-memoryleak-linter/no-cleanup-before-each-rule": "error",
    "@droppedcode/jasmine-memoryleak-linter/no-cleanup-before-all-rule": "error"
  }
}
```

或使用预定义的配置

```json
{
  // ...
  "extends": [
    // ...
    "@droppedcode/eslint-plugin-jasmine-memoryleak-linter/recommended"
  ]
}
```

## 规则

所有规则都有固定的构建,但通常会就如何解决问题提出多条建议.

### (描述中有声明)

寻找在“描述”方法中宣布变量并初始化的个案:

```ts
describe('test-describe-let', () => {
  let a = {}; // This will trigger it

  it('test', () => {
    // use a...
  });
});
```

可以通过多种方式固定:

#### 将声明移至“ it” 块

将声明逻辑移到“ it”块。 这将导致变量是局部的,GC会清理.

```ts
describe('test-describe-let', () => {
  it('test', () => {
    let a = {};
    // use a...
  });
});
```

#### 在“ each” 中初始化值, 在“ each” 中不引用

(这是默认的固定. )

```ts
describe('test-describe-let', () => {
  let a;

  beforeEach(() => {
    a = {};
  });

  afterEach(() => {
    a = undefined;
  });

  it('test', () => {
    // use a...
  });
});
```

#### 在“ Arefore All” 中初始化值, 在“ Arefore All” 中不再引用

```ts
describe('test-describe-let', () => {
  let a;

  beforeAll(() => {
    a = {};
  });

  afterAll(() => {
    a = undefined;
  });

  it('test', () => {
    // use a...
  });
});
```

#### 描述中的声明 (描述中有声明,用于初始化,但被捕获. )

寻找变量在"描述"方法中被宣布并初始化,但在另一个描述中仅用于初始化的情况:

```ts
describe('test-describe-let', () => {
  let a = [1, 2]; // This will trigger it

  describe('inner', () => {
    a.forEach(() => {});
  });
});
```

```ts
describe('test-describe-let', () => {
  describe('inner', () => {
    let a = [1, 2];

    a.forEach(() => {});
  });
});
```

#### 选项

```ts
export type InDescribeRuleOptions = {
  /** Names of the functions that the rule is looking for. */
  functionNames: string[];
  /** Names of the functions that can initialize values before all tests. */
  initializationAllFunctionNames: string[];
  /** Names of the functions that can initialize values before each test. */
  initializationEachFunctionNames: string[];
  /** Names of the functions that can unreference values before all tests. */
  unreferenceAllFunctionNames: string[];
  /** Names of the functions that can unreference values before each test. */
  unreferenceEachFunctionNames: string[];
  /** Names of the functions that can use the values. */
  testFunctionNames: string[];
  /** Prefer using initialization in all function. */
  preferAll: boolean;
};
```

默认选项是茉莉花和摩卡命名惯例的合并,但如果偏好,则有单独的配置.

```ts
export const defaultJasmineInDescribeRuleOptions: InDescribeRuleOptions = {
  functionNames: ['describe', 'fdescribe', 'xdescribe'],
  initializationEachFunctionNames: ['beforeEach'],
  initializationAllFunctionNames: ['beforeAll'],
  unreferenceEachFunctionNames: ['afterEach'],
  unreferenceAllFunctionNames: ['afterAll'],
  testFunctionNames: ['it', 'fit', 'xit'],
  preferAll: false,
};

export const defaultMochaInDescribeRuleOptions: InDescribeRuleOptions = {
  functionNames: ['describe', 'fdescribe', 'xdescribe'],
  initializationEachFunctionNames: ['beforeEach'],
  initializationAllFunctionNames: ['before'],
  unreferenceEachFunctionNames: ['afterEach'],
  unreferenceAllFunctionNames: ['after'],
  testFunctionNames: ['it', 'test', 'fit', 'xit'],
  preferAll: false,
};

export const defaultInDescribeRuleOptions = {
  functionNames: ['describe', 'fdescribe', 'xdescribe'],
  initializationEachFunctionNames: ['beforeEach'],
  initializationAllFunctionNames: ['beforeAll', 'before'],
  unreferenceEachFunctionNames: ['afterEach'],
  unreferenceAllFunctionNames: ['afterAll', 'after'],
  testFunctionNames: ['it', 'test', 'fit', 'xit'],
  preferAll: false,
};
```

### (在描述中存在任务。 )

寻找在描述中向变量转让但不在宣言中的情况.

```ts
describe('test-describe-let', () => {
  let a;
  a = {}; // This will trigger it

  it('test', () => {
    // use a...
  });
});
```

#### 在“ each” 中初始化值, 在“ each” 中不引用

(这是默认的固定. )

```ts
describe('test-describe-let', () => {
  let a;

  beforeEach(() => {
    a = {};
  });

  afterEach(() => {
    a = undefined;
  });

  it('test', () => {
    // use a...
  });
});
```

#### 在“ Arefore All” 中初始化值, 在“ Arefore All” 中不再引用

```ts
describe('test-describe-let', () => {
  let a;

  beforeAll(() => {
    a = {};
  });

  afterAll(() => {
    a = undefined;
  });

  it('test', () => {
    // use a...
  });
});
```

#### 选项

与声明说明规则相同.

### 不得在每一规则之前进行清理(在“每个规则之前”中有任务,但在“每个规则之后”中有任务)

当我们在每次调用之前给变量分配一个值时, 查找大小写, 但是没有, 或最后的调用不是调用 .

如果我们在"后"中贬低它,这个规则仍然会触发(初始化和清理应该在同一级别上).

```ts
describe('test-describe-let', () => {
  let a;

  beforeEach(() => {
    a = {}; // This will trigger it
  });
});
```

在每次调用后, 修补会不参考变量 .

```ts
describe('test-describe-let', () => {
  let a;

  beforeEach(() => {
    a = {};
  });

  afterEach(() => {
    a = undefined;
  });
});
```

#### 选项

```ts
export const defaultNoCleanupEachOptions = {
    initializationFunctionNames: ['beforeEach'],
    unreferenceFunctionNames: ['afterEach']
};

There are mocha and jasmine variant of the options.
```

### (在"Arefore All"中有任务,但在"Arefore All"中没有清理)

当我们在 All 呼叫中指定一个变量的值时, 查找大小写, 但是没有, 或最后一个任务不是引用 .

如果我们在"后Each"中贬低它,这个规则仍然会触发(初始化和清理应该在同一级别上进行).

```ts
describe('test-describe-let', () => {
  let a;

  beforeAll(() => {
    a = {}; // This will trigger it
  });
});
```

在每次调用后, 修补会不参考变量 .

```ts
describe('test-describe-let', () => {
  let a;

  beforeAll(() => {
    a = {};
  });

  afterAll(() => {
    a = undefined;
  });
});
```

#### 选项

```ts
export const defaultNoCleanupAllOptions = {
    initializationFunctionNames: ['beforeAll', 'before'],
    unreferenceFunctionNames: ['afterAll', 'after']
};

There are mocha and jasmine variant of the options.
```

### 不清理它的规则 ("它"中有任务,"它"中没有清理)

当我们在 All 呼叫中指定一个变量的值时, 查找大小写, 但是没有, 或最后一个任务不是引用 .

如果我们在"后Each"中贬低它,这个规则仍然会触发(初始化和清理应该在同一级别上进行).

```ts
describe('test-describe-let', () => {
  let a;

  beforeAll(() => {
    a = {}; // This will trigger it
  });
});
```

在每次调用后, 修补会不参考变量 .

```ts
describe('test-describe-let', () => {
  let a;

  beforeAll(() => {
    a = {};
  });

  afterAll(() => {
    a = undefined;
  });
});
```

#### 选项

```ts
export const defaultNoCleanupTestOptions = {
    initializationFunctionNames: ['it', 'fit', 'xit', 'test'],
    unreferenceFunctionNames: ['it', 'afterEach', 'afterAll', 'after'],
};

There are mocha and jasmine variant of the options.

```

## 如何建设

-   下载源或复制源
-   时间轴:
-   npm 运行构建( 或构建: 样本检查)

## 如何调试

-   vscode F5( 或调试)( 在选中的滑稽测试文件中) 或运行 `npm test`

## 样本

这些样本是一个配置的迷你项目,它只使用这些规则,所以可以在行动中看到. 要尝试打开样本文件夹, 请不要从主文件夹运行, 因为 ESLint 会有错误的结果 .

## 已知问题

-   " (中文(简体) )`var`" 某些情况下的声明(当它没有象使用时)`let`")可以打破一些逻辑.
-   Mocha方法后缀不支持,例如:. `describe.only`。 。 。 .
-   可变捕获外部测试方法可能存在问题,例如,在描述中使用forEach来通过测试数据并调用每个成员,或者在它的名称中使用测试数据来描述块.

## 常见问题

### @droppedcode/eslint-plugin-jasmine-memoryleak-linter 是什么？

一个ESLint扩展以检测并修复茉莉花/摩卡相關内存出漏\.

### 如何安装 @droppedcode/eslint-plugin-jasmine-memoryleak-linter？

在项目目录运行 npm install @droppedcode/eslint-plugin-jasmine-memoryleak-linter，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：@typescript-eslint/utils（^8\.14\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @droppedcode/eslint-plugin-jasmine-memoryleak-linter 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @droppedcode/eslint-plugin-jasmine-memoryleak-linter 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @droppedcode/eslint-plugin-jasmine-memoryleak-linter 可以商用吗？

@droppedcode/eslint-plugin-jasmine-memoryleak-linter 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @droppedcode/eslint-plugin-jasmine-memoryleak-linter 的下载量有多少？

根据 npm 官方下载统计，@droppedcode/eslint-plugin-jasmine-memoryleak-linter 在 2026/10/13 – 2026/11/11 这 30 天内被下载 3,026 次，日均约 101 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @droppedcode/eslint-plugin-jasmine-memoryleak-linter 还在维护吗？

最新版本 1\.0\.13 发布于 2024/11/17（2 年前）；至今共发布 14 个版本，其中过去 12 个月发布了 0 个；首次发布于 2023/07/09。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

