Skip to content

Commit db1fb7f

Browse files
committed
refactor(macros): add global defineStyle type if not used
1 parent bea196d commit db1fb7f

File tree

5 files changed

+40
-31
lines changed

5 files changed

+40
-31
lines changed

packages/eslint/src/rules/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import defineStyle from './define-style'
1+
import _defineStyle from './define-style'
22
import jsxSortProps from './jsx-sort-props'
33
import type { DefineStyleRuleOptions } from './define-style/types'
44
import type { JsxSortPropsRuleOptions } from './jsx-sort-props/types'
55
import type { Linter } from 'eslint'
66

77
const ruleOptions = {
88
'jsx-sort-props': jsxSortProps,
9-
'define-style': defineStyle,
9+
'define-style': _defineStyle,
1010
}
1111

1212
export interface RuleOptions {

packages/eslint/test/define-style.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { createRuleTester } from 'eslint-vitest-rule-tester'
22
import { describe, expect, it } from 'vitest'
3-
import defineStyle from '../src/rules/define-style'
3+
import _defineStyle from '../src/rules/define-style'
44

55
describe('define-style', () => {
66
const { invalid } = createRuleTester({
77
name: 'define-style',
8-
rule: defineStyle,
8+
rule: _defineStyle,
99
})
1010

1111
it('basic', async () => {

packages/macros/src/volar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ const plugin: PluginReturn<Options | undefined> = createPlugin(
2323
const rootMap = getRootMap(options)
2424
if (rootMap.size) {
2525
transformJsxMacros(rootMap, options)
26-
codes.push(getGlobalTypes(options))
2726
}
27+
codes.push(getGlobalTypes(rootMap, options))
2828
},
2929
}
3030
},

packages/macros/src/volar/global-types.ts

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,52 @@
11
import { HELPER_PREFIX } from '@vue-macros/common'
2-
import type { TransformOptions } from '.'
2+
import type { RootMap, TransformOptions } from '.'
3+
4+
export function getGlobalTypes(
5+
rootMap: RootMap,
6+
options: TransformOptions,
7+
): string {
8+
let defineStyle = ''
9+
if (options.defineSlots.alias) {
10+
defineStyle = options.defineStyle.alias
11+
.map(
12+
(alias) =>
13+
`declare const ${alias}: { <T>(...args: ${HELPER_PREFIX}StyleArgs): T; scss: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T; sass: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T; stylus: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T; less: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T; postcss: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T };`,
14+
)
15+
.join('\n')
16+
defineStyle += `type ${HELPER_PREFIX}StyleArgs = [style: string, options?: { scoped?: boolean }];`
17+
}
18+
if (!rootMap.size) {
19+
return defineStyle
20+
}
321

4-
export function getGlobalTypes(options: TransformOptions): string {
522
const defineSlots = options.defineSlots.alias
623
.flatMap((alias) => [
724
`declare function ${alias}<T extends Record<string, any>>(): Partial<T>;`,
8-
`declare function ${alias}<T extends Record<string, any>>(slots: T): T;\n`,
25+
`declare function ${alias}<T extends Record<string, any>>(slots: T): T;`,
926
])
10-
.join('')
27+
.join('\n')
1128
const defineExpose = options.defineExpose.alias
1229
.map(
1330
(alias) =>
1431
`declare function ${alias}<Exposed extends Record<string, any> = Record<string, any>>(exposed?: Exposed): Exposed;`,
1532
)
16-
.join('')
17-
const defineStyle = options.defineStyle.alias
18-
.map(
19-
(alias) =>
20-
`declare const ${alias}: { <T>(...args: ${HELPER_PREFIX}StyleArgs): T; scss: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T; sass: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T; stylus: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T; less: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T; postcss: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T };\n`,
21-
)
22-
.join('')
23-
const defineModel = options.defineModel.alias
24-
.map((alias) =>
25-
alias === 'defineModel' ? 'defineModel' : `defineModel: ${alias}`,
26-
)
27-
.join(', ')
28-
const defineComponent = options.defineComponent.alias
29-
.map((alias) =>
30-
['defineComponent', 'defineVaporComponent'].includes(alias)
31-
? ''
32-
: `defineComponent: ${alias}`,
33-
)
33+
.join('\n')
34+
const defineModel = options.defineModel.alias.map((alias) =>
35+
alias === 'defineModel' ? 'defineModel' : `defineModel: ${alias}`,
36+
)
37+
const defineComponent = options.defineComponent.alias.map((alias) =>
38+
['defineComponent', 'defineVaporComponent'].includes(alias)
39+
? ''
40+
: `defineComponent: ${alias}`,
41+
)
42+
const VueMacros = [...defineModel, ...defineComponent]
3443
.filter(Boolean)
35-
.join(', ')
44+
.join(',')
3645
return `
37-
declare const { ${defineModel}, ${defineComponent} }: typeof import('vue')
46+
${VueMacros ? `declare const { ${VueMacros} }: typeof import('vue');` : ''}
3847
${defineSlots}
3948
${defineExpose}
4049
${defineStyle}
41-
type ${HELPER_PREFIX}StyleArgs = [style: string, options?: { scoped?: boolean }];
4250
// @ts-ignore
4351
type __VLS_IsAny<T> = 0 extends 1 & T ? true : false; type __VLS_PickNotAny<A, B> = __VLS_IsAny<A> extends true ? B : A;
4452
`

packages/macros/src/volar/transform.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export function transformJsxMacros(
1414
transformDefineStyle(defaultStyle, defineStyleIndex++, root, options),
1515
)
1616

17-
if (!root?.body) continue
17+
if (!root?.body || (Object.keys(macros).length === 1 && macros.defineStyle))
18+
continue
1819

1920
const asyncModifier = root.modifiers?.find(
2021
(modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword,

0 commit comments

Comments
 (0)