Skip to content

Commit d512563

Browse files
committedJan 1, 2020
Add prettier && force snapshot tests to use posix paths
1 parent dc8d9f2 commit d512563

File tree

4 files changed

+68
-73
lines changed

4 files changed

+68
-73
lines changed
 

‎.prettierrc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"printWidth": 100,
3+
"tabWidth": 2,
4+
"bracketSpacing": true,
5+
"trailingComma": "es5",
6+
"singleQuote": true
7+
}

‎src/index.js

+34-56
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as _ from 'lodash';
33
import * as ReactDocgen from 'react-docgen';
44
import * as reactDocgenHandlers from 'react-docgen/dist/handlers';
55
import actualNameHandler from './actualNameHandler';
6+
import { relativePath } from './relativePath';
67

78
const defaultHandlers = Object.values(reactDocgenHandlers).map(handler => handler);
89

@@ -12,8 +13,8 @@ export default function({ types: t }) {
1213
Program: {
1314
exit(path, state) {
1415
injectReactDocgenInfo(path, state, this.file.code, t);
15-
}
16-
}
16+
},
17+
},
1718
},
1819
};
1920
}
@@ -28,22 +29,22 @@ function injectReactDocgenInfo(path, state, code, t) {
2829
resolver = ReactDocgen.resolver[state.opts.resolver];
2930
}
3031

31-
let customHandlers = []
32+
let customHandlers = [];
3233
if (state.opts.handlers) {
3334
state.opts.handlers.forEach(handler => {
34-
customHandlers.push(require(handler))
35-
})
35+
customHandlers.push(require(handler));
36+
});
3637
}
3738

38-
const handlers = [...defaultHandlers, ...customHandlers, actualNameHandler]
39+
const handlers = [...defaultHandlers, ...customHandlers, actualNameHandler];
3940
docgenResults = ReactDocgen.parse(code, resolver, handlers);
4041

4142
if (state.opts.removeMethods) {
4243
docgenResults.forEach(function(docgenResult) {
4344
delete docgenResult.methods;
44-
})
45+
});
4546
}
46-
} catch(e) {
47+
} catch (e) {
4748
// this is for debugging the error only, do not ship this console log or else it pollutes the webpack output
4849
// console.log(e);
4950
return;
@@ -61,12 +62,13 @@ function injectReactDocgenInfo(path, state, code, t) {
6162
const docNode = buildObjectExpression(docgenResult, t);
6263
const docgenInfo = t.expressionStatement(
6364
t.assignmentExpression(
64-
"=",
65+
'=',
6566
t.memberExpression(t.identifier(exportName), t.identifier('__docgenInfo')),
6667
docNode
67-
));
68+
)
69+
);
6870

69-
const exportPath = program.get('body').find((node) => isExportCurrent(node, exportName, t));
71+
const exportPath = program.get('body').find(node => isExportCurrent(node, exportName, t));
7072

