Skip to content

Commit

Permalink
Add support for TS enum type variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpschaaf committed Dec 8, 2022
1 parent 7fac64c commit bb8ba31
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/labs/analyzer/src/lib/javascript/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {getClassDeclarationInfo} from './classes.js';
import {
getExportAssignmentVariableDeclarationInfo,
getVariableDeclarationInfo,
getEnumDeclarationInfo,
} from './variables.js';
import {AbsolutePath, PackagePath, absoluteToPackage} from '../paths.js';
import {getPackageInfo} from './packages.js';
Expand Down Expand Up @@ -111,6 +112,8 @@ export const getModule = (
addDeclaration(getClassDeclarationInfo(statement, analyzer));
} else if (ts.isVariableStatement(statement)) {
getVariableDeclarationInfo(statement, analyzer).forEach(addDeclaration);
} else if (ts.isEnumDeclaration(statement)) {
addDeclaration(getEnumDeclarationInfo(statement, analyzer));
} else if (ts.isExportDeclaration(statement) && !statement.isTypeOnly) {
const {exportClause, moduleSpecifier} = statement;
if (exportClause === undefined) {
Expand Down
13 changes: 12 additions & 1 deletion packages/labs/analyzer/src/lib/javascript/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type VariableName =
* ts.Identifier within a potentially nested ts.VariableDeclaration.
*/
const getVariableDeclaration = (
dec: ts.VariableDeclaration,
dec: ts.VariableDeclaration | ts.EnumDeclaration,
name: ts.Identifier,
analyzer: AnalyzerInterface
): VariableDeclaration => {
Expand Down Expand Up @@ -129,3 +129,14 @@ const getExportAssignmentVariableDeclaration = (
type: getTypeForNode(exportAssignment.expression, analyzer),
});
};

export const getEnumDeclarationInfo = (
statement: ts.EnumDeclaration,
analyzer: AnalyzerInterface
) => {
return {
name: statement.name.text,
factory: () => getVariableDeclaration(statement, statement.name, analyzer),
isExport: hasExportKeyword(statement),
};
};
7 changes: 5 additions & 2 deletions packages/labs/analyzer/src/lib/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,15 @@ export abstract class Declaration {
}

export interface VariableDeclarationInit extends DeclarationInit {
node: ts.VariableDeclaration | ts.ExportAssignment;
node: ts.VariableDeclaration | ts.ExportAssignment | ts.EnumDeclaration;
type: Type | undefined;
}

export class VariableDeclaration extends Declaration {
readonly node: ts.VariableDeclaration | ts.ExportAssignment;
readonly node:
| ts.VariableDeclaration
| ts.ExportAssignment
| ts.EnumDeclaration;
readonly type: Type | undefined;
constructor(init: VariableDeclarationInit) {
super(init);
Expand Down

0 comments on commit bb8ba31

Please sign in to comment.