@backstage/eslint-plugin 0.1.11 → 0.2.0-next.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/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @backstage/eslint-plugin
2
2
 
3
+ ## 0.2.0-next.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 926389b: Added `@backstage/no-ui-css-imports-in-non-frontend` rule, which ensures that CSS from `@backstage/ui` is not imported outside of the frontend app.
8
+
9
+ ## 0.1.12
10
+
11
+ ### Patch Changes
12
+
13
+ - 5e98e61: Minor doc updates
14
+ - a1dae71: Allow frontend plugin to import from another frontend plugin with same plugin id.
15
+
16
+ This prevents the ESLint rule from incorrectly flagging these imports in the new frontend system
17
+ where plugin override requires cross-plugin imports.
18
+
3
19
  ## 0.1.11
4
20
 
5
21
  ### Patch Changes
package/README.md CHANGED
@@ -42,3 +42,4 @@ The following rules are provided by this plugin:
42
42
  | [@backstage/no-undeclared-imports](./docs/rules/no-undeclared-imports.md) | Forbid imports of external packages that have not been declared in the appropriate dependencies field in `package.json`. |
43
43
  | [@backstage/no-top-level-material-ui-4-imports](./docs/rules/no-top-level-material-ui-4-imports.md) | Forbid top level import from Material UI v4 packages. |
44
44
  | [@backstage/no-mixed-plugin-imports](./docs/rules/no-mixed-plugin-imports.md) | Disallow mixed plugin imports. |
45
+ | [@backstage/no-ui-css-imports-in-non-frontend](./docs/rules/no-ui-css-imports-in-non-frontend.md) | Ensure that only packages with the `frontend` role can import CSS files from `@backstage/ui`. |
@@ -1,6 +1,25 @@
1
1
  # @backstage/no-mixed-plugin-imports
2
2
 
3
- Disallow mixed imports between backstage plugins.
3
+ This rule ensures that imports between Backstage plugins are consistent with their intended usage.
4
+
5
+ The rule checks the `backstage.role` field in the `package.json` of both the importing and target packages to determine if they are compatible.
6
+
7
+ Plugin roles include:
8
+
9
+ - `frontend-plugin` (or `frontend` for short)
10
+ - `backend-plugin` (or `backend` for short)
11
+ - `web-library` (or `react` for short)
12
+ - `node-library` (or `node` for short)
13
+ - `common-library` (or `common` for short)
14
+
15
+ Prohibited imports include:
16
+
17
+ - A `frontend` plugin importing directly from another `frontend`, `backend`, or `node` package. Instead, it should import from the corresponding `react` or `common` package.
18
+ - With an exception with the new frontend system where frontend plugins with the same plugin id are allowed to import from each other.
19
+ - A `backend` plugin importing directly from another `backend`, `frontend`, or `react` package. Instead, it should import from the corresponding `node` or `common` package.
20
+ - A `react` package importing from `frontend`, `backend`, or `node` packages.
21
+ - A `node` package importing from `frontend`, `backend`, or `react` packages
22
+ - A `common` package importing directly from any other plugin package.
4
23
 
5
24
  ## Usage
6
25
 
@@ -10,7 +10,7 @@ Add the rules as follows, it has no options:
10
10
  "@backstage/no-relative-monorepo-imports": ["error"]
11
11
  ```
12
12
 
13
- The following patterns are considered files used during development, and only need dependencies to be declared in devDependencies:
13
+ The following patterns are considered files used during development, and only need dependencies to be declared in `devDependencies`:
14
14
 
15
15
  ```python
