oxc-coverage-instrument 0.2.12 → 0.2.14

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.
Files changed (3) hide show
  1. package/package.json +22 -10
  2. package/vitest.d.ts +33 -0
  3. package/vitest.js +81 -0
package/package.json CHANGED
@@ -1,12 +1,24 @@
1
1
  {
2
2
  "name": "oxc-coverage-instrument",
3
- "version": "0.2.12",
4
- "description": "Istanbul-compatible JavaScript/TypeScript coverage instrumentation using the Oxc AST. 58x faster than istanbul-lib-instrument.",
3
+ "version": "0.2.14",
4
+ "description": "Istanbul-compatible JavaScript/TypeScript coverage instrumentation using the Oxc AST. 8-48x faster than istanbul-lib-instrument.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./index.d.ts",
10
+ "default": "./index.js"
11
+ },
12
+ "./vitest": {
13
+ "types": "./vitest.d.ts",
14
+ "default": "./vitest.js"
15
+ }
16
+ },
7
17
  "files": [
8
18
  "index.js",
9
- "index.d.ts"
19
+ "index.d.ts",
20
+ "vitest.js",
21
+ "vitest.d.ts"
10
22
  ],
11
23
  "keywords": [
12
24
  "oxc",
@@ -49,12 +61,12 @@
49
61
  "test": "node test.mjs"
50
62
  },
51
63
  "optionalDependencies": {
52
- "@oxc-coverage-instrument/binding-darwin-arm64": "0.2.12",
53
- "@oxc-coverage-instrument/binding-darwin-x64": "0.2.12",
54
- "@oxc-coverage-instrument/binding-linux-x64-gnu": "0.2.12",
55
- "@oxc-coverage-instrument/binding-linux-arm64-gnu": "0.2.12",
56
- "@oxc-coverage-instrument/binding-linux-x64-musl": "0.2.12",
57
- "@oxc-coverage-instrument/binding-win32-x64-msvc": "0.2.12",
58
- "@oxc-coverage-instrument/binding-win32-arm64-msvc": "0.2.12"
64
+ "@oxc-coverage-instrument/binding-darwin-arm64": "0.2.14",
65
+ "@oxc-coverage-instrument/binding-darwin-x64": "0.2.14",
66
+ "@oxc-coverage-instrument/binding-linux-x64-gnu": "0.2.14",
67
+ "@oxc-coverage-instrument/binding-linux-arm64-gnu": "0.2.14",
68
+ "@oxc-coverage-instrument/binding-linux-x64-musl": "0.2.14",
69
+ "@oxc-coverage-instrument/binding-win32-x64-msvc": "0.2.14",
70
+ "@oxc-coverage-instrument/binding-win32-arm64-msvc": "0.2.14"
59
71
  }
60
72
  }
package/vitest.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Creates an instrumenter that implements the istanbul-lib-instrument
3
+ * Instrumenter interface, backed by oxc-coverage-instrument.
4
+ *
5
+ * Use with Vitest's `coverage.instrumenter` option (requires vitest with
6
+ * custom instrumenter support):
7
+ *
8
+ * ```ts
9
+ * import { defineConfig } from 'vitest/config'
10
+ * import { createOxcInstrumenter } from 'oxc-coverage-instrument/vitest'
11
+ *
12
+ * export default defineConfig({
13
+ * test: {
14
+ * coverage: {
15
+ * provider: 'istanbul',
16
+ * instrumenter: () => createOxcInstrumenter(),
17
+ * }
18
+ * }
19
+ * })
20
+ * ```
21
+ */
22
+ export declare function createOxcInstrumenter(options?: {
23
+ /** Name of the global coverage variable (default: "__coverage__"). */
24
+ coverageVariable?: string
25
+ /** Class method names to exclude from function coverage. */
26
+ ignoreClassMethods?: string[]
27
+ /** When true, adds truthy-value tracking (bT) for logical expressions. */
28
+ reportLogic?: boolean
29
+ }): {
30
+ instrumentSync(code: string, filename: string, inputSourceMap?: any): string
31
+ lastSourceMap(): any
32
+ lastFileCoverage(): any
33
+ }
package/vitest.js ADDED
@@ -0,0 +1,81 @@
1
+ // Vitest Istanbul instrumenter adapter for oxc-coverage-instrument.
2
+ //
3
+ // Implements the istanbul-lib-instrument Instrumenter interface so it can be
4
+ // used as a drop-in replacement in @vitest/coverage-istanbul.
5
+ //
6
+ // Usage in vitest.config.ts:
7
+ //
8
+ // import { defineConfig } from 'vitest/config'
9
+ // import { createOxcInstrumenter } from 'oxc-coverage-instrument/vitest'
10
+ //
11
+ // export default defineConfig({
12
+ // test: {
13
+ // coverage: {
14
+ // provider: 'istanbul',
15
+ // instrumenter: createOxcInstrumenter(),
16
+ // }
17
+ // }
18
+ // })
19
+
20
+ const { instrument } = require('./index.js');
21
+
22
+ /**
23
+ * Creates an instrumenter that implements the istanbul-lib-instrument
24
+ * Instrumenter interface, backed by oxc-coverage-instrument.
25
+ *
26
+ * @param {object} [options]
27
+ * @param {string} [options.coverageVariable='__coverage__']
28
+ * @param {string[]} [options.ignoreClassMethods=[]]
29
+ * @param {boolean} [options.reportLogic=false]
30
+ * @returns {Instrumenter}
31
+ */
32
+ function createOxcInstrumenter(options = {}) {
33
+ const coverageVariable = options.coverageVariable || '__coverage__';
34
+ const ignoreClassMethods = options.ignoreClassMethods || [];
35
+ const reportLogic = options.reportLogic || false;
36
+
37
+ let _lastSourceMap = null;
38
+ let _lastFileCoverage = null;
39
+
40
+ return {
41
+ /**
42
+ * Instrument source code synchronously.
43
+ * @param {string} code - Source code to instrument.
44
+ * @param {string} filename - File path for coverage map.
45
+ * @param {object} [inputSourceMap] - Input source map from prior transform.
46
+ * @returns {string} Instrumented source code.
47
+ */
48
+ instrumentSync(code, filename, inputSourceMap) {
49
+ const result = instrument(code, filename, {
50
+ coverageVariable,
51
+ sourceMap: true,
52
+ inputSourceMap: inputSourceMap ? JSON.stringify(inputSourceMap) : undefined,
53
+ reportLogic,
54
+ ignoreClassMethods,
55
+ });
56
+
57
+ _lastFileCoverage = JSON.parse(result.coverageMap);
58
+ _lastSourceMap = result.sourceMap ? JSON.parse(result.sourceMap) : null;
59
+
60
+ return result.code;
61
+ },
62
+
63
+ /**
64
+ * Get the source map of the last instrumented file.
65
+ * @returns {object|null}
66
+ */
67
+ lastSourceMap() {
68
+ return _lastSourceMap;
69
+ },
70
+
71
+ /**
72
+ * Get the coverage object of the last instrumented file.
73
+ * @returns {object}
74
+ */
75
+ lastFileCoverage() {
76
+ return _lastFileCoverage;
77
+ },
78
+ };
79
+ }
80
+
81
+ module.exports = { createOxcInstrumenter };