Skip to content

fix: resolve directives in nested scopes #759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions packages/babel-plugin-jsx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,24 +243,29 @@ h(A, {

#### custom directive

Recommended when using string arguments

```jsx
const App = {
directives: { custom: customDirective },
directives: { custom: vCustom },
setup() {
return () => <a v-custom:arg={val} />;
return () => <a v-custom={val} />;
},
};
```

Directive names will resolve a variable matching `/v[A-Z]/` first, `options.directives` is only needed to prevent the import from being reported as unused.

Arguments and modifiers can be added as an array:

```jsx
const App = {
directives: { custom: customDirective },
setup() {
return () => <a v-custom={[val, 'arg', ['a', 'b']]} />;
},
};
// same as v-custom:arg.a.b="val" in a .vue file
<a v-custom={[val, 'arg', ['a', 'b']]} />
```

Or arguments as part of the attribute name:

```jsx
<a v-custom:arg={val} />
<b v-custom:arg={[val, ['a', 'b']]} />
```

### Slot
Expand Down
15 changes: 9 additions & 6 deletions packages/babel-plugin-jsx/src/parseDirectives.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as t from '@babel/types';
import { type NodePath } from '@babel/traverse';
import { createIdentifier } from './utils';
import { camelize, capitalize, createIdentifier } from './utils';
import type { State } from './interface';

export type Tag =
Expand Down Expand Up @@ -184,11 +184,14 @@ const resolveDirective = (
}
return modelToUse;
}
const referenceName =
'v' + directiveName[0].toUpperCase() + directiveName.slice(1);
if (path.scope.references[referenceName]) {
return t.identifier(referenceName);
}
const referenceName = 'v' + capitalize(camelize(directiveName));
let scope = path.scope;
do {
if (scope.references[referenceName]) {
return t.identifier(referenceName);
}
scope = scope.parent;
} while (scope);
return t.callExpression(createIdentifier(state, 'resolveDirective'), [
t.stringLiteral(directiveName),
]);
Expand Down
8 changes: 8 additions & 0 deletions packages/babel-plugin-jsx/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ const onRE = /^on[^a-z]/;

export const isOn = (key: string) => onRE.test(key);

const camelizeRE = /-(\w)/g;
export const camelize = (str: string): string => {
return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''));
};
export const capitalize = <T extends string>(str: T): Capitalize<T> => {
return (str.charAt(0).toUpperCase() + str.slice(1)) as Capitalize<T>;
};

const mergeAsArray = (
existing: t.ObjectProperty,
incoming: t.ObjectProperty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ _createVNode(_Fragment, null, [_withDirectives(_createVNode(_resolveComponent("A
}]])]);"
`;

exports[`directive in outer scope > directive in outer scope 1`] = `
"import { resolveComponent as _resolveComponent, createVNode as _createVNode, withDirectives as _withDirectives } from "vue";
const vXxx = {};
() => _withDirectives(_createVNode(_resolveComponent("A"), null, null, 512), [[vXxx]]);"
`;

exports[`directive in scope > directive in scope 1`] = `
"import { resolveComponent as _resolveComponent, createVNode as _createVNode, withDirectives as _withDirectives } from "vue";
const vXxx = {};
Expand Down
9 changes: 9 additions & 0 deletions packages/babel-plugin-jsx/test/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ const transpile = (source: string, options: VueJSXPluginOptions = {}) =>
<A v-xxx />
`,
},
{
name: 'directive in outer scope',
from: `
const vXxx = {};
() => (
<A v-xxx />
);
`,
},
{
name: 'vModels',
from: '<C v-models={[[foo, ["modifier"]], [bar, "bar", ["modifier1", "modifier2"]]]} />',
Expand Down