generated from JoshuaKGoldberg/create-typescript-app
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support function expressions (#177)
## PR Checklist - [x] Addresses an existing open issue: fixes #176 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/ts-function-inliner/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/ts-function-inliner/blob/main/.github/CONTRIBUTING.md) were taken ## Overview Refactors the retrieval of function declarations to a separate `getFunctionDeclarationFromCall`.
- Loading branch information
1 parent
b76855b
commit 2ccbf68
Showing
7 changed files
with
171 additions
and
111 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,26 @@ | ||
import ts from "typescript"; | ||
|
||
import { isSmallFunctionLike } from "./isSmallFunctionLike.js"; | ||
|
||
export function getFunctionDeclarationFromCall( | ||
node: ts.CallExpression, | ||
typeChecker: ts.TypeChecker, | ||
) { | ||
let declaration = typeChecker.getSymbolAtLocation(node.expression) | ||
?.valueDeclaration; | ||
|
||
if (!declaration) { | ||
return undefined; | ||
} | ||
|
||
if ( | ||
ts.isPropertyAssignment(declaration) || | ||
ts.isShorthandPropertyAssignment(declaration) | ||
) { | ||
declaration = | ||
typeChecker.getTypeAtLocation(declaration).getSymbol() | ||
?.valueDeclaration ?? declaration; | ||
} | ||
|
||
return isSmallFunctionLike(declaration) ? declaration : undefined; | ||
} |
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,9 +1,12 @@ | ||
import ts from "typescript"; | ||
|
||
import { FunctionDeclarationWithBody } from "./types.js"; | ||
import { FunctionLikeWithBody } from "./types.js"; | ||
|
||
export const isFunctionDeclarationWithBody = ( | ||
export const isFunctionWithBody = ( | ||
node: ts.Node, | ||
): node is FunctionDeclarationWithBody => { | ||
return ts.isFunctionDeclaration(node) && !!node.body; | ||
): node is FunctionLikeWithBody => { | ||
return ( | ||
(ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node)) && | ||
!!node.body | ||
); | ||
}; |
13 changes: 5 additions & 8 deletions
13
src/isSmallFunctionLikeDeclaration.ts → src/isSmallFunctionLike.ts
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
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
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