Skip to main content

Module functions

Module functions 

Source
Expand description

Function extraction for similarity scoring (issue #999, stage 2).

A FunctionExtractor turns a source file into its functions, each with a name, a span and the pre-order sequence of syntax-tree node types inside it. Node types only: identifiers and literal values are not part of the sequence, so the summary describes structure. The scoring in cpd_core::similarity is grammar-agnostic; it only ever compares two functions that carry the same FunctionExtractor::grammar id.

§Adding a language

  1. Implement FunctionExtractor: pick a stable grammar id (for a tree-sitter grammar, its name), list the jscpd formats it serves, and in extract walk the tree, opening a RawFunction at every function-like node and appending each visited node’s type id (any dense u16, e.g. tree-sitter’s node.kind_id()) to every open function.
  2. Add the extractor to EXTRACTORS.

Nothing else changes: the CLI, the MCP tool, the reporters and the fixtures pick the new formats up through supports_functions.

Structs§

OxcExtractor
JavaScript, TypeScript, JSX and TSX through the oxc parser.
RawFunction
A function found in a source, before token ranges are attached.

Constants§

NOT_TEST_CASES
Members of a test function that declare something other than a test case: a suite, a step inside a test, a hook, or configuration (test.describe, test.step, test.beforeEach, test.use).
TEST_CASE_CALLS
Functions that declare one test case in the JavaScript test frameworks (Jest, Vitest, Mocha, Jasmine, node:test, Bun): it('title', fn), with .only, .skip, .each(table) and the like after it. Suites (describe) and hooks (beforeEach) are left out: they group or set up tests, while a test case is what a port carries over one by one.

Statics§

EXTRACTORS
Registered extractors, consulted in order. Add new languages here.

Traits§

FunctionExtractor
Language plug-in for function extraction.

Functions§

extract_functions
Extract every function of a source. Returns an empty vector for formats without an extractor and for sources that fail to parse.
extract_with
Every function of source as extractor finds them; empty without an extractor and for an empty source. For callers that pick extractors from a registry of their own, like --semantic’s.
extractor_for
The extractor serving format, if any.
supported_function_formats
Every format some extractor serves, for messages and docs.
supports_functions
Formats handled by extract_functions.