5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
- import { satisfies } from 'semver' ;
9
8
import * as ts from 'typescript' ;
10
9
11
10
12
11
export function testPrefixClasses ( content : string ) {
12
+ const exportVarSetter = / (?: e x p o r t ) ? (?: v a r | c o n s t ) \s + ( \S + ) \s * = \s * / ;
13
+ const multiLineComment = / \s * (?: \/ \* [ \s \S ] * ?\* \/ ) ? \s * / ;
14
+ const newLine = / \s * \r ? \n \s * / ;
15
+
13
16
const regexes = [
14
- // tslint:disable-next-line:max-line-length
15
- / ^ (?: e x p o r t ) ? ( v a r ( \S + ) = ) (?: \/ \* \* @ c l a s s \* \/ ) ? ( \( f u n c t i o n \( \) \{ \r ? \n (?: \s + (?: \/ \* \* | \* | \* \/ | \/ \/ ) [ ^ \r ? \n ] * \r ? \n ) * \s + f u n c t i o n \2\( [ ^ \) ] * \) \{ \r ? \n ) / ,
16
- / ^ (?: e x p o r t ) ? ( v a r ( \S + ) = ) (?: \/ \* \* @ c l a s s \* \/ ) ? ( \( f u n c t i o n \( _ s u p e r \) \{ \r ? \n \s + \w * _ _ e x t e n d s \( \w + , _ s u p e r \) ; \r ? \n ) / ,
17
- ] ;
17
+ [
18
+ / ^ / ,
19
+ exportVarSetter , multiLineComment ,
20
+ / \( / , multiLineComment ,
21
+ / \s * f u n c t i o n \( \) { / , newLine ,
22
+ multiLineComment ,
23
+ / f u n c t i o n \1\( [ ^ \) ] * \) \{ / , newLine ,
24
+ ] ,
25
+ [
26
+ / ^ / ,
27
+ exportVarSetter , multiLineComment ,
28
+ / \( / , multiLineComment ,
29
+ / \s * f u n c t i o n \( _ s u p e r \) { / , newLine ,
30
+ / \w * _ _ e x t e n d s \( \w + , _ s u p e r \) ; / ,
31
+ ] ,
32
+ ] . map ( arr => new RegExp ( arr . map ( x => x . source ) . join ( '' ) , 'm' ) ) ;
18
33
19
34
return regexes . some ( ( regex ) => regex . test ( content ) ) ;
20
35
}
21
36
22
37
const superParameterName = '_super' ;
23
- const extendsHelperName = ( satisfies ( ts . version , '< 2.5' ) ? '_' : '' ) + '__extends' ;
38
+ const extendsHelperName = '__extends' ;
24
39
25
40
export function getPrefixClassesTransformer ( ) : ts . TransformerFactory < ts . SourceFile > {
26
41
return ( context : ts . TransformationContext ) : ts . Transformer < ts . SourceFile > => {
@@ -218,7 +233,7 @@ function isDownleveledClass(node: ts.Node): boolean {
218
233
const extendCallExpression = firstStatement . expression ;
219
234
220
235
if ( ! isIdentifier ( extendCallExpression . expression )
221
- || extendCallExpression . expression . text !== extendsHelperName ) {
236
+ || ! extendCallExpression . expression . text . endsWith ( extendsHelperName ) ) {
222
237
return false ;
223
238
}
224
239
@@ -236,6 +251,6 @@ function isDownleveledClass(node: ts.Node): boolean {
236
251
237
252
return isFunctionDeclaration ( secondStatement )
238
253
&& secondStatement . name !== undefined
239
- && secondStatement . name . text === className
254
+ && className . endsWith ( secondStatement . name . text )
240
255
&& returnStatement . expression . text === secondStatement . name . text ;
241
256
}
0 commit comments