This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move dtscritic to tslint rule (#218)
* update to newest version of dts-critic * First attempt. Works but tests fail. Because dts-critic inspects the filesystem, and the test filesystem has extra .lint extensions, and I don't care enough to make it understand the tslint test environment. * No new tests, but tests work now I had to hack dts-critic though, with a new entry point: ```js /** @param {string} dts - the text of the d.ts */ dtsCritic.alternate = function (dts, dtsPath) { let header; try { header = headerParser.parseHeaderOrFail(dts); } catch(e) { header = undefined; } const names = findNames(dtsPath, undefined, header) const src = download("https://unpkg.com/" + mangleScoped(names.src)); checkNames(names, header); if (header && !header.nonNpm) { checkSource(names.dts, dts, src); } } ``` and modified findDtsName to know about .lint extension in tests. This second thing is not good and should probably be inverted as well: ``` /** * If dtsName is 'index' (as with DT) then look to the parent directory for the name. * @param {string} dtsPath */ function findDtsName(dtsPath) { const resolved = path.resolve(dtsPath); const baseName = path.basename(resolved, '.d.ts'); if (baseName && baseName !== "index") { const baseName2 = path.basename(resolved, '.d.ts.lint'); if (baseName2 && baseName2 !== "index") { return baseName; } } return path.basename(path.dirname(resolved)); } ``` * Add one test * move dts-critic into new rule * Turn on rule and give more error spans * Revert test changes -- this was a manual test * Missed test revert
- Loading branch information
Showing
7 changed files
with
113 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import * as Lint from "tslint"; | ||
import * as ts from "typescript"; | ||
import critic = require("dts-critic"); | ||
|
||
import { failure, isMainFile } from "../util"; | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
static metadata: Lint.IRuleMetadata = { | ||
ruleName: "npm-naming", | ||
description: "Ensure that package name and DefinitelyTyped header match npm package info.", | ||
optionsDescription: "Not configurable.", | ||
options: null, | ||
type: "functionality", | ||
typescriptOnly: true, | ||
}; | ||
|
||
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithFunction(sourceFile, walk); | ||
} | ||
} | ||
|
||
function walk(ctx: Lint.WalkContext<void>): void { | ||
const { sourceFile } = ctx; | ||
const { text } = sourceFile; | ||
const lookFor = (search: string, explanation: string) => { | ||
const idx = text.indexOf(search); | ||
if (idx !== -1) { | ||
ctx.addFailureAt(idx, search.length, failure(Rule.metadata.ruleName, explanation)); | ||
} | ||
}; | ||
if (isMainFile(sourceFile.fileName)) { | ||
try { | ||
critic(sourceFile.fileName); | ||
} | ||
catch (e) { | ||
// TODO: dts-critic should | ||
// 1. not really be using exceptions, but lists | ||
// 2. export an error code enum | ||
// 3. add an errorCode property to the exception (or return value) | ||
if (e.message.indexOf('d.ts file must have a matching npm package') > -1 || | ||
e.message.indexOf('The non-npm package') > -1) { | ||
lookFor("// Type definitions for", e.message); | ||
} | ||
else if (e.message.indexOf('At least one of the project urls listed') > -1) { | ||
lookFor("// Project:", e.message); | ||
} | ||
else if (e.message.indexOf('export default') > -1) { | ||
lookFor("export default", e.message); | ||
} | ||
else { | ||
// should be unused! | ||
ctx.addFailureAt(0, 1, e.message); | ||
} | ||
} | ||
} | ||
// Don't recur, we're done. | ||
} |
Oops, something went wrong.