Skip to content

feat: allow string arguments on directives #496

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

Merged
merged 2 commits into from
Sep 22, 2021
Merged
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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
"jsx"
],
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.4.1",
"eslint": "^7.7.0",
"@typescript-eslint/eslint-plugin": "^4.30.0",
"eslint": "^7.32.0",
"eslint-config-airbnb-typescript": "^12.3.1",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-import": "^2.24.2",
"lerna": "^3.19.0"
}
}
19 changes: 10 additions & 9 deletions packages/babel-plugin-jsx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"scripts": {
"build": "rm -rf dist && tsc",
"watch": "rm -rf dist && tsc --watch",
"lint": "eslint 'src/*.ts'",
"test": "yarn build && jest --coverage",
"prepublishOnly": "yarn build"
Expand All @@ -34,18 +35,18 @@
"svg-tags": "^1.0.0"
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@types/jest": "^26.0.7",
"@babel/core": "^7.15.5",
"@babel/preset-env": "^7.15.4",
"@types/jest": "^26.0.24",
"@types/svg-tags": "^1.0.0",
"@typescript-eslint/eslint-plugin": "^4.0.1",
"@typescript-eslint/parser": "^4.0.1",
"@vue/compiler-dom": "3.0.5",
"@typescript-eslint/eslint-plugin": "^4.30.0",
"@typescript-eslint/parser": "^4.30.0",
"@vue/compiler-dom": "3.2.8",
"@vue/test-utils": "2.0.0-beta.2",
"jest": "^26.0.1",
"regenerator-runtime": "^0.13.5",
"regenerator-runtime": "^0.13.9",
"ts-jest": "^26.1.3",
"typescript": "^4.2.3",
"vue": "3.0.7"
"typescript": "^4.4.2",
"vue": "3.2.8"
}
}
33 changes: 25 additions & 8 deletions packages/babel-plugin-jsx/src/parseDirectives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,33 @@ const parseDirectives = (params: {
isComponent: boolean
}) => {
const {
name, path, value, state, tag, isComponent,
path, value, state, tag, isComponent,
} = params;
const args: Array<t.Expression | t.NullLiteral> = [];
const vals: t.Expression[] = [];
const modifiersSet: Set<string>[] = [];
const underscoreModifiers = name.split('_');
const directiveName: string = underscoreModifiers.shift()
?.replace(/^v/, '')

let directiveName;
let directiveArgument;
let directiveModifiers;
if ('namespace' in path.node.name) {
[directiveName, directiveArgument] = params.name.split(':');
directiveName = path.node.name.namespace.name;
directiveArgument = path.node.name.name.name;
directiveModifiers = directiveArgument.split('_').slice(1);
} else {
const underscoreModifiers = params.name.split('_');
directiveName = underscoreModifiers.shift() || '';
directiveModifiers = underscoreModifiers;
}
directiveName = directiveName
.replace(/^v/, '')
.replace(/^-/, '')
.replace(/^\S/, (s: string) => s.toLowerCase()) || '';
.replace(/^\S/, (s: string) => s.toLowerCase());

if (directiveArgument) {
args.push(t.stringLiteral(directiveArgument));
}

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

let modifiers = underscoreModifiers;
let modifiers = directiveModifiers;

if (t.isArrayExpression(value)) {
const elementsList = isVModels ? value.elements! : [value];
Expand Down Expand Up @@ -95,9 +112,9 @@ const parseDirectives = (params: {
} else if (isVModel && !shouldResolve) {
// work as v-model={value}
args.push(t.nullLiteral());
modifiersSet.push(new Set(underscoreModifiers));
modifiersSet.push(new Set(directiveModifiers));
} else {
modifiersSet.push(new Set(underscoreModifiers));
modifiersSet.push(new Set(directiveModifiers));
}

return {
Expand Down
5 changes: 3 additions & 2 deletions packages/babel-plugin-jsx/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const patchFlagExpect = (
flag: number,
dynamic: string[] | null,
) => {
const { patchFlag, dynamicProps } = wrapper.vm.$.subTree;
const { patchFlag, dynamicProps } = wrapper.vm.$.subTree as any;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was made internal in v3.2.0: vuejs/core@8610e1c


expect(patchFlag).toBe(flag);
expect(dynamicProps).toEqual(dynamic);
Expand Down Expand Up @@ -260,7 +260,8 @@ describe('directive', () => {
test('vHtml', () => {
const wrapper = shallowMount({
setup() {
return () => <h1 v-html="<div>foo</div>"></h1>;
const html = '<div>foo</div>';
return () => <h1 v-html={ html }></h1>;
},
});
expect(wrapper.html()).toBe('<h1><div>foo</div></h1>');
Expand Down
36 changes: 36 additions & 0 deletions packages/babel-plugin-jsx/test/v-model.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,39 @@ test('underscore modifier should work in custom component', async () => {
await wrapper.trigger('click');
expect(wrapper.html()).toBe('<div>6</div>');
});

test('Named model', async () => {
const Child = defineComponent({
emits: ['update:value'],
props: {
value: {
type: Number,
default: 0,
},
},
setup(props, { emit }) {
const handleClick = () => {
emit('update:value', 2);
};
return () => (
<div onClick={ handleClick }>{ props.value }</div>
);
},
});

const wrapper = mount({
data: () => ({
foo: 0,
}),
render() {
return <Child v-model:value={ this.foo } />;
},
});

expect(wrapper.html()).toBe('<div>0</div>');
wrapper.vm.$data.foo += 1;
await wrapper.vm.$nextTick();
expect(wrapper.html()).toBe('<div>1</div>');
await wrapper.trigger('click');
expect(wrapper.html()).toBe('<div>2</div>');
});
16 changes: 8 additions & 8 deletions packages/jsx-explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
"version": "1.0.4",
"private": true,
"dependencies": {
"monaco-editor": "^0.20.0"
"monaco-editor": "^0.27.0"
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/core": "^7.15.5",
"css-loader": "^3.5.3",
"html-webpack-plugin": "^4.3.0",
"html-webpack-plugin": "^4.5.2",
"style-loader": "^1.2.1",
"ts-loader": "^8.0.0",
"typescript": "^4.2.3",
"vue": "3.0.7",
"webpack": "^4.43.0",
"webpack-dev-server": "^3.11.0"
"ts-loader": "^8.3.0",
"typescript": "^4.4.2",
"vue": "3.2.8",
"webpack": "^4.46.0",
"webpack-dev-server": "^3.11.2"
}
}
2 changes: 1 addition & 1 deletion scripts/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const devServerOptions = {
inline: true,
open: true,
hot: true,
overlay: true,
overlay: false,
};

const server = new WebpackDevServer(compiler, devServerOptions);
Expand Down
1 change: 1 addition & 0 deletions scripts/webpack.base.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
loader: 'ts-loader',
exclude: /node_modules/,
options: {
transpileOnly: true,
compilerOptions: { downlevelIteration: true },
},
},
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"sourceMap": false,
"sourceMap": true,
"target": "es2015",
"module": "commonjs",
"moduleResolution": "node",
Expand All @@ -11,6 +11,7 @@
"resolveJsonModule": true,
"esModuleInterop": true,
"removeComments": false,
"jsx": "preserve",
"lib": [
"esnext",
"dom"
Expand Down
Loading