code-gauge 1.10.0 → 1.11.1
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/README.md +12 -0
- package/dist/cli.cjs +1 -1
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +1 -1
- package/dist/cli.js.map +1 -1
- package/dist/cliConfig.cjs +1 -1
- package/dist/cliConfig.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -1
- package/dist/metrics.cjs +2 -2
- package/dist/metrics.cjs.map +1 -1
- package/dist/metrics.js +2 -2
- package/dist/metrics.js.map +1 -1
- package/dist/nativeMetrics.cjs +2 -0
- package/dist/nativeMetrics.cjs.map +1 -0
- package/dist/nativeMetrics.d.ts +24 -0
- package/dist/nativeMetrics.js +2 -0
- package/dist/nativeMetrics.js.map +1 -0
- package/package.json +22 -18
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";const e=require("./languages.cjs");let t=require("node:module");const n=new Map(e.defaultLanguages.map(e=>[e.name,e]));let r=!1,i;function a(e,t,r){if(!s()||n.get(t.name)!==t||!e.isWellFormed())return;let i=c();if(i)try{return JSON.parse(i.measureCodeNative(e,t.name,r))}catch(e){if(process.env.CODE_GAUGE_NATIVE_STRICT===`1`)throw e;return}}function o(){return s()&&c()!==void 0}function s(){return process.env.CODE_GAUGE_NATIVE!==`0`}function c(){if(r)return i;r=!0;try{i=(0,t.createRequire)(require("url").pathToFileURL(__filename).href)(`../native/code-gauge.node`)}catch{}return i}exports.isNativeBackendAvailable=o,exports.measureWithNativeBackend=a;
|
|
2
|
+
//# sourceMappingURL=nativeMetrics.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nativeMetrics.cjs","names":["defaultLanguages"],"sources":["../src/nativeMetrics.ts"],"sourcesContent":["import { createRequire } from 'node:module';\nimport { defaultLanguages } from './languages.js';\nimport type { CodeMetrics, LanguageDefinition } from './types.js';\n\n/**\n * Halstead counts measured natively; the derived float metrics (volume, difficulty, ...) are\n * computed in TypeScript because V8 and Rust disagree on the last bit of log/log2 results, and\n * the native backend must be bit-identical to the TypeScript one.\n */\nexport interface NativeHalsteadCounts {\n distinctOperators: number;\n distinctOperands: number;\n totalOperators: number;\n totalOperands: number;\n}\n\nexport interface NativeMetricsPayload extends Omit<CodeMetrics, 'halstead' | 'maintainabilityIndex' | 'syntaxTree'> {\n halsteadCounts: NativeHalsteadCounts;\n syntaxTree?: string;\n}\n\ninterface NativeBinding {\n measureCodeNative(code: string, language: string, includeSyntaxTree: boolean): string;\n}\n\nconst defaultLanguageByName = new Map(defaultLanguages.map((language) => [language.name, language]));\n\nlet bindingLoadAttempted = false;\nlet cachedBinding: NativeBinding | undefined;\n\n/**\n * Measures via the Rust addon when it is built and applicable, or returns undefined so the caller\n * falls back to the TypeScript implementation. Custom-registered languages always fall back: the\n * addon only embeds the built-in grammars.\n */\nexport function measureWithNativeBackend(\n code: string,\n language: LanguageDefinition,\n includeSyntaxTree: boolean\n): NativeMetricsPayload | undefined {\n if (!isNativeBackendEnabled() || defaultLanguageByName.get(language.name) !== language) {\n return undefined;\n }\n\n // Lone surrogates cannot cross the N-API boundary losslessly (they become U+FFFD), so\n // ill-formed strings measure through the TypeScript backend, which sees them as-is.\n if (!code.isWellFormed()) {\n return undefined;\n }\n\n const binding = loadBinding();\n if (!binding) {\n return undefined;\n }\n\n try {\n return JSON.parse(binding.measureCodeNative(code, language.name, includeSyntaxTree)) as NativeMetricsPayload;\n } catch (error) {\n // Parity tests set the strict flag: without it, a binding that starts throwing would silently\n // degrade the \"native\" side of every comparison into a TypeScript-vs-TypeScript check.\n if (process.env.CODE_GAUGE_NATIVE_STRICT === '1') {\n throw error;\n }\n // A native failure (e.g. the tree-depth guard on pathological input) falls back to the\n // TypeScript backend instead of turning measureCode into a throwing API.\n return undefined;\n }\n}\n\n/** Whether measureCode currently uses the native backend for built-in languages. */\nexport function isNativeBackendAvailable(): boolean {\n return isNativeBackendEnabled() && loadBinding() !== undefined;\n}\n\n/** Checked per call (not cached) so tests can flip backends within one process. */\nfunction isNativeBackendEnabled(): boolean {\n return process.env.CODE_GAUGE_NATIVE !== '0';\n}\n\nfunction loadBinding(): NativeBinding | undefined {\n if (bindingLoadAttempted) {\n return cachedBinding;\n }\n bindingLoadAttempted = true;\n\n try {\n // Resolved relative to this file, so both src/ (tests) and dist/ (build) find native/.\n const requireNative = createRequire(import.meta.url);\n cachedBinding = requireNative('../native/code-gauge.node') as NativeBinding;\n } catch {\n // The addon has not been built (or this platform/module format cannot load it).\n }\n return cachedBinding;\n}\n"],"mappings":"6EAyBA,MAAM,EAAwB,IAAI,IAAIA,EAAAA,iBAAiB,IAAK,GAAa,CAAC,EAAS,KAAM,CAAQ,CAAC,CAAC,EAEnG,IAAI,EAAuB,GACvB,EAOJ,SAAgB,EACd,EACA,EACA,EACkC,CAOlC,GANI,CAAC,EAAuB,GAAK,EAAsB,IAAI,EAAS,IAAI,IAAM,GAM1E,CAAC,EAAK,aAAa,EACrB,OAGF,IAAM,EAAU,EAAY,EACvB,KAIL,GAAI,CACF,OAAO,KAAK,MAAM,EAAQ,kBAAkB,EAAM,EAAS,KAAM,CAAiB,CAAC,CACrF,OAAS,EAAO,CAGd,GAAI,QAAQ,IAAI,2BAA6B,IAC3C,MAAM,EAIR,MACF,CACF,CAGA,SAAgB,GAAoC,CAClD,OAAO,EAAuB,GAAK,EAAY,IAAM,IAAA,EACvD,CAGA,SAAS,GAAkC,CACzC,OAAO,QAAQ,IAAI,oBAAsB,GAC3C,CAEA,SAAS,GAAyC,CAChD,GAAI,EACF,OAAO,EAET,EAAuB,GAEvB,GAAI,CAGF,GAAA,EAAA,EAAA,cAAA,CAAA,QAAA,KAAA,CAAA,CAAA,cAAA,UAAA,CAAA,CAAA,IAA4B,CAAC,CAAC,2BAA2B,CAC3D,MAAQ,CAER,CACA,OAAO,CACT"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { CodeMetrics, LanguageDefinition } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Halstead counts measured natively; the derived float metrics (volume, difficulty, ...) are
|
|
4
|
+
* computed in TypeScript because V8 and Rust disagree on the last bit of log/log2 results, and
|
|
5
|
+
* the native backend must be bit-identical to the TypeScript one.
|
|
6
|
+
*/
|
|
7
|
+
export interface NativeHalsteadCounts {
|
|
8
|
+
distinctOperators: number;
|
|
9
|
+
distinctOperands: number;
|
|
10
|
+
totalOperators: number;
|
|
11
|
+
totalOperands: number;
|
|
12
|
+
}
|
|
13
|
+
export interface NativeMetricsPayload extends Omit<CodeMetrics, 'halstead' | 'maintainabilityIndex' | 'syntaxTree'> {
|
|
14
|
+
halsteadCounts: NativeHalsteadCounts;
|
|
15
|
+
syntaxTree?: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Measures via the Rust addon when it is built and applicable, or returns undefined so the caller
|
|
19
|
+
* falls back to the TypeScript implementation. Custom-registered languages always fall back: the
|
|
20
|
+
* addon only embeds the built-in grammars.
|
|
21
|
+
*/
|
|
22
|
+
export declare function measureWithNativeBackend(code: string, language: LanguageDefinition, includeSyntaxTree: boolean): NativeMetricsPayload | undefined;
|
|
23
|
+
/** Whether measureCode currently uses the native backend for built-in languages. */
|
|
24
|
+
export declare function isNativeBackendAvailable(): boolean;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{defaultLanguages as e}from"./languages.js";import{createRequire as t}from"node:module";const n=new Map(e.map(e=>[e.name,e]));let r=!1,i;function a(e,t,r){if(!s()||n.get(t.name)!==t||!e.isWellFormed())return;let i=c();if(i)try{return JSON.parse(i.measureCodeNative(e,t.name,r))}catch(e){if(process.env.CODE_GAUGE_NATIVE_STRICT===`1`)throw e;return}}function o(){return s()&&c()!==void 0}function s(){return process.env.CODE_GAUGE_NATIVE!==`0`}function c(){if(r)return i;r=!0;try{i=t(import.meta.url)(`../native/code-gauge.node`)}catch{}return i}export{o as isNativeBackendAvailable,a as measureWithNativeBackend};
|
|
2
|
+
//# sourceMappingURL=nativeMetrics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nativeMetrics.js","names":[],"sources":["../src/nativeMetrics.ts"],"sourcesContent":["import { createRequire } from 'node:module';\nimport { defaultLanguages } from './languages.js';\nimport type { CodeMetrics, LanguageDefinition } from './types.js';\n\n/**\n * Halstead counts measured natively; the derived float metrics (volume, difficulty, ...) are\n * computed in TypeScript because V8 and Rust disagree on the last bit of log/log2 results, and\n * the native backend must be bit-identical to the TypeScript one.\n */\nexport interface NativeHalsteadCounts {\n distinctOperators: number;\n distinctOperands: number;\n totalOperators: number;\n totalOperands: number;\n}\n\nexport interface NativeMetricsPayload extends Omit<CodeMetrics, 'halstead' | 'maintainabilityIndex' | 'syntaxTree'> {\n halsteadCounts: NativeHalsteadCounts;\n syntaxTree?: string;\n}\n\ninterface NativeBinding {\n measureCodeNative(code: string, language: string, includeSyntaxTree: boolean): string;\n}\n\nconst defaultLanguageByName = new Map(defaultLanguages.map((language) => [language.name, language]));\n\nlet bindingLoadAttempted = false;\nlet cachedBinding: NativeBinding | undefined;\n\n/**\n * Measures via the Rust addon when it is built and applicable, or returns undefined so the caller\n * falls back to the TypeScript implementation. Custom-registered languages always fall back: the\n * addon only embeds the built-in grammars.\n */\nexport function measureWithNativeBackend(\n code: string,\n language: LanguageDefinition,\n includeSyntaxTree: boolean\n): NativeMetricsPayload | undefined {\n if (!isNativeBackendEnabled() || defaultLanguageByName.get(language.name) !== language) {\n return undefined;\n }\n\n // Lone surrogates cannot cross the N-API boundary losslessly (they become U+FFFD), so\n // ill-formed strings measure through the TypeScript backend, which sees them as-is.\n if (!code.isWellFormed()) {\n return undefined;\n }\n\n const binding = loadBinding();\n if (!binding) {\n return undefined;\n }\n\n try {\n return JSON.parse(binding.measureCodeNative(code, language.name, includeSyntaxTree)) as NativeMetricsPayload;\n } catch (error) {\n // Parity tests set the strict flag: without it, a binding that starts throwing would silently\n // degrade the \"native\" side of every comparison into a TypeScript-vs-TypeScript check.\n if (process.env.CODE_GAUGE_NATIVE_STRICT === '1') {\n throw error;\n }\n // A native failure (e.g. the tree-depth guard on pathological input) falls back to the\n // TypeScript backend instead of turning measureCode into a throwing API.\n return undefined;\n }\n}\n\n/** Whether measureCode currently uses the native backend for built-in languages. */\nexport function isNativeBackendAvailable(): boolean {\n return isNativeBackendEnabled() && loadBinding() !== undefined;\n}\n\n/** Checked per call (not cached) so tests can flip backends within one process. */\nfunction isNativeBackendEnabled(): boolean {\n return process.env.CODE_GAUGE_NATIVE !== '0';\n}\n\nfunction loadBinding(): NativeBinding | undefined {\n if (bindingLoadAttempted) {\n return cachedBinding;\n }\n bindingLoadAttempted = true;\n\n try {\n // Resolved relative to this file, so both src/ (tests) and dist/ (build) find native/.\n const requireNative = createRequire(import.meta.url);\n cachedBinding = requireNative('../native/code-gauge.node') as NativeBinding;\n } catch {\n // The addon has not been built (or this platform/module format cannot load it).\n }\n return cachedBinding;\n}\n"],"mappings":"8FAyBA,MAAM,EAAwB,IAAI,IAAI,EAAiB,IAAK,GAAa,CAAC,EAAS,KAAM,CAAQ,CAAC,CAAC,EAEnG,IAAI,EAAuB,GACvB,EAOJ,SAAgB,EACd,EACA,EACA,EACkC,CAOlC,GANI,CAAC,EAAuB,GAAK,EAAsB,IAAI,EAAS,IAAI,IAAM,GAM1E,CAAC,EAAK,aAAa,EACrB,OAGF,IAAM,EAAU,EAAY,EACvB,KAIL,GAAI,CACF,OAAO,KAAK,MAAM,EAAQ,kBAAkB,EAAM,EAAS,KAAM,CAAiB,CAAC,CACrF,OAAS,EAAO,CAGd,GAAI,QAAQ,IAAI,2BAA6B,IAC3C,MAAM,EAIR,MACF,CACF,CAGA,SAAgB,GAAoC,CAClD,OAAO,EAAuB,GAAK,EAAY,IAAM,IAAA,EACvD,CAGA,SAAS,GAAkC,CACzC,OAAO,QAAQ,IAAI,oBAAsB,GAC3C,CAEA,SAAS,GAAyC,CAChD,GAAI,EACF,OAAO,EAET,EAAuB,GAEvB,GAAI,CAGF,EADsB,EAAc,OAAO,KAAK,GACpB,CAAC,CAAC,2BAA2B,CAC3D,MAAQ,CAER,CACA,OAAO,CACT"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "code-gauge",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.1",
|
|
4
4
|
"description": "Measure code metrics with tree-sitter.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|
|
@@ -32,17 +32,19 @@
|
|
|
32
32
|
"dist/"
|
|
33
33
|
],
|
|
34
34
|
"scripts": {
|
|
35
|
+
"benchmark": "node scripts/benchmark.mjs",
|
|
35
36
|
"build": "build-ts lib -i src/index.ts -i src/cli.ts",
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"format
|
|
39
|
-
"lint": "
|
|
40
|
-
"lint-fix": "
|
|
37
|
+
"build-native": "node scripts/buildNative.mjs",
|
|
38
|
+
"cleanup": "bun wb lint --fix --format",
|
|
39
|
+
"format": "bun wb lint --format",
|
|
40
|
+
"lint": "bun wb lint",
|
|
41
|
+
"lint-fix": "bun wb lint --fix",
|
|
41
42
|
"prepare": "lefthook install || true",
|
|
42
|
-
"test": "
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"verify
|
|
43
|
+
"test": "bun wb test",
|
|
44
|
+
"test/ci": "bun run build-native && CODE_GAUGE_EXPECT_NATIVE=1 vitest",
|
|
45
|
+
"typecheck": "bun wb typecheck",
|
|
46
|
+
"verify": "bun wb verify",
|
|
47
|
+
"verify-full": "bun wb verify --full"
|
|
46
48
|
},
|
|
47
49
|
"dependencies": {
|
|
48
50
|
"commander": "15.0.0",
|
|
@@ -59,27 +61,29 @@
|
|
|
59
61
|
"typescript": "7.0.2"
|
|
60
62
|
},
|
|
61
63
|
"devDependencies": {
|
|
64
|
+
"@tsconfig/bun": "1.0.10",
|
|
62
65
|
"@tsconfig/node-lts": "24.0.0",
|
|
63
66
|
"@tsconfig/node-ts": "23.6.4",
|
|
67
|
+
"@types/bun": "1.3.14",
|
|
64
68
|
"@types/node": "25.9.4",
|
|
65
69
|
"@willbooster/oxfmt-config": "1.2.2",
|
|
66
70
|
"@willbooster/oxlint-config": "1.4.8",
|
|
67
|
-
"@willbooster/wb": "
|
|
68
|
-
"build-ts": "
|
|
71
|
+
"@willbooster/wb": "19.6.1",
|
|
72
|
+
"build-ts": "21.0.0",
|
|
69
73
|
"conventional-changelog-conventionalcommits": "9.3.1",
|
|
70
|
-
"lefthook": "2.1.
|
|
71
|
-
"oxfmt": "0.
|
|
72
|
-
"oxlint": "1.
|
|
73
|
-
"oxlint-tsgolint": "0.
|
|
74
|
+
"lefthook": "2.1.10",
|
|
75
|
+
"oxfmt": "0.61.0",
|
|
76
|
+
"oxlint": "1.76.0",
|
|
77
|
+
"oxlint-tsgolint": "7.0.2001",
|
|
74
78
|
"semantic-release": "25.0.5",
|
|
75
79
|
"sort-package-json": "4.0.0",
|
|
76
80
|
"vitest": "4.1.9"
|
|
77
81
|
},
|
|
78
|
-
"packageManager": "yarn@4.17.1",
|
|
79
82
|
"engines": {
|
|
80
83
|
"node": ">=24"
|
|
81
84
|
},
|
|
82
85
|
"publishConfig": {
|
|
83
|
-
"access": "public"
|
|
86
|
+
"access": "public",
|
|
87
|
+
"registry": "https://registry.npmjs.org/"
|
|
84
88
|
}
|
|
85
89
|
}
|