Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support tag names which are static class properties #279

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/analyze/util/ast-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,16 @@ export function getInterfaceKeys(
if (resolvedKey == null) {
continue;
}

let keyNode = resolvedKey.node;
let identifier: Node | undefined;
let declaration: Node | undefined;
if (ts.isTypeReferenceNode(member.type)) {
// { ____: MyButton; } or { ____: namespace.MyButton; }
identifier = member.type.typeName;
if (ts.isComputedPropertyName(member.name)) {
// e.g. [MyButton.TAG] : MyButton -> use initial member name node instead of resolved node
keyNode = member.name.expression;
}
} else if (ts.isTypeLiteralNode(member.type)) {
identifier = undefined;
declaration = member.type;
Expand All @@ -211,7 +215,7 @@ export function getInterfaceKeys(
}

if (declaration != null || identifier != null) {
extensions.push({ key: String(resolvedKey.value), keyNode: resolvedKey.node, declaration, identifier });
extensions.push({ key: String(resolvedKey.value), keyNode: keyNode, declaration, identifier });
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/analyze/util/resolve-node-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ export function resolveNodeValue(node: Node | undefined, context: Context): { va
return resolveNodeValue(node.expression, { ...context, depth });
}

// Resolve initializer value of enum members.
else if (ts.isEnumMember(node)) {
// Resolve initializer value of enum members or class properties.
else if (ts.isEnumMember(node) || ts.isPropertyDeclaration(node)) {
if (node.initializer != null) {
return resolveNodeValue(node.initializer, { ...context, depth });
} else {
} else if (node.parent.name) {
return { value: `${node.parent.name.text}.${node.name.getText()}`, node };
} else {
return { value: `${node.name.getText()}`, node };
}
}

Expand Down
26 changes: 26 additions & 0 deletions test/util/resolve-node-value.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,29 @@ type AliasedLiteral = StringLiteral;
t.is(actualValue, "popsicles", `Resolved value for '${name.getText()}' is invalid`);
});
});

test("resolveNodeValue resolves class properties", t => {
const {
analyzedSourceFiles: [sourceFile],
program
} = analyzeText(`
class FooClass {
static readonly FOO_BAR = "foo-bar";
readonly barBaz = "bar-baz";
fooBaz = "foo-baz";
}
`);

const checker = program.getTypeChecker();

const assertPropertyNodeValue = (node: ts.ClassElement, expectedValue: string) => {
const propertyNodeValue = resolveNodeValue(node, { checker, ts })?.value;
t.is(propertyNodeValue, expectedValue, `Resolved value for '${node.name?.getText()}' is invalid`);
};

findChildren(sourceFile, ts.isClassDeclaration, ({ name, members }) => {
assertPropertyNodeValue(members[0], "foo-bar");
assertPropertyNodeValue(members[1], "bar-baz");
assertPropertyNodeValue(members[2], "foo-baz");
});
});