Best JavaScript code snippet using storybook-root
traverse-helpers.js
Source:traverse-helpers.js
1import { isExportStory } from '@storybook/csf';2import estraverse from 'estraverse';3import { handleADD, handleSTORYOF, patchNode, handleExportedName } from './parse-helpers';4export function splitSTORYOF(ast, source) {5 let lastIndex = 0;6 const parts = [source];7 estraverse.traverse(ast, {8 fallback: 'iteration',9 enter: (node) => {10 patchNode(node);11 if (node.type === 'CallExpression') {12 lastIndex = handleSTORYOF(node, parts, source, lastIndex);13 }14 },15 });16 return parts;17}18function isFunctionVariable(declarations, includeExclude) {19 return (20 declarations &&21 declarations.length === 1 &&22 declarations[0].type === 'VariableDeclarator' &&23 declarations[0].id &&24 declarations[0].id.name &&25 declarations[0].init &&26 ['CallExpression', 'ArrowFunctionExpression', 'FunctionExpression'].includes(27 declarations[0].init.type28 ) &&29 isExportStory(declarations[0].id.name, includeExclude)30 );31}32function isFunctionDeclaration(declaration, includeExclude) {33 return (34 declaration.type === 'FunctionDeclaration' &&35 declaration.id &&36 declaration.id.name &&37 isExportStory(declaration.id.name, includeExclude)38 );39}40function getDescriptor(metaDeclaration, propertyName) {41 const property =42 metaDeclaration &&43 metaDeclaration.declaration &&44 metaDeclaration.declaration.properties.find((p) => p.key && p.key.name === propertyName);45 if (!property) {46 return undefined;47 }48 const { type } = property.value;49 switch (type) {50 case 'ArrayExpression':51 return property.value.elements.map((t) => {52 if (!['StringLiteral', 'Literal'].includes(t.type)) {53 throw new Error(`Unexpected descriptor element: ${t.type}`);54 }55 return t.value;56 });57 case 'Literal':58 case 'RegExpLiteral':59 return property.value.value;60 default:61 throw new Error(`Unexpected descriptor: ${type}`);62 }63}64function findIncludeExclude(ast) {65 const program = (ast && ast.program) || ast;66 const metaDeclaration =67 program &&68 program.body &&69 program.body.find(70 (d) =>71 d.type === 'ExportDefaultDeclaration' &&72 d.declaration.type === 'ObjectExpression' &&73 (d.declaration.properties || []).length74 );75 const includeStories = getDescriptor(metaDeclaration, 'includeStories');76 const excludeStories = getDescriptor(metaDeclaration, 'excludeStories');77 return {78 includeStories,79 excludeStories,80 };81}82export function splitExports(ast, source) {83 const parts = [];84 let lastIndex = 0;85 const includeExclude = findIncludeExclude(ast);86 estraverse.traverse(ast, {87 fallback: 'iteration',88 enter: (node) => {89 patchNode(node);90 const isNamedExport = node.type === 'ExportNamedDeclaration' && node.declaration;91 const isFunctionVariableExport =92 isNamedExport && isFunctionVariable(node.declaration.declarations, includeExclude);93 const isFunctionDeclarationExport =94 isNamedExport && isFunctionDeclaration(node.declaration, includeExclude);95 if (isFunctionDeclarationExport || isFunctionVariableExport) {96 const functionNode = isFunctionVariableExport97 ? node.declaration.declarations[0].init98 : node.declaration;99 parts.push({100 source: source.substring(lastIndex, functionNode.start - 1),101 });102 parts.push({103 source: source.substring(functionNode.start, functionNode.end),104 declaration: {105 isVariableDeclaration: isFunctionVariableExport,106 ident: isFunctionVariableExport107 ? node.declaration.declarations[0].id.name108 : functionNode.id.name,109 },110 });111 lastIndex = functionNode.end;112 }113 },114 });115 if (source.length > lastIndex + 1) parts.push({ source: source.substring(lastIndex + 1) });116 if (parts.length === 1) return [source];117 return parts;118}119export function findAddsMap(ast, storiesOfIdentifiers) {120 const addsMap = {};121 estraverse.traverse(ast, {122 fallback: 'iteration',123 enter: (node, parent) => {124 patchNode(node);125 if (node.type === 'MemberExpression') {126 const { toAdd, idToFramework } = handleADD(node, parent, storiesOfIdentifiers);127 Object.assign(addsMap, toAdd);128 }129 },130 });131 return addsMap;132}133export function findExportsMap(ast) {134 const addsMap = {};135 estraverse.traverse(ast, {136 fallback: 'iteration',137 enter: (node, parent) => {138 patchNode(node);139 const isNamedExport = node.type === 'ExportNamedDeclaration' && node.declaration;140 const isFunctionVariableExport =141 isNamedExport &&142 node.declaration.declarations &&143 node.declaration.declarations.length === 1 &&144 node.declaration.declarations[0].type === 'VariableDeclarator' &&145 node.declaration.declarations[0].id &&146 node.declaration.declarations[0].id.name &&147 node.declaration.declarations[0].init &&148 ['CallExpression', 'ArrowFunctionExpression', 'FunctionExpression'].includes(149 node.declaration.declarations[0].init.type150 );151 const isFunctionDeclarationExport =152 isNamedExport &&153 node.declaration.type === 'FunctionDeclaration' &&154 node.declaration.id &&155 node.declaration.id.name;156 if (isFunctionDeclarationExport || isFunctionVariableExport) {157 const exportDeclaration = isFunctionVariableExport158 ? node.declaration.declarations[0]159 : node.declaration;160 const toAdd = handleExportedName(161 exportDeclaration.id.name,162 exportDeclaration.init || exportDeclaration,163 parent164 );165 Object.assign(addsMap, toAdd);166 }167 },168 });169 return addsMap;170}171export function popParametersObjectFromDefaultExport(source, ast) {172 let splicedSource = source;173 let parametersSliceOfCode = '';174 let indexWhereToAppend = -1;175 let foundParametersProperty = false;176 estraverse.traverse(ast, {177 fallback: 'iteration',178 enter: (node) => {179 patchNode(node);180 const isDefaultExport = node.type === 'ExportDefaultDeclaration';181 const isObjectExpression = node.declaration?.type === 'ObjectExpression';182 const isTsAsExpression = node.declaration?.type === 'TSAsExpression';183 const targetNode = isObjectExpression ? node.declaration : node.declaration?.expression;184 if (185 isDefaultExport &&186 (isObjectExpression || isTsAsExpression) &&187 (targetNode.properties || []).length188 ) {189 const parametersProperty = targetNode.properties.find(190 (p) => p.key.name === 'parameters' && p.value.type === 'ObjectExpression'191 );192 foundParametersProperty = !!parametersProperty;193 if (foundParametersProperty) {194 patchNode(parametersProperty.value);195 } else {196 patchNode(targetNode);197 }198 splicedSource = parametersProperty199 ? source.substring(0, parametersProperty.value.start) +200 source.substring(parametersProperty.value.end + 1)201 : splicedSource;202 parametersSliceOfCode = parametersProperty203 ? source.substring(parametersProperty.value.start, parametersProperty.value.end)204 : '{}';205 indexWhereToAppend = parametersProperty206 ? parametersProperty.value.start207 : targetNode.start + 1;208 }209 },210 });211 return { splicedSource, parametersSliceOfCode, indexWhereToAppend, foundParametersProperty };...
Using AI Code Generation
1import { parametersProperty } from 'storybook-root';2import { parametersProperty } from 'storybook-root/parametersProperty';3import { parametersProperty } from 'storybook-root';4import { parametersProperty } from 'storybook-root/parametersProperty';5import { parametersProperty } from 'storybook-root';6import { parametersProperty } from 'storybook-root/parametersProperty';7import { parametersProperty } from 'storybook-root';8import { parametersProperty } from 'storybook-root/parametersProperty';9import { parametersProperty } from 'storybook-root';10import { parametersProperty } from 'storybook-root/parametersProperty';11import { parametersProperty } from 'storybook-root';12import { parametersProperty } from 'storybook-root/parametersProperty';13import { parametersProperty } from 'storybook-root';14import { parametersProperty } from 'storybook-root/parametersProperty';15import { parametersProperty } from 'storybook-root';16import { parametersProperty } from 'storybook-root/parametersProperty';17import { parametersProperty } from 'storybook-root';18import { parametersProperty } from 'storybook-root/parametersProperty';19import { parametersProperty } from 'storybook-root';20import { parametersProperty } from 'storybook-root/parametersProperty';21import { parametersProperty } from 'storybook-root';22import { parametersProperty } from 'storybook-root/parametersProperty';23import { parametersProperty } from 'storybook-root';24import { parametersProperty } from 'storybook-root/parametersProperty';25import { parametersProperty } from '
Using AI Code Generation
1const { parametersProperty } = require('storybook-root');2const parameters = parametersProperty();3console.log(parameters);4const { parametersProperty } = require('storybook-root');5const parameters = parametersProperty();6console.log(parameters);7const { parametersProperty } = require('storybook-root');8const parameters = parametersProperty();9console.log(parameters);10const { parametersProperty } = require('storybook-root');11const parameters = parametersProperty();12console.log(parameters);13const { parametersProperty } = require('storybook-root');14const parameters = parametersProperty();15console.log(parameters);16const { parametersProperty } = require('storybook-root');17const parameters = parametersProperty();18console.log(parameters);19const { parametersProperty } = require('storybook-root');20const parameters = parametersProperty();21console.log(parameters);22const { parametersProperty } = require('storybook-root');23const parameters = parametersProperty();24console.log(parameters);25const { parametersProperty } = require('storybook-root');26const parameters = parametersProperty();27console.log(parameters);28const { parametersProperty } = require('storybook-root');29const parameters = parametersProperty();30console.log(parameters);31const { parametersProperty } = require('storybook-root');32const parameters = parametersProperty();33console.log(parameters);34const { parametersProperty } = require('storybook-root');35const parameters = parametersProperty();36console.log(parameters);37const { parametersProperty } = require('storybook-root');38const parameters = parametersProperty();39console.log(parameters);
Using AI Code Generation
1import { parametersProperty } from 'storybook-root';2const parameters = parametersProperty();3parameters.set('myParameter', 'myValue');4import { parametersProperty } from 'storybook-root';5const parameters = parametersProperty();6parameters.set('myParameter', 'myValue');7import { parametersProperty } from 'storybook-root';8const parameters = parametersProperty();9parameters.set('myParameter', 'myValue');10import { parametersProperty } from 'storybook-root';11const parameters = parametersProperty();12parameters.set('myParameter', 'myValue');13import { parametersProperty } from 'storybook-root';14const parameters = parametersProperty();15parameters.set('myParameter', 'myValue');16import { parametersProperty } from 'storybook-root';17const parameters = parametersProperty();18parameters.set('myParameter', 'myValue');19import { parametersProperty } from 'storybook-root';20const parameters = parametersProperty();21parameters.set('myParameter', 'myValue');22import { parametersProperty } from 'storybook-root';23const parameters = parametersProperty();24parameters.set('myParameter', 'myValue');25import { parametersProperty } from 'storybook-root';26const parameters = parametersProperty();27parameters.set('myParameter', 'myValue');28import { parametersProperty } from 'storybook-root';29const parameters = parametersProperty();30parameters.set('myParameter', 'myValue');31import { parametersProperty } from 'storybook-root';32const parameters = parametersProperty();33parameters.set('myParameter', 'myValue');
Using AI Code Generation
1import { parametersProperty } from 'storybook-root';2const parameters = parametersProperty();3console.log(parameters);4import { addParameters } from '@storybook/react';5import { parameters } from './parameters.js';6export function parametersProperty() {7 return parameters;8}9addParameters(parameters);10export const parameters = {11};12import 'storybook-root';
Using AI Code Generation
1import { addParameters } from '@storybook/react'2addParameters({3 options: {4 },5})6import { configure } from '@storybook/react'7const req = require.context('../src', true, /.stories.js$/)8function loadStories() {9 req.keys().forEach(filename => req(filename))10}11configure(loadStories, module)12const path = require('path')13module.exports = (baseConfig, env, config) => {14 config.resolve.alias = {15 '@storybook/react': path.resolve(__dirname, '../node_modules/@storybook/react'),16 }17}18const path = require('path')19module.exports = (baseConfig, env, config) => {20 config.resolve.alias = {21 '@storybook/react': path.resolve(__dirname, '../node_modules/@storybook/react'),22 }23}
Using AI Code Generation
1const root = document.querySelector('storybook-root');2root.parametersProperty = {3};4addDecorator((storyFn, context) => {5 const story = storyFn();6 <p>Decorator: ${context.parameters.myParameter}</p>7 ${story}8 `;9});10export const myStory = () => html`11`;12myStory.parameters = {13};14export const myOtherStory = () => html`15`;16myOtherStory.parameters = {17};
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!!