@redhat-cloud-services/eslint-config-redhat-cloud-services 1.2.6 → 2.0.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/index.js CHANGED
@@ -22,6 +22,9 @@ module.exports = {
22
22
  'no-unused-vars': ['error', { ignoreRestSiblings: true }],
23
23
  'prettier/prettier': ['error', { singleQuote: true }],
24
24
  'rulesdir/disallow-fec-relative-imports': 2,
25
+ 'rulesdir/deprecated-packages': 1,
26
+ 'rulesdir/no-chrome-api-call-from-window': 2,
27
+ 'rulesdir/forbid-pf-relative-imports': 2,
25
28
  },
26
29
  globals: {
27
30
  CRC_APP_NAME: 'readonly',
@@ -0,0 +1,43 @@
1
+ /**
2
+ * @fileoverview Rule to warn about deprecated packages.
3
+ * @author Karel Hala
4
+ */
5
+
6
+ const deprecatedPackages = {
7
+ '@redhat-cloud-services/frontend-components-pdf-generator': {
8
+ hint: 'https://github.com/RedHatInsights/pdf-generator',
9
+ },
10
+ };
11
+
12
+ module.exports = {
13
+ meta: {
14
+ type: 'suggestion',
15
+ docs: {
16
+ description: 'disallow deprecated packages',
17
+ category: 'Possible run errors',
18
+ recommended: true,
19
+ },
20
+ fixable: 'code',
21
+ messages: {
22
+ avoidUsingDeprecatedWithHint: 'Avoid using deprecated package {{ package }}. More info can be found at {{ hint }}.',
23
+ avoidUsingDeprecated: 'Avoid using deprecated package {{ package }}.',
24
+ },
25
+ },
26
+ create: function (context) {
27
+ return {
28
+ ImportDeclaration: (codePath) => {
29
+ const [deprecatedImport, data] = Object.entries(deprecatedPackages).find(([pckg]) => codePath.source.value.includes(pckg)) || [];
30
+ if (deprecatedImport) {
31
+ context.report({
32
+ node: codePath,
33
+ messageId: data.hint ? 'avoidUsingDeprecatedWithHint' : 'avoidUsingDeprecated',
34
+ data: {
35
+ package: codePath.source.value,
36
+ ...data,
37
+ },
38
+ });
39
+ }
40
+ },
41
+ };
42
+ },
43
+ };
@@ -0,0 +1,194 @@
1
+ /**
2
+ * @fileoverview Rule to disallow relative imports from @patternfly packages to enable "treeshaking" in module federation environment
3
+ * @author Martin Marosi
4
+ */
5
+ const glob = require('glob');
6
+ const path = require('path');
7
+ const PFPackages = ['@patternfly/react-core', '@patternfly/react-icons', '@patternfly/tokens'];
8
+
9
+ const PROPS_MATCH = /Props$/g;
10
+ const VARIANT_MATCH = /Variants?$/g;
11
+ const POSITION_MATCH = /Position$/g;
12
+
13
+ const ICONS_NAME_FIX = {
14
+ AnsibeTowerIcon: 'ansibeTower-icon',
15
+ ChartSpikeIcon: 'chartSpike-icon',
16
+ CloudServerIcon: 'cloudServer-icon',
17
+ };
18
+
19
+ let CORE_CACHE = {};
20
+ let COMPONENTS_CACHE = {};
21
+
22
+ function findCoreComponent(name) {
23
+ if (CORE_CACHE[name]) {
24
+ return CORE_CACHE[name];
25
+ }
26
+ let source = glob
27
+ .sync(path.resolve(process.cwd(), `node_modules/${PFPackages[0]}/dist/esm/**/${name}.js`))
28
+ .filter((path) => !path.includes('deprecated'))?.[0];
29
+ if (!source && name.match(PROPS_MATCH)) {
30
+ source = glob
31
+ .sync(path.resolve(process.cwd(), `node_modules/${PFPackages[0]}/dist/esm/**/${name.replace(PROPS_MATCH, '')}.js`))
32
+ .filter((path) => !path.includes('deprecated'))?.[0];
33
+ }
34
+ if (!source && name.match(VARIANT_MATCH)) {
35
+ source = glob
36
+ .sync(path.resolve(process.cwd(), `node_modules/${PFPackages[0]}/dist/esm/**/${name.replace(VARIANT_MATCH, '')}.js`))
37
+ .filter((path) => !path.includes('deprecated'))?.[0];
38
+ }
39
+ if (!source && name.match(POSITION_MATCH)) {
40
+ source = glob
41
+ .sync(path.resolve(process.cwd(), `node_modules/${PFPackages[0]}/dist/esm/**/${name.replace(POSITION_MATCH, '')}.js`))
42
+ .filter((path) => !path.includes('deprecated'))?.[0];
43
+ }
44
+ if (!source) {
45
+ return false;
46
+ }
47
+ let dynamicSource = source.split('node_modules/').pop().replace('/esm/', '/dynamic/').split('/');
48
+ dynamicSource.pop();
49
+ dynamicSource = dynamicSource.join('/');
50
+ CORE_CACHE[name] = dynamicSource;
51
+ return dynamicSource;
52
+ }
53
+
54
+ function camelToDash(str) {
55
+ return str.replace(/([A-Z])/g, (g) => `-${g[0].toLowerCase()}`).replace(/^-/, '');
56
+ }
57
+
58
+ function findIcon(name) {
59
+ if (COMPONENTS_CACHE[name]) {
60
+ return COMPONENTS_CACHE[name];
61
+ }
62
+ const nameSpecifier = ICONS_NAME_FIX[name] || camelToDash(name);
63
+ return `@patternfly/react-icons/dist/dynamic/icons/${nameSpecifier}`;
64
+ }
65
+
66
+ CORE_CACHE = {
67
+ ...CORE_CACHE,
68
+ getResizeObserver: findCoreComponent('resizeObserver'),
69
+ useOUIAProps: findCoreComponent('ouia'),
70
+ OUIAProps: findCoreComponent('ouia'),
71
+ getDefaultOUIAId: findCoreComponent('ouia'),
72
+ useOUIAId: findCoreComponent('ouia'),
73
+ handleArrows: findCoreComponent('KeyboardHandler'),
74
+ setTabIndex: findCoreComponent('KeyboardHandler'),
75
+ IconComponentProps: findCoreComponent('Icon'),
76
+ TreeViewDataItem: findCoreComponent('TreeView'),
77
+ Popper: findCoreComponent('Popper/Popper'),
78
+ };
79
+
80
+ module.exports = {
81
+ meta: {
82
+ type: 'suggestion',
83
+ docs: {
84
+ description: 'forbid relative imports from PF packages',
85
+ category: 'Possible build errors',
86
+ recommended: true,
87
+ },
88
+ fixable: 'code',
89
+ messages: {
90
+ avoidRelativeImport:
91
+ 'Avoid using relative imports from {{ package }}. Use direct import path to {{ source }}. Module may be found at {{ hint }}.',
92
+ avoidRelativeIconImport:
93
+ 'Avoid using relative imports from {{ package }}. Use direct import path to {{ source }}. Module may be found at {{ hint }}.',
94
+ avoidImportingStyles: 'Avoid importing styles from {{ package }}. Styles are injected with components automatically.',
95
+ },
96
+ },
97
+ create: function (context) {
98
+ return {
99
+ ImportDeclaration: function (codePath) {
100
+ const importString = codePath.source.value;
101
+ const pfImport = importString === PFPackages[0];
102
+ const iconsImport = importString === PFPackages[1];
103
+ if (PFPackages.includes(importString) && importString.match(/(css|scss|sass)/gim)) {
104
+ context.report({
105
+ node: codePath,
106
+ messageId: 'avoidImportingStyles',
107
+ data: {
108
+ package: importString,
109
+ },
110
+ fix: function (fixer) {
111
+ return fixer.remove(codePath.parent);
112
+ },
113
+ });
114
+ }
115
+
116
+ /**
117
+ * Check if import is from FEC package and if it directly matches the package name which means its relative import
118
+ */
119
+ if (pfImport && PFPackages.includes(importString)) {
120
+ const fullImport = context.getSourceCode(codePath.parent).text;
121
+ /**
122
+ * Check if the import is not full import statement
123
+ */
124
+ if (!fullImport.includes('from')) {
125
+ return;
126
+ }
127
+ /**
128
+ * Determine correct variable for direct import
129
+ */
130
+
131
+ let variables = context.getDeclaredVariables(codePath);
132
+ let varName = 'Unknown';
133
+ if (variables.length > 0) {
134
+ varName = variables[0].name;
135
+ }
136
+
137
+ const newText = variables.map(function (data) {
138
+ const importPartial = findCoreComponent(data.name);
139
+ return `import { ${data.name} } from '${importPartial}'`;
140
+ });
141
+ context.report({
142
+ node: codePath,
143
+ messageId: 'avoidRelativeImport',
144
+ data: {
145
+ package: importString,
146
+ source: varName,
147
+ hint: newText.join('\n'),
148
+ },
149
+ fix: function (fixer) {
150
+ return fixer.replaceText(codePath, newText.join('\n'));
151
+ },
152
+ });
153
+ }
154
+
155
+ /**
156
+ * Check icons import
157
+ */
158
+ if (iconsImport) {
159
+ const fullImport = context.getSourceCode(codePath.parent).text;
160
+ /**
161
+ * Check if the import is not full import statement
162
+ */
163
+ if (!fullImport.includes('from')) {
164
+ return;
165
+ }
166
+
167
+ let variables = context.getDeclaredVariables(codePath);
168
+ let varName = 'Unknown';
169
+ if (variables.length > 0) {
170
+ varName = variables[0].name;
171
+ }
172
+
173
+ const newText = variables.map(function (data) {
174
+ const importPartial = findIcon(data.name);
175
+ return `import ${data.name} from '${importPartial}'`;
176
+ });
177
+
178
+ context.report({
179
+ node: codePath,
180
+ messageId: 'avoidRelativeIconImport',
181
+ data: {
182
+ package: importString,
183
+ source: varName,
184
+ hint: newText.join('\n'),
185
+ },
186
+ fix: function (fixer) {
187
+ return fixer.replaceText(codePath, newText.join('\n'));
188
+ },
189
+ });
190
+ }
191
+ },
192
+ };
193
+ },
194
+ };
@@ -0,0 +1,19 @@
1
+ module.exports = {
2
+ meta: {
3
+ type: 'suggestion',
4
+ messages: {
5
+ deprecateChromeApiCallFromWindow: 'Calling chrome API from the window object is deprecated.',
6
+ },
7
+ fixable: 'code',
8
+ schema: [],
9
+ },
10
+ create: function (context) {
11
+ return {
12
+ Identifier(node) {
13
+ if (node?.name === 'window' && node?.parent?.property?.name === 'insights' && node?.parent?.parent?.property?.name === 'chrome') {
14
+ context.report({ node, messageId: 'deprecateChromeApiCallFromWindow' });
15
+ }
16
+ },
17
+ };
18
+ },
19
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@redhat-cloud-services/eslint-config-redhat-cloud-services",
3
- "version": "1.2.6",
3
+ "version": "2.0.0",
4
4
  "description": "Recommended eslint configuration used in cloud.redhat.com frontend apps.",
5
5
  "keywords": [
6
6
  "eslint",