Skip to content

Commit b6b5d06

Browse files
author
Anton Savoskin
committed
update typescript and fix deprecation warnings
1 parent aa34505 commit b6b5d06

5 files changed

Lines changed: 97 additions & 84 deletions

File tree

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
"webpack-sources": "^2.2.0"
4444
},
@@ -71,13 +71,13 @@
7171
"react": "^17.0.1",
7272
"ts-jest": "^26.5.6",
7373
"ts-loader": "^9.1.2",
74-
"typescript": "3.8.3",
74+
"typescript": "4.8.3",
7575
"webpack": "^5.36.2",
7676
"webpack-cli": "^4.7.0",
7777
"yarn-add-no-save": "^1.0.3"
7878
},
7979
"peerDependencies": {
80-
"typescript": ">= 3.x",
80+
"typescript": ">= 4.x",
8181
"webpack": ">= 4"
8282
},
8383
"lint-staged": {

src/dependency.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class DocGenTemplate extends NullDependency.Template
4646
};
4747
}
4848

49-
// @ts-ignore TODO: How to type this correctly?
49+
// @ts-expect-error TODO: How to type this correctly?
5050
DocGenDependency.Template = DocGenTemplate;
5151

5252
// 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
);

src/plugin.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ function processModule(
9191
if (cached) {
9292
// eslint-disable-next-line
9393
// @ts-ignore
94-
// eslint-disable-next-line
9594
debugInclude(`Got cached docgen for "${webpackModule.request}"`);
9695
// eslint-disable-next-line
9796
// @ts-ignore

yarn.lock

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5133,10 +5133,10 @@ rc@^1.2.8:
51335133
minimist "^1.2.0"
51345134
strip-json-comments "~2.0.1"
51355135

5136-
react-docgen-typescript@^1.22.0:
5137-
version "1.22.0"
5138-
resolved "https://registry.yarnpkg.com/react-docgen-typescript/-/react-docgen-typescript-1.22.0.tgz#00232c8e8e47f4437cac133b879b3e9437284bee"
5139-
integrity sha512-MPLbF8vzRwAG3GcjdL+OHQlhgtWsLTXs+7uJiHfEeT3Ur7IsZaNYqRTLQ9sj2nB6M6jylcPCeCmH7qbszJmecg==
5136+
react-docgen-typescript@^2.2.2:
5137+
version "2.2.2"
5138+
resolved "https://registry.yarnpkg.com/react-docgen-typescript/-/react-docgen-typescript-2.2.2.tgz#4611055e569edc071204aadb20e1c93e1ab1659c"
5139+
integrity sha512-tvg2ZtOpOi6QDwsb3GZhOjDkkX0h8Z2gipvTg6OVMUyoYoURhEiRNePT8NZItTVCDh39JJHnLdfCOkzoLbFnTg==
51405140

51415141
react-is@^17.0.1:
51425142
version "17.0.1"
@@ -6186,10 +6186,10 @@ typescript-memoize@^1.0.0-alpha.3:
61866186
dependencies:
61876187
core-js "2.4.1"
61886188

6189-
typescript@3.8.3:
6190-
version "3.8.3"
6191-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.8.3.tgz#409eb8544ea0335711205869ec458ab109ee1061"
6192-
integrity sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==
6189+
typescript@4.8.3:
6190+
version "4.8.3"
6191+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.3.tgz#d59344522c4bc464a65a730ac695007fdb66dd88"
6192+
integrity sha512-goMHfm00nWPa8UvR/CPSvykqf6dVV8x/dp0c5mFTMTIu0u0FlGWRioyy7Nn0PGAdHxpJZnuO/ut+PpQ8UiHAig==
61936193

61946194
typical@^4.0.0:
61956195
version "4.0.0"

0 commit comments

Comments
 (0)