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
- Implement
FunctionExtractor: pick a stablegrammarid (for a tree-sitter grammar, its name), list the jscpdformatsit serves, and inextractwalk the tree, opening aRawFunctionat every function-like node and appending each visited node’s type id (any denseu16, e.g. tree-sitter’snode.kind_id()) to every open function. - 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§
- Function
Extractor - 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
sourceasextractorfinds 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.