|
| 1 | +/* |
| 2 | + * ecoCode JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs |
| 3 | + * Copyright © 2023 Green Code Initiative (https://www.ecocode.io) |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +"use strict"; |
| 20 | + |
| 21 | +//------------------------------------------------------------------------------ |
| 22 | +// Requirements |
| 23 | +//------------------------------------------------------------------------------ |
| 24 | + |
| 25 | +const rule = require("../../../lib/rules/prefer-shorthand-css-notations"); |
| 26 | +const RuleTester = require("eslint").RuleTester; |
| 27 | + |
| 28 | +//------------------------------------------------------------------------------ |
| 29 | +// Tests |
| 30 | +//------------------------------------------------------------------------------ |
| 31 | + |
| 32 | +const ruleTester = new RuleTester({ |
| 33 | + parserOptions: { |
| 34 | + ecmaVersion: 2021, |
| 35 | + sourceType: "module", |
| 36 | + ecmaFeatures: { |
| 37 | + jsx: true, |
| 38 | + }, |
| 39 | + }, |
| 40 | +}); |
| 41 | + |
| 42 | +const createError = (property) => ({ |
| 43 | + messageId: "PreferShorthandCSSNotation", |
| 44 | + data: { property }, |
| 45 | + type: "JSXAttribute", |
| 46 | +}); |
| 47 | + |
| 48 | +ruleTester.run("prefer-shorthand-css-notations", rule, { |
| 49 | + valid: [ |
| 50 | + "<div class='my-class'/>", |
| 51 | + "<div style={{ animation: 'example 5s linear 2s infinite alternate' }}/>", |
| 52 | + "<div style={{ background: 'border-box red' }}/>", |
| 53 | + "<div style={{ border: '2px dotted' }}/>", |
| 54 | + "<div style={{ column: '3 100px' }}/>", |
| 55 | + "<div style={{ columnRule: '4px double #ff00ff' }}/>", |
| 56 | + "<div style={{ flex: '1 0 auto' }}/>", |
| 57 | + "<h1 style={{ font: 'italic bold 18px/150% Arial, sans-serif' }}/>", |
| 58 | + "<div style={{ grid: '1fr 2fr / row minmax(100px, auto) 200px' }}/>", |
| 59 | + "<div style={{ gridTemplate: '1fr 2fr' }}/>", |
| 60 | + "<div style={{ justifyItems: 'stretch' }}/>", |
| 61 | + "<div style={{ listStyle: 'georgian inside' }}/>", |
| 62 | + "<div style={{ margin: '10px 3px 8px 5px' }}/>", |
| 63 | + "<div style={{ offset: 'path(M 50 80 C 150 -20 250 180 350 80) 150px' }}/>", |
| 64 | + "<div style={{ outline: 'inset thick' }}/>", |
| 65 | + "<div style={{ overflow: 'visible hidden' }}/>", |
| 66 | + "<div style={{ padding: '10px 3px 8px 5px' }}/>", |
| 67 | + "<div style={{ placeContent: 'end stretch' }}/>", |
| 68 | + "<div style={{ placeItems: 'end stretch' }}/>", |
| 69 | + "<div style={{ placeSelf: 'end stretch' }}/>", |
| 70 | + "<div style={{ textDecoration: 'underline solid #f00' }}/>", |
| 71 | + "<div style={{ transition: 'width 35s ease-in-out 0s' }}/>", |
| 72 | + { |
| 73 | + code: "<div style={{ animationName: 'example', animationDuration: '5s' }}/>", |
| 74 | + options: [{ disableProperties: ["animation"] }], |
| 75 | + }, |
| 76 | + ], |
| 77 | + invalid: [ |
| 78 | + { |
| 79 | + code: "<div style={{ animationName: 'example', animationDuration: '5s' }}/>", |
| 80 | + errors: [createError("animation")], |
| 81 | + }, |
| 82 | + { |
| 83 | + code: "<div style={{ backgroundColor:'#000', backgroundImage: 'url(images/bg.png)', backgroundRepeat: 'no-repeat', backgroundPosition:'left top' }}/>", |
| 84 | + errors: [createError("background")], |
| 85 | + }, |
| 86 | + { |
| 87 | + code: "<div style={{ borderWidth: 1, borderStyle: 'solid', borderColor: '#000000' }}/>", |
| 88 | + errors: [createError("border")], |
| 89 | + }, |
| 90 | + { |
| 91 | + code: "<div style={{ columnWidth: '100px', columnCount: 3 }}/>", |
| 92 | + errors: [createError("column")], |
| 93 | + }, |
| 94 | + { |
| 95 | + code: "<div style={{ columnRuleWidth: '4px', columnRuleStyle: 'double', columnRuleColor:'#ff00ff'}}/>", |
| 96 | + errors: [createError("columnRule")], |
| 97 | + }, |
| 98 | + { |
| 99 | + code: "<div style={{ flexGrow: 1, flexShrink: 0, flexBasis: 'auto' }}/>", |
| 100 | + errors: [createError("flex")], |
| 101 | + }, |
| 102 | + { |
| 103 | + code: "<h1 style={{ fontStyle: 'normal', fontWeight: 'bold', fontSize: 18, lineHeight: '150%', fontFamily: 'Arial,sans-serif' }}/>", |
| 104 | + errors: [createError("font")], |
| 105 | + }, |
| 106 | + { |
| 107 | + code: "<div style={{ gridTemplateColumns: '1fr', gridAutoFlow: 'row', gridAutoRows:'minmax(100px, auto)', gridAutoColumns: '200px' }}/>", |
| 108 | + errors: [createError("grid")], |
| 109 | + }, |
| 110 | + { |
| 111 | + code: "<div style={{ gridTemplateColumns: '1fr', gridTemplateRows: '2fr' }}/>", |
| 112 | + errors: [createError("gridTemplate")], |
| 113 | + }, |
| 114 | + { |
| 115 | + code: "<ul style={{ listStyleType: 'disc', listStylePosition: 'inside', listStyleImage: 'url(disc.png)' }}/>", |
| 116 | + errors: [createError("listStyle")], |
| 117 | + }, |
| 118 | + { |
| 119 | + code: "<div style={{ marginTop: 10, marginBottom: 8, marginRight: 3, marginLeft: 5 }}/>", |
| 120 | + errors: [createError("margin")], |
| 121 | + }, |
| 122 | + { |
| 123 | + code: "<h1 style={{ offsetPath: 'path(M 50 80 C 150 -20 250 180 350 80)', offsetPosition: '50%' }}/>", |
| 124 | + errors: [createError("offset")], |
| 125 | + }, |
| 126 | + { |
| 127 | + code: " <div style={{ outlineWidth: 1, outlineStyle: 'solid', outlineColor: '#000000' }}/>", |
| 128 | + errors: [createError("outline")], |
| 129 | + }, |
| 130 | + { |
| 131 | + code: "<div style={{ overflowX: 'visible', overflowY: 'hidden' }}/>", |
| 132 | + errors: [createError("overflow")], |
| 133 | + }, |
| 134 | + { |
| 135 | + code: "<div style={{ paddingTop: 10, paddingBottom: 8, paddingRight: 3, paddingLeft: 5 }}/>", |
| 136 | + errors: [createError("padding")], |
| 137 | + }, |
| 138 | + { |
| 139 | + code: "<div style={{ alignContent: 'end', justifyContent: 'stretch' }}/>", |
| 140 | + errors: [createError("placeContent")], |
| 141 | + }, |
| 142 | + { |
| 143 | + code: "<div style={{ alignItems: 'end', justifyItems: 'stretch' }}/>", |
| 144 | + errors: [createError("placeItems")], |
| 145 | + }, |
| 146 | + { |
| 147 | + code: "<div style={{ alignSelf: 'end', justifySelf: 'stretch' }}/>", |
| 148 | + errors: [createError("placeSelf")], |
| 149 | + }, |
| 150 | + { |
| 151 | + code: "<div style={{ textDecorationStyle: 'solid', textDecorationColor: '#f00', textDecorationLine: 'underline' }}/>", |
| 152 | + errors: [createError("textDecoration")], |
| 153 | + }, |
| 154 | + { |
| 155 | + code: "<div style={{ transitionProperty: 'width', transitionDuration:'35s' }}/>", |
| 156 | + errors: [createError("transition")], |
| 157 | + }, |
| 158 | + ], |
| 159 | +}); |
0 commit comments