Skip to content

Commit

Permalink
Add support for initializers which are identifiers
Browse files Browse the repository at this point in the history
Resolves #1730.
  • Loading branch information
Gerrit0 committed Oct 10, 2021
1 parent 0d04c08 commit 4b927ff
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Unreleased

### Features

- Added support for displaying identifiers & property access expressions in initializers, #1730.

### Bug Fixes

- Fixed flash when navigating to a second page when OS theme does not match selected theme, #1709.
Expand Down
18 changes: 17 additions & 1 deletion src/lib/converter/convert-expression.ts
Expand Up @@ -17,14 +17,15 @@ export function convertDefaultValue(
}
}

export function convertExpression(expression: ts.Expression) {
export function convertExpression(expression: ts.Expression): string {
switch (expression.kind) {
case ts.SyntaxKind.StringLiteral:
case ts.SyntaxKind.TrueKeyword:
case ts.SyntaxKind.FalseKeyword:
case ts.SyntaxKind.NullKeyword:
case ts.SyntaxKind.NumericLiteral:
case ts.SyntaxKind.PrefixUnaryExpression:
case ts.SyntaxKind.Identifier:
return expression.getText();
}

Expand All @@ -42,6 +43,21 @@ export function convertExpression(expression: ts.Expression) {
return "{}";
}

// a.b.c.d
if (ts.isPropertyAccessExpression(expression)) {
const parts = [expression.name.getText()];
let iter = expression.expression;
while (ts.isPropertyAccessExpression(iter)) {
parts.unshift(iter.name.getText());
iter = iter.expression;
}

if (ts.isIdentifier(iter)) {
parts.unshift(iter.text);
return parts.join(".");
}
}

// More complex expressions are generally not useful in the documentation.
// Show that there was a value, but not specifics.
return "...";
Expand Down
8 changes: 4 additions & 4 deletions src/test/converter/exports/specs.json
Expand Up @@ -110,7 +110,7 @@
"type": "literal",
"value": 1
},
"defaultValue": "..."
"defaultValue": "Mod.a"
},
{
"id": 24,
Expand All @@ -123,7 +123,7 @@
"id": 29,
"name": "__module"
},
"defaultValue": "..."
"defaultValue": "Mod"
},
{
"id": 27,
Expand All @@ -136,7 +136,7 @@
"id": 29,
"name": "a"
},
"defaultValue": "..."
"defaultValue": "Mod.a"
},
{
"id": 25,
Expand All @@ -149,7 +149,7 @@
"id": 29,
"name": "__module"
},
"defaultValue": "..."
"defaultValue": "Mod"
}
],
"groups": [
Expand Down
2 changes: 1 addition & 1 deletion src/test/converter/function/specs.json
Expand Up @@ -1090,7 +1090,7 @@
"type": "intrinsic",
"name": "number"
},
"defaultValue": "..."
"defaultValue": "Number.NaN"
},
{
"id": 18,
Expand Down
2 changes: 1 addition & 1 deletion src/test/converter/variables/specs.json
Expand Up @@ -911,7 +911,7 @@
"type": "intrinsic",
"name": "string"
},
"defaultValue": "..."
"defaultValue": "z"
}
],
"groups": [
Expand Down

0 comments on commit 4b927ff

Please sign in to comment.