-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
presubmit-checks.js
1028 lines (995 loc) · 31.9 KB
/
presubmit-checks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
var gulp = require('gulp-help')(require('gulp'));
var path = require('path');
var srcGlobs = require('../config').presubmitGlobs;
var util = require('gulp-util');
var through2 = require('through2');
var dedicatedCopyrightNoteSources = /(\.js|\.css|\.go)$/;
var es6polyfill = 'Not available because we do not currently' +
' ship with a needed ES6 polyfill.';
var requiresReviewPrivacy =
'Usage of this API requires dedicated review due to ' +
'being privacy sensitive. Please file an issue asking for permission' +
' to use if you have not yet done so.';
var privateServiceFactory = 'This service should only be installed in ' +
'the whitelisted files. Other modules should use a public function ' +
'typically called serviceNameFor.';
var shouldNeverBeUsed =
'Usage of this API is not allowed - only for internal purposes.';
var backwardCompat = 'This method must not be called. It is only retained ' +
'for backward compatibility during rollout.';
var realiasGetMode = 'Do not re-alias getMode or its return so it can be ' +
'DCE\'d. Use explicitly like "getMode().localDev" instead.';
// Terms that must not appear in our source files.
var forbiddenTerms = {
'DO NOT SUBMIT': '',
// TODO(dvoytenko, #8464): cleanup whitelist.
'(^-amp-|\\W-amp-)': {
message: 'Switch to new internal class form',
whitelist: [
'build-system/amp4test.js',
'build-system/tasks/extension-generator/index.js',
'css/amp.css',
'extensions/amp-pinterest/0.1/amp-pinterest.css',
'extensions/amp-pinterest/0.1/follow-button.js',
'extensions/amp-pinterest/0.1/pin-widget.js',
'extensions/amp-pinterest/0.1/pinit-button.js',
],
},
'(^i-amp-|\\Wi-amp-)': {
message: 'Switch to new internal ID form',
whitelist: [
'build-system/tasks/extension-generator/index.js',
'css/amp.css',
],
},
'describe\\.only': '',
'describes.*\\.only': '',
'it\\.only': '',
'Math\.random[^;()]*=': 'Use Sinon to stub!!!',
'sinon\\.(spy|stub|mock)\\(': {
message: 'Use a sandbox instead to avoid repeated `#restore` calls',
},
'(\\w*([sS]py|[sS]tub|[mM]ock|clock).restore)': {
message: 'Use a sandbox instead to avoid repeated `#restore` calls',
},
'sinon\\.useFake\\w+': {
message: 'Use a sandbox instead to avoid repeated `#restore` calls',
},
'sandbox\\.(spy|stub|mock)\\([^,\\s]*[iI]?frame[^,\\s]*,': {
message: 'Do NOT stub on a cross domain iframe! #5359\n' +
' If this is same domain, mark /*OK*/.\n' +
' If this is cross domain, overwrite the method directly.',
},
'console\\.\\w+\\(': {
message: 'If you run against this, use console/*OK*/.log to ' +
'whitelist a legit case.',
whitelist: [
'build-system/pr-check.js',
'build-system/app.js',
'validator/nodejs/index.js', // NodeJs only.
'validator/engine/parse-css.js',
'validator/engine/validator-in-browser.js',
'validator/engine/validator.js',
],
checkInTestFolder: true,
},
// Match `getMode` that is not followed by a "()." and is assigned
// as a variable.
'\\bgetMode\\([^)]*\\)(?!\\.)': {
message: realiasGetMode,
whitelist: [
'src/mode.js',
'dist.3p/current/integration.js',
],
},
'import[^}]*\\bgetMode as': {
message: realiasGetMode,
},
'\\bgetModeObject\\(': {
message: realiasGetMode,
whitelist: [
'src/mode-object.js',
'src/iframe-attributes.js',
'src/log.js',
'dist.3p/current/integration.js',
],
},
'(?:var|let|const) +IS_DEV +=': {
message: 'IS_DEV local var only allowed in mode.js and ' +
'dist.3p/current/integration.js',
whitelist: [
'src/mode.js',
'dist.3p/current/integration.js',
],
},
'\\.prefetch\\(': {
message: 'Do not use preconnect.prefetch, use preconnect.preload instead.',
},
'iframePing': {
message: 'This is only available in vendor config for ' +
'temporary workarounds.',
whitelist: [
'extensions/amp-analytics/0.1/amp-analytics.js',
],
},
// Service factories that should only be installed once.
'installActionServiceForDoc': {
message: privateServiceFactory,
whitelist: [
'src/service/action-impl.js',
'src/service/standard-actions-impl.js',
'src/runtime.js',
],
},
'installActionHandler': {
message: privateServiceFactory,
whitelist: [
'src/service/action-impl.js',
'extensions/amp-access/0.1/amp-access.js',
'extensions/amp-form/0.1/amp-form.js',
],
},
'installActivityService': {
message: privateServiceFactory,
whitelist: [
'extensions/amp-analytics/0.1/activity-impl.js',
'extensions/amp-analytics/0.1/amp-analytics.js',
],
},
'cidServiceForDocForTesting': {
message: privateServiceFactory,
whitelist: [
'src/service/cid-impl.js',
],
},
'installCryptoService': {
message: privateServiceFactory,
whitelist: [
'src/service/crypto-impl.js',
'src/runtime.js',
],
},
'installDocumentStateService': {
message: privateServiceFactory,
whitelist: [
'src/service/document-state.js',
'src/runtime.js',
],
},
'installDocService': {
message: privateServiceFactory,
whitelist: [
'src/amp.js',
'src/amp-shadow.js',
'src/inabox/amp-inabox.js',
'src/service/ampdoc-impl.js',
'testing/describes.js',
'testing/iframe.js',
],
},
'installPerformanceService': {
message: privateServiceFactory,
whitelist: [
'src/amp.js',
'src/inabox/amp-inabox.js',
'src/service/performance-impl.js',
],
},
'installStorageServiceForDoc': {
message: privateServiceFactory,
whitelist: [
'src/runtime.js',
'src/service/storage-impl.js',
],
},
'installTemplatesService': {
message: privateServiceFactory,
whitelist: [
'src/runtime.js',
'src/service/template-impl.js',
],
},
'installUrlReplacementsServiceForDoc': {
message: privateServiceFactory,
whitelist: [
'src/runtime.js',
'src/service/url-replacements-impl.js',
],
},
'installViewerServiceForDoc': {
message: privateServiceFactory,
whitelist: [
'src/runtime.js',
'src/inabox/amp-inabox.js',
'src/service/viewer-impl.js',
],
},
'setViewerVisibilityState': {
message: privateServiceFactory,
whitelist: [
'src/runtime.js',
'src/service/viewer-impl.js',
],
},
'installViewportServiceForDoc': {
message: privateServiceFactory,
whitelist: [
'src/runtime.js',
'src/service/viewport-impl.js',
],
},
'installVsyncService': {
message: privateServiceFactory,
whitelist: [
'src/runtime.js',
'src/service/resources-impl.js',
'src/service/viewport-impl.js',
'src/service/vsync-impl.js',
],
},
'installResourcesServiceForDoc': {
message: privateServiceFactory,
whitelist: [
'src/runtime.js',
'src/service/resources-impl.js',
'src/service/standard-actions-impl.js',
],
},
'installXhrService': {
message: privateServiceFactory,
whitelist: [
'src/runtime.js',
'src/service/xhr-impl.js',
],
},
'installPositionObserverServiceForDoc': {
message: privateServiceFactory,
whitelist: [
'src/service/position-observer-impl.js',
// TODO(@zhouyx, #9213) Remove this item.
'extensions/amp-ad/0.1/amp-ad-xorigin-iframe-handler.js',
'extensions/amp-animation/0.1/scrollbound-scene.js',
'src/service/video-manager-impl.js',
],
},
'initLogConstructor|setReportError': {
message: 'Should only be called from JS binary entry files.',
whitelist: [
'3p/integration.js',
'3p/ampcontext-lib.js',
'3p/iframe-transport-client-lib.js',
'ads/alp/install-alp.js',
'ads/inabox/inabox-host.js',
'dist.3p/current/integration.js',
'extensions/amp-access/0.1/amp-login-done.js',
'extensions/amp-viewer-integration/0.1/examples/amp-viewer-host.js',
'src/runtime.js',
'src/log.js',
'src/web-worker/web-worker.js',
'tools/experiments/experiments.js',
],
},
'parseUrlWithA': {
message: 'Use parseUrl instead.',
whitelist: [
'src/url.js',
'src/service/document-click.js',
'dist.3p/current/integration.js',
],
},
'\\.sendMessage\\(': {
message: 'Usages must be reviewed.',
whitelist: [
// viewer-impl.sendMessage
'src/service/viewer-impl.js',
'src/service/viewport-impl.js',
'src/service/performance-impl.js',
'src/service/resources-impl.js',
'extensions/amp-app-banner/0.1/amp-app-banner.js',
// iframe-messaging-client.sendMessage
'3p/iframe-messaging-client.js',
'3p/ampcontext.js',
'3p/ampcontext-integration.js',
'dist.3p/current/integration.js', // includes previous
],
},
'\\.sendMessageAwaitResponse\\(': {
message: 'Usages must be reviewed.',
whitelist: [
'src/service/viewer-impl.js',
'src/service/viewer-cid-api.js',
'src/service/storage-impl.js',
'src/service/history-impl.js',
'src/service/cid-impl.js',
'extensions/amp-access/0.1/login-dialog.js',
'extensions/amp-access/0.1/signin.js',
],
},
// Privacy sensitive
'cidForDoc|cidForDocOrNull': {
message: requiresReviewPrivacy,
whitelist: [
'src/ad-cid.js',
'src/services.js',
'src/service/cid-impl.js',
'src/service/url-replacements-impl.js',
'extensions/amp-access/0.1/amp-access.js',
'extensions/amp-experiment/0.1/variant.js',
'extensions/amp-user-notification/0.1/amp-user-notification.js',
],
},
'getBaseCid': {
message: requiresReviewPrivacy,
whitelist: [
'src/service/cid-impl.js',
'src/service/viewer-impl.js',
],
},
'cookie\\W': {
message: requiresReviewPrivacy,
whitelist: [
'build-system/test-server.js',
'src/cookies.js',
'src/service/cid-impl.js',
'extensions/amp-analytics/0.1/vendors.js',
'testing/fake-dom.js',
],
},
'getCookie\\W': {
message: requiresReviewPrivacy,
whitelist: [
'src/service/cid-impl.js',
'src/cookies.js',
'src/experiments.js',
'tools/experiments/experiments.js',
],
},
'setCookie\\W': {
message: requiresReviewPrivacy,
whitelist: [
'src/service/cid-impl.js',
'src/cookies.js',
'src/experiments.js',
'tools/experiments/experiments.js',
],
},
'isTrustedViewer': {
message: requiresReviewPrivacy,
whitelist: [
'src/service/viewer-impl.js',
'src/service/viewer-cid-api.js',
'src/inabox/inabox-viewer.js',
'src/service/cid-impl.js',
'src/impression.js',
],
},
'eval\\(': {
message: shouldNeverBeUsed,
whitelist: [
'extension/amp-bind/0.1/test/test-bind-expr.js',
],
},
'storageForDoc': {
message: requiresReviewPrivacy,
whitelist: [
'src/services.js',
'src/service/cid-impl.js',
'extensions/amp-user-notification/0.1/amp-user-notification.js',
'extensions/amp-app-banner/0.1/amp-app-banner.js',
],
},
'localStorage': {
message: requiresReviewPrivacy,
whitelist: [
'src/service/cid-impl.js',
'src/service/storage-impl.js',
'testing/fake-dom.js',
],
},
'sessionStorage': {
message: requiresReviewPrivacy,
whitelist: [
'extensions/amp-accordion/0.1/amp-accordion.js',
],
},
'indexedDB': {
message: requiresReviewPrivacy,
whitelist: [
// https://docs.google.com/document/d/1tH_sj93Lo8XRpLP0cDSFNrBi1K_jmx_-q1sk_ZW3Nbg/edit#heading=h.ko4gxsan9svq
'src/service-worker/core.js',
'src/service-worker/kill.js',
],
},
'openDatabase': requiresReviewPrivacy,
'requestFileSystem': requiresReviewPrivacy,
'webkitRequestFileSystem': requiresReviewPrivacy,
'getAccessReaderId': {
message: requiresReviewPrivacy,
whitelist: [
'build-system/amp.extern.js',
'extensions/amp-access/0.1/amp-access.js',
'src/service/url-replacements-impl.js',
],
},
'getAuthdataField': {
message: requiresReviewPrivacy,
whitelist: [
'build-system/amp.extern.js',
'extensions/amp-access/0.1/amp-access.js',
'src/service/url-replacements-impl.js',
],
},
'debugger': '',
// Overridden APIs.
'(doc.*)\\.referrer': {
message: 'Use Viewer.getReferrerUrl() instead.',
whitelist: [
'3p/integration.js',
'ads/google/a4a/utils.js',
'dist.3p/current/integration.js',
'src/service/viewer-impl.js',
'src/error.js',
],
},
'getUnconfirmedReferrerUrl': {
message: 'Use Viewer.getReferrerUrl() instead.',
whitelist: [
'extensions/amp-dynamic-css-classes/0.1/amp-dynamic-css-classes.js',
'src/3p-frame.js',
'src/iframe-attributes.js',
'src/service/viewer-impl.js',
'src/inabox/inabox-viewer.js',
],
},
'internalListenImplementation': {
message: 'Use `listen()` in either `event-helper` or `3p-frame-messaging`' +
', depending on your use case.',
whitelist: [
'src/3p-frame-messaging.js',
'src/event-helper.js',
'src/event-helper-listen.js',
'dist.3p/current/integration.js', // includes previous
],
},
'setTimeout.*throw': {
message: 'Use dev.error or user.error instead.',
whitelist: [
'src/log.js',
],
},
'(dev|user)\\(\\)\\.(fine|info|warn|error)\\((?!\\s*([A-Z0-9-]+|[\'"`][A-Z0-9-]+[\'"`]))[^,)\n]*': {
message: 'Logging message require explicitly `TAG`, or an all uppercase' +
' string as the first parameter',
},
'\\.schedulePass\\(': {
message: 'schedulePass is heavy, think twice before using it',
whitelist: [
'src/service/resources-impl.js',
],
},
'\\.requireLayout\\(': {
message: 'requireLayout is restricted b/c it affects non-contained elements',
whitelist: [
'extensions/amp-animation/0.1/web-animations.js',
'src/service/resources-impl.js',
],
},
'\\.updatePriority\\(': {
message: 'updatePriority is a restricted API.',
whitelist: [
'extensions/amp-a4a/0.1/amp-a4a.js',
'src/base-element.js',
'src/service/resources-impl.js',
],
},
'(win|Win)(dow)?(\\(\\))?\\.open\\W': {
message: 'Use dom.openWindowDialog',
whitelist: [
'src/dom.js',
],
},
'\\.getWin\\(': {
message: backwardCompat,
whitelist: [
],
},
'/\\*\\* @type \\{\\!Element\\} \\*/': {
message: 'Use assertElement instead of casting to !Element.',
whitelist: [
'src/log.js', // Has actual implementation of assertElement.
'dist.3p/current/integration.js', // Includes the previous.
],
},
'startupChunk\\(': {
message: 'startupChunk( should only be used during startup',
whitelist: [
'src/amp.js',
'src/chunk.js',
'src/inabox/amp-inabox.js',
'src/runtime.js',
],
},
'style\\.\\w+ = ': {
message: 'Use setStyle instead!',
whitelist: [
'testing/iframe.js',
],
},
'AMP_CONFIG': {
message: 'Do not access AMP_CONFIG directly. Use isExperimentOn() ' +
'and getMode() to access config',
whitelist: [
'build-system/amp.extern.js',
'build-system/app.js',
'build-system/tasks/prepend-global/index.js',
'build-system/tasks/prepend-global/test.js',
'dist.3p/current/integration.js',
'src/config.js',
'src/experiments.js',
'src/mode.js',
'src/service-worker/core.js',
'src/worker-error-reporting.js',
'tools/experiments/experiments.js',
],
},
'data:image/svg(?!\\+xml;charset=utf-8,)[^,]*,': {
message: 'SVG data images must use charset=utf-8: ' +
'"data:image/svg+xml;charset=utf-8,..."',
},
'installWorkerErrorReporting': {
message: 'Should only be used in worker entry points',
whitelist: [
'src/web-worker/web-worker.js',
'src/service-worker/shell.js',
'src/worker-error-reporting.js',
],
},
'new CustomEvent\\(': {
message: 'Use createCustomEvent() helper instead.',
whitelist: [
'src/event-helper.js',
],
},
};
var ThreePTermsMessage = 'The 3p bootstrap iframe has no polyfills loaded and' +
' can thus not use most modern web APIs.';
var forbidden3pTerms = {
// We need to forbid promise usage because we don't have our own polyfill
// available. This whitelisting of callNext is a major hack to allow one
// usage in babel's external helpers that is in a code path that we do
// not use.
'\\.then\\((?!callNext)': ThreePTermsMessage,
};
var bannedTermsHelpString = 'Please review viewport service for helper ' +
'methods or mark with `/*OK*/` or `/*REVIEW*/` and consult the AMP team. ' +
'Most of the forbidden property/method access banned on the ' +
'`forbiddenTermsSrcInclusive` object can be found in ' +
'[What forces layout / reflow gist by Paul Irish]' +
'(https://gist.github.com/paulirish/5d52fb081b3570c81e3a). ' +
'These properties/methods when read/used require the browser ' +
'to have the up-to-date value to return which might possibly be an ' +
'expensive computation and could also be triggered multiple times ' +
'if we are not careful. Please mark the call with ' +
'`object./*OK*/property` if you explicitly need to read or update the ' +
'forbidden property/method or mark it with `object./*REVIEW*/property` ' +
'if you are unsure and so that it stands out in code reviews.';
var forbiddenTermsSrcInclusive = {
'\\.innerHTML(?!_)': bannedTermsHelpString,
'\\.outerHTML(?!_)': bannedTermsHelpString,
'\\.offsetLeft(?!_)': bannedTermsHelpString,
'\\.offsetTop(?!_)': bannedTermsHelpString,
'\\.offsetWidth(?!_)': bannedTermsHelpString,
'\\.offsetHeight(?!_)': bannedTermsHelpString,
'\\.offsetParent(?!_)': bannedTermsHelpString,
'\\.clientLeft(?!_)(?!_)': bannedTermsHelpString,
'\\.clientTop(?!_)': bannedTermsHelpString,
'\\.clientWidth(?!_)': bannedTermsHelpString,
'\\.clientHeight(?!_)': bannedTermsHelpString,
'\\.scrollWidth(?!_)': 'please use `getScrollWidth()` from viewport',
'\\.scrollHeight(?!_)': bannedTermsHelpString,
'\\.scrollTop(?!_)': bannedTermsHelpString,
'\\.scrollLeft(?!_)': bannedTermsHelpString,
'\\.computedRole(?!_)': bannedTermsHelpString,
'\\.computedName(?!_)': bannedTermsHelpString,
'\\.innerText(?!_)': bannedTermsHelpString,
'\\.scrollX(?!_)': bannedTermsHelpString,
'\\.scrollY(?!_)': bannedTermsHelpString,
'\\.pageXOffset(?!_)': bannedTermsHelpString,
'\\.pageYOffset(?!_)': bannedTermsHelpString,
'\\.innerWidth(?!_)': bannedTermsHelpString,
'\\.innerHeight(?!_)': bannedTermsHelpString,
'\\.scrollingElement(?!_)': bannedTermsHelpString,
'\\.computeCTM(?!_)': bannedTermsHelpString,
// Functions
'\\.changeHeight\\(': bannedTermsHelpString,
'\\.changeSize\\(': bannedTermsHelpString,
'\\.attemptChangeHeight\\(0\\)': 'please consider using `attemptCollapse()`',
'\\.collapse\\(': bannedTermsHelpString,
'\\.expand\\(': bannedTermsHelpString,
'\\.focus\\(': bannedTermsHelpString,
'\\.getBBox\\(': bannedTermsHelpString,
'\\.getBoundingClientRect\\(': bannedTermsHelpString,
'\\.getClientRects\\(': bannedTermsHelpString,
'\\.getMatchedCSSRules\\(': bannedTermsHelpString,
'\\.postMessage\\(': bannedTermsHelpString,
'\\.scrollBy\\(': bannedTermsHelpString,
'\\.scrollIntoView\\(': bannedTermsHelpString,
'\\.scrollIntoViewIfNeeded\\(': bannedTermsHelpString,
'\\.scrollTo\\(': bannedTermsHelpString,
'\\.webkitConvertPointFromNodeToPage\\(': bannedTermsHelpString,
'\\.webkitConvertPointFromPageToNode\\(': bannedTermsHelpString,
'\\.scheduleUnlayout\\(': bannedTermsHelpString,
'getComputedStyle\\(': {
message: 'Due to various bugs in Firefox, you must use the computedStyle ' +
'helper in style.js.',
whitelist: [
'src/style.js',
'dist.3p/current/integration.js',
],
},
'decodeURIComponent\\(': {
message: 'decodeURIComponent throws for malformed URL components. Please ' +
'use tryDecodeUriComponent from src/url.js',
whitelist: [
'3p/integration.js',
'dist.3p/current/integration.js',
'examples/pwa/pwa.js',
'validator/engine/parse-url.js',
'validator/engine/validator.js',
'validator/webui/webui.js',
'extensions/amp-pinterest/0.1/util.js',
'src/url.js',
'src/url-try-decode-uri-component.js',
'src/utils/bytes.js',
],
},
'Text(Encoder|Decoder)\\(': {
message: 'TextEncoder/TextDecoder is not supported in all browsers.' +
'Please use UTF8 utilities from src/bytes.js',
whitelist: [
'ads/google/a4a/line-delimited-response-handler.js',
'examples/pwa/pwa.js',
'src/utils/bytes.js',
],
},
// Super complicated regex that says "find any querySelector method call that
// is passed as a variable anything that is not a string, or a string that
// contains a space.
'\\b(?:(?!\\w*[dD]oc\\w*)\\w)+\\.querySelector(?:All)?\\((?=\\s*([^\'"\\s]|[^\\s)]+\\s))[^)]*\\)': {
message: 'querySelector is not scoped to the element, but globally and ' +
'filtered to just the elements inside the element. This leads to ' +
'obscure bugs if you attempt to match a descendant of a descendant (ie ' +
'"div div"). Instead, use the scopedQuerySelector helper in dom.js',
},
'loadExtension': {
message: bannedTermsHelpString,
whitelist: [
'src/element-stub.js',
'src/friendly-iframe-embed.js',
'src/runtime.js',
'src/service/extensions-impl.js',
'src/service/lightbox-manager-discovery.js',
'src/service/crypto-impl.js',
'src/shadow-embed.js',
'src/analytics.js',
'src/extension-analytics.js',
'src/services.js',
'extensions/amp-ad/0.1/amp-ad.js',
'extensions/amp-a4a/0.1/amp-a4a.js',
'extensions/amp-ad-network-adsense-impl/0.1/amp-ad-network-adsense-impl.js',
'extensions/amp-ad-network-doubleclick-impl/0.1/amp-ad-network-doubleclick-impl.js',
'extensions/amp-lightbox-viewer/0.1/amp-lightbox-viewer.js',
],
},
'loadElementClass': {
message: bannedTermsHelpString,
whitelist: [
'src/runtime.js',
'src/service/extensions-impl.js',
'extensions/amp-ad/0.1/amp-ad.js',
'extensions/amp-a4a/0.1/amp-a4a.js',
],
},
'reject\\(\\)': {
message: 'Always supply a reason in rejections. ' +
'error.cancellation() may be applicable.',
whitelist: [
'extensions/amp-access/0.1/access-expr-impl.js',
'extensions/amp-animation/0.1/css-expr-impl.js',
'extensions/amp-bind/0.1/bind-expr-impl.js',
],
},
'[^.]loadPromise': {
message: 'Most users should use BaseElement…loadPromise.',
whitelist: [
'src/base-element.js',
'src/event-helper.js',
'src/friendly-iframe-embed.js',
'src/service/performance-impl.js',
'src/service/resources-impl.js',
'src/service/url-replacements-impl.js',
'src/service/variable-source.js',
'src/validator-integration.js',
'extensions/amp-ad/0.1/amp-ad-xorigin-iframe-handler.js',
'extensions/amp-image-lightbox/0.1/amp-image-lightbox.js',
'extensions/amp-analytics/0.1/transport.js',
'dist.3p/current/integration.js',
],
},
'\\.getTime\\(\\)': {
message: 'Unless you do weird date math (whitelist), use Date.now().',
whitelist: [
'extensions/amp-timeago/0.1/amp-timeago.js',
],
},
'\\.expandStringSync\\(': {
message: requiresReviewPrivacy,
whitelist: [
'extensions/amp-form/0.1/amp-form.js',
'src/service/url-replacements-impl.js',
],
},
'\\.expandStringAsync\\(': {
message: requiresReviewPrivacy,
whitelist: [
'extensions/amp-form/0.1/amp-form.js',
'src/service/url-replacements-impl.js',
],
},
'\\.expandInputValueSync\\(': {
message: requiresReviewPrivacy,
whitelist: [
'extensions/amp-form/0.1/amp-form.js',
'src/service/url-replacements-impl.js',
],
},
'\\.expandInputValueAsync\\(': {
message: requiresReviewPrivacy,
whitelist: [
'extensions/amp-form/0.1/amp-form.js',
'src/service/url-replacements-impl.js',
],
},
'(cdn|3p)\\.ampproject\\.': {
message: 'The CDN domain should typically not be hardcoded in source ' +
'code. Use a property of urls from src/config.js instead.',
whitelist: [
'ads/_a4a-config.js',
'build-system/app.js',
'dist.3p/current/integration.js',
'extensions/amp-iframe/0.1/amp-iframe.js',
'src/config.js',
'testing/local-amp-chrome-extension/background.js',
'tools/errortracker/errortracker.go',
'validator/nodejs/index.js',
'validator/webui/serve-standalone.go',
'build-system/tasks/check-links.js',
'build-system/tasks/extension-generator/index.js',
],
},
'\\<\\<\\<\\<\\<\\<': {
message: 'Unresolved merge conflict.',
},
'\\>\\>\\>\\>\\>\\>': {
message: 'Unresolved merge conflict.',
},
'\\.indexOf\\([\'"][^)]+\\)\\s*===?\\s*0\\b': {
message: 'use startsWith helper in src/string.js',
whitelist: [
'dist.3p/current/integration.js',
],
},
'\\.indexOf\\(.*===?.*\\.length': 'use endsWith helper in src/string.js',
'/url-parse-query-string': {
message: 'Import parseQueryString from `src/url.js`',
whitelist: [
'src/url.js',
'src/mode.js',
'dist.3p/current/integration.js',
],
},
'\\.remove\\(\\)': {
message: 'use removeElement helper in src/dom.js',
},
};
// Terms that must appear in a source file.
var requiredTerms = {
'Copyright 20(15|16|17) The AMP HTML Authors\\.':
dedicatedCopyrightNoteSources,
'Licensed under the Apache License, Version 2\\.0':
dedicatedCopyrightNoteSources,
'http\\://www\\.apache\\.org/licenses/LICENSE-2\\.0':
dedicatedCopyrightNoteSources,
};
/**
* Check if root of path is test/ or file is in a folder named test.
* @param {string} path
* @return {boolean}
*/
function isInTestFolder(path) {
var dirs = path.split('/');
var folder = dirs[dirs.length - 2];
return path.startsWith('test/') || folder == 'test';
}
function stripComments(contents) {
// Multi-line comments
contents = contents.replace(/\/\*(?!.*\*\/)(.|\n)*?\*\//g, function(match) {
// Preserve the newlines
var newlines = [];
for (var i = 0; i < match.length; i++) {
if (match[i] === '\n') {
newlines.push('\n');
}
}
return newlines.join('');
});
// Single line comments either on its own line or following a space,
// semi-colon, or closing brace
return contents.replace(/( |}|;|^) *\/\/.*/g, '$1');
}
/**
* Logs any issues found in the contents of file based on terms (regex
* patterns), and provides any possible fix information for matched terms if
* possible
*
* @param {!File} file a vinyl file object to scan for term matches
* @param {!Array<string, string>} terms Pairs of regex patterns and possible
* fix messages.
* @return {boolean} true if any of the terms match the file content,
* false otherwise
*/
function matchTerms(file, terms) {
var contents = stripComments(file.contents.toString());
var relative = file.relative;
return Object.keys(terms).map(function(term) {
var fix;
var whitelist = terms[term].whitelist;
var checkInTestFolder = terms[term].checkInTestFolder;
// NOTE: we could do a glob test instead of exact check in the future
// if needed but that might be too permissive.
if (Array.isArray(whitelist) && (whitelist.indexOf(relative) != -1 ||
isInTestFolder(relative) && !checkInTestFolder)) {
return false;
}
// we can't optimize building the `RegExp` objects early unless we build
// another mapping of term -> regexp object to be able to get back to the
// original term to get the possible fix value. This is ok as the
// presubmit doesn't have to be blazing fast and this is most likely
// negligible.
var regex = new RegExp(term, 'gm');
var index = 0;
var line = 1;
var column = 0;
var match;
var hasTerm = false;
while ((match = regex.exec(contents))) {
hasTerm = true;
for (index; index < match.index; index++) {
if (contents[index] === '\n') {
line++;
column = 1;
} else {
column++;
}
}
util.log(util.colors.red('Found forbidden: "' + match[0] +
'" in ' + relative + ':' + line + ':' + column));
if (typeof terms[term] == 'string') {
fix = terms[term];
} else {
fix = terms[term].message;
}
// log the possible fix information if provided for the term.
if (fix) {
util.log(util.colors.blue(fix));
}
util.log(util.colors.blue('=========='));
}
return hasTerm;
}).some(function(hasAnyTerm) {
return hasAnyTerm;
});
}
/**
* Test if a file's contents match any of the
* forbidden terms
*
* @param {!File} file file is a vinyl file object
* @return {boolean} true if any of the terms match the file content,
* false otherwise
*/
function hasAnyTerms(file) {
var pathname = file.path;
var basename = path.basename(pathname);
var hasTerms = false;
var hasSrcInclusiveTerms = false;
var has3pTerms = false;
hasTerms = matchTerms(file, forbiddenTerms);
var isTestFile = /^test-/.test(basename) || /^_init_tests/.test(basename)
|| /_test\.js$/.test(basename);
if (!isTestFile) {
hasSrcInclusiveTerms = matchTerms(file, forbiddenTermsSrcInclusive);
}
var is3pFile = /\/(3p|ads)\//.test(pathname) ||
basename == '3p.js' ||
basename == 'style.js';
// Yet another reason to move ads/google/a4a somewhere else
var isA4A = /\/a4a\//.test(pathname);
if (is3pFile && !isTestFile && !isA4A) {
has3pTerms = matchTerms(file, forbidden3pTerms);
}
return hasTerms || hasSrcInclusiveTerms || has3pTerms;
}
/**
* Test if a file's contents fail to match any of the required terms and log
* any missing terms
*
* @param {!File} file file is a vinyl file object
* @return {boolean} true if any of the terms are not matched in the file
* content, false otherwise
*/
function isMissingTerms(file) {
var contents = file.contents.toString();
return Object.keys(requiredTerms).map(function(term) {
var filter = requiredTerms[term];
if (!filter.test(file.path)) {
return false;
}
var matches = contents.match(new RegExp(term));
if (!matches) {
util.log(util.colors.red('Did not find required: "' + term +
'" in ' + file.relative));
util.log(util.colors.blue('=========='));
return true;
}
return false;
}).some(function(hasMissingTerm) {
return hasMissingTerm;
});
}
/**
* Check a file for all the required terms and
* any forbidden terms and log any errors found.