Skip to content

Commit

Permalink
chore(stubs): remove useless check
Browse files Browse the repository at this point in the history
  • Loading branch information
xanf committed Oct 7, 2022
1 parent 70f6737 commit 8b0177f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
73 changes: 73 additions & 0 deletions src/directives.ts
@@ -0,0 +1,73 @@
import {
transformVNodeArgs,
Directive,
ComponentInternalInstance,
VNodeTypes
} from 'vue'
import { isObject } from './utils'

interface CreateDirectivesTransformerConfig {
directives?: Record<string, Directive>
}
type VNodeArgsTransformer = NonNullable<
Parameters<typeof transformVNodeArgs>[0]
>

export function createDirectivesTransformer({
directives = {}
}: CreateDirectivesTransformerConfig): VNodeArgsTransformer {
const directivesTransformerCache: WeakMap<
VNodeTypes & object,
VNodeTypes & object
> = new WeakMap()

if (Object.keys(directives).length === 0) {
return (args) => args
}

return function directivesTransformer(
args,
instance: ComponentInternalInstance | null
) {
// We care about
// * object components
// * functional components
// * legacy components
// * legacy functional components
// * class components (sigh)

const [nodeType, props, children, patchFlag, dynamicProps] = args

if (!isObject(nodeType)) {
return args
}

if ((nodeType as any).__PATCHED) {
console.log('wtf')
}

const cachedTransformation = directivesTransformerCache.get(nodeType)
if (cachedTransformation) {
return [cachedTransformation, props, children, patchFlag, dynamicProps]
}

const nodeTypeWithDirectives = {
__PATCHED: true,
...nodeType,
directives: {
...((nodeType as any)?.directives ?? {}),
...directives
}
}

directivesTransformerCache.set(nodeType, nodeTypeWithDirectives as any)

return [
nodeTypeWithDirectives as any,
props,
children,
patchFlag,
dynamicProps
]
}
}
4 changes: 0 additions & 4 deletions src/vnodeTransformers/stubComponentsTransformer.ts
Expand Up @@ -231,10 +231,6 @@ export function createStubComponentsTransformer({
// Set name when using shallow without stub
const stubName = name || registeredName || componentName

if (!isComponent(type)) {
throw new Error('Attempted to stub a non-component')
}

const newStub =
config.plugins.createStubs?.({
name: stubName,
Expand Down

0 comments on commit 8b0177f

Please sign in to comment.