Skip to content

Commit a328893

Browse files
authored
Part 11 (#573)
1 parent a1afee1 commit a328893

File tree

133 files changed

+510
-431
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+510
-431
lines changed

src/Config.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const debug = require('debug')('npm-package-json-lint:Config');
22
import {cosmiconfigSync} from 'cosmiconfig';
3-
import {validate} from './config/ConfigValidator';
3+
import {validate, validateRules} from './config/ConfigValidator';
44
import {transform} from './config/cosmicConfigTransformer';
55
import {applyExtendsIfSpecified} from './config/applyExtendsIfSpecified';
66
import {applyOverrides} from './config/applyOverrides';
@@ -11,7 +11,13 @@ const noRules = 0;
1111
* Config class
1212
* @class
1313
*/
14-
class Config {
14+
export class Config {
15+
config: any;
16+
cwd: string;
17+
configFile: any;
18+
configBaseDirectory: any;
19+
rules: any;
20+
1521
/**
1622
* Constructor
1723
*
@@ -81,10 +87,8 @@ class Config {
8187
debug('Final Config');
8288
debug(config);
8389

84-
validate(config, 'cli', this.rules);
90+
validateRules(config, 'cli', this.rules);
8591

8692
return config;
8793
}
8894
}
89-
90-
module.exports = Config;

src/NpmPackageJsonLint.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {Rules} from './Rules';
77
import {executeOnPackageJsonFiles, executeOnPackageJsonObject} from './linter/linter';
88
import {getFileList} from './utils/getFileList';
99
import {getIgnorer} from './utils/getIgnorer';
10+
import { Severity } from './types/severity';
1011

1112
const noIssues = 0;
1213

@@ -18,7 +19,7 @@ const noIssues = 0;
1819
* @private
1920
*/
2021
const isIssueAnError = (issue) => {
21-
return issue.severity === 'error';
22+
return issue.severity === Severity.Error;
2223
};
2324

2425
const isPackageJsonObjectValid = (packageJsonObject) => isPlainObj(packageJsonObject);
@@ -67,28 +68,55 @@ const getErrorResults = (results) => {
6768
* @property {Object<string,*>} rules An object of rules to use.
6869
*/
6970

71+
export interface NpmPackageJsonLintOptions {
72+
cwd?: string,
73+
packageJsonObject?: any,
74+
packageJsonFilePath?: string,
75+
config?: any,
76+
configFile?: any,
77+
configBaseDirectory?: any,
78+
patterns?: any,
79+
quiet?: boolean,
80+
ignorePath?: string,
81+
fix?: boolean,
82+
}
83+
7084
/**
7185
* Public CLIEngine class
7286
* @class
7387
*/
7488
export class NpmPackageJsonLint {
89+
cwd: string;
90+
packageJsonObject: any;
91+
packageJsonFilePath: string;
92+
patterns: any;
93+
quiet: boolean;
94+
ignorePath: string;
95+
fix: boolean;
96+
version: string;
97+
rules: Rules;
98+
configHelper: Config;
99+
100+
75101
/**
76102
* constructor
77103
* @param {NpmPackageJsonLint} options The options for the CLIEngine.
78104
* @constructor
79105
*/
80-
constructor({
81-
cwd,
82-
packageJsonObject,
83-
packageJsonFilePath,
84-
config,
85-
configFile,
86-
configBaseDirectory,
87-
patterns,
88-
quiet,
89-
ignorePath,
90-
fix,
91-
}) {
106+
constructor(options: NpmPackageJsonLintOptions) {
107+
const {
108+
cwd,
109+
packageJsonObject,
110+
packageJsonFilePath,
111+
config,
112+
configFile,
113+
configBaseDirectory,
114+
patterns,
115+
quiet,
116+
ignorePath,
117+
fix,
118+
} = options;
119+
92120
this.cwd = slash(cwd || process.cwd());
93121

94122
this.packageJsonObject = packageJsonObject;

src/Rules.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import fs from 'fs';
33
import path from 'path';
44

55
export class Rules {
6+
rules: any;
7+
68
/**
79
* Constructor
810
*/

src/config/ConfigSchema.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const standardRuleSchema = {
2222
},
2323
};
2424

25-
const arrayRuleSchema = (minItems) => ({
25+
const arrayRuleSchema = (minItems: any ): any => ({
2626
type: 'array',
2727
items: [
2828
standardRuleSchema,
@@ -133,7 +133,7 @@ const configurationSchema = {
133133
* @param {Object} ruleConfig The ruleConfig object to validate.
134134
* @returns {boolean} True if valid. Error if not.
135135
*/
136-
export const isStandardRuleSchemaValid = (ruleConfig) => {
136+
export const isStandardRuleSchemaValid = (ruleConfig: any ): any => {
137137
const validate = ajv.compile(standardRuleSchema);
138138
const isValid = validate(ruleConfig);
139139

@@ -151,7 +151,7 @@ export const isStandardRuleSchemaValid = (ruleConfig) => {
151151
* @param {number} minItems Min number of items in the array
152152
* @returns {boolean} True if valid. Error if not.
153153
*/
154-
export const isArrayRuleSchemaValid = (ruleConfig, minItems) => {
154+
export const isArrayRuleSchemaValid = (ruleConfig: any , minItems: any ): any => {
155155
const validate = ajv.compile(arrayRuleSchema(minItems));
156156
const isValid = validate(ruleConfig);
157157

@@ -168,7 +168,7 @@ export const isArrayRuleSchemaValid = (ruleConfig, minItems) => {
168168
* @param {Object} ruleConfig The ruleConfig object to validate.
169169
* @returns {boolean} True if valid. Error if not.
170170
*/
171-
export const isObjectRuleSchemaValid = (ruleConfig) => {
171+
export const isObjectRuleSchemaValid = (ruleConfig: any ): any => {
172172
const validate = ajv.compile(objectRuleSchema);
173173
const isValid = validate(ruleConfig);
174174

@@ -185,7 +185,7 @@ export const isObjectRuleSchemaValid = (ruleConfig) => {
185185
* @param {Object} ruleConfig The ruleConfig object to validate.
186186
* @returns {boolean} True if valid. Error if not.
187187
*/
188-
export const isOptionalObjExceptSchemaValid = (ruleConfig) => {
188+
export const isOptionalObjExceptSchemaValid = (ruleConfig: any ): any => {
189189
const validate = ajv.compile(optionalObjExceptionsSchema);
190190
const isValid = validate(ruleConfig);
191191

@@ -203,7 +203,7 @@ export const isOptionalObjExceptSchemaValid = (ruleConfig) => {
203203
* @param {string} source The name of the configuration source to report in any errors.
204204
* @returns {boolean} True if valid. Error if not.
205205
*/
206-
export const isConfigObjectSchemaValid = (config, source) => {
206+
export const isConfigObjectSchemaValid = (config: any , source: any ): any => {
207207
const validate = ajv.compile(configurationSchema);
208208
const isValid = validate(config);
209209

src/config/ConfigValidator.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {isArrayRuleSchemaValid, isConfigObjectSchemaValid, isObjectRuleSchemaVal
77
* @return {Boolean} True if config is valid, false if not
88
* @static
99
*/
10-
const isObjectRuleConfigValid = (ruleConfig) => {
10+
const isObjectRuleConfigValid = (ruleConfig: any ): any => {
1111
if (typeof ruleConfig === 'string' && ruleConfig === 'off') {
1212
return true;
1313
}
@@ -26,7 +26,7 @@ const isObjectRuleConfigValid = (ruleConfig) => {
2626
* @return {Boolean} True if config is valid, false if not
2727
* @static
2828
*/
29-
const isOptionalObjRuleConfigValid = (ruleConfig) => {
29+
const isOptionalObjRuleConfigValid = (ruleConfig: any ): any => {
3030
const object = 1;
3131

3232
if (typeof ruleConfig === 'string') {
@@ -48,7 +48,7 @@ const isOptionalObjRuleConfigValid = (ruleConfig) => {
4848
* @return {Boolean} True if config is valid, false if not
4949
* @static
5050
*/
51-
const isArrayRuleConfigValid = (ruleConfig, minItems) => {
51+
const isArrayRuleConfigValid = (ruleConfig: any , minItems: any ): any => {
5252
if (typeof ruleConfig === 'string' && ruleConfig === 'off') {
5353
return true;
5454
}
@@ -67,7 +67,7 @@ const isArrayRuleConfigValid = (ruleConfig, minItems) => {
6767
* @return {Boolean} True if config is valid, error if not
6868
* @static
6969
*/
70-
const isStandardRuleConfigValid = (ruleConfig) => isStandardRuleSchemaValid(ruleConfig);
70+
const isStandardRuleConfigValid = (ruleConfig: any ): any => isStandardRuleSchemaValid(ruleConfig);
7171

7272
/**
7373
* Validates configuration of a rule
@@ -78,7 +78,7 @@ const isStandardRuleConfigValid = (ruleConfig) => isStandardRuleSchemaValid(rule
7878
* @param {String|null} source The name of the configuration source to report in any errors.
7979
* @returns {undefined} No return
8080
*/
81-
const validateRule = (ruleModule, ruleName, userConfig, source) => {
81+
const validateRule = (ruleModule: any , ruleName: any , userConfig: any , source: any ): any => {
8282
if (ruleModule) {
8383
try {
8484
switch (ruleModule.ruleType) {
@@ -121,7 +121,7 @@ const validateRule = (ruleModule, ruleName, userConfig, source) => {
121121
* @returns {undefined} No return
122122
* @static
123123
*/
124-
export const validateRules = (rulesConfig, source, rules) => {
124+
export const validateRules = (rulesConfig: any , source: any , rules: any ): any => {
125125
if (!rulesConfig) {
126126
return;
127127
}
@@ -142,7 +142,7 @@ export const validateRules = (rulesConfig, source, rules) => {
142142
* @returns {undefined} No return
143143
* @static
144144
*/
145-
export const validate = (config, source, rules) => {
145+
export const validate = (config: any , source: any , rules: any ): any => {
146146
isConfigObjectSchemaValid(config, source);
147147
validateRules(config.rules, source, rules);
148148
};

src/config/applyExtendsIfSpecified.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {Parser} from '../Parser';
1111
* @returns {Object} A new configuration object with all of the 'extends' fields loaded and merged.
1212
* @private
1313
*/
14-
const applyExtends = (config, parentName, originalFilePath) => {
14+
const applyExtends = (config: any , parentName: any , originalFilePath: any ): any => {
1515
let configExtends = config.extends;
1616

1717
if (!Array.isArray(config.extends)) {
@@ -68,7 +68,7 @@ const applyExtends = (config, parentName, originalFilePath) => {
6868
* @return {Object} Configuration object
6969
* @private
7070
*/
71-
const loadFromModule = (moduleName, originalFilePath) => {
71+
const loadFromModule = (moduleName: any , originalFilePath: any ): any => {
7272
let config = {};
7373
let adjustedModuleName = moduleName;
7474

@@ -83,6 +83,7 @@ const loadFromModule = (moduleName, originalFilePath) => {
8383
config = require(resolvedModule);
8484
}
8585

86+
// @ts-expect-error
8687
if (Object.keys(config).length > 0 && config.extends) {
8788
config = applyExtends(config, adjustedModuleName, originalFilePath);
8889
}
@@ -98,7 +99,7 @@ const loadFromModule = (moduleName, originalFilePath) => {
9899
* @returns {Object} The configuration information.
99100
* @private
100101
*/
101-
const loadConfigFile = (filePath) => {
102+
const loadConfigFile = (filePath: any ): any => {
102103
let config = {};
103104

104105
switch (path.extname(filePath)) {
@@ -125,7 +126,7 @@ const loadConfigFile = (filePath) => {
125126
* @returns {Object} the parsed config object (empty object if there was a parse error)
126127
* @private
127128
*/
128-
export const applyExtendsIfSpecified = (npmPackageJsonLintConfig, filepath) => {
129+
export const applyExtendsIfSpecified = (npmPackageJsonLintConfig: any , filepath: any ): any => {
129130
let config = {...npmPackageJsonLintConfig};
130131

131132
debug('Loading extends, if applicable');

src/config/applyOverrides.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import globby from 'globby';
1111
* @returns {Object} A new configuration object with all of the 'overrides' applied.
1212
* @private
1313
*/
14-
export const applyOverrides = (cwd: string, filePath: string, rules: any, overrides: any): any => {
14+
export const applyOverrides = (cwd: string, filePath: string, rules: any, overrides?: any[]): any => {
1515
let finalRules = {...rules};
1616

1717
debug('overrides');

src/config/cosmicConfigTransformer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const debug = require('debug')('npm-package-json-lint:cosmicConfigTransformer');
33
import {applyExtendsIfSpecified} from './applyExtendsIfSpecified';
44
import {applyOverrides} from './applyOverrides';
55

6-
export const transform = (cwd, configBaseDirectory, filePathBeingLinted) => {
6+
export const transform = (cwd: any , configBaseDirectory: any , filePathBeingLinted: any ): any => {
77
debug(`cwd: ${cwd}`);
88
debug(`configBaseDirectory`);
99
debug(configBaseDirectory);

src/linter/linter.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const debug = require('debug')('npm-package-json-lint:linter');
22
import path from 'path';
33
import {Parser} from '../Parser';
4-
import {resultsHelper} from './results-helper';
4+
import { RuleType } from '../types/rule-type';
5+
import { Severity } from '../types/severity';
6+
import {aggregateCountsPerFile, aggregateOverallCounts} from './results-helper';
57

68
/**
79
* A package.json file linting result.
@@ -48,13 +50,13 @@ const lint = (packageJsonData, configObj, rules) => {
4850
for (const rule in configObj) {
4951
const ruleModule = rules.get(rule);
5052

51-
let severity = 'off';
53+
let severity = Severity.Off;
5254
let ruleConfig = {};
5355

54-
if (ruleModule.ruleType === 'array' || ruleModule.ruleType === 'object') {
56+
if (ruleModule.ruleType === RuleType.Array || ruleModule.ruleType === RuleType.Object) {
5557
severity = typeof configObj[rule] === 'string' && configObj[rule] === 'off' ? configObj[rule] : configObj[rule][0];
5658
ruleConfig = typeof configObj[rule] === 'string' ? {} : configObj[rule][1];
57-
} else if (ruleModule.ruleType === 'optionalObject') {
59+
} else if (ruleModule.ruleType === RuleType.OptionalObject) {
5860
if (typeof configObj[rule] === 'string') {
5961
severity = configObj[rule];
6062
ruleConfig = {};
@@ -66,7 +68,7 @@ const lint = (packageJsonData, configObj, rules) => {
6668
severity = configObj[rule];
6769
}
6870

69-
if (severity !== 'off') {
71+
if (severity !== Severity.Off) {
7072
const lintResult = ruleModule.lint(packageJsonData, severity, ruleConfig);
7173

7274
if (typeof lintResult === 'object') {
@@ -91,7 +93,7 @@ const lint = (packageJsonData, configObj, rules) => {
9193
*/
9294
const processPackageJsonObject = (cwd, packageJsonObj, config, fileName, rules) => {
9395
const lintIssues = lint(packageJsonObj, config, rules);
94-
const counts = resultsHelper.aggregateCountsPerFile(lintIssues);
96+
const counts = aggregateCountsPerFile(lintIssues);
9597
const result = createResultObject({
9698
cwd,
9799
fileName,
@@ -140,7 +142,7 @@ const processPackageJsonFile = (cwd, fileName, config, rules) => {
140142
* @param {Object} rules An instance of `Rules`.
141143
* @returns {LinterResult} The results {@link LinterResult} from linting a collection of package.json files.
142144
*/
143-
export const executeOnPackageJsonObject = ({cwd, packageJsonObject, filename, ignorer, configHelper, rules}) => {
145+
export const executeOnPackageJsonObject = ({cwd, packageJsonObject, filename, ignorer, configHelper, rules}): any => {
144146
debug('executing on package.json object');
145147
const results = [];
146148

@@ -172,7 +174,7 @@ export const executeOnPackageJsonObject = ({cwd, packageJsonObject, filename, ig
172174
}
173175

174176
debug('Aggregating overall counts');
175-
const stats = resultsHelper.aggregateOverallCounts(results);
177+
const stats = aggregateOverallCounts(results);
176178

177179
debug('stats');
178180
debug(stats);
@@ -194,7 +196,7 @@ export const executeOnPackageJsonObject = ({cwd, packageJsonObject, filename, ig
194196
* @param {Object} rules An instance of `Rules`.
195197
* @returns {LinterResult} The results {@link LinterResult} from linting a collection of package.json files.
196198
*/
197-
export const executeOnPackageJsonFiles = ({cwd, fileList, ignorer, configHelper, rules}) => {
199+
export const executeOnPackageJsonFiles = ({cwd, fileList, ignorer, configHelper, rules}): any => {
198200
debug('executing on package.json files');
199201
const results = fileList.map((filePath) => {
200202
const relativeFilePath = path.relative(cwd, filePath);
@@ -221,7 +223,7 @@ export const executeOnPackageJsonFiles = ({cwd, fileList, ignorer, configHelper,
221223
});
222224

223225
debug('Aggregating overall counts');
224-
const stats = resultsHelper.aggregateOverallCounts(results);
226+
const stats = aggregateOverallCounts(results);
225227

226228
debug('stats');
227229
debug(stats);

0 commit comments

Comments
 (0)