Skip to content

Commit

Permalink
Ignore unread anys
Browse files Browse the repository at this point in the history
  • Loading branch information
danvk authored and plantain-00 committed Oct 15, 2020
1 parent 61e15e7 commit a775102
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
28 changes: 17 additions & 11 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function printHelp() {
--ignore-catch boolean? ignore catch
--cache boolean? enable cache
--ignore-files string[]? ignore files
--ignore-unread boolean? allow writes to variables with implicit any types
-h,--help boolean? show help
--is number? fail if coverage rate !== this value
--update boolean? update "typeCoverage" in package.json to current result
Expand All @@ -37,6 +38,7 @@ interface BaseArgs {
project: string
debug: boolean
strict: boolean
ignoreUnreadAnys: boolean;
cache: boolean
detail: boolean
is: number
Expand All @@ -51,11 +53,13 @@ interface CliArgs extends BaseArgs {
['ignore-catch']: boolean
['ignore-files']?: string | string[]
['at-least']: number
['ignore-unread']: boolean
}

interface PkgArgs extends BaseArgs {
ignoreCatch: boolean
ignoreFiles?: string | string[]
ignoreUnread: boolean
atLeast: boolean
}

Expand All @@ -78,17 +82,18 @@ async function executeCommandLine() {
printHelp()
process.exit(0)
}
const { atLeast, debug, detail, enableCache, ignoreCatch, ignoreFiles, is, project, strict, update } = await getTarget(argv);

const { atLeast, debug, detail, enableCache, ignoreCatch, ignoreFiles, ignoreUnread, is, project, strict, update } = await getTarget(argv);

const { correctCount, totalCount, anys } = await lint(project, {
debug: debug,
strict: strict,
enableCache: enableCache,
ignoreCatch: ignoreCatch,
ignoreFiles: ignoreFiles
ignoreFiles: ignoreFiles,
ignoreUnreadAnys: ignoreUnread,
});

const percent = Math.floor(10000 * correctCount / totalCount) / 100
const atLeastFailed = atLeast && percent < atLeast
const isFailed = is && percent !== is
Expand Down Expand Up @@ -125,7 +130,7 @@ async function getTarget(argv: CliArgs) {
pkgCfg = typeCoverage
}
}

const isCliArg = (key:keyof AllArgs):key is keyof CliArgs => key in argv
const isPkgArg = (key:keyof AllArgs):key is keyof PkgArgs => pkgCfg ? key in pkgCfg : false

Expand All @@ -136,9 +141,9 @@ async function getTarget(argv: CliArgs) {
}
if (pkgCfg && isPkgArg(key)) {
return pkgCfg[key]
}
}
}
return undefined
return undefined
}

suppressError = getArgOrCfgVal(['suppressError']) || false
Expand All @@ -149,12 +154,13 @@ async function getTarget(argv: CliArgs) {
const enableCache = getArgOrCfgVal(['cache'])
const ignoreCatch = getArgOrCfgVal(['ignore-catch', 'ignoreCatch'])
const ignoreFiles = getArgOrCfgVal(['ignore-files', 'ignoreFiles'])
const ignoreUnread = getArgOrCfgVal(['ignore-unread', 'ignoreUnread'])
const is = getArgOrCfgVal(['is'])
const project = getArgOrCfgVal(['p', 'project']) || '.'
const strict = getArgOrCfgVal(['strict'])
const update = getArgOrCfgVal(['update'])
const strict = getArgOrCfgVal(['strict'])
const update = getArgOrCfgVal(['update'])

return { atLeast, debug, detail, enableCache, ignoreCatch, ignoreFiles, is, project, strict, update };
return { atLeast, debug, detail, enableCache, ignoreCatch, ignoreFiles, ignoreUnread, is, project, strict, update };
}

async function saveTarget(target: number) {
Expand Down
22 changes: 21 additions & 1 deletion packages/core/src/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ import ts from 'typescript'
import { FileAnyInfoKind, FileContext } from './interfaces'

function collectAny(node: ts.Node, context: FileContext, kind: FileAnyInfoKind) {
const { file, sourceFile, typeCheckResult, ingoreMap, debug, processAny } = context
const { file, sourceFile, typeCheckResult, ingoreMap, ignoreUnreadAnys, debug, processAny } = context
if (processAny !== undefined) {
return processAny(node, context)
}
const { line, character } = ts.getLineAndCharacterOfPosition(sourceFile, node.getStart(sourceFile))
if (ingoreMap[file] && ingoreMap[file].has(line)) {
return false
}
if (ignoreUnreadAnys && isEvolvingAssignment(node)) {
if (debug) {
console.log(`Ignoring assignment to implicit any type: ${file}:${line + 1}:${character + 1}: ${node.parent.getText(sourceFile)}`)
}
return false
}
if (debug) {
console.log(`type === any(${kind}): ${file}:${line + 1}:${character + 1}: ${node.getText(sourceFile)}`)
} else {
Expand Down Expand Up @@ -65,6 +71,20 @@ function typeIsStrictAny(type: ts.Type, strict: boolean): boolean {
return false
}

// See https://github.com/plantain-00/type-coverage/issues/28
function isEvolvingAssignment(node: ts.Node) {
const {parent} = node;
if (ts.isVariableDeclaration(parent)) {
// Match "let foo" and "let foo = null" but not "let foo: any".
return !parent.type;
}
if (ts.isBinaryExpression(parent)) {
// Match "foo = 123".
return parent.operatorToken.kind === ts.SyntaxKind.EqualsToken;
}
return false;
}

function checkNodes(nodes: ts.NodeArray<ts.Node> | undefined, context: FileContext): void {
if (nodes === undefined) {
return
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export async function lint(project: string, options?: Partial<LintOptions>) {
anys: []
},
ignoreCatch: lintOptions.ignoreCatch,
ignoreUnreadAnys: lintOptions.ignoreUnreadAnys,
catchVariables: {},
debug: lintOptions.debug,
strict: lintOptions.strict,
Expand Down Expand Up @@ -152,6 +153,7 @@ const defaultLintOptions: LintOptions = {
enableCache: false,
ignoreCatch: false,
ignoreFiles: undefined,
ignoreUnreadAnys: false,
fileCounts: false,
}

Expand Down Expand Up @@ -207,6 +209,7 @@ export function lintSync(compilerOptions: ts.CompilerOptions, rootNames: string[
anys: []
},
ignoreCatch: lintOptions.ignoreCatch,
ignoreUnreadAnys: lintOptions.ignoreUnreadAnys,
catchVariables: {},
debug: lintOptions.debug,
strict: lintOptions.strict,
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export interface LintOptions {
strict: boolean,
enableCache: boolean,
ignoreCatch: boolean,
ignoreFiles?: string | string[]
ignoreFiles?: string | string[],
ignoreUnreadAnys: boolean,
fileCounts: boolean,
absolutePath?: boolean,
processAny?: ProccessAny,
Expand All @@ -57,6 +58,7 @@ export interface FileContext {
strict: boolean
checker: ts.TypeChecker
ignoreCatch: boolean
ignoreUnreadAnys: boolean
catchVariables: { [variable: string]: boolean }
ingoreMap: { [file: string]: Set<number> }
processAny?: ProccessAny
Expand Down

0 comments on commit a775102

Please sign in to comment.