Skip to content

Commit

Permalink
feat: use prettier options in loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Feb 25, 2020
1 parent 6a523b1 commit b17ba66
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 54 deletions.
72 changes: 54 additions & 18 deletions core/instrument/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as parser from '@babel/parser';
const mdx = require('@mdx-js/mdx');
import traverse from '@babel/traverse';
import generate from '@babel/generator';
import prettier, { Options } from 'prettier';
import prettier, { Options, ResolveConfigOptions } from 'prettier';
import parserBabel from 'prettier/parser-babylon';
import { StoriesGroup, Story } from '@component-controls/specification';
import { extractCSFStories } from './babel/csf-stories';
Expand All @@ -11,20 +11,36 @@ import { removeMDXAttributes } from './babel/remove-mdx-attributes';

type TraverseFn = (stories: StoriesGroup) => any;

const parseSource = (
export type PrettierOptions = Options & {
resolveConfigOptions?: ResolveConfigOptions;
};
const parseSource = async (
code: string,
traverseFn: TraverseFn,
originalSource: string,
prettierOptions?: Options,
): StoriesGroup => {
const source =
prettierOptions !== null
? prettier.format(code, {
parser: 'typescript',
plugins: [parserBabel],
...prettierOptions,
})
: code;
filePath?: string,
prettierOptions?: PrettierOptions,
): Promise<StoriesGroup> => {
let source: string;
if (prettierOptions !== false) {
const { resolveConfigOptions, ...otherOptions } = prettierOptions || {};
let allPrettierOptions = otherOptions;
if (filePath) {
const userOptions = await prettier.resolveConfig(
filePath,
resolveConfigOptions,
);
allPrettierOptions = { ...userOptions, ...allPrettierOptions };
}

source = prettier.format(code, {
parser: 'typescript',
plugins: [parserBabel],
...allPrettierOptions,
});
} else {
source = code;
}

const ast = parser.parse(source, {
sourceType: 'module',
Expand All @@ -45,7 +61,7 @@ const parseSource = (
if (start.line === end.line) {
story.source = lines[start.line - 1].substring(
start.column,
end.column + 1,
end.column,
);
} else {
const startLine = lines[start.line - 1];
Expand All @@ -54,7 +70,7 @@ const parseSource = (
story.source = [
startLine.substring(start.column),
...lines.slice(start.line, end.line - 1),
endLine.substring(0, end.column + 1),
endLine.substring(0, end.column),
].join('\n');
}
}
Expand All @@ -63,11 +79,25 @@ const parseSource = (
return stories;
};

export const parseCSF = async (source: string): Promise<StoriesGroup> => {
return parseSource(source, extractCSFStories, source);
export const parseCSF = async (
source: string,
filePath?: string,
prettierOptions?: PrettierOptions,
): Promise<StoriesGroup> => {
return await parseSource(
source,
extractCSFStories,
source,
filePath,
prettierOptions,
);
};

export const parseMDX = async (source: string): Promise<StoriesGroup> => {
export const parseMDX = async (
source: string,
filePath?: string,
prettierOptions?: PrettierOptions,
): Promise<StoriesGroup> => {
const code = await mdx(source);

const ast = parser.parse(code, {
Expand All @@ -79,5 +109,11 @@ export const parseMDX = async (source: string): Promise<StoriesGroup> => {
retainFunctionParens: true,
retainLines: true,
});
return parseSource(newCode.code, extractMDXStories, source);
return await parseSource(
newCode.code,
extractMDXStories,
source,
filePath,
prettierOptions,
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export { myStory as exportedStory}
"value": "Custom story name",
},
],
"source": "() => {};",
"source": "() => {}",
},
},
}
Expand Down Expand Up @@ -101,7 +101,7 @@ export { myStory }
"value": "Custom story name",
},
],
"source": "() => {};",
"source": "() => {}",
},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ myStory.story = {
"value": "Custom story name",
},
],
"source": "() => {};",
"source": "() => {}",
},
},
}
Expand Down Expand Up @@ -148,7 +148,7 @@ myStory.story = {
],
},
],
"source": "() => {};",
"source": "() => {}",
},
},
}
Expand Down Expand Up @@ -336,7 +336,7 @@ myStory.story = {
],
},
],
"source": "() => {};",
"source": "() => {}",
},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Object {
},
},
"name": "myStory",
"source": "() => {};",
"source": "() => {}",
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion core/instrument/test/__snapshots__/csf-toggle.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Object {
<br />
</div>
);
};",
}",
},
},
"title": "Components/Toggle",
Expand Down
14 changes: 7 additions & 7 deletions core/instrument/test/__snapshots__/get-csf-stories.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Object {
},
},
"name": "myStory",
"source": "() => {};",
"source": "() => {}",
},
},
}
Expand Down Expand Up @@ -79,7 +79,7 @@ Object {
},
},
"name": "myStory",
"source": "props => {};",
"source": "props => {}",
},
},
}
Expand Down Expand Up @@ -177,7 +177,7 @@ Object {
},
},
"name": "myStory",
"source": "({ name: { first, last }, age }) => {};",
"source": "({ name: { first, last }, age }) => {}",
},
},
}
Expand Down Expand Up @@ -231,7 +231,7 @@ Object {
},
},
"name": "myStory",
"source": "(props, context) => {};",
"source": "(props, context) => {}",
},
},
}
Expand Down Expand Up @@ -300,7 +300,7 @@ Object {
},
},
"name": "myStory",
"source": "({ name: MyNam, age }) => {};",
"source": "({ name: MyNam, age }) => {}",
},
},
}
Expand Down Expand Up @@ -369,7 +369,7 @@ Object {
},
},
"name": "myStory",
"source": "({ name, age }) => {};",
"source": "({ name, age }) => {}",
},
},
}
Expand Down Expand Up @@ -409,7 +409,7 @@ Object {
},
},
"name": "myStory",
"source": "(props: Properties) => {};",
"source": "(props: Properties) => {}",
},
},
}
Expand Down
10 changes: 5 additions & 5 deletions core/instrument/test/__snapshots__/mdx-story.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import Button from '../components/BaseButton';
},
},
"name": "story-with-props",
"source": "props => <Button />}",
"source": "props => <Button />",
},
},
"title": "Storybook/MDX",
Expand Down Expand Up @@ -187,7 +187,7 @@ import Button from '../components/BaseButton';
name: { first, last },
age
}) => <Button />}",
}) => <Button />",
},
},
"title": "Storybook/MDX",
Expand Down Expand Up @@ -267,7 +267,7 @@ import Button from '../components/BaseButton';
},
},
"name": "story-with-props",
"source": "(props, context) => <Button />}",
"source": "(props, context) => <Button />",
},
},
"title": "Storybook/MDX",
Expand Down Expand Up @@ -362,7 +362,7 @@ import Button from '../components/BaseButton';
},
},
"name": "story-with-props",
"source": "({ name: MyName, age }) => <Button />}",
"source": "({ name: MyName, age }) => <Button />",
},
},
"title": "Storybook/MDX",
Expand Down Expand Up @@ -457,7 +457,7 @@ import Button from '../components/BaseButton';
},
},
"name": "story-with-props",
"source": "({ name, age }) => <Button />}",
"source": "({ name, age }) => <Button />",
},
},
"title": "Storybook/MDX",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ Object {
<Story first={first} last={last} />
<Age value={age} />
</>
);",
)",
},
},
}
Expand Down Expand Up @@ -223,7 +223,7 @@ Object {
},
},
"name": "myStory",
"source": "props => <Story {...props} />;",
"source": "props => <Story {...props} />",
},
},
"title": undefined,
Expand Down Expand Up @@ -291,7 +291,7 @@ Object {
},
},
"name": "story",
"source": "({ name }) => name;",
"source": "({ name }) => name",
},
},
}
Expand Down Expand Up @@ -373,7 +373,7 @@ Object {
"name": "selectProp",
"source": "({ value }) => (
<div>{JSON.stringify({ value }, null, 2)}</div>
);",
)",
},
},
}
Expand Down Expand Up @@ -464,7 +464,7 @@ Object {
<Story name={name} age={age} />
<Age value={age} />
</>
);",
)",
},
},
}
Expand Down Expand Up @@ -635,7 +635,7 @@ export const story = ({ height, weight, style: { border, color } }) => (
"name": "story",
"source": "({ height, weight, style: { border, color } }) => (
<div height={height} weight={weight} border={border} color={color} />
);",
)",
},
},
}
Expand Down Expand Up @@ -689,7 +689,7 @@ Object {
},
},
"name": "myStory",
"source": "props => <Story {...props} />;",
"source": "props => <Story {...props} />",
},
},
}
Expand Down Expand Up @@ -756,7 +756,7 @@ Object {
},
},
"name": "myStory",
"source": "({ text }) => \`\${text}\`;",
"source": "({ text }) => \`\${text}\`",
},
},
}
Expand Down Expand Up @@ -824,7 +824,7 @@ Object {
},
},
"name": "myStory",
"source": "(props, context) => <Story {...props} />;",
"source": "(props, context) => <Story {...props} />",
},
},
}
Expand Down Expand Up @@ -923,7 +923,7 @@ Object {
"name": "myStory",
"source": "({ name: MyName, age }) => (
<Story name={MyName} age={age} />
);",
)",
},
},
}
Expand Down Expand Up @@ -1020,7 +1020,7 @@ Object {
},
},
"name": "myStory",
"source": "({ name, age }) => <Story name={name} age={age} />;",
"source": "({ name, age }) => <Story name={name} age={age} />",
},
},
}
Expand Down
Loading

0 comments on commit b17ba66

Please sign in to comment.