tsifdef 1.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/CHANGELOG.zh-CN.md +22 -0
- package/CONTRIBUTING.md +52 -0
- package/INTEGRATION.md +166 -0
- package/INTEGRATION.zh-CN.md +159 -0
- package/LICENSE +210 -0
- package/README.md +150 -0
- package/README.zh-CN.md +141 -0
- package/SECURITY.md +15 -0
- package/assets/icon-mono.svg +15 -0
- package/assets/icon.png +0 -0
- package/assets/icon.svg +16 -0
- package/dist/cli/build.d.ts +70 -0
- package/dist/cli/build.js +298 -0
- package/dist/cli/config.d.ts +30 -0
- package/dist/cli/config.js +135 -0
- package/dist/cli/diagnostics.d.ts +3 -0
- package/dist/cli/diagnostics.js +31 -0
- package/dist/cli/index.d.ts +6 -0
- package/dist/cli/index.js +35 -0
- package/dist/cli/main.d.ts +9 -0
- package/dist/cli/main.js +165 -0
- package/dist/cli/precompile.d.ts +38 -0
- package/dist/cli/precompile.js +192 -0
- package/dist/cli/source-files.d.ts +10 -0
- package/dist/cli/source-files.js +75 -0
- package/dist/cli/watch.d.ts +34 -0
- package/dist/cli/watch.js +183 -0
- package/dist/core/conditional.d.ts +18 -0
- package/dist/core/conditional.js +126 -0
- package/dist/core/expression.d.ts +44 -0
- package/dist/core/expression.js +224 -0
- package/dist/core/index.d.ts +7 -0
- package/dist/core/index.js +37 -0
- package/dist/core/projection.d.ts +15 -0
- package/dist/core/projection.js +99 -0
- package/dist/core/scanner.d.ts +33 -0
- package/dist/core/scanner.js +344 -0
- package/dist/eslint/package.d.ts +10 -0
- package/dist/eslint/package.js +55 -0
- package/dist/eslint/parser.d.ts +35 -0
- package/dist/eslint/parser.js +59 -0
- package/dist/eslint/plugin.d.ts +78 -0
- package/dist/eslint/plugin.js +229 -0
- package/dist/eslint/projection.d.ts +11 -0
- package/dist/eslint/projection.js +116 -0
- package/dist/tsserver/host-projection.d.ts +45 -0
- package/dist/tsserver/host-projection.js +160 -0
- package/dist/tsserver/index.d.ts +3 -0
- package/dist/tsserver/index.js +32 -0
- package/dist/tsserver/plugin.d.ts +15 -0
- package/dist/tsserver/plugin.js +129 -0
- package/dist/tsserver/project-controller.d.ts +37 -0
- package/dist/tsserver/project-controller.js +60 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.js +20 -0
- package/dist/vscode/document-analysis.d.ts +62 -0
- package/dist/vscode/document-analysis.js +111 -0
- package/dist/vscode/extension.d.ts +133 -0
- package/dist/vscode/extension.js +241 -0
- package/dist/vscode/host.d.ts +79 -0
- package/dist/vscode/host.js +16 -0
- package/dist/vscode/index.d.ts +7 -0
- package/dist/vscode/index.js +36 -0
- package/dist/vscode/macro-presentation.d.ts +43 -0
- package/dist/vscode/macro-presentation.js +130 -0
- package/dist/vscode/package-profile.d.ts +27 -0
- package/dist/vscode/package-profile.js +142 -0
- package/dist/vscode/profile-state.d.ts +22 -0
- package/dist/vscode/profile-state.js +81 -0
- package/package.json +129 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## v1.1.8 - 2026-09-11
|
|
4
|
+
|
|
5
|
+
First public release.
|
|
6
|
+
|
|
7
|
+
- `#if` / `#elif` / `#else` / `#endif` conditional compilation for TypeScript,
|
|
8
|
+
driven by a Profile: a JSON array of enabled macro names referenced from
|
|
9
|
+
`package.json`.
|
|
10
|
+
- `tsifdef build` compiles through TypeScript with inactive branches masked at
|
|
11
|
+
equal length, so emitted `.js`, `.js.map` sources, `.d.ts`, and error paths
|
|
12
|
+
point at the original files. Incremental builds and `--watch` are supported.
|
|
13
|
+
- VSCode extension that dims and folds inactive branches and shows the active
|
|
14
|
+
Profile in the status bar.
|
|
15
|
+
- tsserver plugin, shipped in the same VSIX, so the language service reports no
|
|
16
|
+
errors on inactive code and treats unknown macros as `false`.
|
|
17
|
+
- ESLint processor and parser wrapper in the same npm package. Diagnostics keep
|
|
18
|
+
their real line and column, autofix and quick fixes keep working, and
|
|
19
|
+
formatting rules such as `prettier/prettier` can stay enabled.
|
|
20
|
+
- Flat config via `tsifdef.configs["flat/recommended"]`, and legacy `.eslintrc`
|
|
21
|
+
via `extends: ["plugin:tsifdef/recommended"]`.
|
|
22
|
+
- Supports ESLint 8.57 and 9 with `@typescript-eslint/parser` 5 through 8, both
|
|
23
|
+
as optional peer dependencies.
|
|
24
|
+
- Portable specification and implementation-neutral conformance fixtures under
|
|
25
|
+
`spec/`.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# 更新日志
|
|
2
|
+
|
|
3
|
+
## v1.1.8 - 2026-09-11
|
|
4
|
+
|
|
5
|
+
首个公开版本。
|
|
6
|
+
|
|
7
|
+
- 为 TypeScript 提供 `#if` / `#elif` / `#else` / `#endif` 条件编译,由 Profile
|
|
8
|
+
驱动:一个列出已启用宏名的 JSON 数组,通过 `package.json` 引用。
|
|
9
|
+
- `tsifdef build` 以等长遮盖未激活分支的方式交给 TypeScript 编译,因此产出的
|
|
10
|
+
`.js`、`.js.map` sources、`.d.ts` 和报错路径都指向原始文件。支持增量编译和
|
|
11
|
+
`--watch`。
|
|
12
|
+
- VSCode 扩展:置灰并折叠未激活分支,在状态栏显示当前 Profile。
|
|
13
|
+
- tsserver 插件随同一个 VSIX 发布,语言服务不再对未激活代码报错,未知宏按
|
|
14
|
+
`false` 处理。
|
|
15
|
+
- ESLint processor 和 parser wrapper 包含在同一个 npm 包中。诊断保留真实行列
|
|
16
|
+
位置,自动修复和快速修复正常工作,`prettier/prettier` 等格式化规则可以保持
|
|
17
|
+
开启。
|
|
18
|
+
- Flat Config 使用 `tsifdef.configs["flat/recommended"]`;旧版 `.eslintrc` 使用
|
|
19
|
+
`extends: ["plugin:tsifdef/recommended"]`。
|
|
20
|
+
- 支持 ESLint 8.57 和 9,以及 `@typescript-eslint/parser` 5 至 8,两者均为
|
|
21
|
+
optional peer dependency。
|
|
22
|
+
- `spec/` 下提供可移植规范和与实现无关的一致性用例。
|
package/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# Contributing to TSIfDef
|
|
2
|
+
|
|
3
|
+
Thank you for helping improve TSIfDef.
|
|
4
|
+
|
|
5
|
+
## Development setup
|
|
6
|
+
|
|
7
|
+
Requirements:
|
|
8
|
+
|
|
9
|
+
- Node.js 18.17 or newer
|
|
10
|
+
- npm
|
|
11
|
+
- VSCode 1.85 or newer for extension changes
|
|
12
|
+
|
|
13
|
+
Install dependencies and build the project:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install
|
|
17
|
+
npm run build
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Making changes
|
|
21
|
+
|
|
22
|
+
- Keep changes focused on one problem.
|
|
23
|
+
- Add or update tests when behavior changes.
|
|
24
|
+
- Use English for code comments and public API documentation.
|
|
25
|
+
- Do not commit generated `dist/`, `.test-dist/`, `release/`, or `node_modules/` files.
|
|
26
|
+
- Update `CHANGELOG.md` for user-visible changes.
|
|
27
|
+
|
|
28
|
+
Run the full test suite before opening a pull request:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm test
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Pull requests must pass the `Test` and `Build` workflows. Maintainers publish
|
|
35
|
+
versioned artifacts through the manually triggered `Release` workflow.
|
|
36
|
+
|
|
37
|
+
Maintainers can verify release artifacts with:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm run release
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
This command builds, packages, installs, and smoke-tests both the npm tarball and
|
|
44
|
+
the VSIX.
|
|
45
|
+
|
|
46
|
+
## Pull requests
|
|
47
|
+
|
|
48
|
+
Describe the problem, the chosen solution, and any compatibility impact. Keep
|
|
49
|
+
commits reviewable and avoid unrelated formatting changes.
|
|
50
|
+
|
|
51
|
+
Report security issues through the process in [`SECURITY.md`](./SECURITY.md),
|
|
52
|
+
not through a public issue.
|
package/INTEGRATION.md
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# Integrating TSIfDef into a Project
|
|
2
|
+
|
|
3
|
+
Language file: [`INTEGRATION.zh-CN.md`](./INTEGRATION.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
How to adopt TSIfDef in a TypeScript project: build, editor, and lint. The three
|
|
6
|
+
are independent — adopt only the pieces you need. All three read the same
|
|
7
|
+
Profile.
|
|
8
|
+
|
|
9
|
+
## 1. Concepts
|
|
10
|
+
|
|
11
|
+
- A **macro** is a plain identifier such as `BROWSER` or `EXPERIMENTAL`.
|
|
12
|
+
- A **Profile** is a JSON array of the enabled macro names. Any name not listed
|
|
13
|
+
evaluates to `false`; unknown names are not an error.
|
|
14
|
+
- `package.json` holds the single pointer to the active Profile via the
|
|
15
|
+
`tsifdef` field. Build, VSCode, tsserver, and ESLint all read it.
|
|
16
|
+
|
|
17
|
+
```jsonc
|
|
18
|
+
// package.json
|
|
19
|
+
{
|
|
20
|
+
"tsifdef": "./Profiles/browser.json",
|
|
21
|
+
"scripts": {
|
|
22
|
+
"compile": "tsifdef build"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```jsonc
|
|
28
|
+
// Profiles/browser.json
|
|
29
|
+
["BROWSER", "EXPERIMENTAL"]
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
How the Profile is produced — by hand, by a script, or by another toolchain — is
|
|
33
|
+
up to your project. If it is generated, keep it out of source control and make
|
|
34
|
+
the build fail when it is missing: a stale Profile silently produces wrong
|
|
35
|
+
output.
|
|
36
|
+
|
|
37
|
+
## 2. Build
|
|
38
|
+
|
|
39
|
+
Install the package and compile with `tsifdef build` instead of `tsc`:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
npm install -D tsifdef
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```jsonc
|
|
46
|
+
"scripts": {
|
|
47
|
+
"compile": "tsifdef build",
|
|
48
|
+
"watch": "tsifdef build --watch"
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Emitted `.js.map` sources, `.d.ts`, and error paths all point at your original
|
|
53
|
+
files, so nothing downstream needs to know TSIfDef ran.
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
tsifdef build -p ./custom.tsconfig.json
|
|
57
|
+
tsifdef build -p ./tsconfig.json -- --module commonjs --outDir dist
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
`-p`/`--project` selects a tsconfig; arguments after `--` are tsc options that
|
|
61
|
+
override it. Switching Profiles triggers a full rebuild automatically.
|
|
62
|
+
`outFile` and composite references (`tsc -b`) are not supported.
|
|
63
|
+
|
|
64
|
+
## 3. Editor (VSCode)
|
|
65
|
+
|
|
66
|
+
Two pieces make `#if` gray out and stop erroring:
|
|
67
|
+
|
|
68
|
+
1. **VSCode extension** — install the TSIfDef VSIX. It grays out and folds
|
|
69
|
+
inactive branches and shows the active Profile in the status bar.
|
|
70
|
+
2. **tsserver plugin** — ships inside that VSIX and makes the language service
|
|
71
|
+
ignore inactive code. It loads through the workspace TypeScript version, so
|
|
72
|
+
point the workspace at its local TypeScript:
|
|
73
|
+
|
|
74
|
+
```jsonc
|
|
75
|
+
// .vscode/settings.json (or the *.code-workspace settings)
|
|
76
|
+
{
|
|
77
|
+
"typescript.tsdk": "node_modules/typescript/lib",
|
|
78
|
+
"typescript.enablePromptUseWorkspaceTsdk": true
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Then run `TypeScript: Restart TS Server`. Inactive code no longer errors, and
|
|
83
|
+
`#if UNKNOWN_MACRO` is treated as `false` rather than "unknown macro".
|
|
84
|
+
|
|
85
|
+
Run `TypeScript: Restart TS Server` after upgrading the extension too — the
|
|
86
|
+
running server keeps the old plugin until it restarts.
|
|
87
|
+
|
|
88
|
+
## 4. ESLint
|
|
89
|
+
|
|
90
|
+
Without TSIfDef, raw `#if` lines are reported as parse errors. The ESLint
|
|
91
|
+
processor is already in the `tsifdef` package installed for the build; no second
|
|
92
|
+
package is required.
|
|
93
|
+
|
|
94
|
+
Add the recommended config after the project's existing TypeScript flat config:
|
|
95
|
+
|
|
96
|
+
```javascript
|
|
97
|
+
// eslint.config.mjs
|
|
98
|
+
import tsParser from "@typescript-eslint/parser";
|
|
99
|
+
import tsifdef from "tsifdef/eslint-plugin";
|
|
100
|
+
|
|
101
|
+
export default [
|
|
102
|
+
{
|
|
103
|
+
files: ["**/*.{ts,tsx,mts,cts}"],
|
|
104
|
+
languageOptions: { parser: tsParser },
|
|
105
|
+
},
|
|
106
|
+
tsifdef.configs["flat/recommended"],
|
|
107
|
+
];
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Keep the project's normal `@typescript-eslint/parser` as the top-level parser so
|
|
111
|
+
editor integrations still recognize TypeScript validation.
|
|
112
|
+
|
|
113
|
+
No rules need to be turned off. Diagnostics report at their real line and
|
|
114
|
+
column, autofix and editor quick fixes work, and formatting rules such as
|
|
115
|
+
`prettier/prettier` stay enabled: inactive branches never produce formatting
|
|
116
|
+
complaints, while real issues in active code are still reported normally. The
|
|
117
|
+
only thing TSIfDef withholds is a fix that would overwrite a `#if` line — that
|
|
118
|
+
diagnostic is still reported, just without the automatic repair.
|
|
119
|
+
|
|
120
|
+
TSIfDef supports ESLint 8.57 and 9 with `@typescript-eslint/parser` 5 through 8.
|
|
121
|
+
Both are optional peer dependencies, so the host project controls their versions.
|
|
122
|
+
|
|
123
|
+
### Legacy `.eslintrc`
|
|
124
|
+
|
|
125
|
+
The legacy config system resolves `tsifdef` as a package named
|
|
126
|
+
`eslint-plugin-tsifdef`, so install the same package under that alias:
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
npm install -D eslint-plugin-tsifdef@npm:tsifdef
|
|
130
|
+
# or, for an offline release artifact:
|
|
131
|
+
npm install -D eslint-plugin-tsifdef@file:./tsifdef-<version>.tgz
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
```jsonc
|
|
135
|
+
// .eslintrc.json
|
|
136
|
+
{
|
|
137
|
+
"parser": "@typescript-eslint/parser",
|
|
138
|
+
"extends": ["plugin:tsifdef/recommended"]
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
That sets the plugin, the processor, and the `import/parsers` setting. Put it
|
|
143
|
+
**last** in `extends` if the project also extends `plugin:import/typescript`:
|
|
144
|
+
that config registers `@typescript-eslint/parser` for the same extensions, and
|
|
145
|
+
`eslint-plugin-import` uses whichever parser is listed first.
|
|
146
|
+
|
|
147
|
+
To wire the pieces up by hand instead:
|
|
148
|
+
|
|
149
|
+
```jsonc
|
|
150
|
+
// .eslintrc.json
|
|
151
|
+
{
|
|
152
|
+
"parser": "@typescript-eslint/parser",
|
|
153
|
+
"plugins": ["tsifdef"],
|
|
154
|
+
"overrides": [
|
|
155
|
+
{
|
|
156
|
+
"files": ["*.ts", "*.mts", "*.cts", "*.tsx"],
|
|
157
|
+
"processor": "tsifdef/macros"
|
|
158
|
+
}
|
|
159
|
+
],
|
|
160
|
+
"settings": {
|
|
161
|
+
"import/parsers": {
|
|
162
|
+
"eslint-plugin-tsifdef/parser": [".ts", ".tsx", ".mts", ".cts"]
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
```
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# 在项目中集成 TSIfDef
|
|
2
|
+
|
|
3
|
+
Language file: [`INTEGRATION.md`](./INTEGRATION.md)
|
|
4
|
+
|
|
5
|
+
如何在 TypeScript 项目里接入 TSIfDef:构建、编辑器、lint。三者相互独立,按需采用
|
|
6
|
+
其中任意部分即可。它们读同一个 Profile。
|
|
7
|
+
|
|
8
|
+
## 1. 概念
|
|
9
|
+
|
|
10
|
+
- **宏**是一个普通标识符,例如 `BROWSER` 或 `EXPERIMENTAL`。
|
|
11
|
+
- **Profile** 是一个 JSON 数组,列出已启用的宏名。未列出的名字求值为 `false`;
|
|
12
|
+
未知名字不报错。
|
|
13
|
+
- `package.json` 通过 `tsifdef` 字段持有指向当前 Profile 的唯一指针。构建、
|
|
14
|
+
VSCode、tsserver 和 ESLint 都从它读取。
|
|
15
|
+
|
|
16
|
+
```jsonc
|
|
17
|
+
// package.json
|
|
18
|
+
{
|
|
19
|
+
"tsifdef": "./Profiles/browser.json",
|
|
20
|
+
"scripts": {
|
|
21
|
+
"compile": "tsifdef build"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```jsonc
|
|
27
|
+
// Profiles/browser.json
|
|
28
|
+
["BROWSER", "EXPERIMENTAL"]
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Profile 如何产生——手写、脚本生成,或由另一套工具链生成——由你的项目决定。如果
|
|
32
|
+
它是生成物,请不要纳入源码控制,并让构建在文件缺失时失败:过期的 Profile 会静默
|
|
33
|
+
产出错误输出。
|
|
34
|
+
|
|
35
|
+
## 2. 构建
|
|
36
|
+
|
|
37
|
+
安装包,用 `tsifdef build` 代替 `tsc` 编译:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install -D tsifdef
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
```jsonc
|
|
44
|
+
"scripts": {
|
|
45
|
+
"compile": "tsifdef build",
|
|
46
|
+
"watch": "tsifdef build --watch"
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
emit 出的 `.js.map` sources、`.d.ts` 和报错路径都指向你的原始文件,因此下游工具
|
|
51
|
+
无需知道 TSIfDef 参与过构建。
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
tsifdef build -p ./custom.tsconfig.json
|
|
55
|
+
tsifdef build -p ./tsconfig.json -- --module commonjs --outDir dist
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
`-p`/`--project` 用于选择 tsconfig;`--` 之后的参数作为 tsc 选项覆盖它。切换
|
|
59
|
+
Profile 会自动触发全量重编。不支持 `outFile` 和 composite references(`tsc -b`)。
|
|
60
|
+
|
|
61
|
+
## 3. 编辑器(VSCode)
|
|
62
|
+
|
|
63
|
+
让 `#if` 置灰、不再报错需要两部分:
|
|
64
|
+
|
|
65
|
+
1. **VSCode 扩展**——安装 TSIfDef VSIX。它置灰并折叠未激活分支,并在状态栏显示
|
|
66
|
+
当前 Profile。
|
|
67
|
+
2. **tsserver 插件**——随该 VSIX 一起发布,让语言服务忽略未激活代码。它通过
|
|
68
|
+
工作区 TypeScript 版本加载,因此需要将工作区指向其本地 TypeScript:
|
|
69
|
+
|
|
70
|
+
```jsonc
|
|
71
|
+
// .vscode/settings.json (或 *.code-workspace 的 settings)
|
|
72
|
+
{
|
|
73
|
+
"typescript.tsdk": "node_modules/typescript/lib",
|
|
74
|
+
"typescript.enablePromptUseWorkspaceTsdk": true
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
然后运行 `TypeScript: Restart TS Server`。之后未激活代码不再报错,
|
|
79
|
+
`#if UNKNOWN_MACRO` 按 `false` 处理而非“未知宏”。
|
|
80
|
+
|
|
81
|
+
升级扩展后同样要执行 `TypeScript: Restart TS Server`——正在运行的 server 会一直
|
|
82
|
+
使用旧插件,直到重启为止。
|
|
83
|
+
|
|
84
|
+
## 4. ESLint
|
|
85
|
+
|
|
86
|
+
不接 TSIfDef 时,原始的 `#if` 行会被报成解析错误。ESLint processor 已包含在构建
|
|
87
|
+
所安装的 `tsifdef` 包中,无需安装第二个包。
|
|
88
|
+
|
|
89
|
+
将推荐配置放在项目现有的 TypeScript Flat Config 之后:
|
|
90
|
+
|
|
91
|
+
```javascript
|
|
92
|
+
// eslint.config.mjs
|
|
93
|
+
import tsParser from "@typescript-eslint/parser";
|
|
94
|
+
import tsifdef from "tsifdef/eslint-plugin";
|
|
95
|
+
|
|
96
|
+
export default [
|
|
97
|
+
{
|
|
98
|
+
files: ["**/*.{ts,tsx,mts,cts}"],
|
|
99
|
+
languageOptions: { parser: tsParser },
|
|
100
|
+
},
|
|
101
|
+
tsifdef.configs["flat/recommended"],
|
|
102
|
+
];
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
顶层仍使用项目正常的 `@typescript-eslint/parser`,让编辑器集成能够识别 TypeScript
|
|
106
|
+
校验。
|
|
107
|
+
|
|
108
|
+
不需要关闭任何规则。诊断报在真实行列位置,自动修复和编辑器快速修复正常工作,
|
|
109
|
+
`prettier/prettier` 等格式化规则保持开启即可:未激活分支不会产生格式告警,而
|
|
110
|
+
激活代码中的真实问题照常报告。TSIfDef 唯一会拦下的,是会覆盖 `#if` 行的那类
|
|
111
|
+
修复——其诊断仍然照报,只是没有自动修复项。
|
|
112
|
+
|
|
113
|
+
TSIfDef 支持 ESLint 8.57 和 9,以及 `@typescript-eslint/parser` 5 至 8。两者均为
|
|
114
|
+
optional peer dependency,具体版本由宿主项目管理。
|
|
115
|
+
|
|
116
|
+
### 旧版 `.eslintrc`
|
|
117
|
+
|
|
118
|
+
旧配置系统会把 `tsifdef` 解析为名为 `eslint-plugin-tsifdef` 的包,因此需要将同一个
|
|
119
|
+
包安装到该别名:
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
npm install -D eslint-plugin-tsifdef@npm:tsifdef
|
|
123
|
+
# 离线使用发布产物时:
|
|
124
|
+
npm install -D eslint-plugin-tsifdef@file:./tsifdef-<version>.tgz
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
```jsonc
|
|
128
|
+
// .eslintrc.json
|
|
129
|
+
{
|
|
130
|
+
"parser": "@typescript-eslint/parser",
|
|
131
|
+
"extends": ["plugin:tsifdef/recommended"]
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
这一行同时配好 plugin、processor 和 `import/parsers`。如果项目同时 extends 了
|
|
136
|
+
`plugin:import/typescript`,请把它放在 `extends` 的**最后**:那份配置会为相同
|
|
137
|
+
扩展名注册 `@typescript-eslint/parser`,而 `eslint-plugin-import` 取先出现的
|
|
138
|
+
那个 parser。
|
|
139
|
+
|
|
140
|
+
若要手动展开:
|
|
141
|
+
|
|
142
|
+
```jsonc
|
|
143
|
+
// .eslintrc.json
|
|
144
|
+
{
|
|
145
|
+
"parser": "@typescript-eslint/parser",
|
|
146
|
+
"plugins": ["tsifdef"],
|
|
147
|
+
"overrides": [
|
|
148
|
+
{
|
|
149
|
+
"files": ["*.ts", "*.mts", "*.cts", "*.tsx"],
|
|
150
|
+
"processor": "tsifdef/macros"
|
|
151
|
+
}
|
|
152
|
+
],
|
|
153
|
+
"settings": {
|
|
154
|
+
"import/parsers": {
|
|
155
|
+
"eslint-plugin-tsifdef/parser": [".ts", ".tsx", ".mts", ".cts"]
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
```
|
package/LICENSE
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
Tencent is pleased to support the open source community by making TsIfDef available.
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2026 Tencent. All rights reserved.
|
|
4
|
+
|
|
5
|
+
TsIfDef is licensed under the Apache-2.0.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
Terms of the Apache-2.0:
|
|
9
|
+
--------------------------------------------------------------------
|
|
10
|
+
Apache License
|
|
11
|
+
Version 2.0, January 2004
|
|
12
|
+
http://www.apache.org/licenses/
|
|
13
|
+
|
|
14
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
15
|
+
|
|
16
|
+
1. Definitions.
|
|
17
|
+
|
|
18
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
19
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
20
|
+
|
|
21
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
22
|
+
the copyright owner that is granting the License.
|
|
23
|
+
|
|
24
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
25
|
+
other entities that control, are controlled by, or are under common
|
|
26
|
+
control with that entity. For the purposes of this definition,
|
|
27
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
28
|
+
direction or management of such entity, whether by contract or
|
|
29
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
30
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
31
|
+
|
|
32
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
33
|
+
exercising permissions granted by this License.
|
|
34
|
+
|
|
35
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
36
|
+
including but not limited to software source code, documentation
|
|
37
|
+
source, and configuration files.
|
|
38
|
+
|
|
39
|
+
"Object" form shall mean any form resulting from mechanical
|
|
40
|
+
transformation or translation of a Source form, including but
|
|
41
|
+
not limited to compiled object code, generated documentation,
|
|
42
|
+
and conversions to other media types.
|
|
43
|
+
|
|
44
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
45
|
+
Object form, made available under the License, as indicated by a
|
|
46
|
+
copyright notice that is included in or attached to the work
|
|
47
|
+
(an example is provided in the Appendix below).
|
|
48
|
+
|
|
49
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
50
|
+
form, that is based on (or derived from) the Work and for which the
|
|
51
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
52
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
53
|
+
of this License, Derivative Works shall not include works that remain
|
|
54
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
55
|
+
the Work and Derivative Works thereof.
|
|
56
|
+
|
|
57
|
+
"Contribution" shall mean any work of authorship, including
|
|
58
|
+
the original version of the Work and any modifications or additions
|
|
59
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
60
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
61
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
62
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
63
|
+
means any form of electronic, verbal, or written communication sent
|
|
64
|
+
to the Licensor or its representatives, including but not limited to
|
|
65
|
+
communication on electronic mailing lists, source code control systems,
|
|
66
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
67
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
68
|
+
excluding communication that is conspicuously marked or otherwise
|
|
69
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
70
|
+
|
|
71
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
72
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
73
|
+
subsequently incorporated within the Work.
|
|
74
|
+
|
|
75
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
76
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
77
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
78
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
79
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
80
|
+
Work and such Derivative Works in Source or Object form.
|
|
81
|
+
|
|
82
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
83
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
84
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
85
|
+
(except as stated in this section) patent license to make, have made,
|
|
86
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
87
|
+
where such license applies only to those patent claims licensable
|
|
88
|
+
by such Contributor that are necessarily infringed by their
|
|
89
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
90
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
91
|
+
institute patent litigation against any entity (including a
|
|
92
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
93
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
94
|
+
or contributory patent infringement, then any patent licenses
|
|
95
|
+
granted to You under this License for that Work shall terminate
|
|
96
|
+
as of the date such litigation is filed.
|
|
97
|
+
|
|
98
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
99
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
100
|
+
modifications, and in Source or Object form, provided that You
|
|
101
|
+
meet the following conditions:
|
|
102
|
+
|
|
103
|
+
(a) You must give any other recipients of the Work or
|
|
104
|
+
Derivative Works a copy of this License; and
|
|
105
|
+
|
|
106
|
+
(b) You must cause any modified files to carry prominent notices
|
|
107
|
+
stating that You changed the files; and
|
|
108
|
+
|
|
109
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
110
|
+
that You distribute, all copyright, patent, trademark, and
|
|
111
|
+
attribution notices from the Source form of the Work,
|
|
112
|
+
excluding those notices that do not pertain to any part of
|
|
113
|
+
the Derivative Works; and
|
|
114
|
+
|
|
115
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
116
|
+
distribution, then any Derivative Works that You distribute must
|
|
117
|
+
include a readable copy of the attribution notices contained
|
|
118
|
+
within such NOTICE file, excluding those notices that do not
|
|
119
|
+
pertain to any part of the Derivative Works, in at least one
|
|
120
|
+
of the following places: within a NOTICE text file distributed
|
|
121
|
+
as part of the Derivative Works; within the Source form or
|
|
122
|
+
documentation, if provided along with the Derivative Works; or,
|
|
123
|
+
within a display generated by the Derivative Works, if and
|
|
124
|
+
wherever such third-party notices normally appear. The contents
|
|
125
|
+
of the NOTICE file are for informational purposes only and
|
|
126
|
+
do not modify the License. You may add Your own attribution
|
|
127
|
+
notices within Derivative Works that You distribute, alongside
|
|
128
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
129
|
+
that such additional attribution notices cannot be construed
|
|
130
|
+
as modifying the License.
|
|
131
|
+
|
|
132
|
+
You may add Your own copyright statement to Your modifications and
|
|
133
|
+
may provide additional or different license terms and conditions
|
|
134
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
135
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
136
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
137
|
+
the conditions stated in this License.
|
|
138
|
+
|
|
139
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
140
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
141
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
142
|
+
this License, without any additional terms or conditions.
|
|
143
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
144
|
+
the terms of any separate license agreement you may have executed
|
|
145
|
+
with Licensor regarding such Contributions.
|
|
146
|
+
|
|
147
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
148
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
149
|
+
except as required for reasonable and customary use in describing the
|
|
150
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
151
|
+
|
|
152
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
153
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
154
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
155
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
156
|
+
implied, including, without limitation, any warranties or conditions
|
|
157
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
158
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
159
|
+
appropriateness of using or redistributing the Work and assume any
|
|
160
|
+
risks associated with Your exercise of permissions under this License.
|
|
161
|
+
|
|
162
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
163
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
164
|
+
unless required by applicable law (such as deliberate and grossly
|
|
165
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
166
|
+
liable to You for damages, including any direct, indirect, special,
|
|
167
|
+
incidental, or consequential damages of any character arising as a
|
|
168
|
+
result of this License or out of the use or inability to use the
|
|
169
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
170
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
171
|
+
other commercial damages or losses), even if such Contributor
|
|
172
|
+
has been advised of the possibility of such damages.
|
|
173
|
+
|
|
174
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
175
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
176
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
177
|
+
or other liability obligations and/or rights consistent with this
|
|
178
|
+
License. However, in accepting such obligations, You may act only
|
|
179
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
180
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
181
|
+
defend, and hold each Contributor harmless for any liability
|
|
182
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
183
|
+
of your accepting any such warranty or additional liability.
|
|
184
|
+
|
|
185
|
+
END OF TERMS AND CONDITIONS
|
|
186
|
+
|
|
187
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
188
|
+
|
|
189
|
+
To apply the Apache License to your work, attach the following
|
|
190
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
191
|
+
replaced with your own identifying information. (Don't include
|
|
192
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
193
|
+
comment syntax for the file format. We also recommend that a
|
|
194
|
+
file or class name and description of purpose be included on the
|
|
195
|
+
same "printed page" as the copyright notice for easier
|
|
196
|
+
identification within third-party archives.
|
|
197
|
+
|
|
198
|
+
Copyright 2026 Tencent
|
|
199
|
+
|
|
200
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
201
|
+
you may not use this file except in compliance with the License.
|
|
202
|
+
You may obtain a copy of the License at
|
|
203
|
+
|
|
204
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
205
|
+
|
|
206
|
+
Unless required by applicable law or agreed to in writing, software
|
|
207
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
208
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
209
|
+
See the License for the specific language governing permissions and
|
|
210
|
+
limitations under the License.
|