-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathStyleValidator.js
68 lines (60 loc) · 1.99 KB
/
StyleValidator.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
import supportMatrix from './supportMatrix.json'
const capsRe = /[A-Z]/g
export default class StyleValidator {
constructor(config) {
this.setConfig(config)
}
setConfig(config) {
this.config = {
strict: true,
warn: true,
platforms: [
'gmail',
'gmail-android',
'apple-mail',
'apple-ios',
'yahoo-mail',
'outlook',
'outlook-legacy',
'outlook-web',
],
...config,
}
}
validate(style, componentName) {
// eslint-disable-next-line no-restricted-syntax
for (const propNameCamelCase of Object.keys(style)) {
const propName = propNameCamelCase.replace(capsRe, match => `-${match[0].toLowerCase()}`)
const supportInfo = supportMatrix[propName]
if (!supportInfo) {
if (this.config.strict) {
return new Error(`Unknown style property \`${propName}\` supplied to \`${componentName}\`.`)
}
} else {
const unsupported = []
const messages = new Map()
this.config.platforms.forEach((platform) => {
if (typeof supportInfo[platform] === 'string') {
const msg = supportInfo[platform]
if (!messages.has(msg)) {
messages.set(msg, [])
}
messages.get(msg).push(platform)
} else if (supportInfo[platform] === false) {
unsupported.push(platform)
}
})
if (this.config.warn) {
// eslint-disable-next-line no-restricted-syntax
for (const [msg, platforms] of messages) {
console.warn(`Warning: Style property \`${propName}\` supplied to \`${componentName}\`, in ${platforms.join(', ')}: ${msg.toLowerCase()}`) // eslint-disable-line no-console
}
}
if (unsupported.length && this.config.strict) {
return new Error(`Style property \`${propName}\` supplied to \`${componentName}\` unsupported in: ${unsupported.join(', ')}.`)
}
}
}
return undefined
}
}