Skip to content

Commit 555f497

Browse files
committed
feat(core): support ts check for native components attrs and refactor globalTypes code
1 parent 39e0972 commit 555f497

File tree

9 files changed

+92
-5
lines changed

9 files changed

+92
-5
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<template>
2+
<view>
3+
<image />
4+
<!-- ^ 报错提示:缺少必要的 src 属性 -->
5+
</view>
6+
</template>
File renamed without changes.
File renamed without changes.

packages/language-core/src/codegen/globalTypes.ts renamed to packages/language-core/src/codegen/globalTypes/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import type { MpxCompilerOptions } from '../types'
2-
import { getSlotsPropertyName } from '../utils/shared'
1+
import type { MpxCompilerOptions } from '../../types'
2+
import { getSlotsPropertyName } from '../../utils/shared'
33
import { defineComponentTypesContents } from './defineComponentTypes'
4+
import { nativeComponentsTypesFileName } from './nativeComponentsTypes'
5+
6+
export * from './nativeComponentsTypes'
47

58
export function getGlobalTypesFileName({
69
checkUnknownProps,
@@ -29,8 +32,8 @@ export function generateGlobalTypes({
2932
const __VLS_placeholder: any;
3033
3134
type __VLS_NativeElements = __VLS_SpreadMerge<SVGElementTagNameMap, HTMLElementTagNameMap>;
32-
type __VLS_IntrinsicElements = ${`import('${lib}/jsx-runtime').JSX.IntrinsicElements;`}
33-
type __VLS_Element = ${`import('${lib}/jsx-runtime').JSX.Element;`}
35+
type __VLS_NativeComponents = ${`import('./${nativeComponentsTypesFileName}').MpxNativeComponents;`}
36+
type __VLS_Element = ${`import('./${nativeComponentsTypesFileName}').MpxElement;`}
3437
type __VLS_GlobalComponents = ${`import('${lib}').GlobalComponents;`}
3538
type __VLS_GlobalDirectives = import('${lib}').GlobalDirectives;
3639
type __VLS_IsAny<T> = 0 extends 1 & T ? true : false;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export const nativeComponentsTypesFileName = 'mpx_native_components'
2+
3+
export const nativeComponentsTypesContents = `
4+
export interface MpxNativeComponents extends NativeComponents {
5+
[name: string]: any
6+
}
7+
8+
type NativeComponents = {
9+
[K in keyof NativeComponentAttrs]: NativeComponentAttrs[K] & ReservedProps
10+
}
11+
12+
interface ReservedProps {
13+
key?: PropertyKey
14+
ref?: any
15+
}
16+
17+
interface NativeComponentAttrs {
18+
img: MpxImg
19+
image: MpxImg
20+
// ..
21+
}
22+
23+
interface MpxImg {
24+
src: number
25+
}
26+
`
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* ! 注意: 类型定义更新后后需要手动同步更新到 `nativeComponents.ts` 文件才能最终生效
3+
*
4+
* Mpx 原生组件类型定义
5+
* - 组件属性必要性检查
6+
* - 组件属性类型检查
7+
*/
8+
9+
export interface MpxNativeComponents extends NativeComponents {
10+
[name: string]: any
11+
}
12+
13+
type NativeComponents = {
14+
[K in keyof NativeComponentAttrs]: NativeComponentAttrs[K] & ReservedProps
15+
}
16+
17+
interface ReservedProps {
18+
key?: PropertyKey
19+
ref?: any
20+
}
21+
22+
interface NativeComponentAttrs {
23+
img: MpxImg
24+
image: MpxImg
25+
// ..
26+
}
27+
28+
interface MpxImg {
29+
src: number
30+
}

packages/language-core/src/codegen/script/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@ export function* generateScript(
187187
return ctx
188188
}
189189

190+
/**
191+
* 用 debuggger 作为 script 部分的默认分隔符
192+
* 可以正常拦截非正常结束部分的报错
193+
* eg:
194+
* <script>
195+
* const a =
196+
* ^ Expression expected.ts-plugin(1109)
197+
* </script>
198+
*/
190199
export function* generateScriptSectionPartiallyEnding(
191200
source: string,
192201
end: number,

packages/language-core/src/codegen/script/template.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function* generateTemplateCtx(options: ScriptCodegenOptions): Generator<Code> {
5454
}
5555

5656
function* generateTemplateElements(): Generator<Code> {
57-
yield `let __VLS_elements!: __VLS_IntrinsicElements${endOfLine}`
57+
yield `let __VLS_elements!: __VLS_NativeComponents${endOfLine}`
5858
}
5959

6060
function* generateTemplateComponents(): Generator<Code> {

packages/language-core/src/utils/ts.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { hyphenateTag } from './shared'
1212
import {
1313
generateGlobalTypes,
1414
getGlobalTypesFileName,
15+
nativeComponentsTypesContents,
16+
nativeComponentsTypesFileName,
1517
} from '../codegen/globalTypes'
1618

1719
export function createParsedCommandLineByJson(
@@ -285,6 +287,7 @@ export function setupGlobalTypes(
285287
}
286288
try {
287289
let dir = rootDir
290+
288291
while (
289292
!host.fileExists(
290293
path.join(dir, 'node_modules', mpxOptions.lib, 'package.json'),
@@ -296,6 +299,7 @@ export function setupGlobalTypes(
296299
}
297300
dir = parentDir
298301
}
302+
299303
const globalTypesPath = path.join(
300304
dir,
301305
'node_modules',
@@ -305,6 +309,15 @@ export function setupGlobalTypes(
305309
const globalTypesContents =
306310
`// @ts-nocheck\n` + `export {};\n` + generateGlobalTypes(mpxOptions)
307311
host.writeFile(globalTypesPath, globalTypesContents)
312+
313+
const nativeComponentsTypesPath = path.join(
314+
dir,
315+
'node_modules',
316+
'.mpx-global-types',
317+
`${nativeComponentsTypesFileName}.d.ts`,
318+
)
319+
host.writeFile(nativeComponentsTypesPath, nativeComponentsTypesContents)
320+
308321
return { absolutePath: globalTypesPath }
309322
} catch {
310323
// noop

0 commit comments

Comments
 (0)