Skip to content

Commit 5513a3e

Browse files
Merge branch 'master' into fix/fix-webpack-4-hmr
2 parents 543e905 + ac554fd commit 5513a3e

9 files changed

Lines changed: 192 additions & 87 deletions

File tree

.all-contributorsrc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,25 @@
5353
"test",
5454
"code"
5555
]
56+
},
57+
{
58+
"login": "zhzz",
59+
"name": "Egor Pogadaev",
60+
"avatar_url": "https://avatars.githubusercontent.com/u/4607770?v=4",
61+
"profile": "https://github.com/zhzz",
62+
"contributions": [
63+
"test",
64+
"code"
65+
]
66+
},
67+
{
68+
"login": "sairus2k",
69+
"name": "Anton Savoskin",
70+
"avatar_url": "https://avatars.githubusercontent.com/u/1106887?v=4",
71+
"profile": "https://github.com/sairus2k",
72+
"contributions": [
73+
"code"
74+
]
5675
}
5776
],
5877
"contributorsPerLine": 7

CHANGELOG.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
# v1.0.2 (Mon Oct 17 2022)
2+
3+
:tada: This release contains work from a new contributor! :tada:
4+
5+
Thank you, Anton Savoskin ([@sairus2k](https://github.com/sairus2k)), for all your work!
6+
7+
#### 🐛 Bug Fix
8+
9+
- Update typescript and fix deprecation warnings [#70](https://github.com/hipstersmoothie/react-docgen-typescript-plugin/pull/70) (a.savoskin@kvarta.ru)
10+
11+
#### Authors: 1
12+
13+
- Anton Savoskin ([@sairus2k](https://github.com/sairus2k))
14+
15+
---
16+
17+
# v1.0.1 (Mon Mar 21 2022)
18+
19+
#### 🐛 Bug Fix
20+
21+
- fix: respect falsy custom options [#49](https://github.com/hipstersmoothie/react-docgen-typescript-plugin/pull/49) ([@zhzz](https://github.com/zhzz))
22+
23+
#### Authors: 1
24+
25+
- Egor Pogadaev ([@zhzz](https://github.com/zhzz))
26+
27+
---
28+
129
# v1.0.0 (Sat Jun 05 2021)
230

331
#### 💥 Breaking Change

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
8282
<td align="center"><a href="http://shilman.net/"><img src="https://avatars2.githubusercontent.com/u/488689?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Michael Shilman</b></sub></a><br /><a href="https://github.com/hipstersmoothie/react-docgen-typescript-plugin/commits?author=shilman" title="Code">💻</a></td>
8383
<td align="center"><a href="https://github.com/kherock"><img src="https://avatars.githubusercontent.com/u/4993980?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Kyle Herock</b></sub></a><br /><a href="https://github.com/hipstersmoothie/react-docgen-typescript-plugin/commits?author=kherock" title="Code">💻</a></td>
8484
<td align="center"><a href="https://survivejs.com/"><img src="https://avatars.githubusercontent.com/u/166921?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Juho Vepsäläinen</b></sub></a><br /><a href="#infra-bebraw" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/hipstersmoothie/react-docgen-typescript-plugin/commits?author=bebraw" title="Tests">⚠️</a> <a href="https://github.com/hipstersmoothie/react-docgen-typescript-plugin/commits?author=bebraw" title="Code">💻</a></td>
85+
<td align="center"><a href="https://github.com/zhzz"><img src="https://avatars.githubusercontent.com/u/4607770?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Egor Pogadaev</b></sub></a><br /><a href="https://github.com/hipstersmoothie/react-docgen-typescript-plugin/commits?author=zhzz" title="Tests">⚠️</a> <a href="https://github.com/hipstersmoothie/react-docgen-typescript-plugin/commits?author=zhzz" title="Code">💻</a></td>
86+
<td align="center"><a href="https://github.com/sairus2k"><img src="https://avatars.githubusercontent.com/u/1106887?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Anton Savoskin</b></sub></a><br /><a href="https://github.com/hipstersmoothie/react-docgen-typescript-plugin/commits?author=sairus2k" title="Code">💻</a></td>
8587
</tr>
8688
</table>
8789

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-docgen-typescript-plugin",
3-
"version": "1.0.0",
3+
"version": "1.0.2",
44
"description": "A webpack plugin to inject react typescript docgen information.",
55
"license": "MIT",
66
"repository": "hipstersmoothie/react-docgen-typescript-plugin",
@@ -38,7 +38,7 @@
3838
"find-cache-dir": "^3.3.1",
3939
"flat-cache": "^3.0.4",
4040
"micromatch": "^4.0.2",
41-
"react-docgen-typescript": "^1.22.0",
41+
"react-docgen-typescript": "^2.2.2",
4242
"tslib": "^2.0.0"
4343
},
4444
"devDependencies": {
@@ -69,13 +69,13 @@
6969
"react": "^17.0.1",
7070
"ts-jest": "^26.5.6",
7171
"ts-loader": "^9.1.2",
72-
"typescript": "3.8.3",
72+
"typescript": "4.8.3",
7373
"webpack": "^5.36.2",
7474
"webpack-cli": "^4.7.0",
7575
"yarn-add-no-save": "^1.0.3"
7676
},
7777
"peerDependencies": {
78-
"typescript": ">= 3.x",
78+
"typescript": ">= 4.x",
7979
"webpack": ">= 4"
8080
},
8181
"lint-staged": {

src/__tests__/plugin.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import webpack, { Configuration } from "webpack";
22
import { createFsFromVolume, IFs, Volume } from "memfs";
33
import ReactDocgenTypeScriptPlugin from "..";
4+
import { LoaderOptions } from "../types";
45

56
// eslint-disable-next-line
67
const joinPath = require("memory-fs/lib/join");
@@ -84,3 +85,34 @@ test("default options", async () => {
8485

8586
expect(result).toContain("STORYBOOK_REACT_CLASSES");
8687
});
88+
89+
describe("custom options", () => {
90+
describe("loader options", () => {
91+
const options: Record<
92+
keyof LoaderOptions,
93+
Array<LoaderOptions[keyof LoaderOptions]>
94+
> = {
95+
setDisplayName: [true, false, undefined],
96+
typePropName: ["customValue", undefined],
97+
docgenCollectionName: ["customValue", null, undefined],
98+
};
99+
const { defaultOptions } = ReactDocgenTypeScriptPlugin;
100+
101+
(Object.keys(options) as Array<keyof LoaderOptions>).forEach(
102+
(optionName) => {
103+
const values = options[optionName];
104+
105+
test.each(values)(`${optionName}: %p`, (value) => {
106+
const plugin = new ReactDocgenTypeScriptPlugin({
107+
[optionName]: value,
108+
});
109+
const { generateOptions: resultOptions } = plugin.getOptions();
110+
111+
expect(resultOptions[optionName]).toBe(
112+
value === undefined ? defaultOptions[optionName] : value
113+
);
114+
});
115+
}
116+
);
117+
});
118+
});

src/dependency.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class DocGenTemplate extends NullDependency.Template
6969

7070
// eslint-disable-next-line
7171
// @ts-ignore TODO: How to type this correctly?
72+
// @ts-expect-error TODO: How to type this correctly?
7273
DocGenDependency.Template = DocGenTemplate;
7374

7475
// Default imports are tricky with CommonJS

src/generateDocgenCodeBlock.ts

Lines changed: 85 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ function insertTsIgnoreBeforeStatement(statement: ts.Statement): ts.Statement {
4444
*/
4545
function setDisplayName(d: ComponentDoc): ts.Statement {
4646
return insertTsIgnoreBeforeStatement(
47-
ts.createExpressionStatement(
48-
ts.createBinary(
49-
ts.createPropertyAccess(
50-
ts.createIdentifier(d.displayName),
51-
ts.createIdentifier("displayName")
47+
ts.factory.createExpressionStatement(
48+
ts.factory.createBinaryExpression(
49+
ts.factory.createPropertyAccessExpression(
50+
ts.factory.createIdentifier(d.displayName),
51+
ts.factory.createIdentifier("displayName")
5252
),
5353
ts.SyntaxKind.EqualsToken,
54-
ts.createLiteral(d.displayName)
54+
ts.factory.createStringLiteral(d.displayName)
5555
)
5656
)
5757
);
@@ -93,8 +93,8 @@ function createPropDefinition(
9393
const setDefaultValue = (
9494
defaultValue: { value: string | number | boolean } | null
9595
) =>
96-
ts.createPropertyAssignment(
97-
ts.createLiteral("defaultValue"),
96+
ts.factory.createPropertyAssignment(
97+
ts.factory.createStringLiteral("defaultValue"),
9898
// Use a more extensive check on defaultValue. Sometimes the parser
9999
// returns an empty object.
100100
defaultValue !== null &&
@@ -104,20 +104,28 @@ function createPropDefinition(
104104
(typeof defaultValue.value === "string" ||
105105
typeof defaultValue.value === "number" ||
106106
typeof defaultValue.value === "boolean")
107-
? ts.createObjectLiteral([
108-
ts.createPropertyAssignment(
109-
ts.createIdentifier("value"),
110-
ts.createLiteral(defaultValue.value)
107+
? ts.factory.createObjectLiteralExpression([
108+
ts.factory.createPropertyAssignment(
109+
ts.factory.createIdentifier("value"),
110+
// eslint-disable-next-line no-nested-ternary
111+
typeof defaultValue.value === "string"
112+
? ts.factory.createStringLiteral(defaultValue.value)
113+
: // eslint-disable-next-line no-nested-ternary
114+
typeof defaultValue.value === "number"
115+
? ts.factory.createNumericLiteral(defaultValue.value)
116+
: defaultValue.value
117+
? ts.factory.createTrue()
118+
: ts.factory.createFalse()
111119
),
112120
])
113-
: ts.createNull()
121+
: ts.factory.createNull()
114122
);
115123

116124
/** Set a property with a string value */
117125
const setStringLiteralField = (fieldName: string, fieldValue: string) =>
118-
ts.createPropertyAssignment(
119-
ts.createLiteral(fieldName),
120-
ts.createLiteral(fieldValue)
126+
ts.factory.createPropertyAssignment(
127+
ts.factory.createStringLiteral(fieldName),
128+
ts.factory.createStringLiteral(fieldValue)
121129
);
122130

123131
/**
@@ -144,9 +152,9 @@ function createPropDefinition(
144152
* @param required Whether prop is required or not.
145153
*/
146154
const setRequired = (required: boolean) =>
147-
ts.createPropertyAssignment(
148-
ts.createLiteral("required"),
149-
required ? ts.createTrue() : ts.createFalse()
155+
ts.factory.createPropertyAssignment(
156+
ts.factory.createStringLiteral("required"),
157+
required ? ts.factory.createTrue() : ts.factory.createFalse()
150158
);
151159

152160
/**
@@ -161,11 +169,11 @@ function createPropDefinition(
161169
const setValue = (typeValue?: any[]) =>
162170
Array.isArray(typeValue) &&
163171
typeValue.every((value) => typeof value.value === "string")
164-
? ts.createPropertyAssignment(
165-
ts.createLiteral("value"),
166-
ts.createArrayLiteral(
172+
? ts.factory.createPropertyAssignment(
173+
ts.factory.createStringLiteral("value"),
174+
ts.factory.createArrayLiteralExpression(
167175
typeValue.map((value) =>
168-
ts.createObjectLiteral([
176+
ts.factory.createObjectLiteralExpression([
169177
setStringLiteralField("value", value.value),
170178
])
171179
)
@@ -188,15 +196,15 @@ function createPropDefinition(
188196
objectFields.push(valueField);
189197
}
190198

191-
return ts.createPropertyAssignment(
192-
ts.createLiteral(options.typePropName),
193-
ts.createObjectLiteral(objectFields)
199+
return ts.factory.createPropertyAssignment(
200+
ts.factory.createStringLiteral(options.typePropName),
201+
ts.factory.createObjectLiteralExpression(objectFields)
194202
);
195203
};
196204

197-
return ts.createPropertyAssignment(
198-
ts.createLiteral(propName),
199-
ts.createObjectLiteral([
205+
return ts.factory.createPropertyAssignment(
206+
ts.factory.createStringLiteral(propName),
207+
ts.factory.createObjectLiteralExpression([
200208
setDefaultValue(prop.defaultValue),
201209
setDescription(prop.description),
202210
setName(prop.name),
@@ -229,35 +237,41 @@ function insertDocgenIntoGlobalCollection(
229237
relativeFilename: string
230238
): ts.Statement {
231239
return insertTsIgnoreBeforeStatement(
232-
ts.createIf(
233-
ts.createBinary(
234-
ts.createTypeOf(ts.createIdentifier(docgenCollectionName)),
240+
ts.factory.createIfStatement(
241+
ts.factory.createBinaryExpression(
242+
ts.factory.createTypeOfExpression(
243+
ts.factory.createIdentifier(docgenCollectionName)
244+
),
235245
ts.SyntaxKind.ExclamationEqualsEqualsToken,
236-
ts.createLiteral("undefined")
246+
ts.factory.createStringLiteral("undefined")
237247
),
238248
insertTsIgnoreBeforeStatement(
239-
ts.createStatement(
240-
ts.createBinary(
241-
ts.createElementAccess(
242-
ts.createIdentifier(docgenCollectionName),
243-
ts.createLiteral(`${relativeFilename}#${d.displayName}`)
249+
ts.factory.createExpressionStatement(
250+
ts.factory.createBinaryExpression(
251+
ts.factory.createElementAccessExpression(
252+
ts.factory.createIdentifier(docgenCollectionName),
253+
ts.factory.createStringLiteral(
254+
`${relativeFilename}#${d.displayName}`
255+
)
244256
),
245257
ts.SyntaxKind.EqualsToken,
246-
ts.createObjectLiteral([
247-
ts.createPropertyAssignment(
248-
ts.createIdentifier("docgenInfo"),
249-
ts.createPropertyAccess(
250-
ts.createIdentifier(d.displayName),
251-
ts.createIdentifier("__docgenInfo")
258+
ts.factory.createObjectLiteralExpression([
259+
ts.factory.createPropertyAssignment(
260+
ts.factory.createIdentifier("docgenInfo"),
261+
ts.factory.createPropertyAccessExpression(
262+
ts.factory.createIdentifier(d.displayName),
263+
ts.factory.createIdentifier("__docgenInfo")
252264
)
253265
),
254-
ts.createPropertyAssignment(
255-
ts.createIdentifier("name"),
256-
ts.createLiteral(d.displayName)
266+
ts.factory.createPropertyAssignment(
267+
ts.factory.createIdentifier("name"),
268+
ts.factory.createStringLiteral(d.displayName)
257269
),
258-
ts.createPropertyAssignment(
259-
ts.createIdentifier("path"),
260-
ts.createLiteral(`${relativeFilename}#${d.displayName}`)
270+
ts.factory.createPropertyAssignment(
271+
ts.factory.createIdentifier("path"),
272+
ts.factory.createStringLiteral(
273+
`${relativeFilename}#${d.displayName}`
274+
)
261275
),
262276
])
263277
)
@@ -287,29 +301,29 @@ function setComponentDocGen(
287301
options: GeneratorOptions
288302
): ts.Statement {
289303
return insertTsIgnoreBeforeStatement(
290-
ts.createStatement(
291-
ts.createBinary(
304+
ts.factory.createExpressionStatement(
305+
ts.factory.createBinaryExpression(
292306
// SimpleComponent.__docgenInfo
293-
ts.createPropertyAccess(
294-
ts.createIdentifier(d.displayName),
295-
ts.createIdentifier("__docgenInfo")
307+
ts.factory.createPropertyAccessExpression(
308+
ts.factory.createIdentifier(d.displayName),
309+
ts.factory.createIdentifier("__docgenInfo")
296310
),
297311
ts.SyntaxKind.EqualsToken,
298-
ts.createObjectLiteral([
312+
ts.factory.createObjectLiteralExpression([
299313
// SimpleComponent.__docgenInfo.description
300-
ts.createPropertyAssignment(
301-
ts.createLiteral("description"),
302-
ts.createLiteral(d.description)
314+
ts.factory.createPropertyAssignment(
315+
ts.factory.createStringLiteral("description"),
316+
ts.factory.createStringLiteral(d.description)
303317
),
304318
// SimpleComponent.__docgenInfo.displayName
305-
ts.createPropertyAssignment(
306-
ts.createLiteral("displayName"),
307-
ts.createLiteral(d.displayName)
319+
ts.factory.createPropertyAssignment(
320+
ts.factory.createStringLiteral("displayName"),
321+
ts.factory.createStringLiteral(d.displayName)
308322
),
309323
// SimpleComponent.__docgenInfo.props
310-
ts.createPropertyAssignment(
311-
ts.createLiteral("props"),
312-
ts.createObjectLiteral(
324+
ts.factory.createPropertyAssignment(
325+
ts.factory.createStringLiteral("props"),
326+
ts.factory.createObjectLiteralExpression(
313327
Object.entries(d.props).map(([propName, prop]) =>
314328
createPropDefinition(propName, prop, options)
315329
)
@@ -333,13 +347,13 @@ export function generateDocgenCodeBlock(options: GeneratorOptions): string {
333347
.replace(/\\/g, "/");
334348

335349
const wrapInTryStatement = (statements: ts.Statement[]): ts.TryStatement =>
336-
ts.createTry(
337-
ts.createBlock(statements, true),
338-
ts.createCatchClause(
339-
ts.createVariableDeclaration(
340-
ts.createIdentifier("__react_docgen_typescript_loader_error")
350+
ts.factory.createTryStatement(
351+
ts.factory.createBlock(statements, true),
352+
ts.factory.createCatchClause(
353+
ts.factory.createVariableDeclaration(
354+
ts.factory.createIdentifier("__react_docgen_typescript_loader_error")
341355
),
342-
ts.createBlock([])
356+
ts.factory.createBlock([])
343357
),
344358
undefined
345359
);

0 commit comments

Comments
 (0)