lowcode-engine/packages/code-generator/test/utils/expressionParser/parseExpressionConvertThis2Context.test.ts
armslave.yy ebc78e8788 refactor: code-generator code style
1. rax 出码合并
2. code style 修复

注:合并的代码中带了 datasource 的

Link: https://code.aone.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/codereview/3717159

* Merge branch 'feat/rax-code-generator' of gitlab.alibaba-inc.com:ali-lowcode/ali-lowcode-engine into feat/rax-code-generator

# Conflicts:
#	packages/code-generator/src/generator/ProjectBuilder.ts
#	packages/code-generator/src/parser/SchemaParser.ts
#	packages/code-generator/src/plugins/component/rax/jsx.ts
#	packages/code-generator/src/plugins/project/constants.ts
#	packages/code-generator/src/plugins/project/framework/rax/plugins/packageJSON.ts
#	packages/code-generator/src/plugins/project/i18n.ts
#	packages/code-generator/src/publisher/disk/index.ts
#	packages/code-generator/src/publisher/disk/utils.ts
#	packages/code-generator/src/types/core.ts
#	packages/code-generator/src/types/schema.ts
#	packages/code-generator/src/utils/compositeType.ts
#	packages/code-generator/src/utils/nodeToJSX.ts

* refactor: code-generator

* Merge remote-tracking branch 'origin/refactor/code-style' into refactor/code-style-code-generator

# Conflicts:
#	.vscode/launch.json
2020-09-12 17:46:44 +08:00

80 lines
2.7 KiB
TypeScript

import test from 'ava';
import type { ExecutionContext, Macro } from 'ava';
import { parseExpressionConvertThis2Context } from '../../../src/utils/expressionParser';
const macro: Macro<any[]> = (
t: ExecutionContext<Record<string, unknown>>,
input: [string, string, string[]],
expected: string,
error?: { message: RegExp },
) => {
if (!error) {
t.deepEqual(parseExpressionConvertThis2Context(input[0], input[1], input[2]), expected);
} else {
t.throws(() => {
t.deepEqual(parseExpressionConvertThis2Context(input[0], input[1], input[2]), expected);
}, error.message);
}
};
macro.title = (providedTitle: string | undefined, ...args: any[]): string => {
const [input, expected] = args;
return providedTitle || `after convert this to context "${input[0]}" should be "${expected}"`.replace(/\n|\s+/g, ' ');
};
test(macro, ['this.hello', '__$$context', []], '__$$context.hello');
test(macro, ['this.utils.recordEvent', '__$$context', []], '__$$context.utils.recordEvent');
test(
macro,
['this.utils.recordEvent.bind(this)', '__$$context', []],
'__$$context.utils.recordEvent.bind(__$$context)',
);
test(macro, ['this.item', '__$$context', ['item']], 'item');
test(macro, ['this.user.name', '__$$context', ['user']], 'user.name');
test(macro, ['function (){}', '__$$context', []], 'function () {}');
test(
macro,
['function (){ this.utils.Toast.show("Hello world!") }', '__$$context'],
'function () {\n __$$context.utils.Toast.show("Hello world!");\n}',
);
// 变量能被替换掉
test(
macro,
['function (){ this.utils.recordEvent("click", this.item) }', '__$$context', ['item']],
'function () {\n __$$context.utils.recordEvent("click", item);\n}',
);
// 只替换顶层的,不替换内层
test(
macro,
['function (){ return function (){ this.utils.recordEvent("click", this.item) } }', '__$$context', ['item']],
'function () {\n return function () {\n this.utils.recordEvent("click", item);\n };\n}',
);
// 只替换顶层的,不替换内层
test(
macro,
['function onClick(){ return function (){ this.utils.recordEvent("click", this.item) } }', '__$$context', ['item']],
'function onClick() {\n return function () {\n this.utils.recordEvent("click", item);\n };\n}',
);
// 只替换顶层的,不替换内层
test(
macro,
['() => { return function (){ this.utils.recordEvent("click", this.item) } }', '__$$context', ['item']],
'() => {\n return function () {\n this.utils.recordEvent("click", item);\n };\n}',
);
// 但是若内层有用箭头函数定义的则还是要替换下
test(
macro,
['() => { return () => { this.utils.recordEvent("click", this.item) } }', '__$$context', ['item']],
'() => {\n return () => {\n __$$context.utils.recordEvent("click", item);\n };\n}',
);