-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathcomponent.ts
181 lines (156 loc) · 4.06 KB
/
component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { isKebabCase, kebabCase } from './utilities';
export interface SourceLocation {
offset: number;
line: number;
column: number;
}
export interface SourceRange {
source: string;
start: SourceLocation;
end: SourceLocation;
}
export interface Addressable {
loc: SourceRange;
}
export interface ImportSource extends Addressable {
moduleName: string;
exportName?: string;
localName: string;
}
export interface ComponentRegistrationInfo extends Addressable {
name: string;
aliases: string[];
kind: 'script';
source: ImportSource;
}
export type TypeInfo =
| {
kind: 'string' | 'number' | 'boolean';
}
| {
kind: 'enum';
values: string[];
}
| {
kind: 'expression';
imports: string[];
expression: string;
};
export type ValueInfo =
| {
kind: 'expression';
imports: string[];
expression: string;
}
| {
kind: 'function';
expression: string;
}
| {
kind: 'value';
value: string;
};
export interface TagInfo {
title: string;
content: string;
}
export interface Taggable {
tags: TagInfo[];
}
export interface PropInfo extends Taggable, Addressable {
name: string;
description: string;
required: boolean;
type: TypeInfo[];
defaultValue: ValueInfo | null;
}
export interface SyntaxError {
message: string;
loc: SourceLocation;
}
export interface ComponentInfo {
components: ComponentRegistrationInfo[];
props: PropInfo[];
options?: ComponentOptionsInfo;
setup?: SetupInfo;
errors: SyntaxError[];
}
export interface ComponentOptionsInfo extends Addressable {
properties: Record<string, Addressable>;
}
export interface SetupInfo extends Addressable {
props?: Addressable;
context?: Addressable;
return?: Addressable;
}
export interface ComponentInfoFactory {
addError(message: string, loc: SourceLocation): ComponentInfoFactory;
addProp(name: string, options?: Partial<PropInfo>): ComponentInfoFactory;
addLocalComponent(name: string, source: ImportSource, loc?: SourceRange): ComponentInfoFactory;
addOption(name: string, address: Addressable): ComponentInfoFactory;
addSetup(name: Exclude<keyof SetupInfo, 'loc'> | '', address: Addressable): ComponentInfoFactory;
info(): ComponentInfo;
}
export function createComponentInfoFactory(): ComponentInfoFactory {
const component: ComponentInfo = {
props: [],
components: [],
errors: [],
};
const factory: ComponentInfoFactory = {
addError(message, loc) {
component.errors.push({ message, loc });
return factory;
},
addProp(name, options = {}) {
const index = component.props.findIndex((prop) => prop.name === name);
if (index >= 0) {
const prop = component.props[index];
Object.assign(prop, { name, ...options });
} else {
component.props.push({
name,
tags: [],
description: '',
required: false,
type: [{ kind: 'expression', imports: [], expression: 'any' }],
defaultValue: null,
loc: null as any,
...options,
});
}
return factory;
},
addLocalComponent(name, source, loc = null as any, kind: ComponentRegistrationInfo['kind'] = 'script') {
component.components.push({ name, aliases: [name], kind, source, loc }); // Vue 3 — Component names are PascalCase.
return factory;
},
addOption(name, address) {
if (!name) {
component.options = {
...address,
properties: {},
};
} else {
if (!component.options) throw new Error('Cannot set option location without setting options');
component.options.properties[name] = address;
}
return factory;
},
addSetup(name, address) {
if (!name) {
component.setup = {
...address,
};
} else {
if (!component.setup) throw new Error('Cannot set setup params location without setting setup');
component.setup[name] = address;
}
return factory;
},
info() {
return component;
},
};
return factory;
}