Skip to content

Commit

Permalink
feat(linter): Support no fail on empty lint run
Browse files Browse the repository at this point in the history
  • Loading branch information
aovens-quantifi committed Oct 31, 2023
1 parent 8ec589d commit 0775a70
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/generated/packages/eslint/executors/lint.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@
"type": "string",
"description": "The equivalent of the `--print-config` flag on the ESLint CLI.",
"x-completion-type": "file"
},
"errorOnUnmatchedPattern": {
"type": "boolean",
"description": "When set to false, equivalent of the `--no-error-on-unmatched-pattern` flag on the ESLint CLI.",
"default": true
}
},
"required": ["lintFilePatterns"],
Expand Down
29 changes: 29 additions & 0 deletions packages/eslint/src/executors/lint/lint.impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function createValidRunBuilderOptions(
resolvePluginsRelativeTo: null,
reportUnusedDisableDirectives: null,
printConfig: null,
errorOnUnmatchedPattern: true,
...additionalOptions,
};
}
Expand Down Expand Up @@ -212,6 +213,34 @@ describe('Linter Builder', () => {
mockIsPathIgnored.mockReturnValue(Promise.resolve(false));
});

it('should not throw if no reports generated and errorOnUnmatchedPattern is false', async () => {
mockReports = [];
setupMocks();
const result = lintExecutor(
createValidRunBuilderOptions({
lintFilePatterns: ['includedFile1'],
errorOnUnmatchedPattern: false,
}),
mockContext
);
await expect(result).resolves.not.toThrow();
});

it('should not throw if pattern excluded and errorOnUnmatchedPattern is false', async () => {
mockReports = [];
setupMocks();
mockIsPathIgnored.mockReturnValue(Promise.resolve(true));
const result = lintExecutor(
createValidRunBuilderOptions({
lintFilePatterns: ['includedFile1'],
errorOnUnmatchedPattern: false,
}),
mockContext
);
await expect(result).resolves.not.toThrow();
mockIsPathIgnored.mockReturnValue(Promise.resolve(false));
});

it('should create a new instance of the formatter with the selected user option', async () => {
setupMocks();
await lintExecutor(
Expand Down
5 changes: 3 additions & 2 deletions packages/eslint/src/executors/lint/lint.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export default async function run(
? joinPathFragments(options.cacheLocation, projectName)
: undefined;

const { printConfig, ...normalizedOptions } = options;
const { printConfig, errorOnUnmatchedPattern, ...normalizedOptions } =
options;

/**
* Until ESLint v9 is released and the new so called flat config is the default
Expand Down Expand Up @@ -119,7 +120,7 @@ Please see https://nx.dev/guides/eslint for full guidance on how to resolve this
throw err;
}

if (lintResults.length === 0) {
if (lintResults.length === 0 && errorOnUnmatchedPattern) {
const ignoredPatterns = (
await Promise.all(
normalizedOptions.lintFilePatterns.map(async (pattern) =>
Expand Down
1 change: 1 addition & 0 deletions packages/eslint/src/executors/lint/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface Schema extends JsonObject {
resolvePluginsRelativeTo: string | null;
reportUnusedDisableDirectives: Linter.StringSeverity | null;
printConfig?: string | null;
errorOnUnmatchedPattern?: boolean;
}

type Formatter =
Expand Down
5 changes: 5 additions & 0 deletions packages/eslint/src/executors/lint/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@
"type": "string",
"description": "The equivalent of the `--print-config` flag on the ESLint CLI.",
"x-completion-type": "file"
},
"errorOnUnmatchedPattern": {
"type": "boolean",
"description": "When set to false, equivalent of the `--no-error-on-unmatched-pattern` flag on the ESLint CLI.",
"default": true
}
},
"required": ["lintFilePatterns"],
Expand Down

0 comments on commit 0775a70

Please sign in to comment.