type-coverage
Advanced tools
Comparing version
import ts from 'typescript'; | ||
export declare function lint(project: string, detail: boolean, debug: boolean, files?: string[], oldProgram?: ts.Program): Promise<{ | ||
export declare function lint(project: string, detail: boolean, debug: boolean, files?: string[], oldProgram?: ts.Program, strict?: boolean): Promise<{ | ||
correctCount: number; | ||
@@ -4,0 +4,0 @@ totalCount: number; |
@@ -8,3 +8,3 @@ "use strict"; | ||
// tslint:disable-next-line:no-big-function | ||
async function lint(project, detail, debug, files, oldProgram) { | ||
async function lint(project, detail, debug, files, oldProgram, strict) { | ||
const { configFilePath, dirname } = tsconfig_1.getTsConfigFilePath(project); | ||
@@ -22,20 +22,36 @@ const config = tsconfig_1.getTsConfig(configFilePath, dirname); | ||
let anys = []; | ||
function collectAny(node, file, sourceFile) { | ||
const { line, character } = typescript_1.default.getLineAndCharacterOfPosition(sourceFile, node.getStart(sourceFile)); | ||
if (debug) { | ||
console.log(`type === any: ${file}:${line + 1}:${character + 1}: ${node.getText(sourceFile)}`); | ||
} | ||
else if (detail) { | ||
anys.push({ file, line, character, text: node.getText(sourceFile) }); | ||
} | ||
} | ||
function collectNotAny(node, file, sourceFile, type) { | ||
correctCount++; | ||
if (debug) { | ||
const { line, character } = typescript_1.default.getLineAndCharacterOfPosition(sourceFile, node.getStart(sourceFile)); | ||
console.log(`type !== any: ${file}:${line + 1}:${character + 1}: ${node.getText(sourceFile)} ${node.kind}(kind) ${type.flags}(flag) ${type.intrinsicName || ''}`); | ||
} | ||
} | ||
function collectData(node, file, sourceFile) { | ||
const type = checker.getTypeAtLocation(node); | ||
if (type) { | ||
const { line, character } = typescript_1.default.getLineAndCharacterOfPosition(sourceFile, node.getStart(sourceFile)); | ||
totalCount++; | ||
if (type.flags === 1 && type.intrinsicName === 'any') { | ||
if (debug) { | ||
console.log(`type === any: ${file}:${line + 1}:${character + 1}: ${node.getText(sourceFile)}`); | ||
if (typeIsAny(type)) { | ||
collectAny(node, file, sourceFile); | ||
} | ||
else if (strict && type.flags === typescript_1.default.TypeFlags.Object) { | ||
const typeArguments = type.typeArguments; | ||
if (typeArguments && typeArguments.some((ypeArgument) => typeIsAny(ypeArgument))) { | ||
collectAny(node, file, sourceFile); | ||
} | ||
else if (detail) { | ||
anys.push({ file, line, character, text: node.getText(sourceFile) }); | ||
else { | ||
collectNotAny(node, file, sourceFile, type); | ||
} | ||
} | ||
else { | ||
correctCount++; | ||
if (debug) { | ||
console.log(`type !== any: ${file}:${line + 1}:${character + 1}: ${node.getText(sourceFile)} ${node.kind}(kind) ${type.flags}(flag) ${type.intrinsicName || ''}`); | ||
} | ||
collectNotAny(node, file, sourceFile, type); | ||
} | ||
@@ -894,1 +910,4 @@ } | ||
exports.lint = lint; | ||
function typeIsAny(type) { | ||
return type.flags === 1 && type.intrinsicName === 'any'; | ||
} |
@@ -25,3 +25,3 @@ "use strict"; | ||
suppressError = argv.suppressError; | ||
const { correctCount, totalCount, anys } = await core_1.lint(argv.p || argv.project || '.', true, argv.debug); | ||
const { correctCount, totalCount, anys } = await core_1.lint(argv.p || argv.project || '.', true, argv.debug, undefined, undefined, argv.strict); | ||
const percent = Math.round(10000 * correctCount / totalCount) / 100; | ||
@@ -28,0 +28,0 @@ const atLeast = await getAtLeast(argv); |
{ | ||
"name": "type-coverage", | ||
"version": "1.6.2", | ||
"version": "1.7.0", | ||
"description": "A CLI tool to check type coverage for typescript code", | ||
@@ -11,8 +11,8 @@ "main": "dist/core.js", | ||
"devDependencies": { | ||
"@commitlint/cli": "7.2.1", | ||
"@commitlint/config-conventional": "7.1.2", | ||
"@commitlint/cli": "7.3.2", | ||
"@commitlint/config-conventional": "7.3.1", | ||
"@types/glob": "7.1.1", | ||
"@types/jasmine": "3.3.2", | ||
"@types/jasmine": "3.3.7", | ||
"@types/minimist": "1.2.0", | ||
"@types/node": "10.12.15", | ||
"@types/node": "10.12.18", | ||
"clean-release": "2.7.0", | ||
@@ -23,5 +23,5 @@ "clean-scripts": "1.9.2", | ||
"no-unused-export": "1.7.0", | ||
"rimraf": "2.6.2", | ||
"rimraf": "2.6.3", | ||
"standard": "12.0.1", | ||
"tslint": "5.11.0", | ||
"tslint": "5.12.1", | ||
"tslint-config-standard": "8.0.1", | ||
@@ -28,0 +28,0 @@ "tslint-sonarts": "1.8.0" |
@@ -31,2 +31,3 @@ # type-coverage | ||
--debug | boolean? | show debug info | ||
--strict | boolean? | if the identifiers' type arguments exist and contain at least one `any`, like `any[]`, `ReadonlyArray<any>`, `Promise<any>`, `Foo<number, any>`, is will considered as `any` too; also, future minor release may introduce stricter type check in this mode, which may lower the code coverage | ||
@@ -52,1 +53,7 @@ ## config in package.json | ||
``` | ||
## FAQ | ||
> Q: Does this count JavaScript files? | ||
Yes, This package calls Typescript API, Typescript can parse Javascript file(with `allowJs`), then this package can too. |
64837
2.07%1104
1.75%58
13.73%