forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement-v1.js
161 lines (154 loc) · 4.28 KB
/
element-v1.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
import {BaseElement} from '../src/base-element';
/**
* @type {!Array<{
* name: string,
* test: function(typeof BaseElement):boolean,
* }>}
*/
const RULES = [
{
name: 'R1=true',
test: (implClass) => implClass.R1() === true,
},
{
name: 'Must not have getLayoutPriority',
notes: 'Can be replaced with getBuildPriority',
test: (implClass) => {
const hasLayoutPriority =
implClass.prototype.getLayoutPriority !==
BaseElement.prototype.getLayoutPriority;
const hasBuildPriority =
implClass.getBuildPriority !== BaseElement.getBuildPriority;
return !hasLayoutPriority || hasBuildPriority;
},
},
{
name: 'Must not have preconnectCallback',
notes: 'Can be replaced with getPreconnects',
test: (implClass) => {
const hasCallback =
implClass.prototype.preconnectCallback !==
BaseElement.prototype.preconnectCallback;
return !hasCallback;
},
},
{
name: 'Must not have layoutCallback',
notes: 'Can be replaced with mountCallback',
test: (implClass) => {
const hasCallback =
implClass.prototype.layoutCallback !==
BaseElement.prototype.layoutCallback;
return !hasCallback;
},
},
{
name: 'Must not have unlayoutCallback',
notes: 'Can be replaced with unmountCallback',
test: (implClass) => {
const hasCallback =
implClass.prototype.unlayoutCallback !==
BaseElement.prototype.unlayoutCallback;
return !hasCallback;
},
},
{
name: 'Must not have resumeCallback',
test: (implClass) => {
const hasCallback =
implClass.prototype.resumeCallback !==
BaseElement.prototype.resumeCallback;
return !hasCallback;
},
},
{
name: 'If load==true, must also have ensureLoaded',
test: (implClass) => {
const load = implClass.usesLoading();
const hasEnsureLoaded =
implClass.prototype.ensureLoaded !== BaseElement.prototype.ensureLoaded;
return !load || hasEnsureLoaded;
},
},
{
name: 'Must not use getLayoutBox',
test: (implClass) => {
return !sourceIncludes(implClass, 'getLayoutBox');
},
},
{
name: 'Must not use getLayoutSize',
test: (implClass) => {
return !sourceIncludes(implClass, 'getLayoutSize');
},
},
{
name: 'Must not have renderOutsideViewport',
test: (implClass) => {
const hasCallback =
implClass.prototype.renderOutsideViewport !==
BaseElement.prototype.renderOutsideViewport;
return !hasCallback;
},
},
{
name: 'Must not have idleRenderOutsideViewport',
test: (implClass) => {
const hasCallback =
implClass.prototype.idleRenderOutsideViewport !==
BaseElement.prototype.idleRenderOutsideViewport;
return !hasCallback;
},
},
{
name: 'Must not have isRelayoutNeeded',
test: (implClass) => {
const hasCallback =
implClass.prototype.isRelayoutNeeded !==
BaseElement.prototype.isRelayoutNeeded;
return !hasCallback;
},
},
];
/**
* @param {typeof BaseElement} implClass
* @param {{
* exceptions: (!Array<string>|undefined),
* }=} options
*/
export function testElementR1(implClass, options = {}) {
const exceptions = options.exceptions || [];
RULES.forEach(({name, notes, test}) => {
if (exceptions.includes(name)) {
expect(test(implClass), 'unused exception: ' + name).to.be.false;
} else {
expect(test(implClass), name + (notes ? `. ${notes}` : '')).to.be.true;
}
});
}
/**
* Returns `true` if the class's source contains the given substring.
*
* @param {typeof BaseElement} implClass
* @param {string} substring
* @return {boolean}
*/
function sourceIncludes(implClass, substring) {
const code = [];
code.push(implClass.toString());
const classProps = Object.getOwnPropertyDescriptors(implClass);
for (const k in classProps) {
const desc = classProps[k];
if (typeof desc.value == 'function') {
code.push(desc.value.toString());
}
}
const protoProps = Object.getOwnPropertyDescriptors(implClass.prototype);
for (const k in protoProps) {
const desc = protoProps[k];
if (typeof desc.value == 'function') {
code.push(desc.value.toString());
}
}
return code.filter((code) => code.includes(substring)).length > 0;
}