Skip to content
This repository has been archived by the owner on Oct 21, 2021. It is now read-only.

Commit

Permalink
Merge pull request #7 from taskworld/fix-werid-things
Browse files Browse the repository at this point in the history
Fix werid things
  • Loading branch information
gkawin committed Aug 30, 2017
2 parents df472a8 + e5bccee commit cb92549
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 24 deletions.
41 changes: 28 additions & 13 deletions lib/rules/background-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,23 @@ const extractStyle = require('../utils/extractStyle')
const platforms = require('../config/platforms')
const supportMatrix = require('../assets/supportMatrix.json')

const _ = require('lodash')
const kebabCase = require('lodash/kebabCase')
const fromPairs = require('lodash/fromPairs')
const map = require('lodash/map')
const includes = require('lodash/includes')
const pick = require('lodash/pick')
const pickBy = require('lodash/pickBy')
const isString = require('lodash/isString')
const isEmpty = require('lodash/isEmpty')
const keys = require('lodash/keys')
const uniq = require('lodash/uniq')
const values = require('lodash/values')
const toLower = require('lodash/toLower')
const concat = require('lodash/concat')
const get = require('lodash/get')
const elementType = require('jsx-ast-utils/elementType')
const toTransformKebabCase = (value, style) => ([ _.kebabCase(style), value ])
const getProp = require('jsx-ast-utils/getProp')
const toTransformKebabCase = (value, style) => ([ kebabCase(style), value ])

