restrict imports from @scm-manager source directories

This commit is contained in:
Sebastian Sdorra
2020-06-16 07:23:23 +02:00
parent 58b2e4b30a
commit 685acbc6fc
4 changed files with 45 additions and 15 deletions

View File

@@ -0,0 +1,6 @@
import React, { FC } from "react";
import { Button } from "@scm-manager/ui-components";
const SpecialButton: FC = () => <Button color="primary">Special</Button>;
export default SpecialButton;

View File

@@ -0,0 +1,6 @@
import React, { FC } from "react";
import Button from "@scm-manager/ui-components/src/buttons/Button";
const SpecialButton: FC = () => <Button color="primary">Special</Button>;
export default SpecialButton;

View File

@@ -37,6 +37,10 @@ const nodeConfiguration = {
} }
}; };
const restrictImportConfig = {
patterns: ["@scm-manager/*/*"]
};
const typescriptConfiguration = { const typescriptConfiguration = {
parser: "@typescript-eslint/parser", parser: "@typescript-eslint/parser",
extends: ["react-app", "plugin:@typescript-eslint/recommended"], extends: ["react-app", "plugin:@typescript-eslint/recommended"],
@@ -45,6 +49,7 @@ const typescriptConfiguration = {
"@typescript-eslint/ban-ts-ignore": "warn", "@typescript-eslint/ban-ts-ignore": "warn",
"no-console": "error", "no-console": "error",
"jsx-a11y/href-no-hash": "off", "jsx-a11y/href-no-hash": "off",
"no-restricted-imports": ["error", restrictImportConfig],
...rules ...rules
} }
}; };

View File

@@ -25,11 +25,10 @@
const { ESLint } = require("eslint"); const { ESLint } = require("eslint");
const path = require("path"); const path = require("path");
describe("should lint files", () => { const eslint = new ESLint();
const eslint = new ESLint(); const resource = path.join(__dirname, "__resources__");
const resource = path.join(__dirname, "__resources__");
const lint = async file => { const lint = async file => {
const results = await eslint.lintFiles([path.join(resource, file)]); const results = await eslint.lintFiles([path.join(resource, file)]);
const { messages } = results[0]; const { messages } = results[0];
@@ -40,12 +39,13 @@ describe("should lint files", () => {
errors, errors,
warnings warnings
}; };
}; };
const expectContains = (results, ...ids) => { const expectContains = (results, ...ids) => {
ids.forEach(id => expect(results).toContain(id)); ids.forEach(id => expect(results).toContain(id));
}; };
describe("should lint different file types", () => {
it("should lint tsx files", async () => { it("should lint tsx files", async () => {
const { errors, warnings } = await lint("TypescriptWithJsx.tsx"); const { errors, warnings } = await lint("TypescriptWithJsx.tsx");
expectContains(errors, "no-console", "quotes", "semi"); expectContains(errors, "no-console", "quotes", "semi");
@@ -64,3 +64,16 @@ describe("should lint files", () => {
expectContains(warnings, "prettier/prettier"); expectContains(warnings, "prettier/prettier");
}); });
}); });
describe("lint @scm-manager imports", () => {
it("should return an error for source imports", async () => {
const { errors } = await lint("AvoidSourceImport.tsx");
expectContains(errors, "no-restricted-imports");
});
it("should return no error for package imports", async () => {
const { errors, warnings } = await lint("AllowRootImport.tsx");
expect(errors).toEqual([]);
expect(warnings).toEqual([]);
});
});