16
16
  !src/** # Any files outside of src are considered dev files
@@ -0,0 +1,50 @@
1
+ # no-ui-css-imports-in-non-frontend
2
+
3
+ Ensures that only packages with `backstage.role` set to `"frontend"` can import CSS files from `@backstage/ui`.
4
+
5
+ This rule prevents non-frontend packages from accidentally importing global CSS styles from `@backstage/ui`. These CSS files should only be imported by the app, never by plugins, modules or libraries.
6
+
7
+ ## Rule Details
8
+
9
+ This rule checks imports from `@backstage/ui` that end with `.css` and verifies that the importing package has `backstage.role: "frontend"` in its `package.json`.
10
+
11
+ If a package does not have a `backstage.role` field defined at all, the import is allowed (the check is skipped).
12
+
13
+ Examples of **incorrect** code for this rule:
14
+
15
+ ```js
16
+ // In a package with "backstage.role": "frontend-plugin"
17
+ import '@backstage/ui/css/styles.css';
18
+ ```
19
+
20
+ ```js
21
+ // In a package with "backstage.role": "web-library"
22
+ import '@backstage/ui/css/styles.css';
23
+ ```
24
+
25
+ ```js
26
+ // In a package with "backstage.role": "backend"
27
+ require('@backstage/ui/css/styles.css');
28
+ ```
29
+
30
+ Examples of **correct** code for this rule:
31
+
32
+ ```js
33
+ // In a package with "backstage.role": "frontend"
34
+ import '@backstage/ui/css/styles.css';
35
+ ```
36
+
37
+ ```js
38
+ // In a package without a "backstage.role" field
39
+ import '@backstage/ui/css/styles.css';
40
+ ```
41
+
42
+ ```js
43
+ // Non-CSS imports are allowed in any package
44
+ import { Button, Text } from '@backstage/ui';
45
+ ```
46
+
47
+ ```js
48
+ // In a package with "backstage.role": "backend"
49
+ import { Button } from '@backstage/ui';
50
+ ```
@@ -10,7 +10,7 @@ Add the rules as follows, it has no options:
10
10
  "@backstage/no-undeclared-imports": ["error"]
11
11
  ```
12
12
 
13
- The following patterns are considered files used during development, and only need dependencies to be declared in devDependencies:
13
+ The following patterns are considered files used during development, and only need dependencies to be declared in `devDependencies`:
14
14
 
15
15
  ```python
16
16
  !src/** # Any files outside of src are considered dev files
package/index.js CHANGED
@@ -23,6 +23,7 @@ module.exports = {
23
23
  '@backstage/no-relative-monorepo-imports': 'error',
24
24
  '@backstage/no-undeclared-imports': 'error',
25
25
  '@backstage/no-mixed-plugin-imports': 'warn',
26
+ '@backstage/no-ui-css-imports-in-non-frontend': 'error',
26
27
  },
27
28
  },
28
29
  },
@@ -32,5 +33,6 @@ module.exports = {
32
33
  'no-undeclared-imports': require('./rules/no-undeclared-imports'),
33
34
  'no-top-level-material-ui-4-imports': require('./rules/no-top-level-material-ui-4-imports'),
34
35
  'no-mixed-plugin-imports': require('./rules/no-mixed-plugin-imports'),
36
+ 'no-ui-css-imports-in-non-frontend': require('./rules/no-ui-css-imports-in-non-frontend'),
35
37
  },
36
38
  };
@@ -21,7 +21,7 @@ const manypkg = require('@manypkg/get-packages');
21
21
 
22
22
  /**
23
23
  * @typedef ExtendedPackage
24
- * @type {import('@manypkg/get-packages').Package & { packageJson: { exports?: Record<string, string>, files?: Array<string>, backstage?: { inline?: boolean, role?: string } }}} packageJson
24
+ * @type {import('@manypkg/get-packages').Package & { packageJson: { exports?: Record<string, string>, files?: Array<string>, backstage?: { inline?: boolean, role?: string, pluginId?: string } }}} packageJson
25
25
  */
26
26
 
27
27
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage/eslint-plugin",
3
- "version": "0.1.11",
3
+ "version": "0.2.0-next.0",
4
4
  "description": "Backstage ESLint plugin",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -22,7 +22,7 @@
22
22
  "minimatch": "^9.0.0"
23
23
  },
24
24
  "devDependencies": {
25
- "@backstage/cli": "^0.33.0",
25
+ "@backstage/cli": "0.34.5-next.0",
26
26
  "@types/estree": "^1.0.5",
27
27
  "eslint": "^8.33.0"
28
28
  }