module.exports = {
meta: {
Expand All @@ -20,34 +34,35 @@ module.exports = {
create: (context) => ({
JSXOpeningElement: (node) => {
if (!hasPropStyle(node)) return
if (isString(extractStyle(node))) return

const componentName = elementType(node)
const styles = _.fromPairs(_.map(extractStyle(node), toTransformKebabCase))
const styles = fromPairs(map(extractStyle(node), toTransformKebabCase))
const configPlaforms = context.options[1] || platforms
const hasUrlImage = detectBackgroundImageUrl(node)

let unsupported = {}
for (const style in styles) {
if (!_.includes(style, 'background')) continue
const platforms = _.pick(supportMatrix[style], configPlaforms)
if (!includes(style, 'background')) continue
const platforms = pick(supportMatrix[style], configPlaforms)
if (hasUrlImage) {
unsupported = _.pickBy(platforms, _.isString)
unsupported = pickBy(platforms, isString)
}
}

if (!_.isEmpty(unsupported)) {
const platforms = _.keys(unsupported)
const msg = _.uniq(_.values(unsupported))
if (!isEmpty(unsupported)) {
const platforms = keys(unsupported)
const msg = uniq(values(unsupported))
context.report({
node,
message: `\`background with image url\` supplied to \`${componentName}\`, in ${_.concat(platforms)}: ${_.toLower(_.concat(msg))}.`
node: get(getProp(node.attributes, 'style'), 'value', node),
message: `\`background with image url\` supplied to \`${componentName}\`, in ${concat(platforms)}: ${toLower(concat(msg))}.`
})
}
}
})
}

function detectBackgroundImageUrl (node) {
const backgroundCssValue = _.get(extractStyle(node), 'background', false)
return _.includes(backgroundCssValue, 'url(')
const backgroundCssValue = get(extractStyle(node), 'background', false)
return includes(backgroundCssValue, 'url(')
}
9 changes: 6 additions & 3 deletions lib/rules/unknown-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ const uniq = require('lodash/uniq')
const compact = require('lodash/compact')
const has = require('lodash/has')
const isEmpty = require('lodash/isEmpty')
const isString = require('lodash/isString')
const get = require('lodash/get')
const elementType = require('jsx-ast-utils/elementType')
const getProp = require('jsx-ast-utils/getProp')

const toTransformKebabCase = (value, style) => ([ kebabCase(style), value ])

Expand All @@ -27,18 +30,18 @@ module.exports = {
create: (context) => ({
JSXOpeningElement: (node) => {
if (!hasPropStyle(node)) return
if (isString(extractStyle(node))) return

const componentName = elementType(node)
const styles = fromPairs(map(extractStyle(node), toTransformKebabCase))
const keyStyles = keys(styles)
const unknownCss = uniq(compact(map(keyStyles, (key) => !has(supportMatrix, key) ? key : null)))

if (!isEmpty(unknownCss)) {
context.report({
node,
node: get(getProp(node.attributes, 'style'), 'value', node),
message: `Unknown style property \`${unknownCss.join(', ')}\` supplied to \`${componentName}\`.`
})
}
}
},
})
}
7 changes: 5 additions & 2 deletions lib/rules/unsupported-css.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const elementType = require('jsx-ast-utils/elementType')
const kebabCase = require('lodash/kebabCase')
const pick = require('lodash/pick')
const isEmpty = require('lodash/isEmpty')
Expand All @@ -11,6 +10,9 @@ const get = require('lodash/get')
const intersection = require('lodash/intersection')
const uniq = require('lodash/uniq')
const keys = require('lodash/keys')
const isString = require('lodash/isString')
const elementType = require('jsx-ast-utils/elementType')
const getProp = require('jsx-ast-utils/getProp')

const supportMatrix = require('../assets/supportMatrix.json')
const extractStyle = require('../utils/extractStyle')
Expand All @@ -29,6 +31,7 @@ module.exports = {
create: (context) => ({
JSXOpeningElement: (node) => {
if (!hasPropStyle(node)) return
if (isString(extractStyle(node))) return

const componentName = elementType(node)
const styles = extractStyle(node)
Expand All @@ -53,7 +56,7 @@ module.exports = {
}
}
if (!isEmpty(unsupportedCSS)) {
report(unsupportedCSS, componentName, false, context, node)
report(unsupportedCSS, componentName, false, context, get(getProp(node.attributes, 'style'), 'value', node))
}
},
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-email-css-rules",
"version": "0.10.3",
"version": "0.10.4",
"description": "linter rules",
"main": "lib/index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/background-image.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ruleTester.run('background css', rule, {
valid: [
{ code: '<div style={{ background: "black", textAlign: "center" }}>foo</div>' },
{ code: '<table><tr style={{ background: "transparent" }}>foo</tr></table>' },
{ code: '<table><tr style={style.foo}>foo</tr></table>' },
],
invalid: [
{
Expand Down
14 changes: 9 additions & 5 deletions tests/lib/rules/unknown-css.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@ const ruleTester = new RuleTester({

ruleTester.run('unknow css', rule, {
valid: [
{
code: '<div style={{ borderRight: "20px" }}>foooo</div>',
options: [ 'strict' ],
}
{ code: '<div style={{ borderRight: "20px" }}>foooo</div>' },
{ code: '<UserRow style={{ borderRight: "20px" }}>foooo</UserRow>' },
{ code: '<UserRow style={style.userRow}>foooo</UserRow>' },
],
invalid: [
{
code: '<div style={{ unknowCSS: "black", barCss: "1px", borderRight: "20px" }}>foo</div>',
options: [ 'strict' ],
errors: [
{ message: 'Unknown style property `unknow-css, bar-css` supplied to `div`.' },
],
},
{
code: '<UserRow style={{ borderRight: "20px", kakaNana: "20px" }}>foooo</UserRow>',
errors: [
{ message: 'Unknown style property `kaka-nana` supplied to `UserRow`.' },
],
},
]
})
1 change: 1 addition & 0 deletions tests/lib/rules/unsupported-css.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ruleTester.run('text overflow with outlook platform', rule, {
{ code: `<td style={{ textOverflow: 'clip' }}>OH BOOM!!</td>`,
options: ['strict', [ 'gmail' ]]
},
{ code: '<UserRow style={style.userRow}>foooo</UserRow>' },
],
invalid: [
{ code: `<td style={{ textOverflow: 'clip', width: '200px' }}>OH BOOM!!</td>`,
Expand Down

0 comments on commit cb92549

Please sign in to comment.