-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Put getter and setter together automatically (#54)
Issue: #49
- Loading branch information
Showing
7 changed files
with
264 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { AST_NODE_TYPES } from "@typescript-eslint/types"; | ||
import { isNode, isPrivateName, isStringLiteral } from "@babel/types"; | ||
import type { Node } from "."; | ||
|
||
export function nameOf(n: Node): string | null { | ||
if ("key" in n) { | ||
switch (n.key.type) { | ||
case AST_NODE_TYPES.Identifier: | ||
case AST_NODE_TYPES.PrivateIdentifier: | ||
if ("computed" in n && n.computed === true) return null; | ||
return n.key.name; | ||
case AST_NODE_TYPES.Literal: { | ||
const value = n.key.value; | ||
if (typeof value !== "string") return null; | ||
return value; | ||
} | ||
} | ||
if (isNode(n.key)) { | ||
// babel nodes | ||
switch (true) { | ||
case isPrivateName(n.key): | ||
if (n.key.id.type !== AST_NODE_TYPES.Identifier) return null; | ||
return n.key.id.name; | ||
case isStringLiteral(n.key): | ||
return n.key.value; | ||
} | ||
} | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,6 @@ | ||
import { AST_NODE_TYPES } from "@typescript-eslint/types"; | ||
import { C, Comparator } from "./comparator"; | ||
import { Node } from "../ast"; | ||
import { isNode, isPrivateName, isStringLiteral } from "@babel/types"; | ||
import { nameOf } from "../ast/name-of"; | ||
|
||
export const keyIdentifierName = (): Comparator<Node> => | ||
C.by(($) => { | ||
if ("key" in $) { | ||
switch ($.key.type) { | ||
case AST_NODE_TYPES.Identifier: | ||
case AST_NODE_TYPES.PrivateIdentifier: | ||
if ("computed" in $ && $.computed === true) return null; | ||
return $.key.name; | ||
case AST_NODE_TYPES.Literal: { | ||
const value = $.key.value; | ||
if (typeof value !== "string") return null; | ||
return value; | ||
} | ||
} | ||
if (isNode($.key)) { | ||
// babel nodes | ||
switch (true) { | ||
case isPrivateName($.key): | ||
if ($.key.id.type !== AST_NODE_TYPES.Identifier) return null; | ||
return $.key.id.name; | ||
case isStringLiteral($.key): | ||
return $.key.value; | ||
} | ||
} | ||
} | ||
return null; | ||
}, C.maybe(C.string)); | ||
C.by(nameOf, C.maybe(C.string)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Node } from "./ast"; | ||
import { nameOf } from "./ast/name-of"; | ||
|
||
export function putGettersAndSettersTogether(members: Node[]): Node[] { | ||
const getterNames = new Set<string>( | ||
members.flatMap((m) => { | ||
const name = nameOf(m); | ||
if (!("kind" in m) || m.kind !== "get" || name === null) return []; | ||
return [name]; | ||
}), | ||
); | ||
const setters = new Map<string, Node>( | ||
members.flatMap((m) => { | ||
const name = nameOf(m); | ||
if (!("kind" in m) || m.kind !== "set" || name === null) return []; | ||
return [[name, m]]; | ||
}), | ||
); | ||
|
||
return members.flatMap((m) => { | ||
const name = nameOf(m); | ||
if (!("kind" in m) || name === null) return [m]; | ||
|
||
switch (m.kind) { | ||
case "get": { | ||
const danglingSetter = setters.get(name); | ||
if (danglingSetter !== undefined) return [m, danglingSetter]; | ||
return [m]; | ||
} | ||
case "set": { | ||
if (getterNames.has(name)) return []; | ||
return [m]; | ||
} | ||
} | ||
return [m]; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.