@@ -2,6 +2,7 @@ import * as ts from 'typescript';
2
2
3
3
import { Checker } from '../../checker' ;
4
4
import { AbsoluteMatcher } from '../absolute_matcher' ;
5
+ import { walkUpPropertyAndElementAccess } from '../ast_tools' ;
5
6
import { isExpressionOfAllowedTrustedType } from '../is_trusted_type' ;
6
7
import { TrustedTypesConfig } from '../trusted_types_configuration' ;
7
8
@@ -21,19 +22,32 @@ function isCalledWithAllowedTrustedType(
21
22
return false ;
22
23
}
23
24
25
+ function isPolyfill ( n : ts . Node , matcher : AbsoluteMatcher ) {
26
+ if ( matcher . filePath === 'GLOBAL' ) {
27
+ const wholeExp = walkUpPropertyAndElementAccess ( n ) ;
28
+ const parent = wholeExp . parent ;
29
+ if ( parent && ts . isBinaryExpression ( parent ) && parent . left === wholeExp &&
30
+ parent . operatorToken . kind === ts . SyntaxKind . EqualsToken ) {
31
+ return true ;
32
+ }
33
+ }
34
+ return false ;
35
+ }
36
+
24
37
function checkIdentifierNode (
25
38
tc : ts . TypeChecker , n : ts . Identifier , matcher : AbsoluteMatcher ,
26
39
allowedTrustedType : TrustedTypesConfig | undefined ) : ts . Node | undefined {
40
+ if ( isPolyfill ( n , matcher ) ) return ;
27
41
if ( ! matcher . matches ( n , tc ) ) return ;
28
42
if ( isCalledWithAllowedTrustedType ( tc , n , allowedTrustedType ) ) return ;
29
43
30
-
31
44
return n ;
32
45
}
33
46
34
47
function checkElementAccessNode (
35
48
tc : ts . TypeChecker , n : ts . ElementAccessExpression , matcher : AbsoluteMatcher ,
36
49
allowedTrustedType : TrustedTypesConfig | undefined ) : ts . Node | undefined {
50
+ if ( isPolyfill ( n , matcher ) ) return ;
37
51
if ( ! matcher . matches ( n . argumentExpression , tc ) ) return ;
38
52
if ( isCalledWithAllowedTrustedType ( tc , n , allowedTrustedType ) ) return ;
39
53
@@ -46,9 +60,9 @@ export class NameEngine extends PatternEngine {
46
60
for ( const value of this . config . values ) {
47
61
const matcher = new AbsoluteMatcher ( value ) ;
48
62
49
- // `String.prototype.split` only returns emtpy array when both the string
50
- // and the splitter are empty. Here we should be able to safely assert pop
51
- // returns a non-null result.
63
+ // `String.prototype.split` only returns emtpy array when both the
64
+ // string and the splitter are empty. Here we should be able to safely
65
+ // assert pop returns a non-null result.
52
66
const bannedIdName = matcher . bannedName . split ( '.' ) . pop ( ) ! ;
53
67
checker . onNamedIdentifier (
54
68
bannedIdName ,
0 commit comments