@react-native/eslint-plugin 0.72.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/BUCK ADDED
@@ -0,0 +1,23 @@
1
+ load("@fbsource//tools/build_defs/third_party:yarn_defs.bzl", "yarn_workspace")
2
+
3
+ yarn_workspace(
4
+ name = "yarn-workspace",
5
+ srcs = glob(
6
+ ["**/*.js"],
7
+ exclude = [
8
+ "**/__fixtures__/**",
9
+ "**/__flowtests__/**",
10
+ "**/__mocks__/**",
11
+ "**/__server_snapshot_tests__/**",
12
+ "**/__tests__/**",
13
+ "**/node_modules/**",
14
+ "**/node_modules/.bin/**",
15
+ "**/.*",
16
+ "**/.*/**",
17
+ "**/.*/.*",
18
+ "**/*.xcodeproj/**",
19
+ "**/*.xcworkspace/**",
20
+ ],
21
+ ),
22
+ visibility = ["PUBLIC"],
23
+ )
package/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # @react-native/eslint-plugin
2
+
3
+ This plugin is intended to be used in [`@react-native/eslint-config`](https://github.com/facebook/react-native/tree/HEAD/packages/eslint-config-react-native-community). You probably want to install that package instead.
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ yarn add --dev eslint @react-native/eslint-plugin
9
+ ```
10
+
11
+ *Note: We're using `yarn` to install deps. Feel free to change commands to use `npm` 3+ and `npx` if you like*
12
+
13
+ ## Usage
14
+
15
+ Add to your eslint config (`.eslintrc`, or `eslintConfig` field in `package.json`):
16
+
17
+ ```json
18
+ {
19
+ "plugins": ["@react-native"]
20
+ }
21
+ ```
22
+
23
+ ## Rules
24
+
25
+ ### `platform-colors`
26
+
27
+ Enforces that calls to `PlatformColor` and `DynamicColorIOS` are statically analyzable to enable performance optimizations.
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ 'use strict';
11
+
12
+ const ESLintTester = require('eslint').RuleTester;
13
+
14
+ ESLintTester.setDefaultConfig({
15
+ parser: require.resolve('@babel/eslint-parser'),
16
+ parserOptions: {
17
+ requireConfigFile: false,
18
+ ecmaVersion: 6,
19
+ sourceType: 'module',
20
+ },
21
+ });
22
+
23
+ module.exports = ESLintTester;
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ * @oncall react_native
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ const ESLintTester = require('./eslint-tester.js');
14
+
15
+ const rule = require('../platform-colors.js');
16
+
17
+ const eslintTester = new ESLintTester();
18
+
19
+ eslintTester.run('../platform-colors', rule, {
20
+ valid: [
21
+ "const color = PlatformColor('labelColor');",
22
+ "const color = PlatformColor('controlAccentColor', 'controlColor');",
23
+ "const color = DynamicColorIOS({light: 'black', dark: 'white'});",
24
+ "const color = DynamicColorIOS({light: PlatformColor('black'), dark: PlatformColor('white')});",
25
+ "const color = DynamicColorIOS({light: PlatformColor('black'), dark: PlatformColor('white'), highContrastLight: PlatformColor('black'), highContrastDark: PlatformColor('white')});",
26
+ ],
27
+ invalid: [
28
+ {
29
+ code: 'const color = PlatformColor();',
30
+ errors: [{message: rule.meta.messages.platformColorArgsLength}],
31
+ },
32
+ {
33
+ code: "const labelColor = 'labelColor'; const color = PlatformColor(labelColor);",
34
+ errors: [{message: rule.meta.messages.platformColorArgTypes}],
35
+ },
36
+ {
37
+ code: "const tuple = {light: 'black', dark: 'white'}; const color = DynamicColorIOS(tuple);",
38
+ errors: [{message: rule.meta.messages.dynamicColorIOSArg}],
39
+ },
40
+ {
41
+ code: "const black = 'black'; const color = DynamicColorIOS({light: black, dark: 'white'});",
42
+ errors: [{message: rule.meta.messages.dynamicColorIOSValue}],
43
+ },
44
+ {
45
+ code: "const white = 'white'; const color = DynamicColorIOS({light: 'black', dark: white});",
46
+ errors: [{message: rule.meta.messages.dynamicColorIOSValue}],
47
+ },
48
+ ],
49
+ });
package/index.js ADDED
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ exports.rules = {
11
+ 'platform-colors': require('./platform-colors'),
12
+ };
package/package.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "@react-native/eslint-plugin",
3
+ "version": "0.72.0",
4
+ "description": "ESLint rules for @react-native/eslint-config",
5
+ "main": "index.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git@github.com:facebook/react-native.git",
9
+ "directory": "packages/eslint-plugin-react-native-community"
10
+ },
11
+ "license": "MIT"
12
+ }
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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() and DynamicColorIOS() 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',
24
+ dynamicColorIOSValue:
25
+ 'DynamicColorIOS() value must be either a literal or a PlatformColor() call.',
26
+ },
27
+ schema: [],
28
+ },
29
+
30
+ create: function (context) {
31
+ return {
32
+ CallExpression: function (node) {
33
+ if (node.callee.name === 'PlatformColor') {
34
+ const args = node.arguments;
35
+ if (args.length === 0) {
36
+ context.report({
37
+ node,
38
+ messageId: 'platformColorArgsLength',
39
+ });
40
+ return;
41
+ }
42
+ if (!args.every(arg => arg.type === 'Literal')) {
43
+ context.report({
44
+ node,
45
+ messageId: 'platformColorArgTypes',
46
+ });
47
+ return;
48
+ }
49
+ } else if (node.callee.name === 'DynamicColorIOS') {
50
+ const args = node.arguments;
51
+ if (!(args.length === 1 && args[0].type === 'ObjectExpression')) {
52
+ context.report({
53
+ node,
54
+ messageId: 'dynamicColorIOSArg',
55
+ });
56
+ return;
57
+ }
58
+ const properties = args[0].properties;
59
+ properties.forEach(property => {
60
+ if (
61
+ !(
62
+ property.type === 'Property' &&
63
+ (property.value.type === 'Literal' ||
64
+ (property.value.type === 'CallExpression' &&
65
+ property.value.callee.name === 'PlatformColor'))
66
+ )
67
+ ) {
68
+ context.report({
69
+ node,
70
+ messageId: 'dynamicColorIOSValue',
71
+ });
72
+ return;
73
+ }
74
+ });
75
+ }
76
+ },
77
+ };
78
+ },
79
+ };