Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addon-docs: Reuse extractSource from source-loader #12225

Merged
merged 2 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 4 additions & 22 deletions addons/docs/src/blocks/enhanceSource.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { combineParameters } from '@storybook/client-api';
import { StoryContext, Parameters } from '@storybook/addons';

interface Location {
line: number;
col: number;
}
import { extractSource, LocationsMap } from '@storybook/source-loader';

interface StorySource {
source: string;
locationsMap: { [id: string]: { startBody: Location; endBody: Location } };
locationsMap: LocationsMap;
}

/**
Expand All @@ -24,23 +20,9 @@ const extract = (targetId: string, { source, locationsMap }: StorySource) => {

const sanitizedStoryName = storyIdToSanitizedStoryName(targetId);
const location = locationsMap[sanitizedStoryName];

const { startBody: start, endBody: end } = location;
const lines = source.split('\n');
if (start.line === end.line && lines[start.line - 1] !== undefined) {
return lines[start.line - 1].substring(start.col, end.col);
}
// NOTE: storysource locations are 1-based not 0-based!
const startLine = lines[start.line - 1];
const endLine = lines[end.line - 1];
if (startLine === undefined || endLine === undefined) {
return source;
}
return [
startLine.substring(start.col),
...lines.slice(start.line, end.line - 1),
endLine.substring(0, end.col),
].join('\n');

return extractSource(location, lines);
};

export const enhanceSource = (context: StoryContext): Parameters => {
Expand Down
22 changes: 1 addition & 21 deletions lib/source-loader/src/abstract-syntax-tree/generate-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
popParametersObjectFromDefaultExport,
findExportsMap as generateExportsMap,
} from './traverse-helpers';
import { extractSource } from '../extract-source';

export function sanitizeSource(source) {
return JSON.stringify(source)
Expand Down Expand Up @@ -176,27 +177,6 @@ export function generateSourcesInExportedParameters(source, ast, additionalParam
return source;
}

/**
* given a location, extract the text from the full source
*/
function extractSource(location, lines) {
const { startBody: start, endBody: end } = location;
if (start.line === end.line && lines[start.line - 1] !== undefined) {
return lines[start.line - 1].substring(start.col, end.col);
}
// NOTE: storysource locations are 1-based not 0-based!
const startLine = lines[start.line - 1];
const endLine = lines[end.line - 1];
if (startLine === undefined || endLine === undefined) {
return null;
}
return [
startLine.substring(start.col),
...lines.slice(start.line, end.line - 1),
endLine.substring(0, end.col),
].join('\n');
}

function addStorySourceParameter(key, snippet) {
const source = sanitizeSource(snippet);
return `${key}.parameters = { storySource: { source: ${source} }, ...${key}.parameters };`;
Expand Down
22 changes: 22 additions & 0 deletions lib/source-loader/src/extract-source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { SourceBlock } from './types';

/**
* given a location, extract the text from the full source
*/
export function extractSource(location: SourceBlock, lines: string[]): string | null {
const { startBody: start, endBody: end } = location;
if (start.line === end.line && lines[start.line - 1] !== undefined) {
return lines[start.line - 1].substring(start.col, end.col);
}
// NOTE: storysource locations are 1-based not 0-based!
const startLine = lines[start.line - 1];
const endLine = lines[end.line - 1];
if (startLine === undefined || endLine === undefined) {
return null;
}
return [
startLine.substring(start.col),
...lines.slice(start.line, end.line - 1),
endLine.substring(0, end.col),
].join('\n');
}
2 changes: 2 additions & 0 deletions lib/source-loader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ import { transform } from './build';
export default transform;

export * from './types';

export { extractSource } from './extract-source';
2 changes: 2 additions & 0 deletions lib/source-loader/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export interface SourceLoc {
}

export interface SourceBlock {
startBody: SourceLoc;
endBody: SourceLoc;
startLoc: SourceLoc;
endLoc: SourceLoc;
}
Expand Down