Intelligent code analysis to determine the context of comments, identifying whether they document functions, methods, classes, properties, or other JavaScript constructs.
Analyzes JavaScript code to determine its type and structure, providing semantic information about what the code represents.
/**
* Parse code context to determine type and structure
* @param {string} str - Code string to analyze
* @param {Object} [parentContext] - Parent context information for nested constructs
* @returns {ContextObject|null} Context information or null if no pattern matches
*/
function parseCodeContext(str, parentContext);Usage Examples:
const dox = require('dox');
// Function declaration
const funcCode = 'function calculateSum(a, b) { return a + b; }';
const funcContext = dox.parseCodeContext(funcCode);
console.log(funcContext);
// Output:
// {
// type: 'function',
// name: 'calculateSum',
// string: 'calculateSum()'
// }
// Method definition
const methodCode = 'User.prototype.getName = function() { return this.name; }';
const methodContext = dox.parseCodeContext(methodCode);
console.log(methodContext);
// Output:
// {
// type: 'method',
// constructor: 'User',
// cons: 'User',
// name: 'getName',
// string: 'User.prototype.getName()'
// }
// Class declaration
const classCode = 'class Vehicle extends Transport { constructor() {} }';
const classContext = dox.parseCodeContext(classCode);
console.log(classContext);
// Output:
// {
// type: 'class',
// constructor: 'Vehicle',
// cons: 'Vehicle',
// name: 'Vehicle',
// extends: 'Transport',
// string: 'new Vehicle()'
// }
// Property assignment
const propCode = 'module.exports.version = "1.0.0"';
const propContext = dox.parseCodeContext(propCode);
console.log(propContext);
// Output:
// {
// type: 'property',
// receiver: 'module.exports',
// name: 'version',
// value: '"1.0.0"',
// string: 'module.exports.version'
// }Array of pattern matching functions that detect different code patterns. This array can be extended to support additional code patterns.
/**
* Array of pattern matching functions for code context detection
* Each function takes (str, parentContext) and returns context object or falsy value
*/
const contextPatternMatchers: Array<(str: string, parentContext?: Object) => ContextObject | null>;Usage Examples:
const dox = require('dox');
// Add custom pattern matcher for arrow functions
dox.contextPatternMatchers.push(function(str, parentContext) {
const arrowMatch = /^\s*(?:const|let|var)\s+([\w$]+)\s*=\s*\(([^)]*)\)\s*=>/;
if (arrowMatch.test(str)) {
return {
type: 'function',
name: RegExp.$1,
string: RegExp.$1 + '()'
};
}
return null;
});
// Now arrow functions will be detected
const arrowCode = 'const add = (a, b) => a + b';
const arrowContext = dox.parseCodeContext(arrowCode);
console.log(arrowContext.type); // 'function'
console.log(arrowContext.name); // 'add'Detects ES6+ class declarations with inheritance support.
// Pattern: class ClassName [extends SuperClass] { ... }
class MyClass extends BaseClass {
// Context: { type: 'class', name: 'MyClass', extends: 'BaseClass', ... }
}
export default class ExportedClass {
// Context: { type: 'class', name: 'ExportedClass', ... }
}Detects function declarations and expressions in various forms.
// Function declaration
function myFunction() {}
// Context: { type: 'function', name: 'myFunction', string: 'myFunction()' }
// Function expression
const func = function namedFunc() {};
// Context: { type: 'function', name: 'namedFunc', ... }
// Variable assignment
var helper = function() {};
// Context: { type: 'function', name: 'helper', ... }
// Export functions
export function exportedFunc() {}
export default function defaultFunc() {}Detects various method definition patterns including prototype methods, class methods, and object methods.
// Prototype method
Object.prototype.method = function() {};
// Context: { type: 'method', constructor: 'Object', name: 'method', ... }
// Class method
class MyClass {
myMethod() {}
// Context: { type: 'method', constructor: 'MyClass', name: 'myMethod', ... }
static staticMethod() {}
// Context: { type: 'method', constructor: 'MyClass', name: 'staticMethod', ... }
}
// Object literal method
const obj = {
method: function() {}
// Context: { type: 'method', name: 'method', ... }
};Detects constructor functions within classes.
class MyClass {
constructor(params) {}
// Context: { type: 'constructor', constructor: 'MyClass', name: 'constructor', ... }
}Detects property assignments and declarations.
// Property assignment
obj.property = 'value';
// Context: { type: 'property', receiver: 'obj', name: 'property', value: "'value'", ... }
// Prototype property
MyClass.prototype.prop = 42;
// Context: { type: 'property', constructor: 'MyClass', name: 'prop', value: '42', ... }
// Object literal property
const obj = {
prop: 'value'
// Context: { type: 'property', name: 'prop', value: "'value'", ... }
};
// Getter/setter
class MyClass {
get value() { return this._value; }
// Context: { type: 'property', constructor: 'MyClass', name: 'value', ... }
}Detects variable declarations and assignments.
const myVar = 'value';
let anotherVar = 42;
var oldStyleVar = true;
// Context: { type: 'declaration', name: 'myVar', value: "'value'", string: 'myVar' }interface ContextObject {
/** Type of code construct */
type: 'function' | 'method' | 'class' | 'constructor' | 'property' | 'declaration' | 'prototype';
/** Name of the identifier */
name: string;
/** Constructor/class name for methods and properties */
constructor?: string;
/** Alias for constructor property */
cons?: string;
/** Object receiving the method/property */
receiver?: string;
/** Assigned value for properties and declarations */
value?: string;
/** Parent class for inheritance */
extends?: string;
/** Formatted string representation */
string: string;
}When parsing nested constructs (like methods within classes), parent context provides information about the containing scope:
interface ParentContext {
/** Name of the parent construct */
name: string;
/** Type of the parent construct */
type?: string;
/** Constructor name if applicable */
constructor?: string;
}You can add custom pattern matchers to support additional code patterns:
const dox = require('dox');
// Add support for TypeScript interfaces
dox.contextPatternMatchers.push(function(str, parentContext) {
if (/^\s*interface\s+([\w$]+)/.test(str)) {
return {
type: 'interface',
name: RegExp.$1,
string: RegExp.$1
};
}
return null;
});
// Add support for TypeScript enums
dox.contextPatternMatchers.push(function(str, parentContext) {
if (/^\s*enum\s+([\w$]+)/.test(str)) {
return {
type: 'enum',
name: RegExp.$1,
string: RegExp.$1
};
}
return null;
});The context detection follows this process:
contextPatternMatchers arraynull if no patterns matchDox tracks namespace context for better method and property detection:
class MyClass {
method1() {} // parentContext: { name: 'MyClass', type: 'class' }
method2() {} // parentContext: { name: 'MyClass', type: 'class' }
}
MyClass.prototype = {
prop1: 'value', // parentContext: { name: 'MyClass', constructor: 'MyClass' }
method3() {} // parentContext: { name: 'MyClass', constructor: 'MyClass' }
};Context parsing is designed to be fault-tolerant:
null rather than throwing errors