Skip to content

Commit 325af32

Browse files
committed
test(linter/plugins): debug build for tests (#15921)
Generate a debug build, in addition to the normal release build. In the debug build, `DEBUG` global is defined as `true` and `assertIsNonNull` does a runtime assertion. In release build, `DEBUG === false` and minifier removes debug-only code. Run the tests in debug mode. Debug build is generated in a separate directory from `dist`, so is *not* included in NPM package.
1 parent 690abec commit 325af32

File tree

8 files changed

+33
-7
lines changed

8 files changed

+33
-7
lines changed

apps/oxlint/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/node_modules/
22
/dist/
3+
/debug/
34
*.node

apps/oxlint/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@
4747
]
4848
},
4949
"imports": {
50-
"#oxlint": "./dist/index.js"
50+
"#oxlint": "./debug/index.js"
5151
}
5252
}

apps/oxlint/scripts/build.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { copyFileSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'
33
import { join } from 'node:path';
44

55
const oxlintDirPath = join(import.meta.dirname, '..'),
6-
distDirPath = join(oxlintDirPath, 'dist');
6+
distDirPath = join(oxlintDirPath, 'dist'),
7+
debugDirPath = join(oxlintDirPath, 'debug');
78

89
// Modify `bindings.js` to use correct package names
910
console.log('Modifying bindings.js...');
@@ -26,6 +27,7 @@ const srcDirPath = join(oxlintDirPath, 'src-js');
2627
for (const filename of readdirSync(srcDirPath)) {
2728
if (!filename.endsWith('.node')) continue;
2829
copyFileSync(join(srcDirPath, filename), join(distDirPath, filename));
30+
copyFileSync(join(srcDirPath, filename), join(debugDirPath, filename));
2931
}
3032

3133
console.log('Build complete!');

apps/oxlint/src-js/globals.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Global constant defined at build time by TSDown
2+
declare const DEBUG: boolean;

apps/oxlint/src-js/plugins/utils.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,20 @@ export function assertIs<T>(value: unknown): asserts value is T {}
4545
/**
4646
* Assert a value is not `null` or `undefined`.
4747
*
48-
* Has no runtime effect - only for guiding the type-checker.
49-
* Minification removes this function and all calls to it, so it has zero runtime cost.
48+
* In release builds, is a no-op. Only does runtime checks in debug builds.
49+
* Minification removes this function and all calls to it in release builds, so it has zero runtime cost.
5050
*
5151
* @param value - Value
5252
*/
5353
// oxlint-disable-next-line no-unused-vars
54-
export function assertIsNonNull<T>(value: T | null | undefined): asserts value is T {}
54+
export function assertIsNonNull<T>(value: T | null | undefined): asserts value is T {
55+
if (!DEBUG) return;
56+
57+
if (value === null || value === undefined) {
58+
// oxlint-disable-next-line typescript/restrict-template-expressions
59+
throw new Error(`Expected non-null value, got ${value}`);
60+
}
61+
}
5562

5663
/**
5764
* Utility type to make specified properties of a type nullable.

apps/oxlint/test/e2e.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { PACKAGE_ROOT_PATH, getFixtures, testFixtureWithCommand } from './utils.
44

55
import type { Fixture } from './utils.ts';
66

7-
const CLI_PATH = pathJoin(PACKAGE_ROOT_PATH, 'dist/cli.js');
7+
const CLI_PATH = pathJoin(PACKAGE_ROOT_PATH, 'debug/cli.js');
88

99
// Use current NodeJS executable, rather than `node`, to avoid problems with a Node version manager
1010
// installed on system resulting in using wrong NodeJS version

apps/oxlint/tsdown.config.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ const commonConfig: UserConfig = {
2121
mangle: false,
2222
codegen: { removeWhitespace: false },
2323
},
24+
define: { DEBUG: 'false' },
2425
};
2526

2627
// Only generate `.d.ts` file for main export, not for CLI
27-
export default defineConfig([
28+
const configs = defineConfig([
2829
{
2930
entry: 'src-js/cli.ts',
3031
...commonConfig,
@@ -37,3 +38,13 @@ export default defineConfig([
3738
attw: true,
3839
},
3940
]);
41+
42+
// Create separate debug build with debug assertions enabled
43+
const debugConfigs = configs.map((config) => ({
44+
...config,
45+
outDir: 'debug',
46+
define: { DEBUG: 'true' },
47+
}));
48+
configs.push(...debugConfigs);
49+
50+
export default configs;

apps/oxlint/vitest.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ export default defineConfig({
44
test: {
55
exclude: [...configDefaults.exclude, 'fixtures/**'],
66
},
7+
define: {
8+
DEBUG: 'true',
9+
},
710
});

0 commit comments

Comments
 (0)