-
Notifications
You must be signed in to change notification settings - Fork 1
adding module complexity #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import { parseSync } from 'oxc-parser'; | ||
| import { walk } from 'estree-walker'; | ||
| import type { Node as EstreeWalkerNode } from 'estree-walker'; | ||
| import type { Context, ESTreeNode } from './types.js'; | ||
| import { | ||
| createModuleAnalysisVisitor, | ||
| type ModuleAnalysisResult, | ||
| type ModuleComplexityOptions, | ||
| } from './module/visitor.js'; | ||
|
|
||
| function createLineOffsetTable(code: string): number[] { | ||
| const lineOffsets: number[] = [0]; | ||
| for (let i = 0; i < code.length; i++) { | ||
| if (code[i] === '\n') { | ||
| lineOffsets.push(i + 1); | ||
| } | ||
| } | ||
| return lineOffsets; | ||
| } | ||
|
|
||
| function offsetToLineCol(offset: number, lineOffsets: number[]): { line: number; column: number } { | ||
| let lo = 0; | ||
| let hi = lineOffsets.length - 1; | ||
| while (lo < hi) { | ||
| const mid = (lo + hi + 1) >>> 1; | ||
| if (lineOffsets[mid] <= offset) lo = mid; | ||
| else hi = mid - 1; | ||
| } | ||
| return { line: lo + 1, column: offset - lineOffsets[lo] }; | ||
| } | ||
|
|
||
| type VisitorHandlerMap = Record<string, ((node: ESTreeNode) => void) | undefined>; | ||
|
|
||
| /** Single-pass AST walk: adds parent/loc references and dispatches visitor handlers. */ | ||
| function walkAndDispatch(ast: ESTreeNode, code: string, visitor: VisitorHandlerMap): void { | ||
| const lineOffsets = createLineOffsetTable(code); | ||
|
|
||
| walk(ast as EstreeWalkerNode, { | ||
| enter(node, parent) { | ||
| const esNode = node as unknown as ESTreeNode; | ||
| const raw = node as unknown as { start?: number; end?: number }; | ||
|
|
||
| if (typeof raw.start === 'number' && typeof raw.end === 'number') { | ||
| Object.defineProperty(esNode, 'loc', { | ||
| value: { | ||
| start: offsetToLineCol(raw.start, lineOffsets), | ||
| end: offsetToLineCol(raw.end, lineOffsets), | ||
| }, | ||
| writable: true, | ||
| enumerable: false, | ||
| configurable: true, | ||
| }); | ||
| } | ||
|
|
||
| Object.defineProperty(esNode, 'parent', { | ||
| value: parent as unknown as ESTreeNode, | ||
| writable: true, | ||
| enumerable: false, | ||
| configurable: true, | ||
| }); | ||
|
|
||
| visitor[esNode.type]?.(esNode); | ||
| visitor['*']?.(esNode); | ||
| }, | ||
| leave(node) { | ||
| const esNode = node as unknown as ESTreeNode; | ||
| visitor[`${esNode.type}:exit`]?.(esNode); | ||
| visitor['*:exit']?.(esNode); | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| function createLibraryContext(code: string): Context { | ||
| return { | ||
| sourceCode: { | ||
| text: code, | ||
| getText: () => code, | ||
| scopeManager: null, | ||
| getScope: () => null, | ||
| }, | ||
| options: [], | ||
| report: () => {}, | ||
| } as unknown as Context; | ||
| } | ||
|
|
||
| /** Standalone module complexity analysis (no linting context required). */ | ||
| export function analyzeModule( | ||
| code: string, | ||
| filename: string = 'module.js', | ||
| options?: ModuleComplexityOptions | ||
| ): ModuleAnalysisResult { | ||
| const { program, errors } = parseSync(filename, code); | ||
|
|
||
| if (errors.length > 0) { | ||
| throw new Error( | ||
| `Parse errors in "${filename}": ${errors.map((e: { message: string }) => e.message).join(', ')}` | ||
| ); | ||
| } | ||
|
|
||
| const ast = program as unknown as ESTreeNode; | ||
| let result: ModuleAnalysisResult | undefined; | ||
|
|
||
| const visitor = createModuleAnalysisVisitor( | ||
| createLibraryContext(code), | ||
| (r) => { | ||
| result = r; | ||
| }, | ||
| undefined, | ||
| options | ||
| ); | ||
|
|
||
| walkAndDispatch(ast, code, visitor as VisitorHandlerMap); | ||
|
|
||
| if (!result) { | ||
| throw new Error('Module analysis did not produce a result'); | ||
| } | ||
|
|
||
| return result; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: itaymendel/oxlint-plugin-complexity
Length of output: 1531
🏁 Script executed:
Repository: itaymendel/oxlint-plugin-complexity
Length of output: 1237
🏁 Script executed:
Repository: itaymendel/oxlint-plugin-complexity
Length of output: 1772
Optional peer dependencies will cause import-time failure if not installed.
oxc-parserandestree-walkerare marked as optional peerDependencies, butsrc/analyze.tsimports them unconditionally at the top level. Sincesrc/index.tsre-exportsanalyzeModulefromsrc/analyze.ts, importing from the main package entry point will trigger aMODULE_NOT_FOUNDerror if these optional dependencies are not installed, even when accessing other exports.Consider one of:
import()inanalyzeModuleso the dependency is only required when the function is calledanalyzeModuleto a separate entry point (e.g.,oxlint-plugin-complexity/analyze) to avoid loading optional deps from the main entryoptional: trueif these dependencies are required for core functionality🤖 Prompt for AI Agents