Skip to content

Commit 1153f66

Browse files
committed
fix(Parser.forExtension): fix bad regexes and memoization
1 parent 4c588fb commit 1153f66

File tree

1 file changed

+57
-35
lines changed

1 file changed

+57
-35
lines changed

src/index.ts

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ export class Parser {
7171
readonly babelParser: BabelParser
7272
readonly parserOpts: ParserOptions
7373

74+
_forJs: Parser | undefined
75+
_forTs: Parser | undefined
76+
_forTsx: Parser | undefined
77+
_forDts: Parser | undefined
78+
7479
constructor(babelParser: BabelParser, parserOpts: ParserOptions) {
7580
this.babelParser = babelParser
7681
this.parserOpts = parserOpts
@@ -117,57 +122,74 @@ export class Parser {
117122
}
118123

119124
get forJs(): Parser {
120-
if (!this.parserOpts.plugins?.some((p) => pluginName(p) === 'typescript'))
121-
return this
122-
return this.removePlugins('typescript', 'decorators-legacy').mergePlugins(
123-
['flow', { all: true }],
124-
'flowComments',
125-
'jsx',
126-
['decorators', { decoratorsBeforeExport: false }]
125+
return (
126+
this._forJs ||
127+
(this._forJs = (() => {
128+
if (
129+
!this.parserOpts.plugins?.some((p) => pluginName(p) === 'typescript')
130+
)
131+
return this
132+
return this.removePlugins(
133+
'typescript',
134+
'decorators-legacy'
135+
).mergePlugins(['flow', { all: true }], 'flowComments', 'jsx', [
136+
'decorators',
137+
{ decoratorsBeforeExport: false },
138+
])
139+
})())
127140
)
128141
}
129142

130143
get forTs(): Parser {
131-
return this.removePlugins(
132-
'flow',
133-
'flowComments',
134-
'decorators',
135-
'jsx'
136-
).mergePlugins(
137-
['typescript', { disallowAmbiguousJSXLike: true, dts: false }],
138-
'decorators-legacy'
144+
return (
145+
this._forTs ||
146+
(this._forTs = this.removePlugins(
147+
'flow',
148+
'flowComments',
149+
'decorators',
150+
'jsx'
151+
).mergePlugins(
152+
['typescript', { disallowAmbiguousJSXLike: true, dts: false }],
153+
'decorators-legacy'
154+
))
139155
)
140156
}
141157

142158
get forTsx(): Parser {
143-
return this.removePlugins(
144-
'flow',
145-
'flowComments',
146-
'decorators'
147-
).mergePlugins(
148-
['typescript', { disallowAmbiguousJSXLike: true, dts: false }],
149-
'decorators-legacy',
150-
'jsx'
159+
return (
160+
this._forTsx ||
161+
(this._forTsx = this.removePlugins(
162+
'flow',
163+
'flowComments',
164+
'decorators'
165+
).mergePlugins(
166+
['typescript', { disallowAmbiguousJSXLike: true, dts: false }],
167+
'decorators-legacy',
168+
'jsx'
169+
))
151170
)
152171
}
153172

154173
get forDts(): Parser {
155-
return this.removePlugins(
156-
'flow',
157-
'flowComments',
158-
'decorators',
159-
'jsx'
160-
).mergePlugins(
161-
['typescript', { disallowAmbiguousJSXLike: true, dts: true }],
162-
'decorators-legacy'
174+
return (
175+
this._forDts ||
176+
(this._forDts = this.removePlugins(
177+
'flow',
178+
'flowComments',
179+
'decorators',
180+
'jsx'
181+
).mergePlugins(
182+
['typescript', { disallowAmbiguousJSXLike: true, dts: true }],
183+
'decorators-legacy'
184+
))
163185
)
164186
}
165187

166188
forExtension(e: string): Parser {
167-
if (/(\.|^)([cm]?jsx?(\.flow)?)/.test(e)) return this.forJs
168-
if (/(\.|^)\.d\.ts/i.test(e)) return this.forDts
169-
if (/(\.|^)\.[cm]?tsx/i.test(e)) return this.forTsx
170-
if (/(\.|^)\.[cm]?ts/i.test(e)) return this.forTs
189+
if (/(\.|^)([cm]?jsx?(\.flow)?)$/i.test(e)) return this.forJs
190+
if (/(\.|^)d\.ts$/i.test(e)) return this.forDts
191+
if (/(\.|^)[cm]?tsx$/i.test(e)) return this.forTsx
192+
if (/(\.|^)[cm]?ts$/i.test(e)) return this.forTs
171193
return this
172194
}
173195
}

0 commit comments

Comments
 (0)