Best JavaScript code snippet using storybook-root
convert-await-sequence-into-parallel.ts
Source:convert-await-sequence-into-parallel.ts
...57const codeMod: CodeModExports = ((fileInfo, api, options) => {58 const j = api.jscodeshift;59 const ast = fileInfo.ast;60 // Collect all await statements61 let s1 = getContainingStatement(j, options.anchorTarget)!;62 let s2 = getContainingStatement(j, options.target)!;63 const parentBlock = s1.parents()[0] as BlockStatement;64 let from = parentBlock.body.indexOf(s1.firstNode()!);65 let to = parentBlock.body.indexOf(s2.firstNode()!);66 if (from > to) {67 [from, to] = [to, from];68 [s2, s1] = [s1, s2];69 }70 let statements = [s1];71 for (let i = from + 1; i < to; i++) {72 statements.push(getContainingStatement(j, j(parentBlock.body[i]))!);73 }74 statements.push(s2);75 // Extract necessary data from the old code76 let needsVarDeclaration = false;77 let needsAssignment = false;78 let dataList: Array<{79 targetVar: Pattern | null;80 awaitArgument: Expression;81 }> = [];82 statements.forEach((s) => {83 const node = s.firstNode();84 if (isExpressionStatement(j, s)) {85 dataList.push({86 targetVar: null,87 awaitArgument: ((node as ExpressionStatement).expression as AwaitExpression)88 .argument!,89 });90 } else if (isVariableDeclaration(j, s)) {91 needsVarDeclaration = true;92 const declarator = (node as VariableDeclaration).declarations[0] as VariableDeclarator;93 dataList.push({94 targetVar: declarator.id,95 awaitArgument: (declarator.init as AwaitExpression).argument!,96 });97 } else if (isAssignmentStatement(j, s)) {98 needsAssignment = true;99 const assignmentExpr = (node as ExpressionStatement).expression as AssignmentExpression;100 dataList.push({101 targetVar: assignmentExpr.left,102 awaitArgument: (assignmentExpr.right as AwaitExpression).argument!,103 });104 } else {105 throw new Error('Not supported');106 }107 });108 // Trailing nulls are truncated, e.g.: [,,result,,] => [,,result]109 let varList = dataList.map((x) => x.targetVar);110 while (varList.length > 0 && !varList[varList.length - 1]) {111 varList.pop();112 }113 // Generating new code114 const awaitExpr = j.awaitExpression(115 j.callExpression(j.memberExpression(j.identifier('Promise'), j.identifier('all')), [116 j.arrayExpression(dataList.map((x) => x.awaitArgument)),117 ])118 );119 let newStatement;120 if (needsVarDeclaration) {121 const targetArray = j.arrayPattern(varList);122 newStatement = j.variableDeclaration('let', [j.variableDeclarator(targetArray, awaitExpr)]);123 } else if (needsAssignment) {124 const targetArray = j.arrayPattern(varList);125 newStatement = j.expressionStatement(j.assignmentExpression('=', targetArray, awaitExpr));126 } else {127 newStatement = j.expressionStatement(awaitExpr);128 }129 parentBlock.body.splice(from, to - from + 1, newStatement);130 const resultText = ast.toSource();131 return resultText;132}) as CodeModExports;133function getContainingStatement(134 j: JsCodeShift,135 c: Collection<AstNode>136): Collection<Statement> | null {137 const s = c.thisOrClosest(j.Statement);138 if (isExpressionStatement(j, s)) {139 // Example: 'await foo();'140 return s;141 }142 if (isAssignmentStatement(j, s)) {143 // Example: 'result = await foo();'144 return s;145 }146 if (isVariableDeclaration(j, s)) {147 // Example: 'let result = await foo();'148 return s;149 }150 return null;151}152codeMod.canRun = (fileInfo, api, options) => {153 const j = api.jscodeshift;154 const path = options.target.firstPath();155 if (!path) {156 return false;157 }158 if (options.selection.anchor === options.selection.active) {159 // Works on range selections only160 return false;161 }162 const s1 = getContainingStatement(j, options.anchorTarget);163 if (!s1) {164 return false;165 }166 const s2 = getContainingStatement(j, options.target);167 if (!s2) {168 return false;169 }170 const s1Parent = s1.parents()[0];171 const s2Parent = s2.parents()[0];172 if (!j.BlockStatement.check(s1Parent) || s1Parent !== s2Parent) {173 // Block statement mismatch174 return false;175 }176 let from = s1Parent.body.indexOf(s1.firstNode()!);177 let to = s1Parent.body.indexOf(s2.firstNode()!);178 if (from > to) {179 to = from + to;180 from = to - from;181 to = to - from;182 }183 for (let i = from + 1; i < to; i++) {184 if (!getContainingStatement(j, j(s1Parent.body[i]))) {185 return false;186 }187 }188 return true;189};190codeMod.scope = 'cursor';191codeMod.title = 'Call in parallel with Promise.All()';192codeMod.description = '';193codeMod.detail = '';...
csf-hoist-story-annotations.js
Source:csf-hoist-story-annotations.js
1const getContainingStatement = (n) => {2 if (n.node.type.endsWith('Statement')) {3 return n;4 }5 return getContainingStatement(n.parent);6};7/**8 * Hoist CSF .story annotations9 *10 * For example:11 *12 * ```13 * export const Basic = () => <Button />14 * Basic.story = {15 * name: 'foo',16 * parameters: { ... },17 * decorators = [ ... ],18 * };19 * ```20 *21 * Becomes:22 *23 * ```24 * export const Basic = () => <Button />25 * Basic.storyName = 'foo';26 * Basic.parameters = { ... };27 * Basic.decorators = [ ... ];28 * ```29 */30export default function transformer(file, api) {31 const j = api.jscodeshift;32 const root = j(file.source);33 const renameKey = (exp) =>34 exp.type === 'Identifier' && exp.name === 'name' ? j.identifier('storyName') : exp;35 // 1. If the program does not have `export default { title: '....' }, skip it36 const defaultExportWithTitle = root37 .find(j.ExportDefaultDeclaration)38 .filter((def) => def.node.declaration.properties.map((p) => p.key.name).includes('title'));39 if (defaultExportWithTitle.size() === 0) {40 return root.toSource();41 }42 // 2. Replace each Foo.story = { x: xVal } with Foo.x = xVal;43 const storyAssignments = root.find(j.AssignmentExpression).filter((exp) => {44 const { left, right } = exp.node;45 return (46 left.type === 'MemberExpression' &&47 left.object.type === 'Identifier' &&48 left.property.type === 'Identifier' &&49 left.property.name === 'story' &&50 right.type === 'ObjectExpression'51 );52 });53 storyAssignments.forEach((exp) => {54 const { left, right } = exp.node;55 right.properties.forEach((prop) => {56 const stmt = getContainingStatement(exp);57 stmt.insertBefore(58 j.assignmentStatement('=', j.memberExpression(left.object, renameKey(prop.key)), prop.value)59 );60 });61 });62 // 3. Remove the .story annotations63 storyAssignments.remove();64 // 4. Replace each Foo.story.x with Foo.x;65 const storyOverrides = root.find(j.AssignmentExpression).filter((exp) => {66 const { left } = exp.node;67 return (68 left.type === 'MemberExpression' &&69 left.object.type === 'MemberExpression' &&70 left.object.property.type === 'Identifier' &&...
Using AI Code Generation
1const storybookRoot = require('storybook-root');2const getContainingStatement = storybookRoot.getContainingStatement;3const storybookRoot = require('storybook-root');4const getContainingStatement = storybookRoot.getContainingStatement;5const storybookRoot = require('storybook-root');6const getContainingStatement = storybookRoot.getContainingStatement;7const storybookRoot = require('storybook-root');8const getContainingStatement = storybookRoot.getContainingStatement;9const storybookRoot = require('storybook-root');10const getContainingStatement = storybookRoot.getContainingStatement;11const storybookRoot = require('storybook-root');12const getContainingStatement = storybookRoot.getContainingStatement;13const storybookRoot = require('storybook-root');14const getContainingStatement = storybookRoot.getContainingStatement;15const storybookRoot = require('storybook-root');16const getContainingStatement = storybookRoot.getContainingStatement;17const storybookRoot = require('storybook-root');18const getContainingStatement = storybookRoot.getContainingStatement;19const storybookRoot = require('storybook-root');20const getContainingStatement = storybookRoot.getContainingStatement;21const storybookRoot = require('storybook-root');22const getContainingStatement = storybookRoot.getContainingStatement;23const storybookRoot = require('storybook-root');24const getContainingStatement = storybookRoot.getContainingStatement;25const storybookRoot = require('storybook-root');26const getContainingStatement = storybookRoot.getContainingStatement;27const storybookRoot = require('storybook
Using AI Code Generation
1var storybookRoot = require('storybook-root');2storybookRoot.getContainingStatement('test');3var storybookRoot = require('storybook-root');4storybookRoot.getContainingStatement('test');5var storybookRoot = require('storybook-root');6storybookRoot.getContainingStatement('test');7var storybookRoot = require('storybook-root');8storybookRoot.getContainingStatement('test');9var storybookRoot = require('storybook-root');10storybookRoot.getContainingStatement('test');11var storybookRoot = require('storybook-root');12storybookRoot.getContainingStatement('test');13var storybookRoot = require('storybook-root');14storybookRoot.getContainingStatement('test');15var storybookRoot = require('storybook-root');16storybookRoot.getContainingStatement('test');17var storybookRoot = require('storybook-root');18storybookRoot.getContainingStatement('test');19var storybookRoot = require('storybook-root');20storybookRoot.getContainingStatement('test');21var storybookRoot = require('storybook-root');22storybookRoot.getContainingStatement('test');23var storybookRoot = require('storybook-root');24storybookRoot.getContainingStatement('test');25var storybookRoot = require('storybook-root');26storybookRoot.getContainingStatement('test');27var storybookRoot = require('storybook-root');28storybookRoot.getContainingStatement('test');
Using AI Code Generation
1const storybookRoot = require('storybook-root');2const getContainingStatement = storybookRoot.getContainingStatement;3const statement = getContainingStatement('Hello!');4const storybookRoot = require('storybook-root');5const getContainingStatement = storybookRoot.getContainingStatement;6const statement = getContainingStatement('Hello!');7const storybookRoot = require('storybook-root');8const getContainingStatement = storybookRoot.getContainingStatement;9const statement = getContainingStatement('Hello!');10const storybookRoot = require('storybook-root');11const getContainingStatement = storybookRoot.getContainingStatement;12const statement = getContainingStatement('Hello!');13const storybookRoot = require('storybook-root');14const getContainingStatement = storybookRoot.getContainingStatement;15const statement = getContainingStatement('Hello!');16const storybookRoot = require('storybook-root');17const getContainingStatement = storybookRoot.getContainingStatement;18const statement = getContainingStatement('Hello!');19const storybookRoot = require('storybook-root');20const getContainingStatement = storybookRoot.getContainingStatement;21const statement = getContainingStatement('Hello!');22const storybookRoot = require('storybook-root');23const getContainingStatement = storybookRoot.getContainingStatement;24const statement = getContainingStatement('Hello!');25const storybookRoot = require('storybook-root');26const getContainingStatement = storybookRoot.getContainingStatement;27const statement = getContainingStatement('Hello!');28const storybookRoot = require('storybook-root');29const getContainingStatement = storybookRoot.getContainingStatement;30const statement = getContainingStatement('Hello!');31const storybookRoot = require('storybook-root');32const getContainingStatement = storybookRoot.getContainingStatement;33const statement = getContainingStatement('Hello!');
Using AI Code Generation
1import { getContainingStatement } from '@storybook/addon-storyshots-puppeteer/dist/api';2const test = async () => {3 const root = await getStorybookRoot();4 const statement = await getContainingStatement(root, 'Button');5 console.log(statement);6};7test();
Using AI Code Generation
1var storybookRoot = require("storybook-root");2var getContainingStatement = storybookRoot.getContainingStatement;3var ast = storybookRoot.parse("var a = 10; var b = 20;");4var statement = getContainingStatement(ast, "a");5console.log(statement);6statement = getContainingStatement(ast, "b");7console.log(statement);8statement = getContainingStatement(ast, "c");9console.log(statement);10statement = getContainingStatement(ast, "d");11console.log(statement);12statement = getContainingStatement(ast, "e");13console.log(statement);14statement = getContainingStatement(ast, "f");15console.log(statement);16statement = getContainingStatement(ast, "g");17console.log(statement);18statement = getContainingStatement(ast, "h");19console.log(statement);20statement = getContainingStatement(ast, "i");21console.log(statement);22statement = getContainingStatement(ast, "j");23console.log(statement);24statement = getContainingStatement(ast, "k");25console.log(statement);26statement = getContainingStatement(ast, "l");27console.log(statement);
Using AI Code Generation
1import {getContainingStatement} from 'storybook-root';2export const test = () => {3 let test = getContainingStatement(1);4 console.log(test);5};6import {test} from 'test';7describe('test', () => {8 it('should return the containing statement', () => {9 expect(test()).toBe('hello');10 });11});12export const getContainingStatement = (id) => {13 let story = document.querySelector(`[data-id="${id}"]`);14 let containingStatement = story.querySelector('[data-testid="containing-statement"]');15 return containingStatement.innerHTML;16};17import {getContainingStatement} from 'storybook-root';18export const test = () => {19 let test = getContainingStatement(1);20 console.log(test);21};22import {test} from 'test';23describe('test', () => {24 it('should return the containing statement', () => {25 expect(test()).toBe('hello');26 });27});28export const getContainingStatement = (id) => {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!