|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + */ |
| 9 | + |
| 10 | +module.exports = { |
| 11 | + meta: { |
| 12 | + type: 'problem', |
| 13 | + docs: { |
| 14 | + description: |
| 15 | + 'Ensure that PlatformColor(), DynamicColorIOS(), and ColorAndroid() are passed literals of the expected shape.', |
| 16 | + }, |
| 17 | + messages: { |
| 18 | + platformColorArgsLength: |
| 19 | + 'PlatformColor() must have at least one argument that is a literal.', |
| 20 | + platformColorArgTypes: |
| 21 | + 'PlatformColor() every argument must be a literal.', |
| 22 | + dynamicColorIOSArg: |
| 23 | + 'DynamicColorIOS() must take a single argument of type Object containing two keys: light and dark.', |
| 24 | + dynamicColorIOSLight: |
| 25 | + 'DynamicColorIOS() light value must be either a literal or a PlatformColor() call.', |
| 26 | + dynamicColorIOSDark: |
| 27 | + 'DynamicColorIOS() dark value must be either a literal or a PlatformColor() call.', |
| 28 | + colorAndroidArg: |
| 29 | + 'ColorAndroid() must take a single argument that is a literal.', |
| 30 | + }, |
| 31 | + schema: [], |
| 32 | + }, |
| 33 | + |
| 34 | + create: function(context) { |
| 35 | + return { |
| 36 | + CallExpression: function(node) { |
| 37 | + if (node.callee.name === 'PlatformColor') { |
| 38 | + const args = node.arguments; |
| 39 | + if (args.length === 0) { |
| 40 | + context.report({ |
| 41 | + node, |
| 42 | + messageId: 'platformColorArgsLength', |
| 43 | + }); |
| 44 | + return; |
| 45 | + } |
| 46 | + if (!args.every(arg => arg.type === 'Literal')) { |
| 47 | + context.report({ |
| 48 | + node, |
| 49 | + messageId: 'platformColorArgTypes', |
| 50 | + }); |
| 51 | + return; |
| 52 | + } |
| 53 | + } else if (node.callee.name === 'DynamicColorIOS') { |
| 54 | + const args = node.arguments; |
| 55 | + if (!(args.length === 1 && args[0].type === 'ObjectExpression')) { |
| 56 | + context.report({ |
| 57 | + node, |
| 58 | + messageId: 'dynamicColorIOSArg', |
| 59 | + }); |
| 60 | + return; |
| 61 | + } |
| 62 | + const properties = args[0].properties; |
| 63 | + if ( |
| 64 | + !( |
| 65 | + properties.length === 2 && |
| 66 | + properties[0].type === 'Property' && |
| 67 | + properties[0].key.name === 'light' && |
| 68 | + properties[1].type === 'Property' && |
| 69 | + properties[1].key.name === 'dark' |
| 70 | + ) |
| 71 | + ) { |
| 72 | + context.report({ |
| 73 | + node, |
| 74 | + messageId: 'dynamicColorIOSArg', |
| 75 | + }); |
| 76 | + return; |
| 77 | + } |
| 78 | + const light = properties[0]; |
| 79 | + if ( |
| 80 | + !( |
| 81 | + light.value.type === 'Literal' || |
| 82 | + (light.value.type === 'CallExpression' && |
| 83 | + light.value.callee.name === 'PlatformColor') |
| 84 | + ) |
| 85 | + ) { |
| 86 | + context.report({ |
| 87 | + node, |
| 88 | + messageId: 'dynamicColorIOSLight', |
| 89 | + }); |
| 90 | + return; |
| 91 | + } |
| 92 | + const dark = properties[1]; |
| 93 | + if ( |
| 94 | + !( |
| 95 | + dark.value.type === 'Literal' || |
| 96 | + (dark.value.type === 'CallExpression' && |
| 97 | + dark.value.callee.name === 'PlatformColor') |
| 98 | + ) |
| 99 | + ) { |
| 100 | + context.report({ |
| 101 | + node, |
| 102 | + messageId: 'dynamicColorIOSDark', |
| 103 | + }); |
| 104 | + return; |
| 105 | + } |
| 106 | + } else if (node.callee.name === 'ColorAndroid') { |
| 107 | + const args = node.arguments; |
| 108 | + if (!(args.length === 1 && args[0].type === 'Literal')) { |
| 109 | + context.report({ |
| 110 | + node, |
| 111 | + messageId: 'colorAndroidArg', |
| 112 | + }); |
| 113 | + return; |
| 114 | + } |
| 115 | + } |
| 116 | + }, |
| 117 | + }; |
| 118 | + }, |
| 119 | +}; |
0 commit comments