Skip to content

Commit 1ab8483

Browse files
authored
feat: allow string arguments on directives (#496)
* chore: update dependencies * feat: allow string arguments on directives resolves #325
1 parent 65ebf6f commit 1ab8483

File tree

10 files changed

+1209
-1176
lines changed

10 files changed

+1209
-1176
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
"jsx"
1717
],
1818
"devDependencies": {
19-
"@typescript-eslint/eslint-plugin": "^4.4.1",
20-
"eslint": "^7.7.0",
19+
"@typescript-eslint/eslint-plugin": "^4.30.0",
20+
"eslint": "^7.32.0",
2121
"eslint-config-airbnb-typescript": "^12.3.1",
22-
"eslint-plugin-import": "^2.22.1",
22+
"eslint-plugin-import": "^2.24.2",
2323
"lerna": "^3.19.0"
2424
}
2525
}

packages/babel-plugin-jsx/package.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
},
1313
"scripts": {
1414
"build": "rm -rf dist && tsc",
15+
"watch": "rm -rf dist && tsc --watch",
1516
"lint": "eslint 'src/*.ts'",
1617
"test": "yarn build && jest --coverage",
1718
"prepublishOnly": "yarn build"
@@ -34,18 +35,18 @@
3435
"svg-tags": "^1.0.0"
3536
},
3637
"devDependencies": {
37-
"@babel/core": "^7.0.0",
38-
"@babel/preset-env": "^7.0.0",
39-
"@types/jest": "^26.0.7",
38+
"@babel/core": "^7.15.5",
39+
"@babel/preset-env": "^7.15.4",
40+
"@types/jest": "^26.0.24",
4041
"@types/svg-tags": "^1.0.0",
41-
"@typescript-eslint/eslint-plugin": "^4.0.1",
42-
"@typescript-eslint/parser": "^4.0.1",
43-
"@vue/compiler-dom": "3.0.5",
42+
"@typescript-eslint/eslint-plugin": "^4.30.0",
43+
"@typescript-eslint/parser": "^4.30.0",
44+
"@vue/compiler-dom": "3.2.8",
4445
"@vue/test-utils": "2.0.0-beta.2",
4546
"jest": "^26.0.1",
46-
"regenerator-runtime": "^0.13.5",
47+
"regenerator-runtime": "^0.13.9",
4748
"ts-jest": "^26.1.3",
48-
"typescript": "^4.2.3",
49-
"vue": "3.0.7"
49+
"typescript": "^4.4.2",
50+
"vue": "3.2.8"
5051
}
5152
}