7173
if (exportPath) {
7274
exportPath.insertBefore(docgenInfo);
@@ -81,80 +83,60 @@ function injectReactDocgenInfo(path, state, code, t) {
8183
function injectDocgenGlobal(className, path, state, t) {
8284
const program = path.scope.getProgramParent().path;
8385

84-
if(!state.opts.DOC_GEN_COLLECTION_NAME) {
86+
if (!state.opts.DOC_GEN_COLLECTION_NAME) {
8587
return;
8688
}
8789

8890
const globalName = state.opts.DOC_GEN_COLLECTION_NAME;
89-
const filePath = Path.relative('./', Path.resolve('./', path.hub.file.opts.filename));
91+
const filePath = relativePath(path.hub.file.opts.filename);
9092
const globalNode = t.ifStatement(
9193
t.binaryExpression(
9294
'!==',
93-
t.unaryExpression(
94-
'typeof',
95-
t.identifier(globalName)
96-
),
95+
t.unaryExpression('typeof', t.identifier(globalName)),
9796
t.stringLiteral('undefined')
9897
),
9998
t.blockStatement([
10099
t.expressionStatement(
101100
t.assignmentExpression(
102101
'=',
103-
t.memberExpression(
104-
t.identifier(globalName),
105-
t.stringLiteral(filePath),
106-
true
107-
),
102+
t.memberExpression(t.identifier(globalName), t.stringLiteral(filePath), true),
108103
t.objectExpression([
109-
t.objectProperty(
110-
t.identifier('name'),
111-
t.stringLiteral(className)
112-
),
104+
t.objectProperty(t.identifier('name'), t.stringLiteral(className)),
113105
t.objectProperty(
114106
t.identifier('docgenInfo'),
115-
t.memberExpression(
116-
t.identifier(className),
117-
t.identifier('__docgenInfo')
118-
)
107+
t.memberExpression(t.identifier(className), t.identifier('__docgenInfo'))
119108
),
120-
t.objectProperty(
121-
t.identifier('path'),
122-
t.stringLiteral(filePath)
123-
)
109+
t.objectProperty(t.identifier('path'), t.stringLiteral(filePath)),
124110
])
125111
)
126-
)
112+
),
127113
])
128114
);
129115
program.pushContainer('body', globalNode);
130116
}
131117

132-
function buildObjectExpression(obj, t){
133-
if(_.isPlainObject(obj)) {
118+
function buildObjectExpression(obj, t) {
119+
if (_.isPlainObject(obj)) {
134120
const children = [];
135121
for (let key in obj) {
136122
if (key === 'actualName') continue;
137-
if(!obj.hasOwnProperty(key) || _.isUndefined(obj[key])) continue;
138-
children.push(
139-
t.objectProperty(
140-
t.stringLiteral(key),
141-
buildObjectExpression(obj[key], t)
142-
));
123+
if (!obj.hasOwnProperty(key) || _.isUndefined(obj[key])) continue;
124+
children.push(t.objectProperty(t.stringLiteral(key), buildObjectExpression(obj[key], t)));
143125
}
144126
return t.objectExpression(children);
145127
} else if (_.isString(obj)) {
146128
return t.stringLiteral(obj);
147129
} else if (_.isBoolean(obj)) {
148130
return t.booleanLiteral(obj);
149-
} else if (_.isNumber(obj)){
131+
} else if (_.isNumber(obj)) {
150132
return t.numericLiteral(obj);
151133
} else if (_.isArray(obj)) {
152134
const children = [];
153-
obj.forEach(function (val) {
135+
obj.forEach(function(val) {
154136
children.push(buildObjectExpression(val, t));
155137
});
156138
return t.ArrayExpression(children);
157-
} else if(_.isNull(obj)) {
139+
} else if (_.isNull(obj)) {
158140
return t.nullLiteral();
159141
}
160142
}
@@ -170,22 +152,18 @@ function isExportCurrent(path, exportName, t) {
170152
if (t.isExportDefaultDeclaration(path)) {
171153
const decl = path.get('declaration');
172154

173-
const identifier = (
174-
decl.isIdentifier() // export default MyComp
175-
? decl.node.name
176-
: getComponentFromHoC(decl) // export default withHoC(MyComp)
177-
);
155+
const identifier = decl.isIdentifier() // export default MyComp
156+
? decl.node.name
157+
: getComponentFromHoC(decl); // export default withHoC(MyComp)
178158

179159
if (identifier === exportName) {
180-
return true
160+
return true;
181161
}
182162
}
183163

184164
if (t.isExportNamedDeclaration(path)) {
185-
return path.get('specifiers').find((sp) => (
186-
sp.node.exported.name === exportName
187-
))
165+
return path.get('specifiers').find(sp => sp.node.exported.name === exportName);
188166
}
189167

190-
return false
168+
return false;
191169
}

‎src/relativePath.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as Path from 'path';
2+
3+
export const relativePath = filename => Path.relative('./', Path.resolve('./', filename));

‎test/index.test.js

+24-17
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
1-
import "jest-specific-snapshot";
2-
import path from "path";
3-
import fs from "fs";
4-
import { transformFileSync } from "@babel/core";
5-
import plugin from "../src";
1+
import 'jest-specific-snapshot';
2+
import path from 'path';
3+
import fs from 'fs';
4+
import { transformFileSync } from '@babel/core';
5+
import plugin from '../src';
66

7-
describe("Add propType doc to react components", () => {
8-
const fixturesDir = path.join(__dirname, "fixtures");
7+
jest.mock('../src/relativePath', () => ({
8+
relativePath: filename => {
9+
const { posix } = require('path');
10+
return posix.relative('./', posix.resolve('./', filename));
11+
},
12+
}));
13+
14+
describe('Add propType doc to react components', () => {
15+
const fixturesDir = path.join(__dirname, 'fixtures');
916
fs.readdirSync(fixturesDir).map(caseName => {
1017
// Ignore macOS directory files
11-
if (caseName.indexOf(".DS_Store") < 0) {
12-
it(`should ${caseName.split("-").join(" ")}`, () => {
18+
if (caseName.indexOf('.DS_Store') < 0) {
19+
it(`should ${caseName.split('-').join(' ')}`, () => {
1320
const fixtureDir = path.join(fixturesDir, caseName);
14-
const inputPath = path.join(fixtureDir, "input.js");
15-
const outputPath = path.join(fixtureDir, "output.js");
21+
const inputPath = path.join(fixtureDir, 'input.js');
22+
const outputPath = path.join(fixtureDir, 'output.js');
1623
const options = {
17-
presets: ["@babel/env", "@babel/flow", "@babel/react"],
24+
presets: ['@babel/env', '@babel/flow', '@babel/react'],
1825
plugins: [
1926
[
2027
plugin,
2128
{
22-
DOC_GEN_COLLECTION_NAME: "STORYBOOK_REACT_CLASSES",
23-
handlers: ["react-docgen-deprecation-handler"]
24-
}
29+
DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES',
30+
handlers: ['react-docgen-deprecation-handler'],
31+
},
2532
],
26-
"@babel/plugin-proposal-class-properties"
33+
'@babel/plugin-proposal-class-properties',
2734
],
28-
babelrc: false
35+
babelrc: false,
2936
};
3037

3138
const output = transformFileSync(inputPath, options).code;

0 commit comments

Comments
 (0)
Please sign in to comment.