Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 0 additions & 43 deletions src/LintIssue.js

This file was deleted.

66 changes: 66 additions & 0 deletions src/lint-issue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import chalk from 'chalk';
import logSymbols from 'log-symbols';
import {Severity} from './types/severity';

/**
* A lint issue
*/
export class LintIssue {
/**
* Unique, lowercase, hyphen-separate name for the lint
*
* @type {string}
* @memberof LintIssue
*/
lintId: string;
/**
* 'error' or 'warning'
*
* @type {Severity}
* @memberof LintIssue
*/
severity: Severity;
/**
* Name of the node in the JSON the lint audits
*
* @type {string}
* @memberof LintIssue
*/
node: string
/**
* Human-friendly message to users
*
* @type {string}
* @memberof LintIssue
*/
lintMessage: string

/**
* Creates an instance of LintIssue.
* @param lintId Unique, lowercase, hyphen-separate name for the lint
* @param severity 'error' or 'warning'
* @param node Name of the node in the JSON the lint audits
* @param lintMessage Human-friendly message to users
* @memberof LintIssue
*/
constructor(lintId: string, severity: Severity, node: string, lintMessage: string) {
this.lintId = lintId;
this.severity = severity;
this.node = node;
this.lintMessage = lintMessage;
}

/**
* Helper to convert the LintIssue to a printable string
*
* @returns {string} Human-friendly message about the lint issue
*/
toString(): string {
const logSymbol = this.severity === Severity.Error ? logSymbols.error : logSymbols.warning;
const formattedLintId = chalk.gray.dim(this.lintId);
const formattedNode = chalk.gray.bold(this.node);
const formattedMessage = this.severity === Severity.Error ? chalk.bold.red(this.lintMessage) : chalk.yellow(this.lintMessage);

return `${logSymbol} ${formattedLintId} - node: ${formattedNode} - ${formattedMessage}`;
}
}
20 changes: 0 additions & 20 deletions src/rules/bin-type.js

This file was deleted.

18 changes: 18 additions & 0 deletions src/rules/bin-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {isObject, isString} from '../validators/type';
import {LintIssue} from '../lint-issue';
import {RuleType} from '../types/rule-type';
import {Severity} from '../types/severity';
import {PackageJson} from 'type-fest';

const lintId = 'bin-type';
const nodeName = 'bin';
const message = 'Type should be either a string or an Object';
export const ruleType = RuleType.Standard;