packages/babel-plugin-jsx/src/parseDirectives.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,33 @@ const parseDirectives = (params: {
4040
isComponent: boolean
4141
}) => {
4242
const {
43-
name, path, value, state, tag, isComponent,
43+
path, value, state, tag, isComponent,
4444
} = params;
4545
const args: Array<t.Expression | t.NullLiteral> = [];
4646
const vals: t.Expression[] = [];
4747
const modifiersSet: Set<string>[] = [];
48-
const underscoreModifiers = name.split('_');
49-
const directiveName: string = underscoreModifiers.shift()
50-
?.replace(/^v/, '')
48+
49+
let directiveName;
50+
let directiveArgument;
51+
let directiveModifiers;
52+
if ('namespace' in path.node.name) {
53+
[directiveName, directiveArgument] = params.name.split(':');
54+
directiveName = path.node.name.namespace.name;
55+
directiveArgument = path.node.name.name.name;
56+
directiveModifiers = directiveArgument.split('_').slice(1);
57+
} else {
58+
const underscoreModifiers = params.name.split('_');
59+
directiveName = underscoreModifiers.shift() || '';
60+
directiveModifiers = underscoreModifiers;
61+
}
62+
directiveName = directiveName
63+
.replace(/^v/, '')
5164
.replace(/^-/, '')
52-
.replace(/^\S/, (s: string) => s.toLowerCase()) || '';
65+
.replace(/^\S/, (s: string) => s.toLowerCase());
66+
67+
if (directiveArgument) {
68+
args.push(t.stringLiteral(directiveArgument));
69+
}
5370

5471
const isVModels = directiveName === 'models';
5572
const isVModel = directiveName === 'model';
@@ -64,7 +81,7 @@ const parseDirectives = (params: {
6481
const shouldResolve = !['html', 'text', 'model', 'models'].includes(directiveName)
6582
|| (isVModel && !isComponent);
6683

67-
let modifiers = underscoreModifiers;
84+
let modifiers = directiveModifiers;
6885

6986
if (t.isArrayExpression(value)) {
7087
const elementsList = isVModels ? value.elements! : [value];
@@ -95,9 +112,9 @@ const parseDirectives = (params: {
95112
} else if (isVModel && !shouldResolve) {
96113
// work as v-model={value}
97114
args.push(t.nullLiteral());
98-
modifiersSet.push(new Set(underscoreModifiers));
115+
modifiersSet.push(new Set(directiveModifiers));
99116
} else {
100-
modifiersSet.push(new Set(underscoreModifiers));
117+
modifiersSet.push(new Set(directiveModifiers));
101118
}
102119

103120
return {

packages/babel-plugin-jsx/test/index.test.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const patchFlagExpect = (
1313
flag: number,
1414
dynamic: string[] | null,
1515
) => {
16-
const { patchFlag, dynamicProps } = wrapper.vm.$.subTree;
16+
const { patchFlag, dynamicProps } = wrapper.vm.$.subTree as any;
1717

1818
expect(patchFlag).toBe(flag);
1919
expect(dynamicProps).toEqual(dynamic);
@@ -260,7 +260,8 @@ describe('directive', () => {
260260
test('vHtml', () => {
261261
const wrapper = shallowMount({
262262
setup() {
263-
return () => <h1 v-html="<div>foo</div>"></h1>;
263+
const html = '<div>foo</div>';
264+
return () => <h1 v-html={ html }></h1>;
264265
},
265266
});
266267
expect(wrapper.html()).toBe('<h1><div>foo</div></h1>');

packages/babel-plugin-jsx/test/v-model.test.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,39 @@ test('underscore modifier should work in custom component', async () => {
230230
await wrapper.trigger('click');
231231
expect(wrapper.html()).toBe('<div>6</div>');
232232
});
233+
234+
test('Named model', async () => {
235+
const Child = defineComponent({
236+
emits: ['update:value'],
237+
props: {
238+
value: {
239+
type: Number,
240+
default: 0,
241+
},
242+
},
243+
setup(props, { emit }) {
244+
const handleClick = () => {
245+
emit('update:value', 2);
246+
};
247+
return () => (
248+
<div onClick={ handleClick }>{ props.value }</div>
249+
);
250+
},
251+
});
252+
253+
const wrapper = mount({
254+
data: () => ({
255+
foo: 0,
256+
}),
257+
render() {
258+
return <Child v-model:value={ this.foo } />;
259+
},
260+
});
261+
262+
expect(wrapper.html()).toBe('<div>0</div>');
263+
wrapper.vm.$data.foo += 1;
264+
await wrapper.vm.$nextTick();
265+
expect(wrapper.html()).toBe('<div>1</div>');
266+
await wrapper.trigger('click');
267+
expect(wrapper.html()).toBe('<div>2</div>');
268+
});

packages/jsx-explorer/package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
"version": "1.0.4",
44
"private": true,
55
"dependencies": {
6-
"monaco-editor": "^0.20.0"
6+
"monaco-editor": "^0.27.0"
77
},
88
"devDependencies": {
9-
"@babel/core": "^7.0.0",
9+
"@babel/core": "^7.15.5",
1010
"css-loader": "^3.5.3",
11-
"html-webpack-plugin": "^4.3.0",
11+
"html-webpack-plugin": "^4.5.2",
1212
"style-loader": "^1.2.1",
13-
"ts-loader": "^8.0.0",
14-
"typescript": "^4.2.3",
15-
"vue": "3.0.7",
16-
"webpack": "^4.43.0",
17-
"webpack-dev-server": "^3.11.0"
13+
"ts-loader": "^8.3.0",
14+
"typescript": "^4.4.2",
15+
"vue": "3.2.8",
16+
"webpack": "^4.46.0",
17+
"webpack-dev-server": "^3.11.2"
1818
}
1919
}

scripts/dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const devServerOptions = {
88
inline: true,
99
open: true,
1010
hot: true,
11-
overlay: true,
11+
overlay: false,
1212
};
1313

1414
const server = new WebpackDevServer(compiler, devServerOptions);

scripts/webpack.base.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = {
1616
loader: 'ts-loader',
1717
exclude: /node_modules/,
1818
options: {
19+
transpileOnly: true,
1920
compilerOptions: { downlevelIteration: true },
2021
},
2122
},

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"sourceMap": false,
3+
"sourceMap": true,
44
"target": "es2015",
55
"module": "commonjs",
66
"moduleResolution": "node",
@@ -11,6 +11,7 @@
1111
"resolveJsonModule": true,
1212
"esModuleInterop": true,
1313
"removeComments": false,
14+
"jsx": "preserve",
1415
"lib": [
1516
"esnext",
1617
"dom"

0 commit comments

Comments
 (0)