@abtasty/codebase-analyzer-typescript
v0.1.0
Published
Analyze codebases to detect feature flag usage across multiple platforms
Maintainers
Readme
@abtasty/codebase-analyzer-typescript
A TypeScript library that scans codebases to detect feature flag usage across multiple platforms and programming languages.
Features
- Multi-platform detection — AB Tasty (Flagship SDK V3), LaunchDarkly, OpenFeature, Optimizely, Split, VWO
- Multi-language support — JavaScript/TypeScript, Go, Python, Java, Kotlin, PHP, Swift, Objective-C, C#/.NET, VB.NET, Dart, C/C++
- Flag metadata extraction — flag keys, default values, types (boolean, string, number, json), line numbers
- Repository linking — generates direct links to flag usages in your source repository
- Custom regex support — extend detection with your own patterns via JSON configuration
- Comment-based detection — supports
fe:flag: flagKey, typecomments for manual flag annotation - Dual module format — ships as both CommonJS and ESM with full TypeScript declarations
Installation
npm install @abtasty/codebase-analyzer-typescriptQuick start
import { analyzeCode, createConfig } from "@abtasty/codebase-analyzer-typescript";
const config = createConfig({
directory: "./src",
repositoryURL: "https://github.com/your-org/your-repo",
repositoryBranch: "main",
});
const results = analyzeCode(config);
for (const file of results) {
if (file.results.length > 0) {
console.log(`${file.file}: ${file.results.length} flag(s) found`);
for (const flag of file.results) {
console.log(` - ${flag.flagKey} (${flag.flagType}, default: ${flag.flagDefaultValue})`);
}
}
}API
createConfig(options?)
Creates an AnalyzerConfig with sensible defaults.
const config = createConfig({
directory: "./src", // Directory to scan (default: ".")
repositoryURL: "https://...", // Repository root URL for generating links
repositoryBranch: "main", // Branch name (default: "main")
nbLineCodeEdges: 1, // Context lines around flag usage (default: 1)
filesToExclude: [".git", "node_modules"], // Exclusion patterns
searchCustomRegex: "", // Custom regex patterns as JSON string
});analyzeCode(config)
Scans the codebase and logs results to the console. Returns an array of FileAnalyzed objects.
const results: FileAnalyzed[] = analyzeCode(config);extractFlagsInfo(config)
Same as analyzeCode but without console output. Use this for silent/programmatic analysis.
const results: FileAnalyzed[] = extractFlagsInfo(config);getFlagType(defaultValue)
Determines the flag type from its default value string.
const { flagType, defaultValue } = getFlagType('"hello"');
// flagType: "string", defaultValue: '"hello"'
const { flagType } = getFlagType("true");
// flagType: "boolean"getPlatformRegexes(platform)
Returns regex patterns for a specific platform, or null if not found.
import { getPlatformRegexes } from "@abtasty/codebase-analyzer-typescript";
const patterns = getPlatformRegexes("launchdarkly");Supported platforms: "launchdarkly", "openfeature", "optimizely", "split", "vwo".
getTemplateRegexes()
Returns template regex patterns that can be used as a starting point for custom patterns.
Types
interface AnalyzerConfig {
directory: string;
repositoryURL: string;
repositoryBranch: string;
nbLineCodeEdges: number;
filesToExclude: string[];
searchCustomRegex: string;
}
interface FileAnalyzed {
file: string; // Relative file path
fileURL: string; // Full URL to the file in the repository
error: string | null; // Error message if analysis failed
results: FlagAnalyzed[]; // Flags found in this file
}
interface FlagAnalyzed {
lineNumber: number; // Line where the flag was found
flagKey: string; // The flag key/name
flagDefaultValue: string; // The default value
flagType: string; // "boolean" | "string" | "number" | "json" | "unknown"
exists: boolean; // Whether the flag exists in the platform
}
interface RegexPattern {
file_extension: string; // File extension regex (e.g. ".[jt]s$")
regexes: string[]; // Regex patterns to apply
}
type Platform = "launchdarkly" | "openfeature" | "optimizely" | "split" | "vwo";Custom regex patterns
You can extend detection with custom regex patterns by passing a JSON string to searchCustomRegex:
const customRegex = JSON.stringify([
{
file_extension: "\\.tsx?$",
regexes: [
"myCustomFlag\\(['\"]([\\w-]+)['\"]\\s*,\\s*([^)]+)\\)"
]
}
]);
const config = createConfig({
directory: "./src",
repositoryURL: "https://github.com/org/repo",
searchCustomRegex: customRegex,
});Each regex must use capture groups:
- Group 1 — the flag key
- Group 2 (optional) — the default value
Comment-based flag annotation
You can annotate flags directly in your code using comments:
// fe:flag: myFlagKey, booleanThis is useful for flags that are not detected by the built-in regex patterns.
Supported SDK methods
AB Tasty (Flagship)
useFsFlag(), getFlag(), getModification(), and key/defaultValue object patterns.
LaunchDarkly
variation(), boolVariation(), stringVariation(), intVariation(), float64Variation(), jsonVariation(), and more.
OpenFeature
getBooleanValue(), getStringValue(), getNumberValue(), getObjectValue(), and their Details variants.
Optimizely
isFeatureEnabled(), getFeatureVariable*(), decide(), and more.
Split
getTreatment(), getTreatmentWithConfig(), and similar.
VWO
isFeatureEnabled(), getFeatureVariableValue(), activate(), and more.
Development
# Install dependencies
npm install
# Build
npm run build
# Run tests
npm test
# Type check
npm run typecheck
# Lint
npm run lintLicense
MIT