@@ -64,6 +64,7 @@ module.exports = {
64
64
  messages: {
65
65
  forbidden:
66
66
  '{{sourcePackage}} ({{sourceRole}}) uses forbidden import from {{targetPackage}} ({{targetRole}}).',
67
+ useSamePluginId: `Import of {{targetPackage}} ({{targetRole}}) from {{sourceRole}} is forbidden unless you are overriding the plugin, in which case the \`backstage.pluginId\` in {{sourcePackage}}/package.json must be the same as in {{targetPackage}}`,
67
68
  useReactPlugin:
68
69
  'Use web library {{targetPackage}}-react or common library instead.',
69
70
  useNodePlugin:
@@ -146,11 +147,24 @@ module.exports = {
146
147
  }
147
148
 
148
149
  const sourceRole = pkg.packageJson.backstage?.role;
150
+ const sourcePluginId = pkg.packageJson.backstage?.pluginId;
149
151
  const targetRole = targetPackage.packageJson.backstage?.role;
152
+ const targetPluginId = targetPackage.packageJson.backstage?.pluginId;
150
153
  if (!sourceRole || !targetRole) {
151
154
  return;
152
155
  }
153
156
 
157
+ // Allow frontend plugins to import from other frontend plugins with the same pluginId for NFS
158
+ if (
159
+ sourceRole === 'frontend-plugin' &&
160
+ targetRole === 'frontend-plugin' &&
161
+ sourcePluginId &&
162
+ targetPluginId &&
163
+ sourcePluginId === targetPluginId
164
+ ) {
165
+ return;
166
+ }
167
+
154
168
  if (
155
169
  roleRules.some(
156
170
  rule =>
@@ -165,6 +179,19 @@ module.exports = {
165
179
  (sourceRole === 'frontend-plugin' || sourceRole === 'web-library') &&
166
180
  targetRole === 'frontend-plugin'
167
181
  ) {
182
+ suggest.push({
183
+ messageId: 'useSamePluginId',
184
+ data: {
185
+ targetPackage: targetName,
186
+ targetRole: targetRole,
187
+ sourcePackage: sourceName,
188
+ sourceRole: sourceRole,
189
+ },
190
+ /** @param {import('eslint').Rule.RuleFixer} _fixer */
191
+ fix(_fixer) {
192
+ // Not a fixable case, just give a suggestion to change plugin id
193
+ },
194
+ });
168
195
  suggest.push({
169
196
  messageId: 'useReactPlugin',
170
197
  data: {
@@ -0,0 +1,77 @@
1
+ /*
2
+ * Copyright 2023 The Backstage Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ // @ts-check
18
+
19
+ const visitImports = require('../lib/visitImports');
20
+ const getPackages = require('../lib/getPackages');
21
+
22
+ /** @type {import('eslint').Rule.RuleModule} */
23
+ module.exports = {
24
+ meta: {
25
+ type: 'problem',
26
+ messages: {
27
+ noCssImport:
28
+ 'CSS imports from @backstage/ui are only allowed in packages with backstage.role set to "frontend". Current role: "{{role}}"',
29
+ },
30
+ docs: {
31
+ description:
32
+ 'Ensure that only packages with backstage.role set to "frontend" can import CSS files from @backstage/ui.',
33
+ url: 'https://github.com/backstage/backstage/blob/master/packages/eslint-plugin/docs/rules/no-ui-css-imports-in-non-frontend.md',
34
+ },
35
+ },
36
+ create(context) {
37
+ const packages = getPackages(context.getCwd());
38
+ if (!packages) {
39
+ return {};
40
+ }
41
+
42
+ const currentPackage = packages.byPath(context.filename);
43
+ if (!currentPackage) {
44
+ return {};
45
+ }
46
+
47
+ return visitImports(context, (node, imp) => {
48
+ const isBuiImport =
49
+ (imp.type === 'external' || imp.type === 'internal') &&
50
+ imp.packageName === '@backstage/ui';
51
+ if (!isBuiImport) {
52
+ return;
53
+ }
54
+
55
+ const isCssImport = imp.path?.endsWith('.css');
56
+ if (!isCssImport) {
57
+ return;
58
+ }
59
+
60
+ const backstageRole = currentPackage.packageJson.backstage?.role;
61
+ if (!backstageRole) {
62
+ // Allow if no role is defined
63
+ return;
64
+ }
65
+
66
+ if (backstageRole !== 'frontend') {
67
+ context.report({
68
+ node: node,
69
+ messageId: 'noCssImport',
70
+ data: {
71
+ role: backstageRole,
72
+ },
73
+ });
74
+ }
75
+ });
76
+ },
77
+ };
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@internal/bar",
3
3
  "backstage": {
4
- "role": "frontend-plugin"
4
+ "role": "frontend-plugin",
5
+ "pluginId": "bar"
5
6
  },
6
7
  "exports": {
7
8
  ".": "./src/index.ts",
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@internal/bar-override",
3
+ "backstage": {
4
+ "role": "frontend-plugin",
5
+ "pluginId": "bar"
6
+ },
7
+ "exports": {
8
+ ".": "./src/index.ts",
9
+ "./BarPage": "./src/components/Bar.tsx",
10
+ "./package.json": "./package.json"
11
+ },
12
+ "dependencies": {
13
+ "inline-dep": "*",
14
+ "react-router": "*"
15
+ },
16
+ "devDependencies": {
17
+ "react-router-dom": "*"
18
+ }
19
+ }
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@internal/foo",
3
3
  "backstage": {
4
- "role": "frontend-plugin"
4
+ "role": "frontend-plugin",
5
+ "pluginId": "foo"
5
6
  },
6
7
  "files": [
7
8
  "dist",
@@ -0,0 +1,6 @@
1
+ {
2
+ "name": "@internal/frontend-pkg",
3
+ "backstage": {
4
+ "role": "frontend"
5
+ }
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "name": "@internal/frontend-plugin-pkg",
3
+ "backstage": {
4
+ "role": "frontend-plugin"
5
+ }
6
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "name": "@internal/no-role-pkg"
3
+ }
@@ -0,0 +1,99 @@
1
+ /*
2
+ * Copyright 2023 The Backstage Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { RuleTester } from 'eslint';
18
+ import path from 'path';
19
+ import rule from '../rules/no-ui-css-imports-in-non-frontend';
20
+
21
+ const RULE = 'no-ui-css-imports-in-non-frontend';
22
+ const FIXTURE = path.resolve(__dirname, '__fixtures__/monorepo');
23
+
24
+ const ERR = (role: string) => ({
25
+ message: `CSS imports from @backstage/ui are only allowed in packages with backstage.role set to "frontend". Current role: "${role}"`,
26
+ });
27
+
28
+ // cwd must be restored
29
+ const origDir = process.cwd();
30
+ afterAll(() => {
31
+ process.chdir(origDir);
32
+ });
33
+ process.chdir(FIXTURE);
34
+
35
+ const ruleTester = new RuleTester({
36
+ parserOptions: {
37
+ sourceType: 'module',
38
+ ecmaVersion: 2021,
39
+ },
40
+ });
41
+
42
+ ruleTester.run(RULE, rule, {
43
+ valid: [
44
+ // Frontend package can import CSS from @backstage/ui
45
+ {
46
+ code: `import '@backstage/ui/css/styles.css'`,
47
+ filename: path.join(FIXTURE, 'packages/frontend-pkg/index.ts'),
48
+ },
49
+ {
50
+ code: `import '@backstage/ui/css/other.css'`,
51
+ filename: path.join(FIXTURE, 'packages/frontend-pkg/index.ts'),
52
+ },
53
+ {
54
+ code: `require('@backstage/ui/css/styles.css')`,
55
+ filename: path.join(FIXTURE, 'packages/frontend-pkg/index.ts'),
56
+ },
57
+ // Package without backstage.role can import CSS (skip check)
58
+ {
59
+ code: `import '@backstage/ui/css/styles.css'`,
60
+ filename: path.join(FIXTURE, 'packages/no-role-pkg/index.ts'),
61
+ },
62
+ // Non-CSS imports are allowed in any package
63
+ {
64
+ code: `import { Button } from '@backstage/ui'`,
65
+ filename: path.join(FIXTURE, 'packages/frontend-plugin-pkg/index.ts'),
66
+ },
67
+ {
68
+ code: `import { Text } from '@backstage/ui/components'`,
69
+ filename: path.join(FIXTURE, 'packages/frontend-plugin-pkg/index.ts'),
70
+ },
71
+ // Imports from other packages are allowed
72
+ {
73
+ code: `import './styles.css'`,
74
+ filename: path.join(FIXTURE, 'packages/frontend-plugin-pkg/index.ts'),
75
+ },
76
+ {
77
+ code: `import 'some-other-package/styles.css'`,
78
+ filename: path.join(FIXTURE, 'packages/frontend-plugin-pkg/index.ts'),
79
+ },
80
+ ],
81
+ invalid: [
82
+ // Backend package cannot import CSS from @backstage/ui
83
+ {
84
+ code: `import '@backstage/ui/css/styles.css'`,
85
+ filename: path.join(FIXTURE, 'packages/frontend-plugin-pkg/index.ts'),
86
+ errors: [ERR('frontend-plugin')],
87
+ },
88
+ {
89
+ code: `import '@backstage/ui/css/other.css'`,
90
+ filename: path.join(FIXTURE, 'packages/frontend-plugin-pkg/index.ts'),
91
+ errors: [ERR('frontend-plugin')],
92
+ },
93
+ {
94
+ code: `require('@backstage/ui/css/styles.css')`,
95
+ filename: path.join(FIXTURE, 'packages/frontend-plugin-pkg/index.ts'),
96
+ errors: [ERR('frontend-plugin')],
97
+ },
98
+ ],
99
+ });