Skip to content

Commit

Permalink
Fix #3071
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyo930021 committed Oct 6, 2021
1 parent 5d2d20b commit 0df3ad4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
32 changes: 22 additions & 10 deletions server/src/modes/script/componentInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
MethodInfo,
ChildComponent
} from '../../services/vueInfoService';
import { VueVersion } from '../../utils/vueVersion';
import { analyzeComponentsDefine } from './childComponents';
import { getGlobalComponents } from './globalComponents';

Expand All @@ -19,6 +20,7 @@ export function getComponentInfo(
service: ts.LanguageService,
fileFsPath: string,
globalComponentInfos: BasicComponentInfo[],
vueVersion: VueVersion,
config: any
): VueFileInfo | undefined {
const program = service.getProgram();
Expand All @@ -38,7 +40,7 @@ export function getComponentInfo(
return undefined;
}

const vueFileInfo = analyzeDefaultExportExpr(tsModule, defaultExportNode, checker);
const vueFileInfo = analyzeDefaultExportExpr(tsModule, defaultExportNode, checker, vueVersion);

const defaultExportType = checker.getTypeAtLocation(defaultExportNode);
const componentsDefineInfo = analyzeComponentsDefine(
Expand All @@ -57,7 +59,9 @@ export function getComponentInfo(
documentation: c.documentation,
definition: c.definition,
global: false,
info: c.defaultExportNode ? analyzeDefaultExportExpr(tsModule, c.defaultExportNode, checker) : undefined
info: c.defaultExportNode
? analyzeDefaultExportExpr(tsModule, c.defaultExportNode, checker, vueVersion)
: undefined
});
});
vueFileInfo.componentInfo.childComponents = childComponents;
Expand All @@ -78,7 +82,9 @@ export function getComponentInfo(
documentation: c.documentation,
definition: c.definition,
global: true,
info: c.defaultExportNode ? analyzeDefaultExportExpr(tsModule, c.defaultExportNode, checker) : undefined
info: c.defaultExportNode
? analyzeDefaultExportExpr(tsModule, c.defaultExportNode, checker, vueVersion)
: undefined
}))
];
}
Expand All @@ -89,13 +95,14 @@ export function getComponentInfo(
export function analyzeDefaultExportExpr(
tsModule: RuntimeLibrary['typescript'],
defaultExportNode: ts.Node,
checker: ts.TypeChecker
checker: ts.TypeChecker,
vueVersion: VueVersion
): VueFileInfo {
const defaultExportType = checker.getTypeAtLocation(defaultExportNode);

const insertInOptionAPIPos = getInsertInOptionAPIPos(tsModule, defaultExportType, checker);
const emits = getEmits(tsModule, defaultExportType, checker);
const props = getProps(tsModule, defaultExportType, checker);
const props = getProps(tsModule, defaultExportType, checker, vueVersion);
const data = getData(tsModule, defaultExportType, checker);
const computed = getComputed(tsModule, defaultExportType, checker);
const methods = getMethods(tsModule, defaultExportType, checker);
Expand Down Expand Up @@ -327,7 +334,8 @@ function getEmits(
function getProps(
tsModule: RuntimeLibrary['typescript'],
defaultExportType: ts.Type,
checker: ts.TypeChecker
checker: ts.TypeChecker,
vueVersion: VueVersion
): PropInfo[] | undefined {
const result: PropInfo[] = markPropBoundToModel(
defaultExportType,
Expand All @@ -337,9 +345,11 @@ function getProps(
return result.length === 0 ? undefined : result;

function markPropBoundToModel(type: ts.Type, props: PropInfo[]) {
const vModelPropName = vueVersion === VueVersion.V30 ? 'modelValue' : 'value';

function markValuePropBoundToModel() {
return props.map(prop => {
if (prop.name === 'value') {
if (prop.name === vModelPropName) {
prop.isBoundToModel = true;
}
return prop;
Expand Down Expand Up @@ -368,9 +378,11 @@ function getProps(
});
}

function getPropValidatorInfo(
propertyValue: ts.Node | undefined
): { hasObjectValidator: boolean; required: boolean; typeString?: string } {
function getPropValidatorInfo(propertyValue: ts.Node | undefined): {
hasObjectValidator: boolean;
required: boolean;
typeString?: string;
} {
if (!propertyValue) {
return { hasObjectValidator: false, required: true };
}
Expand Down
9 changes: 8 additions & 1 deletion server/src/modes/script/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,14 @@ export async function getJavascriptMode(

const { service } = updateCurrentVueTextDocument(doc);
const fileFsPath = getFileFsPath(doc.uri);
const info = getComponentInfo(tsModule, service, fileFsPath, globalComponentInfos, env.getConfig());
const info = getComponentInfo(
tsModule,
service,
fileFsPath,
globalComponentInfos,
env.getVueVersion(),
env.getConfig()
);
if (info) {
vueInfoService.updateInfo(doc, info);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<ts-child v-model="state.isZeroCount" :str="`count: ${state.count}`" :bool="state.isZeroCount" :callback="increment" :arr="['a', 'b']" />
<ts-child v-model="state.isZeroCount" :value="state.count" :str="`count: ${state.count}`" :bool="state.isZeroCount" :callback="increment" :arr="['a', 'b']" />
</template>

<script lang="ts">
Expand Down
3 changes: 2 additions & 1 deletion test/vue3/fixture/diagnostics/propValidation/TSChild.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export default defineComponent({
},
arr: Array as PropType<string[]>,
camelCase: { type: Boolean, default: false },
modelValue: { type: Boolean, required: true }
modelValue: { type: Boolean, required: true },
value: { type: Boolean, required: true }
}
});
</script>

0 comments on commit 0df3ad4

Please sign in to comment.