-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathclassic.js
More file actions
148 lines (124 loc) · 4.06 KB
/
classic.js
File metadata and controls
148 lines (124 loc) · 4.06 KB
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
const j = require('jscodeshift').withParser('ts');
const SilentError = require('../silent-error');
function isProperty(path, name) {
let node = path.value;
return node.type === 'ObjectProperty' && node.key.type === 'Identifier' && node.key.name === name;
}
function isStartsWithProperty(path, name) {
let node = path.value;
if (node.type === 'ObjectProperty') {
if (node.key.type === 'Identifier') {
return node.key.name.startsWith(name);
}
if (node.key.type === 'StringLiteral') {
return node.key.value.startsWith(name);
}
}
}
function isMethod(path, name) {
let node = path.value;
return node.type === 'ObjectMethod' && node.key.type === 'Identifier' && node.key.name === name;
}
function findStringProperty(properties, name, defaultValue = null) {
let propertyPath = properties.filter(path => isProperty(path, name))[0];
if (!propertyPath) {
return defaultValue;
}
let valuePath = propertyPath.get('value');
if (valuePath.value.type !== 'StringLiteral') {
throw new SilentError(`Unexpected \`${name}\` value: ${j(valuePath).toSource()}`);
}
return valuePath.value.value;
}
function findTagName(properties) {
return findStringProperty(properties, 'tagName', 'div');
}
function findElementId(properties) {
return findStringProperty(properties, 'elementId');
}
function findAriaRole(properties) {
return findStringProperty(properties, 'ariaRole');
}
function findStringArrayProperties(properties, name) {
let propertyPath = properties.filter(path => isProperty(path, name))[0];
if (!propertyPath) {
return [];
}
let arrayPath = propertyPath.get('value');
if (arrayPath.value.type !== 'ArrayExpression') {
throw new SilentError(`Unexpected \`${name}\` value: ${j(arrayPath).toSource()}`);
}
return arrayPath.get('elements').value.map(element => {
if (element.type !== 'StringLiteral') {
throw new SilentError(`Unexpected \`${name}\` value: ${j(arrayPath).toSource()}`);
}
return element.value;
});
}
function findAttributeBindings(properties) {
let attrBindings = new Map();
for (let binding of findStringArrayProperties(properties, 'attributeBindings')) {
let [value, attr] = binding.split(':');
attrBindings.set(attr || value, value);
}
return attrBindings;
}
function findClassNames(properties) {
return findStringArrayProperties(properties, 'classNames');
}
function findClassNameBindings(properties) {
let classNameBindings = new Map();
for (let binding of findStringArrayProperties(properties, 'classNameBindings')) {
let parts = binding.split(':');
if (parts.length === 1) {
throw new SilentError(`Unsupported non-boolean \`classNameBindings\` value: ${binding}`);
} else if (parts.length === 2) {
classNameBindings.set(parts[0], [parts[1], null]);
} else if (parts.length === 3) {
classNameBindings.set(parts[0], [parts[1] || null, parts[2]]);
} else {
throw new SilentError(`Unexpected \`classNameBindings\` value: ${binding}`);
}
}
return classNameBindings;
}
function findDataTestAttributes(properties) {
let nodes = properties.filter(path => isStartsWithProperty(path, 'data-test-'));
if (!nodes) {
return [];
}
const mappedProperties = nodes.map(node => {
return {
name: node.value.key.value,
value: node.value.value.value,
valueType: node.value.value.type,
};
});
let dataTestAttributes = new Map();
for (let { name, value, valueType } of mappedProperties) {
if (valueType === 'BooleanLiteral') {
if (value) {
dataTestAttributes.set(name, null);
}
} else if (valueType === 'NumericLiteral' || valueType === 'StringLiteral') {
dataTestAttributes.set(name, String(value));
} else {
throw new SilentError(`Unsupported value for '${name}'`);
}
}
return dataTestAttributes;
}
module.exports = {
isProperty,
isStartsWithProperty,
isMethod,
findStringProperty,
findStringArrayProperties,
findAriaRole,
findAttributeBindings,
findClassNames,
findClassNameBindings,
findDataTestAttributes,
findElementId,
findTagName,
};