From 29fb2e7222b9931031100f1cf1b86b3b722096cb Mon Sep 17 00:00:00 2001 From: Edoardo Scibona <12040076+velut@users.noreply.github.com> Date: Tue, 9 Jan 2024 21:11:30 +0100 Subject: [PATCH] feat: add sourceFilePath --- src/extract-class.ts | 9 +++++---- src/extract-expression.ts | 3 ++- src/extract-function-expression.ts | 3 ++- src/extract-function.ts | 3 ++- src/extract-type-alias.ts | 3 ++- src/extract-variable.ts | 3 ++- src/source-file-path.ts | 5 +++++ 7 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 src/source-file-path.ts diff --git a/src/extract-class.ts b/src/extract-class.ts index d94ada14..84c9a0fe 100644 --- a/src/extract-class.ts +++ b/src/extract-class.ts @@ -15,6 +15,7 @@ import { headText } from "./head-text"; import { id } from "./id"; import { isHidden } from "./is-hidden"; import { modifiersText } from "./modifiers-text"; +import { sourceFilePath } from "./source-file-path"; import { typeCheckerType } from "./type-checker-type"; export type ExtractedClass = { @@ -71,7 +72,7 @@ export const extractClass = async ( id: classId, name: exportName, docs: docs(declaration), - file: declaration.getSourceFile().getFilePath() as string, + file: sourceFilePath(declaration), line: declaration.getStartLineNumber(), signature: await classSignature(declaration), constructors: await extractClassConstructors(classId, declaration), @@ -112,7 +113,7 @@ const extractClassConstructors = async ( id: id(classId, "constructor", index > 0 ? `${index}` : ""), name: "constructor", docs: docs(declaration), - file: declaration.getSourceFile().getFilePath() as string, + file: sourceFilePath(declaration), line: declaration.getStartLineNumber(), signature: await classConstructorSignature(declaration), }); @@ -159,7 +160,7 @@ const extractClassProperties = async ( id: id(classId, "property", name), name, docs: docs(declaration), - file: declaration.getSourceFile().getFilePath() as string, + file: sourceFilePath(declaration), line: declaration.getStartLineNumber(), signature: await classPropertySignature(name, declaration), }); @@ -220,7 +221,7 @@ const extractClassMethods = async ( id: id(classId, "method", name), name, docs: docs(declaration), - file: declaration.getSourceFile().getFilePath() as string, + file: sourceFilePath(declaration), line: declaration.getStartLineNumber(), signature: await classMethodSignature(name, declaration), }); diff --git a/src/extract-expression.ts b/src/extract-expression.ts index 461ca807..e2700b08 100644 --- a/src/extract-expression.ts +++ b/src/extract-expression.ts @@ -4,6 +4,7 @@ import { docs } from "./docs"; import type { ExtractedVariable } from "./extract-variable"; import { formatSignature } from "./format-signature"; import { id } from "./id"; +import { sourceFilePath } from "./source-file-path"; export const extractExpression = async ( containerName: string, @@ -14,7 +15,7 @@ export const extractExpression = async ( id: id(containerName, "variable", exportName), name: exportName, docs: docs(declaration), - file: declaration.getSourceFile().getFilePath() as string, + file: sourceFilePath(declaration), line: declaration.getStartLineNumber(), signature: await expressionSignature(exportName, declaration), }); diff --git a/src/extract-function-expression.ts b/src/extract-function-expression.ts index 01572d82..68f13f72 100644 --- a/src/extract-function-expression.ts +++ b/src/extract-function-expression.ts @@ -3,6 +3,7 @@ import { docs } from "./docs"; import type { ExtractedFunction } from "./extract-function"; import { formatSignature } from "./format-signature"; import { id } from "./id"; +import { sourceFilePath } from "./source-file-path"; import { typeCheckerType } from "./type-checker-type"; export const extractFunctionExpression = async ( @@ -14,7 +15,7 @@ export const extractFunctionExpression = async ( id: id(containerName, "function", exportName), name: exportName, docs: docs(declaration), - file: declaration.getSourceFile().getFilePath() as string, + file: sourceFilePath(declaration), line: declaration.getStartLineNumber(), signature: await functionExpressionSignature(exportName, declaration), }); diff --git a/src/extract-function.ts b/src/extract-function.ts index 378b2993..9a28b212 100644 --- a/src/extract-function.ts +++ b/src/extract-function.ts @@ -2,6 +2,7 @@ import type { FunctionDeclaration } from "ts-morph"; import { docs } from "./docs"; import { formatSignature } from "./format-signature"; import { id } from "./id"; +import { sourceFilePath } from "./source-file-path"; import { typeCheckerType } from "./type-checker-type"; export type ExtractedFunction = { @@ -23,7 +24,7 @@ export const extractFunction = async ( id: id(containerName, "function", exportName), name: exportName, docs: docs(declaration), - file: declaration.getSourceFile().getFilePath() as string, + file: sourceFilePath(declaration), line: declaration.getStartLineNumber(), signature: await functionSignature(exportName, declaration), }); diff --git a/src/extract-type-alias.ts b/src/extract-type-alias.ts index 682a7920..c1f6bf45 100644 --- a/src/extract-type-alias.ts +++ b/src/extract-type-alias.ts @@ -2,6 +2,7 @@ import type { TypeAliasDeclaration } from "ts-morph"; import { docs } from "./docs"; import { formatSignature } from "./format-signature"; import { id } from "./id"; +import { sourceFilePath } from "./source-file-path"; export type ExtractedTypeAlias = { kind: "type-alias"; @@ -22,7 +23,7 @@ export const extractTypeAlias = async ( id: id(containerName, "type-alias", exportName), name: exportName, docs: docs(declaration), - file: declaration.getSourceFile().getFilePath() as string, + file: sourceFilePath(declaration), line: declaration.getStartLineNumber(), signature: await typeAliasSignature(declaration), }); diff --git a/src/extract-variable.ts b/src/extract-variable.ts index 61bbd9ef..128e2ebd 100644 --- a/src/extract-variable.ts +++ b/src/extract-variable.ts @@ -3,6 +3,7 @@ import { apparentType } from "./apparent-type"; import { docs } from "./docs"; import { formatSignature } from "./format-signature"; import { id } from "./id"; +import { sourceFilePath } from "./source-file-path"; export type ExtractedVariable = { kind: "variable"; @@ -23,7 +24,7 @@ export const extractVariable = async ( id: id(containerName, "variable", exportName), name: exportName, docs: docs(declaration), - file: declaration.getSourceFile().getFilePath() as string, + file: sourceFilePath(declaration), line: declaration.getStartLineNumber(), signature: await variableSignature(exportName, declaration), }); diff --git a/src/source-file-path.ts b/src/source-file-path.ts new file mode 100644 index 00000000..526fac11 --- /dev/null +++ b/src/source-file-path.ts @@ -0,0 +1,5 @@ +import type { Node } from "ts-morph"; + +export const sourceFilePath = (node: Node): string => { + return node.getSourceFile().getFilePath(); +};