Skip to content

Commit 1b558b4

Browse files
committed
refactor(feat): include filePath, and improve internals in that area
1 parent 9d91730 commit 1b558b4

File tree

6 files changed

+66
-42
lines changed

6 files changed

+66
-42
lines changed

src/extractinator.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import type { FileParserContext } from './files/files'
12
import type { ParsedFile } from './types'
23

34
import { l, b, n, o, g, r, dim } from './utils/log'
45
import { parseSvelteFile } from './files/svelte'
56
import { parseTSFile } from './files/typescript'
67
import { createTSDocParser } from './comments'
7-
import { mkdir, writeFile } from 'fs/promises'
88
import { basename } from 'node:path'
99
import { Project } from 'ts-morph'
1010
import { emit_dts } from './emit'
@@ -27,16 +27,15 @@ export async function extractinator(options: ExtractinatorOptions) {
2727

2828
const tsdoc = createTSDocParser(options.tsdocConfigPath)
2929

30-
//? Map of input_file_path:file
31-
const parsed_files = new Map<string, ParsedFile>()
30+
const parsed_files: ParsedFile[] = []
3231

3332
//? Loop over all the loaded source files
34-
for (const sourceFile of project.getSourceFiles()) {
33+
for (const source_file of project.getSourceFiles()) {
3534
//? Get the filename e.g. KitchenSink.svelte.d.ts
36-
const dts_file_name = sourceFile.getBaseName()
35+
const dts_file_name = source_file.getBaseName()
3736

3837
//? Get the input file name
39-
const input_file_path = dts.dts_file_map.get(sourceFile.getFilePath())!
38+
const input_file_path = dts.dts_file_map.get(source_file.getFilePath())!
4039
const file_name = basename(input_file_path)
4140

4241
//? Work out the file extension
@@ -48,11 +47,18 @@ export async function extractinator(options: ExtractinatorOptions) {
4847

4948
l(`Processing File (${dim(ext)}) "${o(file_name)}"`)
5049

50+
const ctx: FileParserContext = {
51+
file_name,
52+
input_file_path,
53+
file: source_file,
54+
tsdoc,
55+
}
56+
5157
switch (ext) {
5258
//? Handle Svelte Files
5359
case '.svelte.d.ts': {
54-
const file = parseSvelteFile(file_name, sourceFile, tsdoc)
55-
parsed_files.set(input_file_path, file)
60+
const file = parseSvelteFile(ctx)
61+
parsed_files.push(file)
5662

5763
l(' ⤷', o(file.componentName))
5864
l(' ', dim('Props: '), b(file.props.length))
@@ -67,8 +73,8 @@ export async function extractinator(options: ExtractinatorOptions) {
6773
case '.d.ts': {
6874
const basename = dts_file_name.replace(ext, '')
6975

70-
const file = parseTSFile(file_name, sourceFile, tsdoc)
71-
parsed_files.set(input_file_path, file)
76+
const file = parseTSFile(ctx)
77+
parsed_files.push(file)
7278

7379
l(' ⤷', o(basename))
7480

@@ -91,5 +97,5 @@ export async function extractinator(options: ExtractinatorOptions) {
9197

9298
await dts.cleanup()
9399

94-
return Array.from(parsed_files.values())
100+
return parsed_files
95101
}

src/files/files.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { TSDocParser } from '@microsoft/tsdoc'
2+
import type { SourceFile } from 'ts-morph'
3+
4+
export interface FileParserContext {
5+
file_name: string
6+
input_file_path: string
7+
file: SourceFile
8+
tsdoc: TSDocParser
9+
}

src/files/svelte.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
import type { SlotBit, Bit, ParsedSvelteFile } from '../types'
22
import type { TSDocParser } from '@microsoft/tsdoc'
3+
import type { FileParserContext } from './files'
34
import type { SourceFile, Node } from 'ts-morph'
45

56
import { getName, getType, isExported, toBit } from '../utils/nodes'
67
import { parseCommentFromNode } from '../comments'
78
import ts from 'typescript'
89

9-
export function parseSvelteFile(
10-
file_name: string,
11-
file: SourceFile,
12-
parser: TSDocParser,
13-
): ParsedSvelteFile {
10+
export function parseSvelteFile({
11+
file_name,
12+
input_file_path,
13+
file,
14+
tsdoc,
15+
}: FileParserContext): ParsedSvelteFile {
1416
//? Get the component name from the file name
1517
const componentName = file_name.replace('.svelte', '')
1618

1719
//? Get the props, events, slots nodes
1820
const stuff = extractSvelteTypeNodes(file)
1921

2022
//? Parse the props, events, slots nodes to bits
21-
const slots = stuff.slots?.map<SlotBit>((n) => parseSlot(n, parser)) || []
22-
const events = stuff.events?.map<Bit>((n) => toBit(n, parser)) || []
23-
const props = stuff.props?.map<Bit>((n) => toBit(n, parser)) || []
23+
const slots = stuff.slots?.map<SlotBit>((n) => parseSlot(n, tsdoc)) || []
24+
const events = stuff.events?.map<Bit>((n) => toBit(n, tsdoc)) || []
25+
const props = stuff.props?.map<Bit>((n) => toBit(n, tsdoc)) || []
2426

2527
//? Extract the export bits
26-
const variables = extractModuleExports(componentName, file, parser)
28+
const variables = extractModuleExports(componentName, file, tsdoc)
2729

2830
return {
2931
fileName: file_name,
32+
filePath: input_file_path,
3033
componentName,
3134
props,
3235
events,
@@ -121,7 +124,7 @@ function extractSvelteTypeNodes(file: SourceFile) {
121124
}
122125
}
123126

124-
function parseSlot(node: Node, parser: TSDocParser): SlotBit {
127+
function parseSlot(node: Node, tsdoc: TSDocParser): SlotBit {
125128
/**
126129
* ? Slots look like this
127130
*
@@ -146,16 +149,16 @@ function parseSlot(node: Node, parser: TSDocParser): SlotBit {
146149
.getChildrenOfKind(ts.SyntaxKind.TypeLiteral)[0]
147150
?.getChildSyntaxList()
148151
?.getChildren()
149-
?.map((node) => toBit(node, parser))
152+
?.map((node) => toBit(node, tsdoc))
150153

151154
return {
152-
comment: parseCommentFromNode(node, parser),
155+
comment: parseCommentFromNode(node, tsdoc),
153156
name: getName(node) || 'it broke',
154157
props: props || [],
155158
}
156159
}
157160

158-
function extractModuleExports(componentName: string, file: SourceFile, parser: TSDocParser) {
161+
function extractModuleExports(componentName: string, file: SourceFile, tsdoc: TSDocParser) {
159162
//? Nodes we don't care about, they are handled elsewhere
160163
const ignoredNodeNames = [
161164
'default',
@@ -181,14 +184,14 @@ function extractModuleExports(componentName: string, file: SourceFile, parser: T
181184
)!
182185

183186
return {
184-
comment: parseCommentFromNode(node, parser),
187+
comment: parseCommentFromNode(node, tsdoc),
185188
name: getName(declaration) || 'it broke ahjk',
186189
type: getType(declaration),
187190
}
188191
}
189192

190193
default:
191-
return toBit(node, parser)
194+
return toBit(node, tsdoc)
192195
}
193196
})
194197
)

src/files/typescript.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import type { ExportBit, ParsedTSFile } from '../types'
2-
import type { TSDocParser } from '@microsoft/tsdoc'
3-
import type { SourceFile } from 'ts-morph'
2+
import type { FileParserContext } from './files'
43

54
import { parseCommentFromNode } from '../comments'
65
import { getType } from '../utils/nodes'
76
import ts from 'typescript'
87

9-
export function parseTSFile(file_name: string, file: SourceFile, tsdoc: TSDocParser): ParsedTSFile {
8+
export function parseTSFile({
9+
file_name,
10+
input_file_path,
11+
file,
12+
tsdoc,
13+
}: FileParserContext): ParsedTSFile {
1014
const export_bits: ExportBit[] = []
1115

1216
//? Loop over all the export declarations
@@ -38,6 +42,7 @@ export function parseTSFile(file_name: string, file: SourceFile, tsdoc: TSDocPar
3842

3943
return {
4044
fileName: file_name,
45+
filePath: input_file_path,
4146
exports: export_bits,
4247
}
4348
}

src/types.d.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ declare global {
44

55
export type ParsedFile = ParsedSvelteFile | ParsedTSFile
66

7-
export interface ParsedTSFile {
7+
export interface BaseParsedFile {
88
/**
99
* The name of the file read
1010
*
@@ -14,13 +14,23 @@ export interface ParsedTSFile {
1414
*/
1515
fileName: string
1616

17+
/**
18+
* The absolute path to the source file used
19+
*
20+
* @example
21+
* /home/ghost/Desktop/extractinator/playground/KitchenSink.svelte
22+
*/
23+
filePath: string
24+
}
25+
26+
export interface ParsedTSFile extends BaseParsedFile {
1727
/**
1828
* The exports from the component
1929
*/
2030
exports: Bit[]
2131
}
2232

23-
export interface ParsedSvelteFile {
33+
export interface ParsedSvelteFile extends BaseParsedFile {
2434
/**
2535
* The name of the component
2636
*
@@ -29,15 +39,6 @@ export interface ParsedSvelteFile {
2939
*/
3040
componentName: string
3141

32-
/**
33-
* The name of the file read
34-
*
35-
* @example
36-
* KitchenSink.svelte
37-
* example.ts
38-
*/
39-
fileName: string
40-
4142
props: Bit[]
4243
events: Bit[]
4344
slots: SlotBit[]

src/utils/nodes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ export function getType(node: Node) {
3434
/**
3535
* Convert the node into a {@link Bit}
3636
*/
37-
export function toBit(node: Node, parser: TSDocParser): Bit {
37+
export function toBit(node: Node, tsdoc: TSDocParser): Bit {
3838
return {
39-
comment: parseCommentFromNode(node, parser),
39+
comment: parseCommentFromNode(node, tsdoc),
4040
name: getName(node) || 'it broke',
4141
type: getType(node),
4242
}

0 commit comments

Comments
 (0)