Best JavaScript code snippet using storybook-root
type-resolver.ts
Source:type-resolver.ts
1import * as _ from 'lodash';2import * as ts from 'typescript';3import { resolvePath } from './path';4import {5 getTypeAliasDeclaration,6 getTypeReferenceNode,7 getUnionMemberTypes,8 getJSDocComments,9 printJSDocComments,10} from './typescript';11export type ConsoleTypeDeclarations = Record<'CodeRef' | 'EncodedCodeRef', ts.Declaration>;12type ContainsJSDoc = {13 /** JSDoc comments attached to the corresponding AST node. */14 docComments: string[];15};16type ExtensionPropertyInfo = {17 /** Name of the property. */18 name: string;19 /** Value type signature, e.g. `CodeRef<() => void>`. */20 valueType: string;21 /** If `true`, this property has the optional modifier. */22 optional: boolean;23} & ContainsJSDoc;24export type ExtensionTypeInfo = {25 /** Name of the extension type, e.g. `Foo`. */26 name: string;27 /** Console extension `type` value, e.g. `console.foo`. */28 type: string;29 /** Console extension `properties` object representation. */30 properties: ExtensionPropertyInfo[];31} & ContainsJSDoc;32type ConsoleTypeResolver = {33 getDeclarations: () => ConsoleTypeDeclarations;34 getConsoleExtensions: (35 exitOnErrors?: boolean,36 ) => {37 result: ExtensionTypeInfo[];38 diagnostics: { errors: string[]; warnings: string[] };39 };40};41const parseExtensionTypeInfo = (42 type: ts.Type,43 typeChecker: ts.TypeChecker,44 errors: string[],45): ExtensionTypeInfo => {46 const typeString = typeChecker.typeToString(type);47 const typeDeclaration = _.head(type.aliasSymbol?.declarations);48 if (!typeDeclaration || !ts.isTypeAliasDeclaration(typeDeclaration)) {49 errors.push(`Extension type '${typeString}' must be declared as type alias`);50 return null;51 }52 const refToExtensionDeclaration = getTypeReferenceNode(typeDeclaration, 'ExtensionDeclaration');53 if (refToExtensionDeclaration?.typeArguments?.length !== 2) {54 errors.push(`Extension type '${typeString}' must reference ExtensionDeclaration<T, P> type`);55 return null;56 }57 const [typeArgT, typeArgP] = refToExtensionDeclaration.typeArguments;58 if (!ts.isLiteralTypeNode(typeArgT) || !ts.isStringLiteral(typeArgT.literal)) {59 errors.push(`Extension type '${typeString}' must declare T type parameter as string literal`);60 return null;61 }62 const consoleExtensionType = typeArgT.literal.text;63 const consoleExtensionProperties: ExtensionPropertyInfo[] = [];64 typeChecker65 .getTypeFromTypeNode(typeArgP)66 .getProperties()67 .forEach((p) => {68 consoleExtensionProperties.push({69 name: p.getName(),70 // TODO(vojtech): using ts.TypeFormatFlags.MultilineObjectLiterals flag doesn't seem71 // to insert newline characters as expected, should revisit this issue in the future72 valueType: typeChecker.typeToString(73 typeChecker.getTypeOfSymbolAtLocation(p, typeArgP),74 typeArgP,75 // eslint-disable-next-line no-bitwise76 ts.TypeFormatFlags.AllowUniqueESSymbolType |77 ts.TypeFormatFlags.UseAliasDefinedOutsideCurrentScope |78 ts.TypeFormatFlags.UseSingleQuotesForStringLiteralType,79 ),80 // eslint-disable-next-line no-bitwise81 optional: !!(p.flags & ts.SymbolFlags.Optional),82 docComments: getJSDocComments(_.head(p.declarations)),83 });84 });85 return {86 name: typeDeclaration.name.text,87 type: consoleExtensionType,88 properties: consoleExtensionProperties,89 docComments: getJSDocComments(typeDeclaration),90 };91};92const validateExtensionTypes = (93 types: ExtensionTypeInfo[],94 errors: string[],95 warnings: string[],96) => {97 const getDuplicates = (arr: string[]) => Object.keys(_.pickBy(_.countBy(arr), (c) => c > 1));98 getDuplicates(types.map((t) => t.name)).forEach((typeName) => {99 errors.push(`Extension type '${typeName}' has multiple declarations`);100 });101 const checkComments = (prefix: string, docComments: string[]) => {102 if (!printJSDocComments(docComments)) {103 warnings.push(`${prefix} has no JSDoc comments`);104 }105 };106 types.forEach((t) => {107 checkComments(`Extension type '${t.name}'`, t.docComments);108 t.properties.forEach((p) => {109 checkComments(`Extension type '${t.name}' property '${p.name}'`, p.docComments);110 });111 });112};113export const getConsoleTypeResolver = (program: ts.Program): ConsoleTypeResolver => {114 const srcFile = (filePath: string) => program.getSourceFile(resolvePath(filePath));115 const typeChecker = program.getTypeChecker();116 return {117 getDeclarations: () => ({118 CodeRef: getTypeAliasDeclaration(srcFile('src/types.ts'), 'CodeRef'),119 EncodedCodeRef: getTypeAliasDeclaration(srcFile('src/types.ts'), 'EncodedCodeRef'),120 }),121 getConsoleExtensions: (exitOnErrors = false) => {122 const types = getUnionMemberTypes(123 typeChecker,124 getTypeAliasDeclaration(srcFile('src/schema/console-extensions.ts'), 'SupportedExtension'),125 );126 const errors: string[] = [];127 const warnings: string[] = [];128 if (types.length === 0) {129 errors.push('Union type SupportedExtension has no members');130 }131 const result = _.compact(types.map((t) => parseExtensionTypeInfo(t, typeChecker, errors)));132 validateExtensionTypes(result, errors, warnings);133 if (errors.length > 0 && exitOnErrors) {134 console.error('Detected errors while parsing Console extension type declarations');135 process.exit(1);136 }137 return { result, diagnostics: { errors, warnings } };138 },139 };...
Using AI Code Generation
1const { containsJsDoc } = require('storybook-root');2const { containsJsDoc } = require('../storybook-root');3const { containsJsDoc } = require('storybook-root');4const { containsJsDoc } = require('../storybook-root');5const { containsJsDoc } = require('storybook-root');6const { containsJsDoc } = require('../storybook-root');7const { containsJsDoc } = require('storybook-root');8const { containsJsDoc } = require('../storybook-root');9const { containsJsDoc } = require('storybook-root');10const { containsJsDoc } = require('../storybook-root');11const { containsJsDoc } = require('storybook-root');12const { containsJsDoc } = require('../storybook-root');13const { containsJsDoc } = require('storybook-root');14const { containsJsDoc } = require('../storybook-root');15const { containsJsDoc } = require('storybook-root');16const { containsJsDoc } = require('../storybook-root');17const { containsJsDoc } = require('storybook-root');18const { containsJsDoc } = require('../storybook-root');
Using AI Code Generation
1const containsJsDoc = require('storybook-root').containsJsDoc;2const path = require('path');3const file = path.join(__dirname, 'test.js');4containsJsDoc(file).then(isJsDoc => {5 console.log(isJsDoc);6});7const containsJsDoc = require('storybook-root').containsJsDoc;8const path = require('path');9const file = path.join(__dirname, 'test.js');10containsJsDoc(file).then(isJsDoc => {11 console.log(isJsDoc);12});
Using AI Code Generation
1const storybookRoot = require('storybook-root');2const path = require('path');3const filePath = path.resolve(__dirname, 'src', 'stories', 'index.js');4const result = storybookRoot.containsJsDoc(filePath);5console.log(result);6containsJsDoc(filePath)7containsStoryOf(filePath)8containsStoryOfWithKind(filePath, kind)9containsStoryOfWithName(filePath, name)10containsStoryOfWithKindAndName(filePath, kind, name)11containsStoryOfWithNameAndKind(filePath, name, kind)12containsStoryOfWithNameAndKindAndStoryName(filePath, name, kind, storyName)13containsStoryOfWithNameAndKindAndStoryNameAndStoryFn(filePath, name, kind, storyName, storyFn)14containsStoryOfWithNameAndKindAndStoryNameAndStoryFnAndStoryOptions(filePath, name, kind, storyName, storyFn, storyOptions)15containsStoryOfWithNameAndKindAndStoryNameAndStoryFnAndStoryOptionsAndParameters(filePath, name, kind, storyName, storyFn, storyOptions, parameters)16containsStoryOfWithNameAndKindAndStoryNameAndStoryFnAndStoryOptionsAndParametersAndDecorators(filePath, name, kind, storyName, storyFn, storyOptions, parameters, decorators)17containsStoryOfWithNameAndKindAndStoryNameAndStoryFnAndStoryOptionsAndParametersAndDecoratorsAndDocs(filePath, name, kind, storyName, storyFn, storyOptions, parameters, decorators, docs)
Using AI Code Generation
1import { containsJsDoc } from '@storybook/react/dist/client/preview/index.js';2const story = { parameters: { docs: { source: { code: 'some code' } } } };3const result = containsJsDoc(story);4console.log(result);5import { containsJsDoc } from '@storybook/addon-docs/dist/frameworks/common/config';6const story = { parameters: { docs: { source: { code: 'some code' } } } };7const result = containsJsDoc(story);8console.log(result);
Using AI Code Generation
1const { containsJsDoc } = require('storybook-root');2if (containsJsDoc('test.js')) {3}4const { containsJsDoc } = require('storybook-root');5if (containsJsDoc('test.js')) {6}7const { containsJsDoc } = require('storybook-root');8if (containsJsDoc('test.js')) {9}10const { containsJsDoc } = require('storybook-root');11if (containsJsDoc('test.js')) {12}13const { containsJsDoc } = require('storybook-root');14if (containsJsDoc('test.js')) {15}16const { containsJsDoc } = require('storybook-root');17if (containsJsDoc('test.js')) {18}19const { containsJsDoc } = require('storybook-root');20if (containsJsDoc('test.js')) {21}22const { containsJsDoc } = require('storybook-root');23if (containsJsDoc('test.js')) {24}25const { containsJsDoc } = require('storybook-root');26if (containsJsDoc('test.js')) {27}28const { containsJs
Using AI Code Generation
1const containsJsDoc = require('@storybook/addon-docs/blocks').containsJsDoc;2const path = require('path');3const test = async () => {4 const result = await containsJsDoc(path.resolve(__dirname, './test.js'));5 console.log(result);6};7test();
Using AI Code Generation
1const isJsDoc = containsJsDoc();2console.log(isJsDoc);3module.exports = {4}5const { containsJsDoc } = require('../lib');6module.exports = {7}8const { containsJsDoc } = require('../storybook-root');9test('containsJsDoc method should return true when there is a JSDoc comment in the code', () => {10 expect(containsJsDoc()).toBe(true);11});12 ✓ containsJsDoc method should return true when there is a JSDoc comment in the code (2ms)13 ✕ containsJsDoc method should return true when there is a JSDoc comment in the code (2ms)
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!!