How to use createSimpleExpression method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

compiler.js

Source: compiler.js Github

copy
1const {
2  baseParse: parse,
3  transform,
4  generate,
5  createCallExpression,
6  createArrayExpression,
7  createObjectExpression,
8  createObjectProperty,
9  createSimpleExpression,
10} = require('@vue/compiler-core')
11
12module.exports = { compile }
13
14/**
15 * @param {string} source
16 */
17function compile(source) {
18  const ast = parse(source)
19
20  transform(ast, {
21    hoistStatic: false,
22    isBuiltInComponent() {
23      return false
24    },
25    cacheHandlers: false,
26    ssr: false,
27    nodeTransforms: [transformElement],
28  })
29
30  // override root codegenNode
31  ast.codegenNode =
32    ast.children.length === 1
33      ? ast.children[0]
34      : createArrayExpression(ast.children)
35
36  // clear out helpers as we are not using any
37  ast.helpers = []
38
39  return generate(ast, { mode: 'module', sourceMap: false })
40    .code.trim()
41    .replace(
42      /^export function render\(_ctx, _cache\)/,
43      'export function render()'
44    )
45}
46
47/**
48 *
49 * @param {import('@vue/compiler-core').ElementNode} node
50 * @param {import('@vue/compiler-core').TransformContext} context
51 */
52function transformElement(node, context) {
53  if (node.type !== 1) return // Not an element, return.
54
55  return () => {
56    const props = createObjectExpression(
57      node.props.map(prop =>
58        createObjectProperty(
59          prop.name,
60          prop.value ? createSimpleExpression(prop.value.content, true) : 'true'
61        )
62      )
63    )
64
65    node.codegenNode = createCallExpression('h', [
66      createSimpleExpression(node.tag, true),
67      props,
68      createArrayExpression(node.children),
69    ])
70  }
71}
72
Full Screen

vue-forms-module.js

Source: vue-forms-module.js Github

