Skip to content

Commit ec26b1c

Browse files
authored
tools: add lint rule to ensure assertions are reached
PR-URL: #60125 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Marco Ippolito <[email protected]>
1 parent b757a8f commit ec26b1c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+456
-215
lines changed

test/eslint.config_partial.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ export default [
154154
],
155155
},
156156
},
157+
{
158+
files: [
159+
'test/{message,module-hooks,node-api,pummel,pseudo-tty,v8-updates,wasi}/**/*.{js,mjs,cjs}',
160+
],
161+
rules: {
162+
'node-core/must-call-assert': 'error',
163+
},
164+
},
157165
{
158166
files: [
159167
'test/{common,fixtures,wpt}/**/*.{js,mjs,cjs}',
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

33
require('../common');
4-
const assert = require('assert').strict;
4+
const assert = require('assert/strict');
55

66
assert.throws(() => { throw new Error('foo'); }, { bar: true });

test/message/node_run_non_existent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
require('../common');
4-
const assert = require('node:assert').strict;
4+
const assert = require('node:assert/strict');
55
const childProcess = require('node:child_process');
66
const fixtures = require('../common/fixtures');
77

test/module-hooks/test-module-hooks-import-wasm.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// This tests that module.registerHooks() can be used to support unknown formats, like
22
// import(wasm)
3-
import '../common/index.mjs';
3+
import { mustCall } from '../common/index.mjs';
44

55
import assert from 'node:assert';
66
import { registerHooks, createRequire } from 'node:module';
77
import { readFileSync } from 'node:fs';
88

99
registerHooks({
10-
load(url, context, nextLoad) {
10+
load: mustCall((url, context, nextLoad) => {
1111
assert.match(url, /simple\.wasm$/);
1212
const source =
1313
`const buf = Buffer.from([${Array.from(readFileSync(new URL(url))).join(',')}]);
@@ -21,7 +21,7 @@ registerHooks({
2121
source,
2222
format: 'module',
2323
};
24-
},
24+
}),
2525
});
2626

2727
// Checks that it works with require.

test/module-hooks/test-module-hooks-load-buffers.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
44
const assert = require('assert');
55
const { registerHooks } = require('module');
66

@@ -15,15 +15,7 @@ const hook1 = registerHooks({
1515
resolve(specifier, context, nextResolve) {
1616
return { shortCircuit: true, url: `test://${specifier}` };
1717
},
18-
load(url, context, nextLoad) {
19-
const result = nextLoad(url, context);
20-
if (url === 'test://array_buffer') {
21-
assert.deepStrictEqual(result.source, encoder.encode(arrayBufferSource).buffer);
22-
} else if (url === 'test://array_buffer_view') {
23-
assert.deepStrictEqual(result.source, encoder.encode(arrayBufferViewSource));
24-
}
25-
return result;
26-
},
18+
load: common.mustNotCall(),
2719
});
2820

2921
const hook2 = registerHooks({

test/module-hooks/test-module-hooks-load-chained.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
44
const assert = require('assert');
55
const { registerHooks } = require('module');
66

77
// Test that multiple loaders works together.
88
const hook1 = registerHooks({
9-
load(url, context, nextLoad) {
9+
load: common.mustCall((url, context, nextLoad) => {
1010
const result = nextLoad(url, context);
1111
assert.strictEqual(result.source, '');
1212
return {
1313
source: 'exports.hello = "world"',
1414
format: 'commonjs',
1515
};
16-
},
16+
}),
1717
});
1818

1919
const hook2 = registerHooks({
20-
load(url, context, nextLoad) {
20+
load: common.mustCall((url, context, nextLoad) => {
2121
const result = nextLoad(url, context);
2222
assert.strictEqual(result.source, 'exports.hello = "world"');
2323
return {
2424
source: 'export const hello = "world"',
2525
format: 'module',
2626
};
27-
},
27+
}),
2828
});
2929

3030
const mod = require('../fixtures/empty.js');

test/module-hooks/test-module-hooks-load-detection.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
44
const assert = require('assert');
55
const { registerHooks } = require('module');
66

77
// Test that module syntax detection works.
88
const hook = registerHooks({
9-
load(url, context, nextLoad) {
9+
load: common.mustCall((url, context, nextLoad) => {
1010
const result = nextLoad(url, context);
1111
assert.strictEqual(result.source, '');
1212
return {
1313
source: 'export const hello = "world"',
1414
};
15-
},
15+
}),
1616
});
1717

1818
const mod = require('../fixtures/empty.js');

test/module-hooks/test-module-hooks-load-url-change-import.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import { fileURL } from '../common/fixtures.mjs';
77
// It changes `foo` package name into `redirected-fs` and then loads `redirected-fs`
88

99
const hook = registerHooks({
10-
resolve(specifier, context, nextResolve) {
10+
resolve: mustCall((specifier, context, nextResolve) => {
1111
assert.strictEqual(specifier, 'foo');
1212
return {
1313
url: 'foo://bar',
1414
shortCircuit: true,
1515
};
16-
},
16+
}),
1717
load: mustCall(function load(url, context, nextLoad) {
1818
assert.strictEqual(url, 'foo://bar');
1919
return nextLoad(fileURL('module-hooks', 'redirected-fs.js').href, context);

test/module-hooks/test-module-hooks-load-url-change-require.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ const fixtures = require('../common/fixtures');
99
// It changes `foo` package name into `redirected-fs` and then loads `redirected-fs`
1010

1111
const hook = registerHooks({
12-
resolve(specifier, context, nextResolve) {
12+
resolve: common.mustCall((specifier, context, nextResolve) => {
1313
assert.strictEqual(specifier, 'foo');
1414
return {
1515
url: 'foo://bar',
1616
shortCircuit: true,
1717
};
18-
},
18+
}),
1919
load: common.mustCall(function load(url, context, nextLoad) {
2020
assert.strictEqual(url, 'foo://bar');
2121
return nextLoad(

test/module-hooks/test-module-hooks-require-wasm.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const { registerHooks } = require('module');
99
const { readFileSync } = require('fs');
1010

1111
registerHooks({
12-
load(url, context, nextLoad) {
12+
load: common.mustCall((url, context, nextLoad) => {
1313
assert.match(url, /simple\.wasm$/);
1414
const source =
1515
`const buf = Buffer.from([${Array.from(readFileSync(new URL(url))).join(',')}]);
@@ -20,7 +20,7 @@ registerHooks({
2020
source,
2121
format: 'commonjs',
2222
};
23-
},
23+
}, 2),
2424
});
2525

2626
// Checks that it works with require.

0 commit comments

Comments
 (0)