zhaoyongjie commented on a change in pull request #19239:
URL: https://github.com/apache/superset/pull/19239#discussion_r829665971



##########
File path: superset-frontend/eslint/eslint-plugin-literal-colors/index.js
##########
@@ -0,0 +1,132 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * @fileoverview Rule to warn about literal colors
+ * @author Apache
+ */
+
+const cssData = require('mdn-data').css;
+const _ = require('lodash');
+
+const ALLOWED_VALUES = [
+  'auto',
+  'inherit',
+  'initial',
+  'unset',
+  'none',
+  'revert',
+  'invert',
+];
+
+function getObjectExpressionPropertyNodes(
+  objectExpressionNode,
+  propertyNodes = [],
+) {
+  _.each(objectExpressionNode.properties, propertyNode => {
+    propertyNodes.push(propertyNode);
+
+    if (
+      propertyNode.value &&
+      propertyNode.value.type &&
+      propertyNode.value.type === 'ObjectExpression'

Review comment:
       use optional chaining to simplify expression
   ```suggestion
         propertyNode?.value?.type === 'ObjectExpression'
   ```

##########
File path: superset-frontend/eslint/eslint-plugin-literal-colors/index.js
##########
@@ -0,0 +1,132 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * @fileoverview Rule to warn about literal colors
+ * @author Apache
+ */
+
+const cssData = require('mdn-data').css;
+const _ = require('lodash');
+
+const ALLOWED_VALUES = [
+  'auto',
+  'inherit',
+  'initial',
+  'unset',
+  'none',
+  'revert',
+  'invert',
+];
+
+function getObjectExpressionPropertyNodes(
+  objectExpressionNode,
+  propertyNodes = [],
+) {
+  _.each(objectExpressionNode.properties, propertyNode => {
+    propertyNodes.push(propertyNode);
+
+    if (
+      propertyNode.value &&
+      propertyNode.value.type &&
+      propertyNode.value.type === 'ObjectExpression'
+    ) {
+      propertyNodes.concat(
+        getObjectExpressionPropertyNodes(propertyNode.value, propertyNodes),
+      );
+    }
+  });
+
+  return propertyNodes;
+}
+
+// returns all CSS properties that might contain a color
+function getColorProperties() {
+  const colorProperties = [];
+  const cssProperties = Object.keys(cssData.properties);
+
+  cssProperties.forEach(key => {
+    const cssProp = cssData.properties[key];
+    if (cssProp.syntax.includes('<color>')) {
+      const positions = cssProp.syntax.includes('||')
+        ? cssProp.syntax.split(' || ')
+        : ['<color>'];
+      // the position of the color in the CSS property value
+      const colorPosition = positions.indexOf('<color>');
+      colorProperties.push({
+        name: key,
+        colorPosition,
+        ...cssProp,
+      });
+    }
+  });
+  return colorProperties;
+}
+
+//------------------------------------------------------------------------------
+// Rule Definition
+//------------------------------------------------------------------------------
+
+/** @type {import('eslint').Rule.RuleModule} */
+module.exports = {
+  rules: {
+    'no-literal-colors': {
+      create(context) {
+        return {
+          ObjectExpression(node) {
+            const propertyNodes = _.uniqBy(
+              getObjectExpressionPropertyNodes(node),
+              'loc',
+            );
+            propertyNodes.forEach(prop => {
+              if (
+                prop.value &&
+                prop.value.type &&
+                prop.value.type === 'Literal' &&

Review comment:
       use conditional chaining

##########
File path: superset-frontend/package.json
##########
@@ -126,6 +126,7 @@
     "d3-scale": "^2.1.2",
     "dom-to-image": "^2.6.0",
     "emotion-rgba": "0.0.9",
+    "eslint-plugin-literal-colors": "file:eslint/eslint-plugin-literal-colors",

Review comment:
       use relative directory. An additional suggestion, use a more generic 
directory name, for example: `build-tools` or other wises.
   
   ```
       "eslint-plugin-literal-colors": 
"./file:eslint/eslint-plugin-literal-colors",
   ```

##########
File path: superset-frontend/eslint/eslint-plugin-literal-colors/index.js
##########
@@ -0,0 +1,132 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * @fileoverview Rule to warn about literal colors
+ * @author Apache
+ */
+
+const cssData = require('mdn-data').css;
+const _ = require('lodash');
+
+const ALLOWED_VALUES = [
+  'auto',
+  'inherit',
+  'initial',
+  'unset',
+  'none',
+  'revert',
+  'invert',
+];
+
+function getObjectExpressionPropertyNodes(
+  objectExpressionNode,
+  propertyNodes = [],
+) {
+  _.each(objectExpressionNode.properties, propertyNode => {
+    propertyNodes.push(propertyNode);
+
+    if (
+      propertyNode.value &&
+      propertyNode.value.type &&
+      propertyNode.value.type === 'ObjectExpression'

Review comment:
       use optional chaining to simplify expression
   ```suggestion
         propertyNode?.value?.type === 'ObjectExpression'
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org
For additional commands, e-mail: notifications-h...@superset.apache.org

Reply via email to