Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
stop checking whether storybook is imported
Browse files Browse the repository at this point in the history
  • Loading branch information
fa93hws committed Aug 15, 2023
1 parent b20ce57 commit d095a96
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 47 deletions.
1 change: 0 additions & 1 deletion __tests__/fixtures/feature-storybook-ref/b.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright 2021 Canva Inc. All Rights Reserved.

import { storiesOf } from "@storybook/react";
import * as React from "react";

export default {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/fixtures/feature-storybook-ref/c.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2021 Canva Inc. All Rights Reserved.

import { storiesOf, type Meta } from "@storybook/react";
import { type Meta } from "@storybook/react";
import * as React from "react";

export default {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/fixtures/feature-storybook-ref/d_e.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2021 Canva Inc. All Rights Reserved.

import { storiesOf, type Meta } from "@storybook/react";
import { type Meta } from "@storybook/react";
import * as React from "react";

export default {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@canva/dependency-tree",
"version": "3.3.1",
"version": "3.3.2",
"description": "Calculates a dependency tree for set of files",
"main": "dist/index.js",
"author": "Canva Pty Ltd",
Expand Down
44 changes: 1 addition & 43 deletions src/processors/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const warn = logger.extend('warn');

const STORIES_IMPORT = 'storiesOf';
const CSF3_EXPORT_TITLE_FIELD = 'title';
const STORIES_PACKAGE = '@storybook/react';
const STORIES_FILE_RE = new RegExp(
`([^${escapeRegExp(path.sep)}]+)\\.stories\\.tsx?$`,
);
Expand Down Expand Up @@ -51,41 +50,10 @@ export class FeatureFileProcessor extends TypeScriptFileProcessor {
return ts.isIdentifier(node) && node.text === STORIES_IMPORT;
}

private static isStoriesImport(node: ts.Node): boolean {
if (!ts.isImportDeclaration(node)) {
return false;
}

if (
!(
node.moduleSpecifier &&
(node.moduleSpecifier as ts.StringLiteral).text === STORIES_PACKAGE
)
) {
return false;
}

const namedBindings =
node.importClause && (node.importClause.namedBindings as ts.NamedImports);
if (!namedBindings) {
return false;
}

return (
namedBindings.elements &&
namedBindings.elements.some(
(is: ts.ImportSpecifier) =>
(is.name as ts.Identifier).text === STORIES_IMPORT,
)
);
}

private static walkStories(
sourceFile: ts.SourceFile,
callback: (storybook: Storybook) => void,
) {
let importFound = false;

const walkTree = (node: ts.Node): void => {
if (
ts.isCallExpression(node) &&
Expand Down Expand Up @@ -132,17 +100,7 @@ export class FeatureFileProcessor extends TypeScriptFileProcessor {
ts.forEachChild(node, walkTree);
};

const walkFirstLevel = (node: ts.Node): void => {
// Look for stories import first
if (!importFound && FeatureFileProcessor.isStoriesImport(node)) {
importFound = true;
} else if (importFound && !ts.isImportDeclaration(node)) {
// Look for `storiesOf` calls only if there was import
walkTree(node);
}
};

return ts.forEachChild(sourceFile, walkFirstLevel);
return ts.forEachChild(sourceFile, walkTree);
}

private static walkSteps(
Expand Down

0 comments on commit d095a96

Please sign in to comment.