Skip to content

Commit

Permalink
fix: remove @types/eslint dependency as eslint is optional
Browse files Browse the repository at this point in the history
Closes: #379
  • Loading branch information
piotr-oles committed Jan 21, 2020
1 parent 3c3b894 commit a9db0d0
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 40 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -85,7 +85,6 @@
"devDependencies": {
"@commitlint/config-conventional": "^7.5.0",
"@types/babel-code-frame": "^6.20.1",
"@types/eslint": "^4.16.6",
"@types/jest": "^24.0.11",
"@types/lodash": "^4.14.134",
"@types/micromatch": "^3.1.0",
Expand Down
5 changes: 2 additions & 3 deletions src/ApiIncrementalChecker.ts
@@ -1,5 +1,3 @@
import * as eslint from 'eslint'; // Imported for types alone

import {
IncrementalCheckerInterface,
IncrementalCheckerParams
Expand All @@ -11,11 +9,12 @@ import {
createIssuesFromTsDiagnostics,
createIssuesFromEsLintReports
} from './issue';
import { LintReport } from './types/eslint';

export class ApiIncrementalChecker implements IncrementalCheckerInterface {
protected readonly tsIncrementalCompiler: CompilerHost;

private currentEsLintErrors = new Map<string, eslint.CLIEngine.LintReport>();
private currentEsLintErrors = new Map<string, LintReport>();
private lastUpdatedFiles: string[] = [];
private lastRemovedFiles: string[] = [];

Expand Down
4 changes: 2 additions & 2 deletions src/FilesRegister.ts
@@ -1,10 +1,10 @@
import * as ts from 'typescript'; // import for types alone
import * as eslint from 'eslint'; // import for types alone
import { LintReport } from './types/eslint';

export interface DataShape {
source?: ts.SourceFile;
linted: boolean;
eslints: eslint.CLIEngine.LintReport[];
eslints: LintReport[];
}

export class FilesRegister {
Expand Down
6 changes: 3 additions & 3 deletions src/IncrementalChecker.ts
@@ -1,7 +1,6 @@
import * as fs from 'fs';
import * as path from 'path';
import * as ts from 'typescript'; // Imported for types alone; actual requires take place in methods below
import * as eslint from 'eslint';

import { FilesRegister } from './FilesRegister';
import { CancellationToken } from './CancellationToken';
Expand All @@ -22,6 +21,7 @@ import {
createIssuesFromEsLintReports,
createIssuesFromTsDiagnostics
} from './issue';
import { LintReport } from './types/eslint';

export class IncrementalChecker implements IncrementalCheckerInterface {
private files = new FilesRegister(() => ({
Expand Down Expand Up @@ -261,7 +261,7 @@ export class IncrementalChecker implements IncrementalCheckerInterface {
!IncrementalChecker.isFileExcluded(filePath)
);

const currentEsLintErrors = new Map<string, eslint.CLIEngine.LintReport>();
const currentEsLintErrors = new Map<string, LintReport>();
filesToLint.forEach(fileName => {
cancellationToken.throwIfCancellationRequested();

Expand Down Expand Up @@ -289,7 +289,7 @@ export class IncrementalChecker implements IncrementalCheckerInterface {

const reports = this.files
.keys()
.reduce<eslint.CLIEngine.LintReport[]>(
.reduce<LintReport[]>(
(innerLints, filePath) =>
innerLints.concat(this.files.getData(filePath).eslints),
[]
Expand Down
6 changes: 3 additions & 3 deletions src/createEslinter.ts
@@ -1,16 +1,16 @@
import * as eslint from 'eslint'; // import for types alone
import * as path from 'path';

import { LintReport } from './types/eslint';
import { throwIfIsInvalidSourceFileError } from './FsHelper';

export function createEslinter(eslintOptions: object) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { CLIEngine }: typeof eslint = require('eslint');
const { CLIEngine } = require('eslint');

// See https://eslint.org/docs/1.0.0/developer-guide/nodejs-api#cliengine
const eslinter = new CLIEngine(eslintOptions);

function getReport(filepath: string) {
function getReport(filepath: string): LintReport | undefined {
try {
if (
eslinter.isPathIgnored(filepath) ||
Expand Down
12 changes: 5 additions & 7 deletions src/issue/eslint/EsLintIssueFactory.ts
@@ -1,8 +1,8 @@
import * as eslint from 'eslint';
import { FileAwareEsLintMessage } from './FileAwareEsLintMessage';
import { deduplicateAndSortIssues, Issue } from '../Issue';
import { IssueOrigin } from '../IssueOrigin';
import { IssueSeverity } from '../IssueSeverity';
import { LintReport, LintResult } from '../../types/eslint';

function createIssueFromEsLintMessage(message: FileAwareEsLintMessage): Issue {
return {
Expand All @@ -18,7 +18,7 @@ function createIssueFromEsLintMessage(message: FileAwareEsLintMessage): Issue {
}

function createFileAwareEsLintMessagesFromEsLintResult(
result: eslint.CLIEngine.LintResult
result: LintResult
): FileAwareEsLintMessage[] {
return result.messages.map(message => ({
...message,
Expand All @@ -27,7 +27,7 @@ function createFileAwareEsLintMessagesFromEsLintResult(
}

function createFileAwareEsLintMessagesFromEsLintReport(
report: eslint.CLIEngine.LintReport
report: LintReport
): FileAwareEsLintMessage[] {
return report.results.reduce<FileAwareEsLintMessage[]>(
(messages, result) => [
Expand All @@ -39,7 +39,7 @@ function createFileAwareEsLintMessagesFromEsLintReport(
}

function createFileAwareEsLintMessagesFromEsLintReports(
reports: eslint.CLIEngine.LintReport[]
reports: LintReport[]
): FileAwareEsLintMessage[] {
return reports.reduce<FileAwareEsLintMessage[]>(
(messages, report) => [
Expand All @@ -56,9 +56,7 @@ function createIssuesFromEsLintMessages(
return deduplicateAndSortIssues(messages.map(createIssueFromEsLintMessage));
}

function createIssuesFromEsLintReports(
reports: eslint.CLIEngine.LintReport[]
): Issue[] {
function createIssuesFromEsLintReports(reports: LintReport[]): Issue[] {
return createIssuesFromEsLintMessages(
createFileAwareEsLintMessagesFromEsLintReports(reports)
);
Expand Down
4 changes: 2 additions & 2 deletions src/issue/eslint/FileAwareEsLintMessage.ts
@@ -1,10 +1,10 @@
import * as eslint from 'eslint';
import { LintMessage } from '../../types/eslint';

/**
* We need to define custom interface because of eslint architecture which
* groups lint messages per file
*/
interface FileAwareEsLintMessage extends eslint.Linter.LintMessage {
interface FileAwareEsLintMessage extends LintMessage {
filePath?: string;
}

Expand Down
36 changes: 36 additions & 0 deletions src/types/eslint.ts
@@ -0,0 +1,36 @@
// copy eslint types as installation of eslint package is optional
export interface LintMessage {
column: number;
line: number;
endColumn?: number;
endLine?: number;
ruleId: string | null;
message: string;
nodeType: string;
fatal?: true;
severity: 0 | 1 | 2;
fix?: {
range: [number, number];
text: string;
};
source: string | null;
}

export interface LintResult {
filePath: string;
messages: LintMessage[];
errorCount: number;
warningCount: number;
fixableErrorCount: number;
fixableWarningCount: number;
output?: string;
source?: string;
}

export interface LintReport {
results: LintResult[];
errorCount: number;
warningCount: number;
fixableErrorCount: number;
fixableWarningCount: number;
}
10 changes: 5 additions & 5 deletions test/unit/issue/eslint/EsLintIssueFactory.spec.ts
@@ -1,11 +1,11 @@
import { createIssuesFromEsLintReports } from '../../../../lib/issue';
import * as eslint from 'eslint';
import {
LintMessage,
LintReport,
LintResult
} from '../../../../lib/types/eslint';

describe('[UNIT] issue/eslint/EsLintIssueFactory', () => {
type LintMessage = eslint.Linter.LintMessage;
type LintResult = eslint.CLIEngine.LintResult;
type LintReport = eslint.CLIEngine.LintReport;

const ES_LINT_MESSAGE_ERROR: LintMessage = {
column: 0,
line: 13,
Expand Down
15 changes: 1 addition & 14 deletions yarn.lock
Expand Up @@ -490,19 +490,6 @@
resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d"
integrity sha512-OCutwjDZ4aFS6PB1UZ988C4YgwlBHJd6wCeQqaLdmadZ/7e+w79+hbMUFC1QXDNCmdyoRfAFdm0RypzwR+Qpag==

"@types/eslint@^4.16.6":
version "4.16.6"
resolved "https://registry.yarnpkg.com/@types/eslint/-/eslint-4.16.6.tgz#96d4ecddbea618ab0b55eaf0dffedf387129b06c"
integrity sha512-GL7tGJig55FeclpOytU7nCCqtR143jBoC7AUdH0DO9xBSIFiNNUFCY/S3KNWsHeQJuU3hjw/OC1+kRTFNXqUZQ==
dependencies:
"@types/estree" "*"
"@types/json-schema" "*"

"@types/estree@*":
version "0.0.39"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==

"@types/events@*":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@types/events/-/events-1.2.0.tgz#81a6731ce4df43619e5c8c945383b3e62a89ea86"
Expand Down Expand Up @@ -533,7 +520,7 @@
dependencies:
"@types/jest-diff" "*"

"@types/json-schema@*", "@types/json-schema@^7.0.3":
"@types/json-schema@^7.0.3":
version "7.0.3"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.3.tgz#bdfd69d61e464dcc81b25159c270d75a73c1a636"
integrity sha512-Il2DtDVRGDcqjDtE+rF8iqg1CArehSK84HZJCT7AMITlyXRBpuPhqGLDQMowraqqu1coEaimg4ZOqggt6L6L+A==
Expand Down

0 comments on commit a9db0d0

Please sign in to comment.