Skip to content

Commit e76cfa3

Browse files
author
LongYinan
committed
fix: compatible issue with ts < 4.0
Close #310
1 parent faca2e8 commit e76cfa3

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

src/index.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ interface ImportInfo extends ModuleConfig {
2020
type: 'namedImport' | 'namespaceImport' | 'defaultImport'
2121
}
2222

23+
const FACTORY: typeof ts.factory = 'factory' in ts ? ts.factory : ts
24+
2325
export interface Options {
2426
sourcemap?: boolean
2527
autoLabel?: boolean
@@ -55,24 +57,24 @@ const getPackageRootPath = memoize((filename: string) => findRoot(filename))
5557
const hashArray = (arr: Array<string>) => hashString(arr.join(''))
5658

5759
const createImportJSXAst = memoize((propertyName: string | undefined) => {
58-
const importClause = ts.factory.createImportClause(
60+
const importClause = FACTORY.createImportClause(
5961
false,
6062
undefined,
61-
ts.factory.createNamedImports([
63+
FACTORY.createNamedImports([
6264
propertyName
63-
? ts.factory.createImportSpecifier(
64-
ts.factory.createIdentifier('jsx'),
65-
ts.factory.createIdentifier(propertyName),
65+
? FACTORY.createImportSpecifier(
66+
FACTORY.createIdentifier('jsx'),
67+
FACTORY.createIdentifier(propertyName),
6668
)
67-
: ts.factory.createImportSpecifier(
69+
: FACTORY.createImportSpecifier(
6870
undefined,
69-
ts.factory.createIdentifier('jsx'),
71+
FACTORY.createIdentifier('jsx'),
7072
),
7173
]),
7274
)
73-
const moduleSpecifier = ts.factory.createStringLiteral('@emotion/react')
75+
const moduleSpecifier = FACTORY.createStringLiteral('@emotion/react')
7476

75-
return ts.factory.createImportDeclaration(
77+
return FACTORY.createImportDeclaration(
7678
undefined,
7779
undefined,
7880
importClause,
@@ -214,10 +216,10 @@ export const createEmotionPlugin = (pluginOptions?: Options) => {
214216
)
215217
})
216218
if (info) {
217-
expression = ts.factory.createCallExpression(
219+
expression = FACTORY.createCallExpression(
218220
expression.expression,
219221
[],
220-
[ts.factory.createStringLiteral(expression.name.text)],
222+
[FACTORY.createStringLiteral(expression.name.text)],
221223
)
222224
}
223225
}
@@ -254,28 +256,28 @@ export const createEmotionPlugin = (pluginOptions?: Options) => {
254256
stuffToHash,
255257
)}${positionInFile}`
256258
const [el, opts] = exp.arguments
257-
const targetAssignment = ts.factory.createPropertyAssignment(
258-
ts.factory.createIdentifier('target'),
259-
ts.factory.createStringLiteral(stableClassName),
259+
const targetAssignment = FACTORY.createPropertyAssignment(
260+
FACTORY.createIdentifier('target'),
261+
FACTORY.createStringLiteral(stableClassName),
260262
)
261263
const args = [el]
262264
args.push(
263-
ts.factory.createObjectLiteralExpression(
265+
FACTORY.createObjectLiteralExpression(
264266
opts && ts.isObjectLiteralExpression(opts)
265267
? opts.properties.concat(targetAssignment)
266268
: [targetAssignment],
267269
true,
268270
),
269271
)
270272

271-
const updatedCall = ts.factory.updateCallExpression(
273+
const updatedCall = FACTORY.updateCallExpression(
272274
exp,
273275
exp.expression,
274276
exp.typeArguments,
275277
args,
276278
)
277279

278-
return ts.factory.updateCallExpression(
280+
return FACTORY.updateCallExpression(
279281
transformedNode,
280282
updatedCall,
281283
transformedNode.typeArguments,
@@ -306,12 +308,12 @@ export const createEmotionPlugin = (pluginOptions?: Options) => {
306308
if (localNameNode && ts.isIdentifier(localNameNode)) {
307309
const local = localNameNode.text
308310
const fileName = basename(rawPath, extname(rawPath))
309-
transformedNode = ts.factory.updateCallExpression(
311+
transformedNode = FACTORY.updateCallExpression(
310312
transformedNode,
311313
transformedNode.expression,
312314
transformedNode.typeArguments,
313315
transformedNode.arguments.concat([
314-
ts.factory.createStringLiteral(
316+
FACTORY.createStringLiteral(
315317
`label:${options
316318
.labelFormat!.replace('[local]', local)
317319
.replace('[filename]', fileName)};`,
@@ -351,12 +353,12 @@ export const createEmotionPlugin = (pluginOptions?: Options) => {
351353
const comment = convert
352354
.fromObject(sourcemapGenerator)
353355
.toComment({ multiline: true })
354-
transformedNode = ts.factory.updateCallExpression(
356+
transformedNode = FACTORY.updateCallExpression(
355357
transformedNode,
356358
transformedNode.expression,
357359
transformedNode.typeArguments,
358360
transformedNode.arguments.concat([
359-
ts.factory.createStringLiteral(comment),
361+
FACTORY.createStringLiteral(comment),
360362
]),
361363
)
362364
}

0 commit comments

Comments
 (0)