Skip to content

Commit

Permalink
refactor: improve types by removing anys, add stricter ts requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
toddbluhm committed Dec 7, 2020
1 parent b681c19 commit 6ae2a64
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 34 deletions.
4 changes: 2 additions & 2 deletions dist/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async function lintStdIn(linter, options) {
}
// If we performed fixes then maybe return the fixed text
if (options.fix) {
if (lintReport.results[0].output !== undefined) {
if (lintReport.results[0].output != null) {
// Code contained fixable errors, so print the fixed code
process.stdout.write(lintReport.results[0].output);
}
Expand Down Expand Up @@ -118,7 +118,7 @@ async function printReport(lintReport, options) {
// Check for any fixable rules
const isFixable = lintReport.results.some((res) => {
return res.messages.some((msg) => {
return msg.fix !== undefined;
return msg.fix != null;
});
});
// If there were fixable rules, then that means that `--fix` was not provided
Expand Down
1 change: 1 addition & 0 deletions dist/options/default-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function _getTSConfigFromDefaultLocations(cwd) {
return absPath;
}
}
return undefined;
}
exports._getTSConfigFromDefaultLocations = _getTSConfigFromDefaultLocations;
function _isValidPath(pathToValidate) {
Expand Down
3 changes: 2 additions & 1 deletion dist/options/package-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ function getPackageOptions(cwd) {
}
exports.getPackageOptions = getPackageOptions;
function _resolveTSConfigPath(cwd, settingsProjectPath) {
if (settingsProjectPath !== undefined) {
if (settingsProjectPath != null) {
const settingsPath = path_1.join(cwd, settingsProjectPath);
if (default_options_1._isValidPath(settingsPath)) {
return settingsPath;
}
}
return undefined;
}
exports._resolveTSConfigPath = _resolveTSConfigPath;
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "jest",
"coverage": "npm test -- --coverage",
"coveralls": "coveralls < coverage/lcov.info",
"lint": "prettier --write '**/*.{ts*,js*,yml}' && bin/cmd.js --fix && tsc --noEmit",
"lint": "prettier --write '**/*.{ts*,js*,yml}' && bin/cmd.js --fix && tsc -p './tsconfig.eslint.json' --noEmit",
"build": "tsc",
"watch": "tsc -w"
},
Expand Down Expand Up @@ -125,6 +125,7 @@
},
"prettier": {
"singleQuote": true,
"semi": false
"semi": false,
"trailingComma": "none"
}
}
2 changes: 1 addition & 1 deletion src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const mockCustomReporter = jest.fn()
jest.mock('custom-reporter', () => mockCustomReporter, { virtual: true })
jest.mock('custom-bad-reporter', () => undefined, { virtual: true })

const mockStylish = stylish as jest.MockedFunction<any>
const mockStylish = stylish as jest.MockedFunction<typeof stylish>
const mockGetStdin = getStdin as jest.MockedFunction<typeof getStdin>
const mockProcess = process

