Skip to content
This repository has been archived by the owner on Feb 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #6 from sandersn/stop-checking-project-url
Browse files Browse the repository at this point in the history
Stop checking project URL.
  • Loading branch information
sandersn authored Jun 5, 2019
2 parents a0dc667 + 395e17a commit 25d9012
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 105 deletions.
48 changes: 3 additions & 45 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ function dtsCritic(dtsPath, sourcePath) {
header = undefined;
}
const names = findNames(dtsPath, sourcePath, header)
checkNames(names, header);
if (header && !header.nonNpm) {
checkSource(names.dts, dts, readSource(sourcePath, names.src, header));
}
}
dtsCritic.findDtsName = findDtsName;
dtsCritic.findNames = findNames;
dtsCritic.retrieveNpmHomepageOrFail = retrieveNpmHomepageOrFail;
dtsCritic.checkNames = checkNames;
dtsCritic.checkSource = checkSource;

module.exports = dtsCritic;
Expand Down Expand Up @@ -84,6 +82,9 @@ function findNames(dtsPath, sourcePath, header) {
let homepage;
if (sourcePath) {
src = findSourceName(sourcePath);
if (dts !== src) {
throw new Error(`d.ts name '${dts}' must match source name '${src}'.`);
}
}
else {
let nonNpmHasMatchingPackage = false;
Expand Down Expand Up @@ -163,30 +164,6 @@ function mangleScoped(baseName) {
return baseName;
}

/**
* @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
* either 'default' or '__esModule' or 'react-side-effect' or '@flow' somewhere.
Expand All @@ -208,25 +185,6 @@ ${src}`);
}
}

/** @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 */
function isExistingSquatter(name) {
return name === "atom" ||
Expand Down
69 changes: 9 additions & 60 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { findDtsName, findNames, retrieveNpmHomepageOrFail, checkNames, checkSource } = require("./index");
const { findDtsName, findNames, retrieveNpmHomepageOrFail, checkSource } = require("./index");
/**
* @param {string} description
* @param {{ [s: string]: () => void }} tests
Expand Down Expand Up @@ -29,36 +29,20 @@ suite("findParent", {
})
suite("findNames", {
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'.`);
},
mismatchPackageFailNoHeader() {
// surely parseltongue will never exist
Expand Down Expand Up @@ -131,41 +115,6 @@ suite("retrieveNpmHomepageOrFail", {
expect(retrieveNpmHomepageOrFail("shelljs")).toBe("http://github.com/shelljs/shelljs")
}
})
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", {
badExportDefault() {
Expand Down

0 comments on commit 25d9012

Please sign in to comment.