@backstage/eslint-plugin 0.1.12 → 0.2.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,17 @@
1
1
  # @backstage/eslint-plugin
2
2
 
3
+ ## 0.2.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.2.0-next.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 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.
14
+
3
15
  ## 0.1.12
4
16
 
5
17
  ### 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`. |
@@ -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
+ ```
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
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage/eslint-plugin",
3
- "version": "0.1.12",
3
+ "version": "0.2.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.34.4",
25
+ "@backstage/cli": "^0.34.5",
26
26
  "@types/estree": "^1.0.5",
27
27
  "eslint": "^8.33.0"
28
28
  }
@@ -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
+ };
@@ -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
+ });