Skip to content

Commit 0f93cb8

Browse files
committed
refactor: ComponentInternalProperties -> getGlobalPropNames
1 parent 817f1dc commit 0f93cb8

File tree

2 files changed

+55
-28
lines changed

2 files changed

+55
-28
lines changed

packages/vue-component-meta/src/index.ts

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,6 @@ export type PropertyMetaSchema = string
1717
| { kind: 'event', type: string, schema: PropertyMetaSchema[]; }
1818
| { kind: 'object', type: string, schema: Record<string, PropertyMeta>; };
1919

20-
/**
21-
* Helper array to map internal properties added by vue to any components
22-
*
23-
* @example
24-
* ```ts
25-
* import { createComponentMetaChecker, ComponentInternalProperties } from 'vue-component-meta'
26-
*
27-
* const checker = createComponentMetaChecker('path/to/tsconfig.json')
28-
* const meta = checker.getComponentMeta('path/to/component.vue')
29-
* const props = meta.props.filter(prop => !ComponentInternalProperties.includes(prop.name))
30-
* ```
31-
*/
32-
export const ComponentInternalProperties = [
33-
'ref',
34-
'key',
35-
'ref_for',
36-
'ref_key',
37-
'onVnodeBeforeMount',
38-
'onVnodeMounted',
39-
'onVnodeBeforeUpdate',
40-
'onVnodeBeforeUnmount',
41-
'onVnodeUpdated',
42-
'onVnodeUnmounted',
43-
'class',
44-
'style',
45-
];
46-
4720
function createSchemaResolvers(typeChecker: ts.TypeChecker, symbolNode: ts.Expression) {
4821
function reducer(acc: any, cur: any) {
4922
acc[cur.name] = cur;
@@ -128,6 +101,7 @@ export function createComponentMetaChecker(tsconfigPath: string) {
128101
readFile: ts.sys.readFile,
129102
}, tsconfigPath);
130103
const scriptSnapshot: Record<string, ts.IScriptSnapshot> = {};
104+
const globalComponentName = tsconfigPath.replace(/\\/g, '/') + '.global.ts';
131105
const core = vue.createLanguageContext({
132106
...ts.sys,
133107
getDefaultLibFileName: (options) => ts.getDefaultLibFilePath(options), // should use ts.getDefaultLibFilePath not ts.getDefaultLibFileName
@@ -137,13 +111,27 @@ export function createComponentMetaChecker(tsconfigPath: string) {
137111
return [
138112
...parsedCommandLine.fileNames,
139113
...parsedCommandLine.fileNames.map(getMetaFileName),
114+
globalComponentName,
115+
getMetaFileName(globalComponentName),
140116
];
141117
},
142118
getProjectReferences: () => parsedCommandLine.projectReferences,
143119
getScriptVersion: (fileName) => '0',
144120
getScriptSnapshot: (fileName) => {
145121
if (!scriptSnapshot[fileName]) {
146-
const fileText = fileName.endsWith('.meta.ts') ? getMetaScriptContent(fileName) : ts.sys.readFile(fileName);
122+
let fileText: string | undefined;
123+
if (fileName.endsWith('.meta.ts')) {
124+
fileText = getMetaScriptContent(fileName);
125+
}
126+
else if (fileName === globalComponentName) {
127+
fileText = `
128+
import { defineComponent } from 'vue';
129+
export default defineComponent({});
130+
`;
131+
}
132+
else {
133+
fileText = ts.sys.readFile(fileName);
134+
}
147135
if (fileText !== undefined) {
148136
scriptSnapshot[fileName] = ts.ScriptSnapshot.fromString(fileText);
149137
}
@@ -158,10 +146,29 @@ export function createComponentMetaChecker(tsconfigPath: string) {
158146
const typeChecker = program.getTypeChecker();
159147

160148
return {
149+
getGlobalPropNames,
161150
getExportNames,
162151
getComponentMeta,
163152
};
164153

154+
/**
155+
* Get helper array to map internal properties added by vue to any components
156+
*
157+
* @example
158+
* ```ts
159+
* import { createComponentMetaChecker } from 'vue-component-meta'
160+
*
161+
* const checker = createComponentMetaChecker('path/to/tsconfig.json')
162+
* const meta = checker.getComponentMeta('path/to/component.vue')
163+
* const globalPropNames = checker.getGlobalPropNames();
164+
* const props = meta.props.filter(prop => !globalPropNames.includes(prop.name))
165+
* ```
166+
*/
167+
function getGlobalPropNames() {
168+
const meta = getComponentMeta(globalComponentName);
169+
return meta.props.map(prop => prop.name);
170+
}
171+
165172
function getMetaFileName(fileName: string) {
166173
return (fileName.endsWith('.vue') ? fileName : fileName.substring(0, fileName.lastIndexOf('.'))) + '.meta.ts';
167174
}

packages/vue-component-meta/tests/index.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ describe(`vue-component-meta`, () => {
77
const tsconfigPath = path.resolve(__dirname, '../../vue-test-workspace/vue-component-meta/tsconfig.json');
88
const checker = metaChecker.createComponentMetaChecker(tsconfigPath);
99

10+
test('global-props', () => {
11+
12+
const globalProps = checker.getGlobalPropNames();
13+
14+
expect(globalProps).toEqual([
15+
'key',
16+
'ref',
17+
'ref_for',
18+
'ref_key',
19+
'onVnodeBeforeMount',
20+
'onVnodeMounted',
21+
'onVnodeBeforeUpdate',
22+
'onVnodeUpdated',
23+
'onVnodeBeforeUnmount',
24+
'onVnodeUnmounted',
25+
'class',
26+
'style',
27+
]);
28+
});
29+
1030
test('reference-type-props', () => {
1131

1232
const componentPath = path.resolve(__dirname, '../../vue-test-workspace/vue-component-meta/reference-type-props/component.vue');

0 commit comments

Comments
 (0)