Skip to content

Commit

Permalink
Support keyof keyword (#91)
Browse files Browse the repository at this point in the history
* Support keyof keyword

* Update src/main/java/org/openrewrite/javascript/tree/JS.java

Co-authored-by: Knut Wannheden <[email protected]>

* Update

---------

Co-authored-by: Knut Wannheden <[email protected]>
  • Loading branch information
kunli2 and knutwannheden authored Nov 28, 2023
1 parent d184d42 commit 6ab4caf
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ public J visitTypeOperator(JS.TypeOperator typeOperator, PrintOutputCapture<P> p
String keyword = "";
if (typeOperator.getOperator() == JS.TypeOperator.Type.ReadOnly) {
keyword = "readonly";
} else if (typeOperator.getOperator() == JS.TypeOperator.Type.KeyOf) {
keyword = "keyof";
}

p.append(keyword);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2333,6 +2333,9 @@ private JS.TypeOperator visitTypeOperator(TSCNode node) {
if (op == TSCSyntaxKind.ReadonlyKeyword) {
before = sourceBefore(TSCSyntaxKind.ReadonlyKeyword);
operator = JS.TypeOperator.Type.ReadOnly;
} else if (op == TSCSyntaxKind.KeyOfKeyword) {
before = sourceBefore(TSCSyntaxKind.KeyOfKeyword);
operator = JS.TypeOperator.Type.KeyOf;
} else {
implementMe(node);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/openrewrite/javascript/tree/JS.java
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,8 @@ public CoordinateBuilder.Expression getCoordinates() {
}

public enum Type {
ReadOnly
ReadOnly,
KeyOf,
}

public JS.TypeOperator.Padding getPadding() {
Expand Down
9 changes: 3 additions & 6 deletions src/test/java/org/openrewrite/javascript/tree/UnaryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,17 @@ void unaryMinusAndPlus(String arg) {
);
}

@ExpectedToFail
@Test
void keyofKeyword() {
rewriteRun(
javaScript(
"""
type Person = { name: string; age: number };
type Person = { name: string , age: number };
type KeysOfPerson = keyof Person;
let person: Person = {
name: "John",
age: 42,
};
"""
)
);
}


}

0 comments on commit 6ab4caf

Please sign in to comment.