Expand Down
10 changes: 5 additions & 5 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type * as eslint from 'eslint'
import type { LintReport, LintResult } from 'standard-engine'
import type { LintReport, LintResult, LintMessage } from 'standard-engine'
import * as getStdin from 'get-stdin'
import {
getCLIOptions,
Expand Down Expand Up @@ -128,7 +128,7 @@ export async function lintStdIn (

// If we performed fixes then maybe return the fixed text
if (options.fix) {
if (lintReport.results[0].output !== undefined) {
if (lintReport.results[0].output != null) {
// Code contained fixable errors, so print the fixed code
process.stdout.write(lintReport.results[0].output)
} else {
Expand Down Expand Up @@ -170,9 +170,9 @@ export async function printReport (
console.error(`${CMD}: ${TAGLINE} (${HOMEPAGE})`)

// Check for any fixable rules
const isFixable: boolean = lintReport.results.some((res: any) => {
return res.messages.some((msg: any) => {
return msg.fix !== undefined
const isFixable: boolean = lintReport.results.some((res: LintResult) => {
return res.messages.some((msg: LintMessage) => {
return msg.fix != null
})
})

Expand Down
1 change: 1 addition & 0 deletions src/options/default-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function _getTSConfigFromDefaultLocations (
return absPath
}
}
return undefined
}

export function _isValidPath (pathToValidate: string): boolean {
Expand Down
3 changes: 2 additions & 1 deletion src/options/package-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ export function _resolveTSConfigPath (
cwd: string,
settingsProjectPath?: string
): string | undefined {
if (settingsProjectPath !== undefined) {
if (settingsProjectPath != null) {
const settingsPath = join(cwd, settingsProjectPath)
if (_isValidPath(settingsPath)) {
return settingsPath
}
}
return undefined
}
31 changes: 19 additions & 12 deletions src/standard-engine-api.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import type {
lintFiles as lintFilesType,
lintText as lintTextType,
parseOpts as parseOptsType
} from './standard-engine-api'
import * as tsStandardLib from './ts-standard'

let parseOpts: Function, lintFiles: any, lintText: any
let parseOpts: typeof parseOptsType,
lintFiles: typeof lintFilesType,
lintText: typeof lintTextType
jest.mock('standard-engine')

const customEslint = jest.fn().mockReturnThis()
Expand Down Expand Up @@ -98,7 +105,7 @@ describe('standard-engine-api', () => {
lintTextSpy.mockResolvedValueOnce('success!')

expect.assertions(6)
lintText(text, (err: any, res: any) => {
lintText(text, (err, res) => {
try {
expect(err).toBeUndefined()
expect(res).toEqual('success!')
Expand All @@ -118,7 +125,7 @@ describe('standard-engine-api', () => {
lintTextSpy.mockResolvedValueOnce('success!')

expect.assertions(6)
lintText(text, options, (err: any, res: any) => {
lintText(text, options, (err, res) => {
try {
expect(err).toBeUndefined()
expect(res).toEqual('success!')
Expand All @@ -140,7 +147,7 @@ describe('standard-engine-api', () => {
expect.assertions(3)
parseOpts({})
expect(tsStandardSpy).toHaveBeenCalledTimes(1)
lintText(text, options, (err: any, res: any) => {
lintText(text, options, (err) => {
try {
expect(err).toBeUndefined()
expect(tsStandardSpy).toHaveBeenCalledTimes(1)
Expand All @@ -156,9 +163,9 @@ describe('standard-engine-api', () => {
lintTextSpy.mockRejectedValueOnce(new Error('the darkside'))

expect.assertions(3)
lintText(text, options, (err: { message: any }, res: any) => {
lintText(text, options, (err, res) => {
try {
expect(err.message).toMatch(/the darkside/gi)
expect(err?.message).toMatch(/the darkside/gi)
expect(tsStandardSpy).toHaveBeenCalledTimes(1)
expect(res).toBeUndefined()
cb()
Expand All @@ -178,7 +185,7 @@ describe('standard-engine-api', () => {
{
filename: 'file:///some/path/to/the/darkside'
},
(err: any, res: any) => {
(err, res) => {
try {
expect(err).toBeUndefined()
expect(res).toEqual('success!')
Expand Down Expand Up @@ -220,7 +227,7 @@ describe('standard-engine-api', () => {
lintFilesSpy.mockResolvedValueOnce('success!')

expect.assertions(6)
lintFiles(files, (err: any, res: any) => {
lintFiles(files, (err, res) => {
try {
expect(err).toBeUndefined()
expect(res).toEqual('success!')
Expand All @@ -240,7 +247,7 @@ describe('standard-engine-api', () => {
lintFilesSpy.mockResolvedValueOnce('success!')

expect.assertions(6)
lintFiles(files, options, (err: any, res: any) => {
lintFiles(files, options, (err, res) => {
try {
expect(err).toBeUndefined()
expect(res).toEqual('success!')
Expand All @@ -262,7 +269,7 @@ describe('standard-engine-api', () => {
expect.assertions(3)
parseOpts({})
expect(tsStandardSpy).toHaveBeenCalledTimes(1)
lintFiles(files, options, (err: any, res: any) => {
lintFiles(files, options, (err) => {
try {
expect(err).toBeUndefined()
expect(tsStandardSpy).toHaveBeenCalledTimes(1)
Expand All @@ -278,9 +285,9 @@ describe('standard-engine-api', () => {
lintFilesSpy.mockRejectedValueOnce(new Error('the darkside'))

expect.assertions(3)
lintFiles(files, options, (err: { message: any }, res: any) => {
lintFiles(files, options, (err, res) => {
try {
expect(err.message).toMatch(/the darkside/gi)
expect(err?.message).toMatch(/the darkside/gi)
expect(tsStandardSpy).toHaveBeenCalledTimes(1)
expect(res).toBeUndefined()
cb()
Expand Down
12 changes: 6 additions & 6 deletions src/ts-standard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('ts-standard', () => {

it('should lint the given text with default options', async (): Promise<void> => {
const text = 'The darkside is strong in this one.'
lintTextSpy.mockImplementationOnce((text, options, cb) =>
lintTextSpy.mockImplementationOnce((_text, _options, cb) =>
cb(undefined, 'success!')
)
const res = await tsStandard.lintText(text)
Expand All @@ -138,7 +138,7 @@ describe('ts-standard', () => {

it('should overide default options with method options', async (): Promise<void> => {
const text = 'The darkside is strong in this one.'
lintTextSpy.mockImplementationOnce((text, options, cb) =>
lintTextSpy.mockImplementationOnce((_text, _options, cb) =>
cb(undefined, 'success!')
)
const newOptions = {
Expand All @@ -158,7 +158,7 @@ describe('ts-standard', () => {

it('should return error if linting failed', async (): Promise<void> => {
const text = 'The darkside is strong in this one.'
lintTextSpy.mockImplementationOnce((text, options, cb) =>
lintTextSpy.mockImplementationOnce((_text, _options, cb) =>
cb(new Error('the darkside'))
)
expect.assertions(2)
Expand Down Expand Up @@ -194,7 +194,7 @@ describe('ts-standard', () => {

it('should lint the given files with default options', async (): Promise<void> => {
const files = ['The darkside is strong in this one.']
lintFilesSpy.mockImplementationOnce((files, options, cb) =>
lintFilesSpy.mockImplementationOnce((_files, _options, cb) =>
cb(undefined, 'success!')
)
const res = await tsStandard.lintFiles(files)
Expand All @@ -207,7 +207,7 @@ describe('ts-standard', () => {

it('should overide default options with method options', async (): Promise<void> => {
const files = ['The darkside is strong in this one.']
lintFilesSpy.mockImplementationOnce((files, options, cb) =>
lintFilesSpy.mockImplementationOnce((_files, _options, cb) =>
cb(undefined, 'success!')
)
const newOptions = {
Expand All @@ -227,7 +227,7 @@ describe('ts-standard', () => {

it('should return error if linting failed', async (): Promise<void> => {
const files = ['The darkside is strong in this one.']
lintFilesSpy.mockImplementationOnce((files, options, cb) =>
lintFilesSpy.mockImplementationOnce((_files, _options, cb) =>
cb(new Error('the darkside'))
)
expect.assertions(2)
Expand Down
6 changes: 5 additions & 1 deletion src/types/stylish.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
declare module 'eslint/lib/cli-engine/formatters/stylish' {
export {}
import type { LintResult } from 'standard-engine'

function stylish (results: LintResult[]): string
namespace stylish {}
export = stylish
}
10 changes: 8 additions & 2 deletions tsconfig.eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
"resolveJsonModule": true,
"lib": ["es2018", "es2019", "es2020"],
"allowJs": true,
"checkJs": true
"checkJs": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"newLine": "lf",
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true
},
"include": ["src/**/*.ts", "bin/*.js", "*.json"]
"include": ["src", "bin", "*.json"]
}
10 changes: 9 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
"strict": true,
"declaration": true,
"resolveJsonModule": true,
"lib": ["es2018", "es2019", "es2020"]
"lib": ["es2018", "es2019", "es2020"],
"allowJs": true,
"checkJs": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"newLine": "lf",
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true
},
"include": ["src/**/*.ts"],
"exclude": ["src/**/*.test.ts"]
Expand Down

0 comments on commit 6ae2a64

Please sign in to comment.