Skip to content

Commit

Permalink
Allow cwd to be passed to linter (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
fa93hws committed Apr 16, 2021
1 parent 1f23605 commit 7af7ca5
Show file tree
Hide file tree
Showing 11 changed files with 2,457 additions and 2,086 deletions.
9 changes: 8 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ module.exports = {
},
],
'import/no-extraneous-dependencies': 'off',
'import/no-dynamic-require': 'off',
'import/extensions': [
'error',
{
js: 'never',
ts: 'never,',
},
],
'@typescript-eslint/explicit-function-return-type': 'off',
'no-underscore-dangle': 'off',
'import/no-dynamic-require': 'off',
'global-require': 'off',
'no-useless-constructor': 'off',
'no-empty-function': 'off',
Expand Down
9 changes: 4 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"eslint.validate": [
"javascript",
{ "language": "typescript", "autoFix": true }
],
"eslint.autoFixOnSave": true,
"eslint.validate": ["javascript", "typescript"],
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"files.exclude": {
"node_modules": true
},
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
}
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@
"codecov": "codecov"
},
"devDependencies": {
"@types/jest": "^24.0.22",
"@types/jest": "^26.0.22",
"@types/lodash.merge": "^4.6.6",
"@types/node": "^12.12.7",
"@typescript-eslint/eslint-plugin": "^2.8.0",
"@typescript-eslint/experimental-utils": "^2.8.0",
"@typescript-eslint/parser": "^2.8.0",
"@typescript-eslint/typescript-estree": "^2.8.0",
"@typescript-eslint/eslint-plugin": "^4.21.0",
"@typescript-eslint/experimental-utils": "^4.21.0",
"@typescript-eslint/parser": "^4.21.0",
"@typescript-eslint/typescript-estree": "^4.21.0",
"codecov": "^3.6.1",
"eslint": "^6.6.0",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-config-prettier": "^6.7.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jest": "^23.0.4",
"jest": "^24.9.0",
"prettier": "^1.19.1",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"rimraf": "^3.0.0",
"ts-jest": "^24.1.0",
"typescript": "^3.7.2"
"ts-jest": "^26.5.5",
"typescript": "^4.2.4"
},
"peerDependencies": {
"eslint": ">5.0.0"
Expand Down
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import {
} from '@typescript-eslint/experimental-utils/dist/ts-eslint';
import { RuleRunner } from './rule-runner';

type SnapshotCreatorConfig = RuleTesterConfig & {
cwd?: string;
};

export class SnapshotCreator {
private readonly linter: Linter = new Linter();
private readonly linter: Linter;

public constructor(private readonly config: RuleTesterConfig) {
public constructor(private readonly config: SnapshotCreatorConfig) {
this.linter = new Linter({ cwd: config.cwd });
// eslint-disable-next-line @typescript-eslint/no-var-requires
this.linter.defineParser(config.parser, require(config.parser));
}
Expand All @@ -23,7 +28,7 @@ export class SnapshotCreator {
ruleName: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
rule: RuleModule<any, TOption, any>;
}) {
}): RuleRunner<TOption> {
if (!this.linter.getRules().has(ruleName)) {
this.linter.defineRule(ruleName, rule);
}
Expand Down
4 changes: 2 additions & 2 deletions src/mark-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ export const markResult: MarkResultFn = ({
positionHelper,
}) => {
const result: MarkedLine[] = [];
lintResult.forEach(r => {
lintResult.forEach((r) => {
const range = PositionHelper.getRange(r);
const positions = positionHelper.parsePosition(range);
positions.forEach(position => {
positions.forEach((position) => {
const waveString = drawWaveString(position.column);
result.push({
afterLine: position.line,
Expand Down
2 changes: 1 addition & 1 deletion src/position-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class PositionHelper {
private readonly lineWidths: readonly number[];

public constructor(lines: readonly string[]) {
this.lineWidths = lines.map(l => l.length);
this.lineWidths = lines.map((l) => l.length);
}

public static getRange(result: Linter.LintMessage): Range {
Expand Down
19 changes: 11 additions & 8 deletions src/rule-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import {
RuleTesterConfig,
Linter,
} from '@typescript-eslint/experimental-utils/dist/ts-eslint';
import merge from 'lodash.merge';
import { PositionHelper } from './position-helper';
import { assertExist } from './utils/preconditions';
import { markResult as _markResult, MarkResultFn } from './mark-result';

import merge = require('lodash.merge');

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type TestConfig<TOption extends readonly any[]> = Omit<
ValidTestCase<TOption>,
Expand Down Expand Up @@ -71,33 +70,37 @@ export class RuleRunner<TOption extends readonly any[]> {
return this._codeLines;
}

public withOptions(options: TOption) {
public withOptions(options: TOption): RuleRunner<TOption> {
this.ruleOption = ['error', ...options];
return this;
}

public overrideConfig(config: TestConfig<TOption>) {
public overrideConfig(config: TestConfig<TOption>): RuleRunner<TOption> {
this.config = merge(this.config, config);
return this;
}

public withFileName(fileName: string) {
public withFileName(fileName: string): RuleRunner<TOption> {
this.filename = fileName;
return this;
}

private get parameter(): [string, Linter.Config, string | undefined] {
private get parameter(): [string, Linter.Config, { filename?: string }] {
return [
this.code,
{
...this.config,
rules: { [this.ruleName]: this.ruleOption },
},
this.filename,
{ filename: this.filename },
];
}

public render() {
public render(): {
lintMessages: Linter.LintMessage[];
snapshot: string;
fixedOutput?: string;
} {
assertExist(this.ruleName, 'rule name must not be empty');
const rule = this.linter.getRules().get(this.ruleName);
assertExist(rule, `rule not found with name ${this.ruleName}`);
Expand Down
8 changes: 4 additions & 4 deletions src/tests/rule-runner.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('ruleRunner', () => {
it('overrides the filename', () => {
const wantFileName = 'want.filename.tsx';
const callback = jest.fn();
const rule = createAssertConfigRule(context =>
const rule = createAssertConfigRule((context) =>
callback(context.getFilename()),
);
linter.defineRule(ruleName, rule);
Expand All @@ -32,7 +32,7 @@ describe('ruleRunner', () => {

it('produces <input> if the filename is not provided', () => {
const callback = jest.fn();
const rule = createAssertConfigRule(context => {
const rule = createAssertConfigRule((context) => {
callback(context.getFilename());
});
linter.defineRule(ruleName, rule);
Expand All @@ -44,7 +44,7 @@ describe('ruleRunner', () => {
it('can add the settings', () => {
const callback = jest.fn();
const wantSetting = { a: 1, b: 2, c: 3 };
const rule = createAssertConfigRule(context => {
const rule = createAssertConfigRule((context) => {
callback(context.settings);
});
linter.defineRule(ruleName, rule);
Expand All @@ -60,7 +60,7 @@ describe('ruleRunner', () => {
const wantParserOptions = {
ecmaVersion: 6,
};
const rule = createAssertConfigRule(context => {
const rule = createAssertConfigRule((context) => {
callback(context.parserOptions);
});
linter.defineRule(ruleName, rule);
Expand Down
10 changes: 7 additions & 3 deletions src/utils/testing-rules/assert-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { RuleContext } from '@typescript-eslint/experimental-utils/dist/ts-eslint';
import type {
RuleContext,
RuleListener,
RuleModule,
} from '@typescript-eslint/experimental-utils/dist/ts-eslint';
import { createRule } from './create-rule';

type MessageIds = 'config';
Expand All @@ -10,7 +14,7 @@ const messages: Record<MessageIds, string> = {

export function createAssertConfigRule(
callback: (context: RuleContext<'config', []>) => void,
) {
): RuleModule<'config', [], RuleListener> {
return createRule<Option, MessageIds>({
name,
meta: {
Expand All @@ -24,7 +28,7 @@ export function createAssertConfigRule(
},
},
defaultOptions: [],
create: context => ({
create: (context) => ({
Program: () => callback(context),
}),
});
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"compilerOptions": {
"strict": true,
"esModuleInterop": true,
"module": "commonjs",
"moduleResolution": "node",
"target": "es2020",
"target": "ES2019",
"baseUrl": ".",
"allowSyntheticDefaultImports": true
}
Expand Down
Loading

0 comments on commit 7af7ca5

Please sign in to comment.