Skip to content

Commit da3f9ec

Browse files
authored
TS conversion part 1 (#564)
1 parent 223eca0 commit da3f9ec

17 files changed

+277
-248
lines changed

src/LintIssue.js

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/lint-issue.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import chalk from 'chalk';
2+
import logSymbols from 'log-symbols';
3+
import {Severity} from './types/severity';
4+
5+
/**
6+
* A lint issue
7+
*/
8+
export class LintIssue {
9+
/**
10+
* Unique, lowercase, hyphen-separate name for the lint
11+
*
12+
* @type {string}
13+
* @memberof LintIssue
14+
*/
15+
lintId: string;
16+
/**
17+
* 'error' or 'warning'
18+
*
19+
* @type {Severity}
20+
* @memberof LintIssue
21+
*/
22+
severity: Severity;
23+
/**
24+
* Name of the node in the JSON the lint audits
25+
*
26+
* @type {string}
27+
* @memberof LintIssue
28+
*/
29+
node: string
30+
/**
31+
* Human-friendly message to users
32+
*
33+
* @type {string}
34+
* @memberof LintIssue
35+
*/
36+
lintMessage: string
37+
38+
/**
39+
* Creates an instance of LintIssue.
40+
* @param lintId Unique, lowercase, hyphen-separate name for the lint
41+
* @param severity 'error' or 'warning'
42+
* @param node Name of the node in the JSON the lint audits
43+
* @param lintMessage Human-friendly message to users
44+
* @memberof LintIssue
45+
*/
46+
constructor(lintId: string, severity: Severity, node: string, lintMessage: string) {
47+
this.lintId = lintId;
48+
this.severity = severity;
49+
this.node = node;
50+
this.lintMessage = lintMessage;
51+
}
52+
53+
/**
54+
* Helper to convert the LintIssue to a printable string
55+
*
56+
* @returns {string} Human-friendly message about the lint issue
57+
*/
58+
toString(): string {
59+
const logSymbol = this.severity === Severity.Error ? logSymbols.error : logSymbols.warning;
60+
const formattedLintId = chalk.gray.dim(this.lintId);
61+
const formattedNode = chalk.gray.bold(this.node);
62+
const formattedMessage = this.severity === Severity.Error ? chalk.bold.red(this.lintMessage) : chalk.yellow(this.lintMessage);
63+
64+
return `${logSymbol} ${formattedLintId} - node: ${formattedNode} - ${formattedMessage}`;
65+
}
66+
}

src/rules/bin-type.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/rules/bin-type.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {isObject, isString} from '../validators/type';
2+
import {LintIssue} from '../lint-issue';
3+
import {RuleType} from '../types/rule-type';
4+
import {Severity} from '../types/severity';
5+
import {PackageJson} from 'type-fest';
6+
7+
const lintId = 'bin-type';
8+
const nodeName = 'bin';
9+
const message = 'Type should be either a string or an Object';
10+
export const ruleType = RuleType.Standard;
11+
12+
export const lint = (packageJsonData: PackageJson, severity: Severity): LintIssue | boolean => {
13+
if (!isString(packageJsonData, nodeName) && !isObject(packageJsonData, nodeName)) {
14+
return new LintIssue(lintId, severity, nodeName, message);
15+
}
16+
17+
return true;
18+
};

src/types/lint-result.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface LintResult {
2+
3+
}

src/types/rule-type.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export enum RuleType {
2+
Standard = 'standard',
3+
4+
}

src/types/severity.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export enum Severity {
2+
Error = 'error',
3+
Warning = 'warning'
4+
}

src/validators/alphabetical-sort.js renamed to src/validators/alphabetical-sort.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1+
import {PackageJson} from 'type-fest';
2+
13
const increment = 1;
24

5+
export interface IsInAlphabeticalOrderResult {
6+
status: boolean;
7+
data: {
8+
invalidNode: string | null;
9+
validNode: string | null;
10+
}
11+
}
12+
313
/**
414
* Determines whether an array is in alphabetical order
5-
* @param {object} packageJsonData Valid JSON
6-
* @param {string} nodeName Name of a node in the package.json file
7-
* @return {object} Object containing the status and the dependencies that are out of order, if applicable
15+
*
16+
* @param packageJsonData Valid JSON
17+
* @param nodeName Name of a node in the package.json file
18+
* @return Object containing the status and the dependencies that are out of order, if applicable
819
*/
9-
const isInAlphabeticalOrder = (packageJsonData, nodeName) => {
20+
export const isInAlphabeticalOrder = (packageJsonData: PackageJson, nodeName: string): IsInAlphabeticalOrderResult => {
1021
let isValid = true;
1122
let data = {
1223
invalidNode: null,
@@ -31,7 +42,3 @@ const isInAlphabeticalOrder = (packageJsonData, nodeName) => {
3142
data,
3243
};
3344
};
34-
35-
module.exports = {
36-
isInAlphabeticalOrder,
37-
};

src/validators/dependency-audit.js renamed to src/validators/dependency-audit.ts

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
/* eslint no-restricted-syntax: 'off', guard-for-in: 'off', no-continue: 'off' */
2-
const semver = require('semver');
1+
import semver from 'semver';
2+
import {PackageJson} from 'type-fest';
33

4-
const hasExceptions = (config) => typeof config === 'object' && config.hasOwnProperty('exceptions');
4+
const hasExceptions = (config: any): boolean => typeof config === 'object' && config.hasOwnProperty('exceptions');
55

66
/**
77
* Determines whether or not the package has a given dependency
@@ -10,7 +10,7 @@ const hasExceptions = (config) => typeof config === 'object' && config.hasOwnPro
1010
* @param {string} depsToCheckFor An array of packages to check for
1111
* @return {boolean} True if the package has a dependency. False if it is not or the node is missing.
1212
*/
13-
const hasDependency = (packageJsonData, nodeName, depsToCheckFor) => {
13+
export const hasDependency = (packageJsonData: PackageJson, nodeName: string, depsToCheckFor: string[]): boolean => {
1414
if (!packageJsonData.hasOwnProperty(nodeName)) {
1515
return false;
1616
}
@@ -40,7 +40,7 @@ const hasDependency = (packageJsonData, nodeName, depsToCheckFor) => {
4040
* @param {string} depsToCheckFor An array of packages to check for
4141
* @return {boolean} True if the package has a pre-release version of a dependency. False if it is not or the node is missing.
4242
*/
43-
const hasDepPrereleaseVers = (packageJsonData, nodeName, depsToCheckFor) => {
43+
export const hasDepPrereleaseVers = (packageJsonData: PackageJson, nodeName: string, depsToCheckFor: string[]): boolean => {
4444
if (!packageJsonData.hasOwnProperty(nodeName)) {
4545
return false;
4646
}
@@ -65,7 +65,7 @@ const hasDepPrereleaseVers = (packageJsonData, nodeName, depsToCheckFor) => {
6565
* @param {object} config Rule configuration
6666
* @return {boolean} True if the package has a dependency with version 0. False if it does not or the node is missing.
6767
*/
68-
const hasDepVersZero = (packageJsonData, nodeName, config) => {
68+
export const hasDepVersZero = (packageJsonData: PackageJson, nodeName: string, config: any): boolean => {
6969
for (const dependencyName in packageJsonData[nodeName]) {
7070
if (hasExceptions(config) && config.exceptions.includes(dependencyName)) {
7171
continue;
@@ -96,7 +96,7 @@ const hasDepVersZero = (packageJsonData, nodeName, config) => {
9696
* @param {String} rangeSpecifier A version range specifier
9797
* @return {Boolean} True if the version starts with the range, false if it doesn't.
9898
*/
99-
const doesVersStartsWithRange = (dependencyVersion, rangeSpecifier) => {
99+
export const doesVersStartsWithRange = (dependencyVersion: string, rangeSpecifier: string): boolean => {
100100
const firstCharOfStr = 0;
101101

102102
return dependencyVersion.startsWith(rangeSpecifier, firstCharOfStr);
@@ -110,7 +110,7 @@ const doesVersStartsWithRange = (dependencyVersion, rangeSpecifier) => {
110110
* @param {object} config Rule configuration
111111
* @return {boolean} False if the package has an invalid range. True if it is not or the node is missing.
112112
*/
113-
const areVersRangesValid = (packageJsonData, nodeName, rangeSpecifier, config) => {
113+
export const areVersRangesValid = (packageJsonData: PackageJson, nodeName: string, rangeSpecifier: string, config: any): boolean => {
114114
if (!packageJsonData.hasOwnProperty(nodeName)) {
115115
return true;
116116
}
@@ -140,7 +140,7 @@ const areVersRangesValid = (packageJsonData, nodeName, rangeSpecifier, config) =
140140
* @param {object} config Rule configuration
141141
* @return {Boolean} True if any dependencies versions start with the invalid range, false if they don't.
142142
*/
143-
const doVersContainInvalidRange = (packageJsonData, nodeName, rangeSpecifier, config) => {
143+
export const doVersContainInvalidRange = (packageJsonData: PackageJson, nodeName: string, rangeSpecifier: string, config: any): boolean => {
144144
if (!packageJsonData.hasOwnProperty(nodeName)) {
145145
return false;
146146
}
@@ -162,14 +162,19 @@ const doVersContainInvalidRange = (packageJsonData, nodeName, rangeSpecifier, co
162162
return containsInvalidVersion;
163163
};
164164

165+
export interface AbsoluteVersionCheckerResult {
166+
onlyAbsoluteVersionDetected: boolean;
167+
dependenciesChecked: number;
168+
}
169+
165170
/**
166171
* Determines whether or not all dependency versions are absolut
167172
* @param {object} packageJsonData Valid JSON
168173
* @param {string} nodeName Name of a node in the package.json file
169174
* @param {object} config Rule configuration
170175
* @return {boolean} False if the package has an non-absolute version. True if it is not or the node is missing.
171176
*/
172-
const absoluteVersionChecker = (packageJsonData, nodeName, config) => {
177+
const absoluteVersionChecker = (packageJsonData: PackageJson, nodeName: string, config: any): AbsoluteVersionCheckerResult => {
173178
const notFound = -1;
174179
const firstCharOfStr = 0;
175180
let onlyAbsoluteVersionDetected = true;
@@ -208,7 +213,7 @@ const absoluteVersionChecker = (packageJsonData, nodeName, config) => {
208213
* @param {object} config Rule configuration
209214
* @return {boolean} False if the package has an non-absolute version. True if it is not or the node is missing.
210215
*/
211-
const areVersionsAbsolute = (packageJsonData, nodeName, config) => {
216+
export const areVersionsAbsolute = (packageJsonData: PackageJson, nodeName: string, config: any): boolean => {
212217
const {onlyAbsoluteVersionDetected, dependenciesChecked} = absoluteVersionChecker(packageJsonData, nodeName, config);
213218

214219
return dependenciesChecked > 0 ? onlyAbsoluteVersionDetected : false;
@@ -221,7 +226,7 @@ const areVersionsAbsolute = (packageJsonData, nodeName, config) => {
221226
* @param {object} config Rule configuration
222227
* @return {boolean} False if the package has an non-absolute version. True if it is not or the node is missing.
223228
*/
224-
const doVersContainNonAbsolute = (packageJsonData, nodeName, config) => {
229+
export const doVersContainNonAbsolute = (packageJsonData: PackageJson, nodeName: string, config: any): boolean => {
225230
const {onlyAbsoluteVersionDetected, dependenciesChecked} = absoluteVersionChecker(packageJsonData, nodeName, config);
226231

227232
return dependenciesChecked > 0 ? !onlyAbsoluteVersionDetected : false;
@@ -234,21 +239,21 @@ const GITHUB_SHORTCUT_URL = /^(github:)?[^/]+\/[^/]+/;
234239
* @param version value of package's version
235240
* @return {boolean} True if the version is a shortcut to github repository
236241
*/
237-
const isGithubRepositoryShortcut = (version) => GITHUB_SHORTCUT_URL.test(version);
242+
const isGithubRepositoryShortcut = (version): boolean => GITHUB_SHORTCUT_URL.test(version);
238243

239244
/**
240245
* Determines whether or not version is url to archive
241246
* @param version value of package's version
242247
* @return {boolean} True if the version is url to archive
243248
*/
244-
const isArchiveUrl = (version) => version.endsWith('.tgz') || version.endsWith('.tar.gz') || version.endsWith('.zip');
249+
const isArchiveUrl = (version: string): boolean => version.endsWith('.tgz') || version.endsWith('.tar.gz') || version.endsWith('.zip');
245250

246251
/**
247252
* Determines whether or not version is git repository url
248253
* @param version value of package's version
249254
* @return {boolean} True if the version is an git repo url.
250255
*/
251-
const isGitRepositoryUrl = (version) => {
256+
const isGitRepositoryUrl = (version: string): boolean => {
252257
if (isArchiveUrl(version)) {
253258
return false;
254259
}
@@ -275,7 +280,7 @@ const isGitRepositoryUrl = (version) => {
275280
* @param {object} config Rule configuration
276281
* @return {boolean} True if the package has an git repo.
277282
*/
278-
const doVersContainGitRepository = (packageJsonData, nodeName, config) => {
283+
export const doVersContainGitRepository = (packageJsonData: PackageJson, nodeName: string, config: any): boolean => {
279284
for (const dependencyName in packageJsonData[nodeName]) {
280285
if (hasExceptions(config) && config.exceptions.includes(dependencyName)) {
281286
continue;
@@ -298,7 +303,7 @@ const doVersContainGitRepository = (packageJsonData, nodeName, config) => {
298303
* @param {object} config Rule configuration
299304
* @return {boolean} True if the package contain archive url.
300305
*/
301-
const doVersContainArchiveUrl = (packageJsonData, nodeName, config) => {
306+
export const doVersContainArchiveUrl = (packageJsonData: PackageJson, nodeName: string, config: any): boolean => {
302307
for (const dependencyName in packageJsonData[nodeName]) {
303308
if (hasExceptions(config) && config.exceptions.includes(dependencyName)) {
304309
continue;
@@ -321,7 +326,7 @@ const doVersContainArchiveUrl = (packageJsonData, nodeName, config) => {
321326
* @param {object} config Rule configuration
322327
* @return {boolean} True if the package contain file url.
323328
*/
324-
const doVersContainFileUrl = (packageJsonData, nodeName, config) => {
329+
export const doVersContainFileUrl = (packageJsonData: PackageJson, nodeName: string, config: any): boolean => {
325330
for (const dependencyName in packageJsonData[nodeName]) {
326331
if (hasExceptions(config) && config.exceptions.includes(dependencyName)) {
327332
continue;
@@ -336,17 +341,3 @@ const doVersContainFileUrl = (packageJsonData, nodeName, config) => {
336341

337342
return false;
338343
};
339-
340-
module.exports = {
341-
hasDependency,
342-
hasDepPrereleaseVers,
343-
hasDepVersZero,
344-
doesVersStartsWithRange,
345-
areVersRangesValid,
346-
doVersContainInvalidRange,
347-
areVersionsAbsolute,
348-
doVersContainNonAbsolute,
349-
doVersContainGitRepository,
350-
doVersContainArchiveUrl,
351-
doVersContainFileUrl,
352-
};

0 commit comments

Comments
 (0)