copy
1const {assert} = require("@vue/compiler-core");
2const {createSimpleExpression, createCompoundExpression, createStructuralDirectiveTransform, processExpression, findProp, findDir, createObjectProperty, createCompilerError, locStub} = require("@vue/compiler-core");
3
4/**
5 * Node-трансформер для vue 3, который делает следующую работу:
6 * - `<FForm>` заменяется на `<FForm #="{$cont, $value}">`
7 * - `<FArray name="xxx">` заменяется на `<FArray name="xxx" :value="$value['xxx']" #="{$cont, $value, $index, $parentCont}">`
8 * - `<FObject name="xxx">` заменяется на `<FObject name="xxx" :value="$value['xxx']" #="{$cont, $value, $parentCont}" @setDefault="$value['xxx'] = $event">`
9 * - `<FCont>` заменяется на `<FCont #="{$cont, $value}">`
10 * - любой элемент внутри `FForm`, `FArray`, `FObject`, `FCont`, который содержит аттрибут `name` преобразуется так:
11 * >- аттрибут `name` удаляется
12 * >- добавляется директива `v-fname="{ name: xxx, cont: $cont }"`
13 * >- добавляется директива `v-model="$value['xxx']"`
14 * >- добавляется атрибут `:disabled="$cont.isDisabled"` или, если у элемента уже был атрибут `:disabled="???"`, то он заменяется на `:disabled="??? || $cont.isDisabled"`
15 * </ul>
16 *
17 * Установка в webpack:
18 * ```JavaScript
19 * { loader: 'vue-loader', options: {compilerOptions: { nodeTransforms: [formsNodeTransform] }} }
20 * ```
21 */
22module.exports = function (node, context) {
23    if (node.type === 1 && node.tag === 'FForm') {
24        if (node.props.some(x=>x.type===7 && x.name === 'slot')) {
25            context.onError(createCompilerError(10001,  nameProp.loc, null, 'FForm не должен содержать явного указания слотов #=...'))
26        }
27        let slotProp = {
28            type: 7 /*NodeTypes.DIRECTIVE*/,
29            name: 'slot',
30            exp: processExpression(createSimpleExpression("{$value, $cont}"), context, true),// createCompoundExpression(["{$value, $cont}"]),
31            arg: undefined,
32            modifiers: [''],
33            loc: node.loc
34        }
35        node.props.push(slotProp)
36        if (context.prefixIdentifiers) {
37            context.addIdentifiers(slotProp.exp)
38        }
39        context.scopes.vSlot++
40        context.scopes.f_inCont = context.scopes.f_inCont ? context.scopes.f_inCont + 1 : 1
41        return () => {
42            if (context.prefixIdentifiers) {
43                context.removeIdentifiers(slotProp.exp);
44            }
45            context.scopes.vSlot--;
46            context.scopes.f_inCont--;
47        };
48    }
49    if (node.type === 1 && node.tag === 'FCont') {
50        if (node.props.some(x=>x.type===7 && x.name === 'slot')) {
51            context.onError(createCompilerError(10001,  nameProp.loc, null, 'FCont не должен содержать явного указания слотов #=...'))
52        }
53        let slotProp = {
54            type: 7 /*NodeTypes.DIRECTIVE*/,
55            name: 'slot',
56            exp: processExpression(createSimpleExpression("{$value, $cont}"), context, true),
57            arg: undefined,
58            modifiers: [''],
59            loc: node.loc
60        }
61        node.props.push(slotProp)
62        if (context.prefixIdentifiers) {
63            context.addIdentifiers(slotProp.exp)
64        }
65        context.scopes.vSlot++
66        context.scopes.f_inCont = context.scopes.f_inCont ? context.scopes.f_inCont + 1 : 1
67        return () => {
68            if (context.prefixIdentifiers) {
69                context.removeIdentifiers(slotProp.exp);
70            }
71            context.scopes.vSlot--;
72            context.scopes.f_inCont--;
73        };
74    }
75    if (node.type === 1 && (node.tag === 'FArray' || node.tag === 'FObject')) {
76        let nameProp = findProp(node, 'name')
77        if (node.props.some(x=>x.type===7 && x.name === 'slot')) {
78            context.onError(createCompilerError(10001,  nameProp.loc, null, 'FArray и FObject не должены содержать явного указания слотов #=...'))
79        }
80        if (!nameProp) {
81            context.onError(createCompilerError(10001,  nameProp.loc, null, 'FArray и FObject должены содержать свойство name'))
82        }
83        let nameExpr
84        if (nameProp.type === 6 /*ATTRIBUTE*/) {
85            nameExpr = JSON.stringify(nameProp.value.content)
86        } else if (nameProp.type === 7 /*DIRECTIVE*/ && nameProp.name === 'bind') {
87            nameExpr = nameProp.exp.content
88        } else {
89            context.onError(createCompilerError(10001,  nameProp.loc, null, 'Неизвестный тип узла имени'))
90        }
91        node.props.push({
92            type: 7 /*NodeTypes.DIRECTIVE*/,
93            name: 'bind',
94            exp: `$value[${nameExpr}]`,
95            arg: createSimpleExpression('value', true, locStub, true),
96            modifiers: [],
97            loc: node.loc
98        })
99        if (findProp(node, 'default')) {
100            node.props.push({
101                type: 7 /*NodeTypes.DIRECTIVE*/,
102                name: 'on',
103                exp: createSimpleExpression(`$value[${nameExpr}]=$event`),
104                arg: createSimpleExpression('setDefault', true, locStub, true),
105                modifiers: [],
106                loc: node.loc
107            })
108        }
109        let slotProp = {
110            type: 7 /*NodeTypes.DIRECTIVE*/,
111            name: 'slot',
112            exp: processExpression(createSimpleExpression(node.tag === 'FArray' ? "{$parentCont, $cont, $value, $index}" : "{$parentCont, $cont, $value}"), context, true),// createCompoundExpression(["{$value, $cont}"]),
113            arg: undefined,
114            modifiers: [''],
115            loc: node.loc
116        }
117        node.props.push(slotProp)
118        if (context.prefixIdentifiers) {
119            context.addIdentifiers(slotProp.exp)
120        }
121        context.scopes.vSlot++
122        context.scopes.f_inCont = context.scopes.f_inCont ? context.scopes.f_inCont + 1 : 1
123        return () => {
124            if (context.prefixIdentifiers) {
125                context.removeIdentifiers(slotProp.exp);
126            }
127            context.scopes.vSlot--;
128            context.scopes.f_inCont--;
129        };
130    }
131    if (node.type === 1 && context.scopes.f_inCont) {
132        let nameProp = findProp(node, 'name')
133        if (nameProp) {
134            let isComponent = /^([A-Z]{2}|[a-z]-)/.test(node.tag)
135            let nameExpr
136            if (nameProp.type === 6 /*ATTRIBUTE*/) {
137                nameExpr = JSON.stringify(nameProp.value.content)
138            } else if (nameProp.type === 7 /*DIRECTIVE*/ && nameProp.name === 'bind') {
139                nameExpr = nameProp.exp.content
140            } else {
141                context.onError(createCompilerError(10001,  nameProp.loc, null, 'Неизвестный тип узла имени'))
142            }
143            if (!isComponent) {
144                node.props.push({
145                    type: 7 /*NodeTypes.DIRECTIVE*/,
146                    name: 'fname',
147                    exp: processExpression(createSimpleExpression(`{name: ${nameExpr}, cont: $cont}`), context),
148                    arg: undefined,
149                    modifiers: [],
150                    loc: nameProp.loc
151                })
152            }
153            let vModelExpr = `$value[${nameExpr}]`
154            node.props.push({
155                type: 7 /*NodeTypes.DIRECTIVE*/,
156                name: 'model',
157                exp: processExpression(createSimpleExpression(vModelExpr, false, {...nameProp.loc, source: vModelExpr}), context),
158                arg: undefined,
159                modifiers: [],
160                loc: nameProp.loc
161            })
162            if (!isComponent) {
163                for (let i = node.props.length - 1; i >= 0; i--) {
164                    if (node.props[i] === nameProp) {
165                        node.props.splice(i, 1)
166                    }
167                }
168            }
169        }
170        if (nameProp || node.tag === 'f-button' || node.tag === 'FButton' || node.tag === 'button' || node.tag === 'fieldset') {
171            let disabledProp = findProp(node, 'disabled', false, true)
172            if (disabledProp) {
173                if (disabledProp.type === 7 /*DIRECTIVE*/) {
174                    disabledProp.exp = createCompoundExpression(
175                        [
176                            '(',
177                            disabledProp.exp,
178                            ')||(',
179                            processExpression(createSimpleExpression("$cont.isDisabled"), context),
180                            ')'
181                        ],
182                        disabledProp.exp.loc
183                    )
184                }
185            } else {
186                node.props.push({
187                    type: 7 /*NodeTypes.DIRECTIVE*/,
188                    name: 'bind',
189                    exp: processExpression(createSimpleExpression("$cont.isDisabled"), context),
190                    arg: createSimpleExpression('disabled', true, locStub, true),
191                    modifiers: [],
192                    loc: node.loc
193                })
194            }
195        }
196    }
197}
Full Screen

exportMagrin.js

Source: exportMagrin.js Github

copy
1import { createSimpleExpression } from "@vue/compiler-core";
2import axios from "axios";
3import apiRouters from "../../views/apiRoutes";
4
5export default {
6    state: {
7        treeExport: [],
8    },
9    getters: {
10        getTreeExport: (state) => {
11            return state.treeExport;
12        }
13    },
14    mutations: {
15        setTreeExport: (state, data) => {
16            state.treeExport = data;
17        }
18    },
19    actions: {
20        fetchTreeExport(ctx) {
21            return createSimpleExpression(1).then(function (doneStr) {
22                return axios.get(apiRouters.treeExport).then((res) => {
23                    ctx.commit("setTreeExport", res.data)
24                });
25            });
26        }
27    }
28
29};
30
31function sleep(sec) {
32    return new Promise(function (resolve, reject) {
33        setTimeout(() => { resolve("done!"); }, sec * 1000)
34    })
35}
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)