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