Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/remove sheriff.config from lint programatically #168

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"build:all": "npx nx run-many --target=build && chmod +x dist/packages/core/src/bin/main.js",
"commit": "commit",
"lint:all": "eslint ./packages",
"link:sheriff": "npm run build:all && yalc publish dist/packages/core && yalc publish dist/packages/eslint-plugin && yalc link @softarc/sheriff-core @softarc/eslint-plugin-sheriff",
"link:sheriff": "yarn run build:all && yalc publish dist/packages/core && yalc publish dist/packages/eslint-plugin && yalc link @softarc/sheriff-core @softarc/eslint-plugin-sheriff",
"run:cli": "nx build core && chmod +x dist/packages/core/src/bin/main.js",
"test": "vitest",
"test:ci": "vitest -c vitest.config.ci.ts"
Expand Down
27 changes: 20 additions & 7 deletions packages/eslint-plugin/src/lib/rules/create-rule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Rule } from 'eslint';
import {Executor, ExecutorNode} from './executor';
import { UserError } from '@softarc/sheriff-core';
import { Rule } from 'eslint';
import path from 'path';
import { Executor, ExecutorNode } from './executor';
import { FileFilter } from './file-filter';

/**
* Factory function generating a rule that traverses
Expand All @@ -15,15 +17,18 @@ import { UserError } from '@softarc/sheriff-core';
*/
export const createRule: (
ruleName: string,
fileFilter: FileFilter,
executor: Executor,
) => Rule.RuleModule = (ruleName, executor) => ({
) => Rule.RuleModule = (ruleName, fileFilter, executor) => ({
create: (context) => {
const filename = context.filename ?? context.getFilename();
if (isExcluded(fileFilter, filename)) {
return {};
}

let isFirstRun = true;
let hasInternalError = false;
const executeRuleWithContext = (
node: ExecutorNode,
) => {
const filename = context.filename ?? context.getFilename();
const executeRuleWithContext = (node: ExecutorNode) => {
const sourceCode =
context.sourceCode?.text ?? context.getSourceCode().text;

Expand Down Expand Up @@ -60,3 +65,11 @@ export const createRule: (
};
},
});

function isExcluded({ include, exclude }: FileFilter, filePath: string) {
const fileName = path.basename(filePath);
return (
(include && !include.some((includeFile) => fileName === includeFile)) ||
(exclude && exclude.some((excludeFile) => fileName === excludeFile))
);
}
4 changes: 3 additions & 1 deletion packages/eslint-plugin/src/lib/rules/deep-import.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { violatesEncapsulationRule } from '@softarc/sheriff-core';
import { createRule } from './create-rule';
import { excludeSheriffConfig } from './file-filter';

export const deepImport = createRule(
'Deep Import',
excludeSheriffConfig,
(context, node, isFirstRun, filename, sourceCode) => {
const importValue = (node.source as { value: string }).value;
const message = violatesEncapsulationRule(
filename,
importValue,
isFirstRun,
sourceCode,
true
true,
);
if (message) {
context.report({
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/lib/rules/dependency-rule.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { violatesDependencyRule } from '@softarc/sheriff-core';
import { createRule } from './create-rule';
import { excludeSheriffConfig } from './file-filter';

export const dependencyRule = createRule(
'Dependency Rule',
excludeSheriffConfig,
(context, node, isFirstRun, filename, sourceCode) => {
const importValue = (node.source as { value: string }).value;
const message = violatesDependencyRule(
Expand Down
4 changes: 3 additions & 1 deletion packages/eslint-plugin/src/lib/rules/encapsulation.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { violatesEncapsulationRule } from '@softarc/sheriff-core';
import { createRule } from './create-rule';
import { excludeSheriffConfig } from './file-filter';

export const encapsulation = createRule(
'Encapsulation',
excludeSheriffConfig,
(context, node, isFirstRun, filename, sourceCode) => {
const importValue = (node.source as { value: string }).value;
const message = violatesEncapsulationRule(
filename,
importValue,
isFirstRun,
sourceCode,
false
false,
);
if (message) {
context.report({
Expand Down
14 changes: 14 additions & 0 deletions packages/eslint-plugin/src/lib/rules/file-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface FileFilter {
include?: string[];
exclude?: string[];
}

export const sheriffConfigFileName = 'sheriff.config.ts';

export const excludeSheriffConfig: FileFilter = {
exclude: [sheriffConfigFileName],
};

export const onlySheriffConfig: FileFilter = {
include: [sheriffConfigFileName],
};
26 changes: 21 additions & 5 deletions packages/eslint-plugin/src/lib/rules/tests/create-rule.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { UserError } from '@softarc/sheriff-core';
import { RuleTester } from 'eslint';
import { parser } from 'typescript-eslint';
import { afterEach, describe, expect, it, vitest } from 'vitest';
import { createRule } from '../create-rule';
import { UserError } from '@softarc/sheriff-core';
import { parser } from 'typescript-eslint';
import { excludeSheriffConfig, sheriffConfigFileName } from '../file-filter';

const tester = new RuleTester({
languageOptions: { parser, sourceType: 'module' }
languageOptions: { parser, sourceType: 'module' },
});

const ruleExecutor = { foo: () => void true };
const spy = vitest.spyOn(ruleExecutor, 'foo');

export const testRule = createRule('Test Rule', () => {
export const testRule = createRule('Test Rule', excludeSheriffConfig, () => {
ruleExecutor.foo();
});

Expand All @@ -34,6 +35,21 @@ describe('create rule', () => {
expect(spy).toHaveBeenCalledTimes(2);
});

it('should NOT call the rule executor for any import or export types when filename is "sheriff.config.ts"', () => {
tester.run('test-rule', testRule, {
valid: [
{
filename: sheriffConfigFileName,
code: `import {AppComponent} from './app.component'
const a = new AppComponent();
import('../util.ts')`,
},
],
invalid: [],
});
expect(spy).toHaveBeenCalledTimes(0);
});

for (const { throwing, message } of [
{
throwing: new Error('Test Error'),
Expand Down Expand Up @@ -84,7 +100,7 @@ describe('create rule', () => {
`,
},
],
invalid: []
invalid: [],
});

expect(spy).toHaveBeenCalledTimes(2);
Expand Down
1 change: 0 additions & 1 deletion tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"extends": "./tsconfig.base.json",
"include": ["**/*.spec.ts"]
]
}
Loading