dts-critic
Advanced tools
Comparing version 1.1.0 to 1.2.0
48
index.js
@@ -21,3 +21,2 @@ const yargs = require("yargs"); | ||
const names = findNames(dtsPath, sourcePath, header) | ||
checkNames(names, header); | ||
if (header && !header.nonNpm) { | ||
@@ -30,3 +29,2 @@ checkSource(names.dts, dts, readSource(sourcePath, names.src, header)); | ||
dtsCritic.retrieveNpmHomepageOrFail = retrieveNpmHomepageOrFail; | ||
dtsCritic.checkNames = checkNames; | ||
dtsCritic.checkSource = checkSource; | ||
@@ -89,2 +87,5 @@ | ||
src = findSourceName(sourcePath); | ||
if (dts !== src) { | ||
throw new Error(`d.ts name '${dts}' must match source name '${src}'.`); | ||
} | ||
} | ||
@@ -170,26 +171,2 @@ else { | ||
/** | ||
* @param {Names} names | ||
* @param {headerParser.Header | undefined} header | ||
*/ | ||
function checkNames(names, header) { | ||
if (names.dts !== names.src) { | ||
throw new Error(`d.ts name '${names.dts}' must match source name '${names.src}'.`); | ||
} | ||
if (names.homepage && header) { | ||
const homepage = normalise(names.homepage); | ||
if (!header.projects.some(p => homepage === normalise(p)) && !isExistingSquatter(names.dts)) { | ||
const e = new Error(`At least one of the project urls listed in the header, ${JSON.stringify(header.projects)}, must match the homepage listed by npm, '${homepage}'. | ||
If your d.ts file is not for the npm package with URL ${homepage}, | ||
change the name by adding -browser to the end and change the first line | ||
of the Definitely Typed header to | ||
// Type definitions for non-npm package ${names.dts}-browser | ||
`); | ||
/** @type {*} */(e).homepage = homepage; | ||
throw e; | ||
} | ||
} | ||
} | ||
/** | ||
* A d.ts with 'export default' and no ambient modules should have source that contains | ||
@@ -215,21 +192,2 @@ * either 'default' or '__esModule' or 'react-side-effect' or '@flow' somewhere. | ||
/** @param {string} url */ | ||
function normalise(url) { | ||
url = url.toLowerCase(); | ||
url = skipEnd(url, "#readme"); | ||
url = skipEnd(url, "/"); | ||
return url; | ||
} | ||
/** | ||
* @param {string} s | ||
* @param {string} suffix | ||
*/ | ||
function skipEnd(s, suffix) { | ||
if (s.endsWith(suffix)) { | ||
return s.slice(0, s.length - suffix.length); | ||
} | ||
return s; | ||
} | ||
/** @param {string} name */ | ||
@@ -236,0 +194,0 @@ function isExistingSquatter(name) { |
@@ -1,2 +0,2 @@ | ||
const { findDtsName, findNames, retrieveNpmHomepageOrFail, checkNames, checkSource } = require("./index"); | ||
const { findDtsName, findNames, retrieveNpmHomepageOrFail, checkSource } = require("./index"); | ||
/** | ||
@@ -32,32 +32,16 @@ * @param {string} description | ||
absolutePathsBoth() { | ||
expect(findNames("jquery/index.d.ts", "~/dts-critic", undefined)).toEqual({ | ||
dts: "jquery", | ||
src: "dts-critic", | ||
homepage: undefined, | ||
project: undefined | ||
}) | ||
expect(() => findNames("jquery/index.d.ts", "~/dts-critic", undefined)).toThrow( | ||
`d.ts name 'jquery' must match source name 'dts-critic'.`) | ||
}, | ||
currentDirectorySource() { | ||
expect(findNames("jquery/index.d.ts", ".", undefined)).toEqual({ | ||
dts: "jquery", | ||
src: "dts-critic", | ||
homepage: undefined, | ||
project: undefined | ||
}) | ||
expect(() => findNames("jquery/index.d.ts", ".", undefined)).toThrow( | ||
`d.ts name 'jquery' must match source name 'dts-critic'.`); | ||
}, | ||
mistakenFileNameSource() { | ||
expect(findNames("jquery/index.d.ts", "/home/lol/oops.index.js", undefined)).toEqual({ | ||
dts: "jquery", | ||
src: "lol", | ||
homepage: undefined, | ||
project: undefined | ||
}) | ||
expect(() => findNames("jquery/index.d.ts", "/home/lol/oops.index.js", undefined)).toThrow( | ||
`d.ts name 'jquery' must match source name 'lol'.`); | ||
}, | ||
trailingSlashSource() { | ||
expect(findNames("jquery/index.d.ts", "/home/lol/", undefined)).toEqual({ | ||
dts: "jquery", | ||
src: "lol", | ||
homepage: undefined, | ||
project: undefined | ||
}) | ||
expect(() => findNames("jquery/index.d.ts", "/home/lol/", undefined)).toThrow( | ||
`d.ts name 'jquery' must match source name 'lol'.`); | ||
}, | ||
@@ -135,38 +119,3 @@ mismatchPackageFailNoHeader() { | ||
}) | ||
suite("checkNames", { | ||
standaloneFail() { | ||
expect(() => checkNames({ dts: "a", src: "b" }, undefined)).toThrow("d.ts name 'a' must match source name 'b'.") | ||
}, | ||
okWithJustHomepage() { | ||
expect(checkNames({ dts: "a", src: "a", homepage: "zombo.com" }, undefined)).toBeUndefined() | ||
}, | ||
okWithJustHeader() { | ||
expect(checkNames({ dts: "a", src: "a" }, { | ||
nonNpm: false, | ||
libraryName: "a", | ||
libraryMajorVersion: 1, | ||
libraryMinorVersion: 2, | ||
typeScriptVersion: "3.2", | ||
contributors: [], | ||
projects: ["welcome-to-zombo.com", "this-is-zombo.com"] | ||
})).toBeUndefined() | ||
}, | ||
homepageFail() { | ||
expect(() => checkNames({ dts: "a", src: "a", homepage: "zombo.com" }, { | ||
nonNpm: false, | ||
libraryName: "a", | ||
libraryMajorVersion: 1, | ||
libraryMinorVersion: 2, | ||
typeScriptVersion: "3.2", | ||
contributors: [], | ||
projects: ["welcome-to-zombo.com", "this-is-zombo.com"] | ||
})).toThrow(`At least one of the project urls listed in the header, ["welcome-to-zombo.com","this-is-zombo.com"], must match the homepage listed by npm, 'zombo.com'. | ||
If your d.ts file is not for the npm package with URL zombo.com, | ||
change the name by adding -browser to the end and change the first line | ||
of the Definitely Typed header to | ||
// Type definitions for non-npm package a-browser`) | ||
} | ||
}); | ||
suite("checkSource", { | ||
@@ -173,0 +122,0 @@ badExportDefault() { |
{ | ||
"name": "dts-critic", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"author": "Nathan Shively-Sanders", | ||
@@ -5,0 +5,0 @@ "description": "Checks a new .d.ts against the Javascript source and tells you what problems it has", |
18008
388