Skip to content

Commit

Permalink
refactor: use getScriptSnapshot instead of readFile when find prop de…
Browse files Browse the repository at this point in the history
…faults
  • Loading branch information
johnsoncodehk committed Jul 28, 2022
1 parent eac2dca commit 1468bc3
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions packages/vue-component-meta/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function createComponentMetaChecker(tsconfigPath: string) {
}, tsconfigPath);
const scriptSnapshot: Record<string, ts.IScriptSnapshot> = {};
const globalComponentName = tsconfigPath.replace(/\\/g, '/') + '.global.ts';
const core = vue.createLanguageContext({
const host: vue.LanguageServiceHost = {
...ts.sys,
getDefaultLibFileName: (options) => ts.getDefaultLibFilePath(options), // should use ts.getDefaultLibFilePath not ts.getDefaultLibFileName
useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,
Expand Down Expand Up @@ -140,7 +140,8 @@ export function createComponentMetaChecker(tsconfigPath: string) {
},
getTypeScriptModule: () => ts,
getVueCompilationSettings: () => parsedCommandLine.vueOptions,
});
};
const core = vue.createLanguageContext(host);
const tsLs = ts.createLanguageService(core.typescriptLanguageServiceHost);
const program = tsLs.getProgram()!;
const typeChecker = program.getTypeChecker();
Expand Down Expand Up @@ -218,7 +219,8 @@ export function createComponentMetaChecker(tsconfigPath: string) {

// fill defaults
if (componentPath.endsWith('.vue') && exportName === 'default') {
const defaults = findCmponentDefaultProps(componentPath);
const snapshot = host.getScriptSnapshot(componentPath)!;
const defaults = readCmponentDefaultProps(snapshot.getText(0, snapshot.getLength()));
for (const propName in defaults) {
const prop = result.find(p => p.name === propName);
if (prop) {
Expand Down Expand Up @@ -326,14 +328,9 @@ export function createComponentMetaChecker(tsconfigPath: string) {
}
}

export function findCmponentDefaultProps(componentPath: string) {

const fileText = ts.sys.readFile(componentPath);
if (fileText === undefined) {
throw new Error(`${componentPath} not found`);
}
function readCmponentDefaultProps(fileText: string) {

const vueSourceFile = vue.createSourceFile(componentPath, fileText, {}, {}, ts);
const vueSourceFile = vue.createSourceFile('/tmp.vue', fileText, {}, {}, ts);
const descriptor = vueSourceFile.getDescriptor();
const scriptSetupRanges = vueSourceFile.getScriptSetupRanges();
const result: Record<string, string> = {};
Expand Down

0 comments on commit 1468bc3

Please sign in to comment.