mirror of
https://github.com/storybookjs/storybook.git
synced 2025-03-19 05:02:40 +08:00
storysource: pass file path to prettier config / support to parser changes at Prettier v0.13.0 #3657
This commit is contained in:
parent
8ec5a40ec8
commit
86a4cb1f41
@ -27,14 +27,21 @@ function generateSourceWithoutUglyComments(source, { comments, uglyCommentsRegex
|
||||
return parts.join('');
|
||||
}
|
||||
|
||||
function prettifyCode(source, { prettierConfig, parser }) {
|
||||
function prettifyCode(source, { prettierConfig, parser, filepath }) {
|
||||
let config = prettierConfig;
|
||||
|
||||
if (!config.parser && parser && parser !== 'javascript') {
|
||||
config = {
|
||||
...prettierConfig,
|
||||
parser,
|
||||
};
|
||||
if (!config.parser) {
|
||||
if (parser) {
|
||||
config = {
|
||||
...prettierConfig,
|
||||
parser: parser === 'javascript' ? 'babylon' : parser,
|
||||
};
|
||||
} else if (filepath) {
|
||||
config = {
|
||||
...prettierConfig,
|
||||
filepath,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return prettier.format(source, config);
|
||||
|
@ -5,7 +5,7 @@ const ADD_DECORATOR_STATEMENT = '.addDecorator(withStorySource(__STORY__, __ADDS
|
||||
|
||||
function transform(source) {
|
||||
const options = getOptions(this) || {};
|
||||
const result = injectDecorator(source, ADD_DECORATOR_STATEMENT, options);
|
||||
const result = injectDecorator(source, ADD_DECORATOR_STATEMENT, this.resourcePath, options);
|
||||
|
||||
if (!result.changed) {
|
||||
return source;
|
||||
|
@ -6,16 +6,17 @@ import {
|
||||
generateAddsMap,
|
||||
} from './generate-helpers';
|
||||
|
||||
function extendOptions(source, comments, options) {
|
||||
function extendOptions(source, comments, filepath, options) {
|
||||
return {
|
||||
...defaultOptions,
|
||||
...options,
|
||||
source,
|
||||
comments,
|
||||
filepath,
|
||||
};
|
||||
}
|
||||
|
||||
function inject(source, decorator, options = {}) {
|
||||
function inject(source, decorator, filepath, options = {}) {
|
||||
const { changed, source: newSource, comments } = generateSourceWithDecorators(
|
||||
source,
|
||||
decorator,
|
||||
@ -30,7 +31,7 @@ function inject(source, decorator, options = {}) {
|
||||
};
|
||||
}
|
||||
|
||||
const storySource = generateStorySource(extendOptions(source, comments, options));
|
||||
const storySource = generateStorySource(extendOptions(source, comments, filepath, options));
|
||||
const addsMap = generateAddsMap(storySource, options.parser);
|
||||
|
||||
return {
|
||||
|
@ -1,12 +1,19 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import injectDecorator from './inject-decorator';
|
||||
|
||||
const ADD_DECORATOR_STATEMENT = '.addDecorator(withStorySource(__STORY__, __ADDS_MAP__))';
|
||||
|
||||
describe('inject-decorator', () => {
|
||||
describe('positive', () => {
|
||||
const source = fs.readFileSync('./__mocks__/inject-decorator.stories.txt', 'utf-8');
|
||||
const result = injectDecorator(source, ADD_DECORATOR_STATEMENT);
|
||||
const mockFilePath = './__mocks__/inject-decorator.stories.txt';
|
||||
const source = fs.readFileSync(mockFilePath, 'utf-8');
|
||||
const result = injectDecorator(
|
||||
source,
|
||||
ADD_DECORATOR_STATEMENT,
|
||||
path.resolve(__dirname, mockFilePath),
|
||||
{ parser: 'javascript' }
|
||||
);
|
||||
|
||||
it('returns "changed" flag', () => {
|
||||
expect(result.changed).toBeTruthy();
|
||||
@ -22,8 +29,14 @@ describe('inject-decorator', () => {
|
||||
});
|
||||
|
||||
describe('positive - angular', () => {
|
||||
const source = fs.readFileSync('./__mocks__/inject-decorator.angular-stories.txt', 'utf-8');
|
||||
const result = injectDecorator(source, ADD_DECORATOR_STATEMENT, { parser: 'typescript' });
|
||||
const mockFilePath = './__mocks__/inject-decorator.angular-stories.txt';
|
||||
const source = fs.readFileSync(mockFilePath, 'utf-8');
|
||||
const result = injectDecorator(
|
||||
source,
|
||||
ADD_DECORATOR_STATEMENT,
|
||||
path.resolve(__dirname, mockFilePath),
|
||||
{ parser: 'typescript' }
|
||||
);
|
||||
|
||||
it('returns "changed" flag', () => {
|
||||
expect(result.changed).toBeTruthy();
|
||||
@ -39,8 +52,14 @@ describe('inject-decorator', () => {
|
||||
});
|
||||
|
||||
describe('positive - ts', () => {
|
||||
const source = fs.readFileSync('./__mocks__/inject-decorator.ts.txt', 'utf-8');
|
||||
const result = injectDecorator(source, ADD_DECORATOR_STATEMENT, { parser: 'typescript' });
|
||||
const mockFilePath = './__mocks__/inject-decorator.ts.txt';
|
||||
const source = fs.readFileSync(mockFilePath, 'utf-8');
|
||||
const result = injectDecorator(
|
||||
source,
|
||||
ADD_DECORATOR_STATEMENT,
|
||||
path.resolve(__dirname, mockFilePath),
|
||||
{ parser: 'typescript' }
|
||||
);
|
||||
|
||||
it('returns "changed" flag', () => {
|
||||
expect(result.changed).toBeTruthy();
|
||||
@ -56,11 +75,14 @@ describe('inject-decorator', () => {
|
||||
});
|
||||
|
||||
describe('stories with ugly comments', () => {
|
||||
const source = fs.readFileSync(
|
||||
'./__mocks__/inject-decorator.ugly-comments-stories.txt',
|
||||
'utf-8'
|
||||
const mockFilePath = './__mocks__/inject-decorator.ugly-comments-stories.txt';
|
||||
const source = fs.readFileSync(mockFilePath, 'utf-8');
|
||||
const result = injectDecorator(
|
||||
source,
|
||||
ADD_DECORATOR_STATEMENT,
|
||||
path.resolve(__dirname, mockFilePath),
|
||||
{ parser: 'javascript' }
|
||||
);
|
||||
const result = injectDecorator(source, ADD_DECORATOR_STATEMENT);
|
||||
|
||||
it('should delete ugly comments from the generated story source', () => {
|
||||
expect(result.storySource).toMatchSnapshot();
|
||||
@ -68,11 +90,14 @@ describe('inject-decorator', () => {
|
||||
});
|
||||
|
||||
describe('stories with ugly comments in ts', () => {
|
||||
const source = fs.readFileSync(
|
||||
'./__mocks__/inject-decorator.ts.ugly-comments-stories.txt',
|
||||
'utf-8'
|
||||
const mockFilePath = './__mocks__/inject-decorator.ts.ugly-comments-stories.txt';
|
||||
const source = fs.readFileSync(mockFilePath, 'utf-8');
|
||||
const result = injectDecorator(
|
||||
source,
|
||||
ADD_DECORATOR_STATEMENT,
|
||||
path.resolve(__dirname, mockFilePath),
|
||||
{ parser: 'typescript' }
|
||||
);
|
||||
const result = injectDecorator(source, ADD_DECORATOR_STATEMENT, { parser: 'typescript' });
|
||||
|
||||
it('should delete ugly comments from the generated story source', () => {
|
||||
expect(result.storySource).toMatchSnapshot();
|
||||
@ -80,9 +105,14 @@ describe('inject-decorator', () => {
|
||||
});
|
||||
|
||||
it('will not change the source when there are no "storiesOf" functions', () => {
|
||||
const source = fs.readFileSync('./__mocks__/inject-decorator.no-stories.txt', 'utf-8');
|
||||
const mockFilePath = './__mocks__/inject-decorator.no-stories.txt';
|
||||
const source = fs.readFileSync(mockFilePath, 'utf-8');
|
||||
|
||||
const result = injectDecorator(source, ADD_DECORATOR_STATEMENT);
|
||||
const result = injectDecorator(
|
||||
source,
|
||||
ADD_DECORATOR_STATEMENT,
|
||||
path.resolve(__dirname, mockFilePath)
|
||||
);
|
||||
|
||||
expect(result.changed).toBeFalsy();
|
||||
expect(result.addsMap).toEqual({});
|
||||
|
@ -1,7 +1,7 @@
|
||||
import parseJs from 'prettier/parser-babylon';
|
||||
|
||||
function parse(source) {
|
||||
return parseJs(source);
|
||||
return parseJs.parsers.babylon.parse(source);
|
||||
}
|
||||
|
||||
export default {
|
||||
|
@ -1,7 +1,7 @@
|
||||
import parseTs from 'prettier/parser-typescript';
|
||||
|
||||
function parse(source) {
|
||||
return parseTs(source);
|
||||
return parseTs.parsers.typescript.parse(source);
|
||||
}
|
||||
|
||||
export default {
|
||||
|
@ -13344,7 +13344,11 @@ preserve@^0.2.0:
|
||||
version "0.2.0"
|
||||
resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"
|
||||
|
||||
prettier@^1.12.1, prettier@^1.5.2:
|
||||
prettier@^1.12.1:
|
||||
version "1.13.2"
|
||||
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.13.2.tgz#412b87bc561cb11074d2877a33a38f78c2303cda"
|
||||
|
||||
prettier@^1.5.2:
|
||||
version "1.12.1"
|
||||
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.12.1.tgz#c1ad20e803e7749faf905a409d2367e06bbe7325"
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user