Skip to content

Commit

Permalink
Merge pull request #12225 from phated/extract-source
Browse files Browse the repository at this point in the history
Addon-docs: Reuse extractSource from source-loader
  • Loading branch information
shilman committed Sep 4, 2020
2 parents 8a52700 + 0407776 commit 194c07d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 43 deletions.
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

0 comments on commit 194c07d

Please sign in to comment.