export const lint = (packageJsonData: PackageJson, severity: Severity): LintIssue | boolean => {
if (!isString(packageJsonData, nodeName) && !isObject(packageJsonData, nodeName)) {
return new LintIssue(lintId, severity, nodeName, message);
}

return true;
};
3 changes: 3 additions & 0 deletions src/types/lint-result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface LintResult {

}
4 changes: 4 additions & 0 deletions src/types/rule-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum RuleType {
Standard = 'standard',

}
4 changes: 4 additions & 0 deletions src/types/severity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum Severity {
Error = 'error',
Warning = 'warning'
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import {PackageJson} from 'type-fest';

const increment = 1;

export interface IsInAlphabeticalOrderResult {
status: boolean;
data: {
invalidNode: string | null;
validNode: string | null;
}
}

/**
* Determines whether an array is in alphabetical order
* @param {object} packageJsonData Valid JSON
* @param {string} nodeName Name of a node in the package.json file
* @return {object} Object containing the status and the dependencies that are out of order, if applicable
*
* @param packageJsonData Valid JSON
* @param nodeName Name of a node in the package.json file
* @return Object containing the status and the dependencies that are out of order, if applicable
*/
const isInAlphabeticalOrder = (packageJsonData, nodeName) => {
export const isInAlphabeticalOrder = (packageJsonData: PackageJson, nodeName: string): IsInAlphabeticalOrderResult => {
let isValid = true;
let data = {
invalidNode: null,
Expand All @@ -31,7 +42,3 @@ const isInAlphabeticalOrder = (packageJsonData, nodeName) => {
data,
};
};

module.exports = {
isInAlphabeticalOrder,
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint no-restricted-syntax: 'off', guard-for-in: 'off', no-continue: 'off' */
const semver = require('semver');
import semver from 'semver';
import {PackageJson} from 'type-fest';

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

/**
* Determines whether or not the package has a given dependency
Expand All @@ -10,7 +10,7 @@ const hasExceptions = (config) => typeof config === 'object' && config.hasOwnPro
* @param {string} depsToCheckFor An array of packages to check for
* @return {boolean} True if the package has a dependency. False if it is not or the node is missing.
*/
const hasDependency = (packageJsonData, nodeName, depsToCheckFor) => {
export const hasDependency = (packageJsonData: PackageJson, nodeName: string, depsToCheckFor: string[]): boolean => {
if (!packageJsonData.hasOwnProperty(nodeName)) {
return false;
}
Expand Down Expand Up @@ -40,7 +40,7 @@ const hasDependency = (packageJsonData, nodeName, depsToCheckFor) => {
* @param {string} depsToCheckFor An array of packages to check for
* @return {boolean} True if the package has a pre-release version of a dependency. False if it is not or the node is missing.
*/
const hasDepPrereleaseVers = (packageJsonData, nodeName, depsToCheckFor) => {
export const hasDepPrereleaseVers = (packageJsonData: PackageJson, nodeName: string, depsToCheckFor: string[]): boolean => {
if (!packageJsonData.hasOwnProperty(nodeName)) {
return false;
}
Expand All @@ -65,7 +65,7 @@ const hasDepPrereleaseVers = (packageJsonData, nodeName, depsToCheckFor) => {
* @param {object} config Rule configuration
* @return {boolean} True if the package has a dependency with version 0. False if it does not or the node is missing.
*/
const hasDepVersZero = (packageJsonData, nodeName, config) => {
export const hasDepVersZero = (packageJsonData: PackageJson, nodeName: string, config: any): boolean => {
for (const dependencyName in packageJsonData[nodeName]) {
if (hasExceptions(config) && config.exceptions.includes(dependencyName)) {
continue;
Expand Down Expand Up @@ -96,7 +96,7 @@ const hasDepVersZero = (packageJsonData, nodeName, config) => {
* @param {String} rangeSpecifier A version range specifier
* @return {Boolean} True if the version starts with the range, false if it doesn't.
*/
const doesVersStartsWithRange = (dependencyVersion, rangeSpecifier) => {
export const doesVersStartsWithRange = (dependencyVersion: string, rangeSpecifier: string): boolean => {
const firstCharOfStr = 0;

return dependencyVersion.startsWith(rangeSpecifier, firstCharOfStr);
Expand All @@ -110,7 +110,7 @@ const doesVersStartsWithRange = (dependencyVersion, rangeSpecifier) => {
* @param {object} config Rule configuration
* @return {boolean} False if the package has an invalid range. True if it is not or the node is missing.
*/
const areVersRangesValid = (packageJsonData, nodeName, rangeSpecifier, config) => {
export const areVersRangesValid = (packageJsonData: PackageJson, nodeName: string, rangeSpecifier: string, config: any): boolean => {
if (!packageJsonData.hasOwnProperty(nodeName)) {
return true;
}
Expand Down Expand Up @@ -140,7 +140,7 @@ const areVersRangesValid = (packageJsonData, nodeName, rangeSpecifier, config) =
* @param {object} config Rule configuration
* @return {Boolean} True if any dependencies versions start with the invalid range, false if they don't.
*/
const doVersContainInvalidRange = (packageJsonData, nodeName, rangeSpecifier, config) => {
export const doVersContainInvalidRange = (packageJsonData: PackageJson, nodeName: string, rangeSpecifier: string, config: any): boolean => {
if (!packageJsonData.hasOwnProperty(nodeName)) {
return false;
}
Expand All @@ -162,14 +162,19 @@ const doVersContainInvalidRange = (packageJsonData, nodeName, rangeSpecifier, co
return containsInvalidVersion;
};

export interface AbsoluteVersionCheckerResult {
onlyAbsoluteVersionDetected: boolean;
dependenciesChecked: number;
}

/**
* Determines whether or not all dependency versions are absolut
* @param {object} packageJsonData Valid JSON
* @param {string} nodeName Name of a node in the package.json file
* @param {object} config Rule configuration
* @return {boolean} False if the package has an non-absolute version. True if it is not or the node is missing.
*/
const absoluteVersionChecker = (packageJsonData, nodeName, config) => {
const absoluteVersionChecker = (packageJsonData: PackageJson, nodeName: string, config: any): AbsoluteVersionCheckerResult => {
const notFound = -1;
const firstCharOfStr = 0;
let onlyAbsoluteVersionDetected = true;
Expand Down Expand Up @@ -208,7 +213,7 @@ const absoluteVersionChecker = (packageJsonData, nodeName, config) => {
* @param {object} config Rule configuration
* @return {boolean} False if the package has an non-absolute version. True if it is not or the node is missing.
*/
const areVersionsAbsolute = (packageJsonData, nodeName, config) => {
export const areVersionsAbsolute = (packageJsonData: PackageJson, nodeName: string, config: any): boolean => {
const {onlyAbsoluteVersionDetected, dependenciesChecked} = absoluteVersionChecker(packageJsonData, nodeName, config);

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

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

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

/**
* Determines whether or not version is git repository url
* @param version value of package's version
* @return {boolean} True if the version is an git repo url.
*/
const isGitRepositoryUrl = (version) => {
const isGitRepositoryUrl = (version: string): boolean => {
if (isArchiveUrl(version)) {
return false;
}
Expand All @@ -275,7 +280,7 @@ const isGitRepositoryUrl = (version) => {
* @param {object} config Rule configuration
* @return {boolean} True if the package has an git repo.
*/
const doVersContainGitRepository = (packageJsonData, nodeName, config) => {
export const doVersContainGitRepository = (packageJsonData: PackageJson, nodeName: string, config: any): boolean => {
for (const dependencyName in packageJsonData[nodeName]) {
if (hasExceptions(config) && config.exceptions.includes(dependencyName)) {
continue;
Expand All @@ -298,7 +303,7 @@ const doVersContainGitRepository = (packageJsonData, nodeName, config) => {
* @param {object} config Rule configuration
* @return {boolean} True if the package contain archive url.
*/
const doVersContainArchiveUrl = (packageJsonData, nodeName, config) => {
export const doVersContainArchiveUrl = (packageJsonData: PackageJson, nodeName: string, config: any): boolean => {
for (const dependencyName in packageJsonData[nodeName]) {
if (hasExceptions(config) && config.exceptions.includes(dependencyName)) {
continue;
Expand All @@ -321,7 +326,7 @@ const doVersContainArchiveUrl = (packageJsonData, nodeName, config) => {
* @param {object} config Rule configuration
* @return {boolean} True if the package contain file url.
*/
const doVersContainFileUrl = (packageJsonData, nodeName, config) => {
export const doVersContainFileUrl = (packageJsonData: PackageJson, nodeName: string, config: any): boolean => {
for (const dependencyName in packageJsonData[nodeName]) {
if (hasExceptions(config) && config.exceptions.includes(dependencyName)) {
continue;
Expand All @@ -336,17 +341,3 @@ const doVersContainFileUrl = (packageJsonData, nodeName, config) => {

return false;
};

module.exports = {
hasDependency,
hasDepPrereleaseVers,
hasDepVersZero,
doesVersStartsWithRange,
areVersRangesValid,
doVersContainInvalidRange,
areVersionsAbsolute,
doVersContainNonAbsolute,
doVersContainGitRepository,
doVersContainArchiveUrl,
doVersContainFileUrl,
};
Loading