MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / extractProperty

Method extractProperty

src/extraction/tree-sitter.ts:1992–2046  ·  view source on GitHub ↗

* Extract a class property declaration (e.g. C# `public string Name { get; set; }`). * Extracts as 'property' kind node inside the owning class.

(node: SyntaxNode)

Source from the content-addressed store, hash-verified

1990 * Extracts as 'property' kind node inside the owning class.
1991 */
1992 private extractProperty(node: SyntaxNode): Node | null {
1993 if (!this.extractor) return null;
1994
1995 const docstring = getPrecedingDocstring(node, this.source);
1996 const visibility = this.extractor.getVisibility?.(node);
1997 const isStatic = this.extractor.isStatic?.(node) ?? false;
1998
1999 const hookName = this.extractor.extractPropertyName?.(node, this.source);
2000 // JS `field_definition` names its key the `property` field (TS uses
2001 // `name`) — try both before the generic identifier scan (#808).
2002 const nameNode = hookName
2003 ? null
2004 : getChildByField(node, 'name') ||
2005 getChildByField(node, 'property') ||
2006 node.namedChildren.find(c => c.type === 'identifier');
2007 const name = hookName ?? (nameNode ? getNodeText(nameNode, this.source) : null);
2008 if (!name) return null;
2009
2010 // Get property type. TS/JS field definitions carry an explicit `type`
2011 // field (a `type_annotation`); their other named children are the name
2012 // and the initializer VALUE, which the generic finder below would
2013 // wrongly pick — so fields use the type field only (#808). Other
2014 // languages (C# property_declaration) keep the generic scan.
2015 const isTsJsField =
2016 node.type === 'public_field_definition' || node.type === 'field_definition';
2017 const typeNode = isTsJsField
2018 ? getChildByField(node, 'type')
2019 : node.namedChildren.find(
2020 c => c.type !== 'modifier' && c.type !== 'modifiers'
2021 && c.type !== 'identifier' && c.type !== 'accessor_list'
2022 && c.type !== 'accessors' && c.type !== 'equals_value_clause'
2023 );
2024 const typeText = typeNode
2025 ? getNodeText(typeNode, this.source).replace(/^:\s*/, '')
2026 : undefined;
2027 const signature = typeText ? `${typeText} ${name}` : name;
2028
2029 const propNode = this.createNode('property', name, node, {
2030 docstring,
2031 signature,
2032 visibility,
2033 isStatic,
2034 });
2035
2036 // `@Inject() private svc: Foo` and similar — capture the
2037 // decorator->target relationship for class properties too.
2038 if (propNode) {
2039 this.extractDecoratorsFor(node, propNode.id);
2040 // Emit `references` edges from the property to types named in its
2041 // type annotation (#381). The generic walker handles TS-style
2042 // `type_annotation` children; the C# branch walks the `type` field.
2043 this.extractTypeAnnotations(node, propNode.id);
2044 }
2045 return propNode;
2046 }
2047
2048 /**
2049 * Extract a class field declaration (e.g. Java field_declaration, C# field_declaration).

Callers 1

visitNodeMethod · 0.95

Calls 6

createNodeMethod · 0.95
extractDecoratorsForMethod · 0.95
getPrecedingDocstringFunction · 0.90
getChildByFieldFunction · 0.90
getNodeTextFunction · 0.90

Tested by

no test coverage detected