Skip to content

Commit 9f2572a

Browse files
committed
chore: add lint checker
1 parent a7477e7 commit 9f2572a

15 files changed

+77
-67
lines changed

.github/workflows/ci.yml

+24-21
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,33 @@ jobs:
2828
- name: Lint
2929
run: nr lint
3030

31-
# test:
32-
# runs-on: ${{ matrix.os }}
31+
test:
32+
runs-on: ${{ matrix.os }}
3333

34-
# strategy:
35-
# matrix:
36-
# node: [16.x, 18.x]
37-
# os: [ubuntu-latest, windows-latest, macos-latest]
38-
# fail-fast: false
34+
strategy:
35+
matrix:
36+
node: [16.x, 18.x]
37+
os: [ubuntu-latest, windows-latest, macos-latest]
38+
fail-fast: false
3939

40-
# steps:
41-
# - uses: actions/checkout@v3
42-
# - name: Set node ${{ matrix.node }}
43-
# uses: actions/setup-node@v3
44-
# with:
45-
# node-version: ${{ matrix.node }}
40+
steps:
41+
- uses: actions/checkout@v3
42+
- name: Set node ${{ matrix.node }}
43+
uses: actions/setup-node@v3
44+
with:
45+
node-version: ${{ matrix.node }}
4646

47-
# - name: Setup
48-
# run: npm i -g @antfu/ni
47+
- name: Setup
48+
run: npm i -g @antfu/ni
49+
50+
- name: Install
51+
run: nci
4952

50-
# - name: Install
51-
# run: nci
53+
- name: Build
54+
run: nr build
5255

53-
# - name: Build
54-
# run: nr build
56+
- name: Type Check
57+
run: nr typecheck
5558

56-
# - name: Test
57-
# run: nr test
59+
- name: Test
60+
run: nr test
File renamed without changes.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"build": "tsup",
8787
"dev": "tsup --watch src",
8888
"build:fix": "esno scripts/postbuild.ts",
89+
"typecheck": "tsc --noEmit",
8990
"lint": "eslint .",
9091
"play": "npm -C playground run dev",
9192
"prepublishOnly": "npm run build",

playground/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"module": "esnext",
99
"moduleResolution": "node",
1010
"resolveJsonModule": true,
11+
"types": ["vite/client"],
1112
"strict": true,
1213
"strictNullChecks": true,
1314
"esModuleInterop": true

src/astro.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import type { Options } from './types'
2-
31
import unplugin from '.'
2+
import type { Options } from './types'
43

