|
| 1 | +import { relative } from "node:path"; |
| 2 | +import { defineRule, type ESTree } from "@oxlint/plugins"; |
| 3 | +import { propertyName } from "../property.ts"; |
| 4 | + |
| 5 | +const STORY_SUFFIX = /\.stories\.[cm]?[jt]sx?$/; |
| 6 | +const PATH_SEPARATOR = /[\\/]/; |
| 7 | + |
| 8 | +/** Storybook ignores case and punctuation when it turns a path or title into an id. */ |
| 9 | +const normalizedSegments = (segments: string[]) => |
| 10 | + segments.map((segment) => segment.replaceAll(/[^a-zA-Z0-9]/g, "").toLowerCase()); |
| 11 | + |
| 12 | +/** The title Storybook derives from this file when `meta.title` is absent. */ |
| 13 | +function automaticTitle(filename: string, cwd: string): string[] | undefined { |
| 14 | + const segments = relative(cwd, filename).split(PATH_SEPARATOR); |
| 15 | + const src = segments.findIndex( |
| 16 | + (segment, index) => segment === "src" && (index === 0 || segments[index - 1] === "webapp"), |
| 17 | + ); |
| 18 | + if (src === -1) return undefined; |
| 19 | + const owned = segments.slice(src + 1); |
| 20 | + const file = owned.at(-1); |
| 21 | + if (file === undefined || !STORY_SUFFIX.test(file)) return undefined; |
| 22 | + owned[owned.length - 1] = file.replace(STORY_SUFFIX, ""); |
| 23 | + return owned; |
| 24 | +} |
| 25 | + |
| 26 | +function objectExpression(expression: ESTree.Expression | null | undefined) { |
| 27 | + let current = expression; |
| 28 | + while ( |
| 29 | + current?.type === "TSSatisfiesExpression" || |
| 30 | + current?.type === "TSAsExpression" || |
| 31 | + current?.type === "TSInstantiationExpression" |
| 32 | + ) { |
| 33 | + current = current.expression; |
| 34 | + } |
| 35 | + return current?.type === "ObjectExpression" ? current : undefined; |
| 36 | +} |
| 37 | + |
| 38 | +export const preferAutoStoryTitle = defineRule({ |
| 39 | + meta: { |
| 40 | + type: "suggestion", |
| 41 | + docs: { |
| 42 | + description: |
| 43 | + "Omit a Storybook meta title when it only restates the title Storybook derives from the story file. Explicit titles are reserved for a reader-facing hierarchy the source path cannot express.", |
| 44 | + }, |
| 45 | + messages: { |
| 46 | + redundant: |
| 47 | + "This title restates `{{automatic}}`, which Storybook already derives from the file path. Delete `title`; keep an explicit title only when it deliberately relocates the component in the reader-facing tree.", |
| 48 | + }, |
| 49 | + }, |
| 50 | + create(context) { |
| 51 | + const automatic = automaticTitle(context.filename, context.cwd); |
| 52 | + if (automatic === undefined) return {}; |
| 53 | + return { |
| 54 | + VariableDeclarator(node) { |
| 55 | + if (node.id.type !== "Identifier" || node.id.name !== "meta") return; |
| 56 | + const meta = objectExpression(node.init); |
| 57 | + if (meta === undefined) return; |
| 58 | + const title = meta.properties.find( |
| 59 | + (property) => property.type === "Property" && propertyName(property) === "title", |
| 60 | + ); |
| 61 | + if ( |
| 62 | + title?.type !== "Property" || |
| 63 | + title.value.type !== "Literal" || |
| 64 | + typeof title.value.value !== "string" |
| 65 | + ) { |
| 66 | + return; |
| 67 | + } |
| 68 | + const declared = title.value.value.split("/"); |
| 69 | + if (normalizedSegments(declared).join("/") !== normalizedSegments(automatic).join("/")) { |
| 70 | + return; |
| 71 | + } |
| 72 | + context.report({ |
| 73 | + node: title, |
| 74 | + messageId: "redundant", |
| 75 | + data: { automatic: automatic.join("/") }, |
| 76 | + }); |
| 77 | + }, |
| 78 | + }; |
| 79 | + }, |
| 80 | +}); |
0 commit comments