54
export default (options: Options) => ({
65
name: 'unplugin-vue-jsx-vapor',
76
hooks: {
8-
'astro:config:setup': async (astro: any) => {
7+
'astro:config:setup': (astro: any) => {
98
astro.config.vite.plugins ||= []
109
astro.config.vite.plugins.push(unplugin.vite(options))
1110
},

src/core/compiler/compile.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import {
77
import { extend, isString } from '@vue/shared'
88
import {
99
type VaporCodegenResult as BaseVaporCodegenResult,
10-
type HackOptions,
1110
generate,
1211
} from '@vue-vapor/compiler-vapor'
13-
import { type Overwrite, babelParse } from '@vue-macros/common'
12+
import { babelParse } from '@vue-macros/common'
1413
import {
1514
type DirectiveTransform,
1615
type NodeTransform,
@@ -19,7 +18,12 @@ import {
1918

2019
import { transformElement } from './transforms/transformElement'
2120
import { transformChildren } from './transforms/transformChildren'
22-
import { IRNodeTypes, type RootIRNode, type RootNode } from './ir'
21+
import {
22+
type HackOptions,
23+
IRNodeTypes,
24+
type RootIRNode,
25+
type RootNode,
26+
} from './ir'
2327
import { transformText } from './transforms/transformText'
2428
import type { JSXElement, JSXFragment, Program } from '@babel/types'
2529

@@ -108,12 +112,7 @@ export function compile(
108112
return generate(ir as any, resolvedOptions) as unknown as VaporCodegenResult
109113
}
110114

111-
export type CompilerOptions = Overwrite<
112-
HackOptions<BaseCompilerOptions>,
113-
{
114-
nodeTransforms?: NodeTransform[]
115-
}
116-
>
115+
export type CompilerOptions = HackOptions<BaseCompilerOptions>
117116
export type TransformPreset = [
118117
NodeTransform[],
119118
Record<string, DirectiveTransform>,

src/core/compiler/ir/component.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import type { SimpleExpressionNode } from '@vue/compiler-dom'
22
import type { IRFor } from '@vue-vapor/compiler-vapor'
3-
import type { JSXAttribute } from '@babel/types'
43
import type { DirectiveTransformResult } from '../transform'
54
import type { BlockIRNode } from './index'
65

76
// props
87
export interface IRProp extends Omit<DirectiveTransformResult, 'value'> {
9-
values: JSXAttribute['value'][]
8+
values: SimpleExpressionNode[]
109
}
1110

1211
export enum IRDynamicPropsKind {

src/core/compiler/transform.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
import {
2+
type TransformOptions as BaseTransformOptions,
23
type CommentNode,
34
type CompilerCompatOptions,
45
type SimpleExpressionNode,
56
defaultOnError,
67
defaultOnWarn,
78
} from '@vue/compiler-dom'
89
import { EMPTY_OBJ, NOOP, extend, isArray } from '@vue/shared'
10+
import { newBlock, newDynamic } from './transforms/utils'
11+
import { isConstantExpression } from './utils'
912
import {
13+
type BlockIRNode,
1014
DynamicFlag,
15+
type HackOptions,
1116
type IRDynamicInfo,
1217
IRNodeTypes,
1318
type IRSlots,
1419
type OperationNode,
20+
type RootIRNode,
21+
type RootNode,
1522
type VaporDirectiveNode,
16-
} from '@vue-vapor/compiler-vapor'
17-
import { newBlock, newDynamic } from './transforms/utils'
18-
import { isConstantExpression } from './utils'
19-
import type { CompilerOptions } from './compile'
20-
import type { JSXAttribute, JSXElement } from '@babel/types'
21-
import type { BlockIRNode, RootIRNode, RootNode } from './ir/index'
23+
} from './ir/index'
24+
import type { JSXElement, JSXFragment } from '@babel/types'
2225

2326
export type NodeTransform = (
2427
node: BlockIRNode['node'],
@@ -32,17 +35,16 @@ export type DirectiveTransform = (
3235
) => DirectiveTransformResult | void
3336

3437
export interface DirectiveTransformResult {
35-
key: JSXAttribute['name']
36-
value: JSXAttribute['value']
38+
key: SimpleExpressionNode
39+
value: SimpleExpressionNode
3740
modifier?: '.' | '^'
3841
runtimeCamelize?: boolean
3942
handler?: boolean
4043
model?: boolean
4144
modelModifiers?: string[]
4245
}
4346

44-
export type TransformOptions = CompilerOptions
45-
47+
export type TransformOptions = HackOptions<BaseTransformOptions>
4648
const defaultOptions = {
4749
filename: '',
4850
prefixIdentifiers: false,
@@ -70,7 +72,7 @@ const defaultOptions = {
7072
export class TransformContext<
7173
T extends BlockIRNode['node'] = BlockIRNode['node'],
7274
> {
73-
parent: TransformContext<RootNode> | null = null
75+
parent: TransformContext<RootNode | JSXElement | JSXFragment> | null = null
7476
root: TransformContext<RootNode>
7577
index: number = 0
7678

@@ -100,7 +102,7 @@ export class TransformContext<
100102
options: TransformOptions = {},
101103
) {
102104
this.options = extend({}, defaultOptions, options)
103-
this.root = this as TransformContext<T>
105+
this.root = this as TransformContext<RootNode>
104106
}
105107

106108
enterBlock(ir: BlockIRNode, isVFor: boolean = false): () => void {

src/core/compiler/transforms/transformElement.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import {
1616
type IRPropsDynamicAttribute,
1717
type IRPropsStatic,
1818
} from '../ir'
19-
import { isComponent as _isComponent } from '../utils'
19+
import { isComponent as _isComponent, resolveSimpleExpression } from '../utils'
20+
import { EMPTY_EXPRESSION } from './utils'
2021
import type { SimpleExpressionNode } from '@vue/compiler-dom'
2122
import type { JSXAttribute, JSXElement, JSXSpreadAttribute } from '@babel/types'
2223
import type {
@@ -61,7 +62,7 @@ export const transformElement: NodeTransform = (node, context) => {
6162
function transformComponentElement(
6263
tag: string,
6364
propsResult: PropsResult,
64-
context: TransformContext,
65+
context: TransformContext<JSXElement>,
6566
) {
6667
let asset = true
6768

@@ -104,7 +105,7 @@ function transformComponentElement(
104105
function resolveSetupReference(name: string, context: TransformContext) {
105106
const bindings = context.options.bindingMetadata
106107
// TODO
107-
if (bindings) return name
108+
if (!context.options.prefixIdentifiers) return name
108109
if (!bindings || bindings.__isScriptSetup === false) {
109110
return
110111
}
@@ -164,6 +165,7 @@ function transformNativeElement(
164165
if (
165166
context.parent &&
166167
context.parent.node.type === 'JSXElement' &&
168+
context.parent.node.openingElement.name.type === 'JSXIdentifier' &&
167169
!isValidHTMLNesting(context.parent.node.openingElement.name.name, tag)
168170
) {
169171
context.reference()
@@ -234,13 +236,17 @@ function transformProp(
234236
): DirectiveTransformResult | void {
235237
if (prop.type === 'JSXSpreadAttribute') return
236238

237-
let { name, value } = prop
239+
let name = prop.name.type === 'JSXIdentifier' ? prop.name.name : ''
240+
const value = prop.value?.type === 'StringLiteral' ? prop.value.value : ''
238241

239242
if (prop.type === 'JSXAttribute') {
240-
if (isReservedProp(name.name)) return
243+
if (isReservedProp(name)) return
241244
return {
242-
key: name,
243-
value: prop.value ? value : null,
245+
key: resolveSimpleExpression(name, true, prop.name.loc!),
246+
value:
247+
prop.value && prop.value.type === 'StringLiteral'
248+
? resolveSimpleExpression(value, true, prop.value.loc!)
249+
: EMPTY_EXPRESSION,
244250
}
245251
}
246252

src/core/transform.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type RootNodes = {
1717
export function transformVueJsxVapor(
1818
code: string,
1919
id: string,
20-
options?: Options,
20+
_options?: Options,
2121
) {
2222
const s = new MagicStringAST(code)
2323
const rootNodes: RootNodes = []
@@ -43,7 +43,6 @@ export function transformVueJsxVapor(
4343
mode: 'module',
4444
prefixIdentifiers: false,
4545
inline: true,
46-
source: s.sliceNode(node),
4746
})
4847
vaporHelpers.forEach((i) => importSet.add(`${i} as _${i}`))
4948
preamble = preamble.replace(/^[^\n]*;\n?/, '')

src/nuxt.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default defineNuxtModule<ModuleOptions>({
1111
name: 'nuxt-unplugin-vue-jsx-vapor',
1212
configKey: 'unpluginStarter',
1313
},
14-
setup(options, _nuxt) {
14+
setup(options) {
1515
addVitePlugin(() => vite(options))
1616
addWebpackPlugin(() => webpack(options))
1717

test/transforms/transformChildren.spec.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { describe, expect, test } from 'vitest'
22

33
import {
4-
compile,
54
transformChildren,
65
transformElement,
76
transformText,
@@ -21,17 +20,17 @@ const compileWithElementTransform = makeCompile({
2120
describe('compiler: children transform', () => {
2221
test.todo('basic')
2322

24-
test('children & sibling references', () => {
23+
test.only('children & sibling references', () => {
2524
const { code, vaporHelpers } = compileWithElementTransform(
26-
`<div>
25+
`<div id>
2726
<p>{ first }</p>
2827
{ second }
2928
<p>{ forth }</p>
3029
</div>`,
3130
)
3231
expect(code).toMatchInlineSnapshot(`
3332
"import { next as _next, createTextNode as _createTextNode, insert as _insert, renderEffect as _renderEffect, setText as _setText, template as _template } from 'vue/vapor';
34-
const t0 = _template("<div><p></p> <!><p></p></div>")
33+
const t0 = _template("<div id><p></p> <!><p></p></div>")
3534
3635
export function render(_ctx) {
3736
const n4 = t0()

test/transforms/transformElement.spec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ describe('compiler: element transform', () => {
3030
test('import + resolve component', () => {
3131
const { code, vaporHelpers } = compileWithElementTransform(`<Foo/>`)
3232
expect(code).toMatchInlineSnapshot(`
33-
"import { createComponent as _createComponent } from 'vue/vapor';
33+
"import { resolveComponent as _resolveComponent, createComponent as _createComponent } from 'vue/vapor';
3434
3535
export function render(_ctx) {
36-
const n0 = _createComponent(_ctx.Foo, null, null, true)
36+
const _component_Foo = _resolveComponent("Foo")
37+
const n0 = _createComponent(_component_Foo, null, null, true)
3738
return n0
3839
}"
3940
`)

tsconfig.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"resolveJsonModule": true,
1111
"strict": true,
1212
"strictNullChecks": true,
13-
"esModuleInterop": true
13+
"esModuleInterop": true,
14+
"skipLibCheck": true
1415
},
15-
"exclude": ["dist", "eslint.config.js"]
16+
"exclude": ["dist", "eslint.config.js", "playground"]
1617
}

tsup.config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { Options } from 'tsup'
22

3-
export default <Options>{
3+
export default {
44
entryPoints: ['src/*.ts'],
55
clean: true,
66
format: ['cjs', 'esm'],
77
dts: true,
88
onSuccess: 'npm run build:fix',
9-
}
9+
} satisfies Options

0 commit comments

Comments
 (0)