Skip to content

Commit 78853eb

Browse files
authored
Ts part 14 (#576)
* Update esbuild.config.js * Add @types/node * Reporter * Update tsconfig.json * Tests * Define more types * Add build to github workflows * Update tsconfig.json
1 parent a8509f0 commit 78853eb

Some content is hidden

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

45 files changed

+687
-430
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ jobs:
3838
with:
3939
node-version: ${{ matrix.node }}
4040
- run: npm ci --no-progress
41+
- run: npm run build
4142
- run: npm run test:ci

.github/workflows/prerelease.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
node-version: '14'
1515
registry-url: 'https://registry.npmjs.org'
1616
- run: npm ci --no-progress --production
17+
- run: npm run build
1718
- run: npm version --no-push --no-git-tag-version --yes ${{ github.event.release.tag_name }}
1819
- run: npm publish --tag next
1920
env:

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
node-version: '14'
1515
registry-url: 'https://registry.npmjs.org'
1616
- run: npm ci --no-progress --production
17+
- run: npm run build
1718
- run: npm version --no-push --no-git-tag-version --yes ${{ github.event.release.tag_name }}
1819
- run: npm publish --tag latest
1920
env:

esbuild.config.js

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,50 @@
1-
// eslint-disable-next-line @typescript-eslint/no-var-requires, import/no-extraneous-dependencies
2-
const esbuild = require('esbuild');
1+
/* eslint-disable @typescript-eslint/no-var-requires, import/no-extraneous-dependencies */
32

3+
const esbuild = require('esbuild');
44
// Automatically exclude all node_modules from the bundled version
5-
// eslint-disable-next-line import/no-extraneous-dependencies, @typescript-eslint/no-var-requires
65
const {nodeExternalsPlugin} = require('esbuild-node-externals');
6+
const {readdirSync} = require('fs');
7+
const path = require('path');
8+
9+
const rulesDirectory = path.join(__dirname, 'src', 'rules');
10+
const bundle = true;
11+
const minify = true;
12+
const platform = 'node';
13+
const sourcemap = true;
14+
const target = 'node12';
15+
const plugins = [nodeExternalsPlugin()];
16+
17+
readdirSync(rulesDirectory).forEach((file) => {
18+
const ruleFilePath = path.join(rulesDirectory, file);
19+
const beginIndex = 0;
20+
const endIndex = -3;
21+
const ruleFileNameWithoutExtension = file.slice(beginIndex, endIndex);
22+
23+
esbuild
24+
.build({
25+
entryPoints: [ruleFilePath],
26+
outfile: `dist/rules/${ruleFileNameWithoutExtension}.js`,
27+
bundle,
28+
minify,
29+
platform,
30+
sourcemap: false,
31+
target,
32+
plugins,
33+
})
34+
// eslint-disable-next-line unicorn/no-process-exit
35+
.catch(() => process.exit(1));
36+
});
737

838
esbuild
939
.build({
1040
entryPoints: ['./src/api.ts'],
1141
outfile: 'dist/api.js',
12-
bundle: true,
13-
minify: true,
14-
platform: 'node',
15-
sourcemap: true,
16-
target: 'node14',
17-
plugins: [nodeExternalsPlugin()],
42+
bundle,
43+
minify,
44+
platform,
45+
sourcemap,
46+
target,
47+
plugins,
1848
})
1949
// eslint-disable-next-line unicorn/no-process-exit
2050
.catch(() => process.exit(1));
@@ -23,12 +53,12 @@ esbuild
2353
.build({
2454
entryPoints: ['./src/cli.ts'],
2555
outfile: 'dist/cli.js',
26-
bundle: true,
27-
minify: true,
28-
platform: 'node',
29-
sourcemap: true,
30-
target: 'node14',
31-
plugins: [nodeExternalsPlugin()],
56+
bundle,
57+
minify,
58+
platform,
59+
sourcemap,
60+
target,
61+
plugins,
3262
})
3363
// eslint-disable-next-line unicorn/no-process-exit
3464
.catch(() => process.exit(1));

package-lock.json

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@
3030
"main": "dist/api.js",
3131
"types": "index.d.ts",
3232
"scripts": {
33-
"build": "node esbuild.config.js",
33+
"build": "npm run esbuild && npm run tsc",
34+
"esbuild": "node esbuild.config.js",
3435
"eslint": "eslint . --format=node_modules/eslint-formatter-pretty",
3536
"npmpackagejsonlint": "node src/cli.js ./package.json",
3637
"lint": "npm run eslint && npm run npmpackagejsonlint",
3738
"test": "jest",
38-
"test:ci": "jest --runInBand"
39+
"test:ci": "jest --runInBand",
40+
"tsc": "tsc --project tsconfig.json"
3941
},
4042
"dependencies": {
4143
"ajv": "^8.10.0",
@@ -58,6 +60,7 @@
5860
},
5961
"devDependencies": {
6062
"@types/jest": "^27.4.1",
63+
"@types/node": "^17.0.21",
6164
"@typescript-eslint/eslint-plugin": "^5.12.1",
6265
"esbuild": "^0.14.23",
6366
"esbuild-node-externals": "^1.4.1",

src/Config.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {validateRules} from './config/ConfigValidator';
33
import {transform} from './config/cosmicConfigTransformer';
44
import {applyExtendsIfSpecified} from './config/applyExtendsIfSpecified';
55
import {applyOverrides} from './config/applyOverrides';
6+
import {Rules} from './rules';
67

78
// eslint-disable-next-line @typescript-eslint/no-var-requires
89
const debug = require('debug')('npm-package-json-lint:Config');
@@ -14,30 +15,37 @@ const noRules = 0;
1415
* @class
1516
*/
1617
export class Config {
18+
/**
19+
* The user passed config object.
20+
*/
1721
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1822
config: any;
1923

24+
/**
25+
* The current working directory.
26+
*/
2027
cwd: string;
2128

29+
/**
30+
* The user passed configFile path.
31+
*/
2232
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2333
configFile: any;
2434

35+
/**
36+
* The base directory that config should be pulled from.
37+
*/
2538
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2639
configBaseDirectory: any;
2740

41+
/**
42+
* Rules object
43+
*/
2844
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2945
rules: any;
3046

31-
/**
32-
* Constructor
33-
*
34-
* @param {string} cwd The current working directory.
35-
* @param {Object} config The user passed config object.
36-
* @param {string} configFile The user passed configFile path.
37-
* @param {string} configBaseDirectory The base directory that config should be pulled from.
38-
* @param {Object} rules Rules object
39-
*/
40-
constructor(cwd, config, configFile, configBaseDirectory, rules) {
47+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
48+
constructor(cwd: string, config: any, configFile: any, configBaseDirectory: any, rules: Rules) {
4149
if (config) {
4250
this.config = applyExtendsIfSpecified(config, 'PassedConfig');
4351
}
@@ -51,12 +59,12 @@ export class Config {
5159
/**
5260
* Gets the config for a file.
5361
*
54-
* @param {string} filePath File path of the file being linted.
62+
* @param filePath File path of the file being linted.
5563
* @returns {Object} A config object.
5664
* @memberof Config
5765
*/
5866
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
59-
getConfigForFile(filePath) {
67+
getConfigForFile(filePath: string) {
6068
debug(`Getting config for ${filePath}`);
6169
const filePathToSearch = filePath;
6270

src/NpmPackageJsonLint.ts

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import isPlainObj from 'is-plain-obj';
22
import slash from 'slash';
3+
import {PackageJson} from 'type-fest';
34
import {Config} from './Config';
4-
import {Rules} from './Rules';
5-
import {executeOnPackageJsonFiles, executeOnPackageJsonObject} from './linter/linter';
5+
import {Rules} from './rules';
6+
import {executeOnPackageJsonFiles, executeOnPackageJsonObject, OverallLintingResult} from './linter/linter';
67
import {getFileList} from './utils/getFileList';
78
import {getIgnorer} from './utils/getIgnorer';
89
import {Severity} from './types/severity';
10+
import {PackageJsonFileLintingResult} from './types/package-json-linting-result';
11+
import {LintIssue} from './lint-issue';
912

1013
// eslint-disable-next-line @typescript-eslint/no-var-requires
1114
const debug = require('debug')('npm-package-json-lint:NpmPackageJsonLint');
@@ -17,29 +20,27 @@ const noIssues = 0;
1720
/**
1821
* Checks if the given issue is an error issue.
1922
*
20-
* @param {LintIssue} issue npm-package-json-lint issue
21-
* @returns {boolean} True if error, false if warning.
23+
* @param issue A {@link LintIssue} object
24+
* @returns True if error, false if warning.
2225
* @private
2326
*/
24-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
25-
const isIssueAnError = (issue) => issue.severity === Severity.Error;
27+
const isIssueAnError = (issue: LintIssue): boolean => issue.severity === Severity.Error;
2628

27-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
28-
const isPackageJsonObjectValid = (packageJsonObject) => isPlainObj(packageJsonObject);
29+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
30+
const isPackageJsonObjectValid = (packageJsonObject: PackageJson | any): boolean => isPlainObj(packageJsonObject);
2931

30-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
31-
const areRequiredOptionsValid = (packageJsonObject, patterns) =>
32+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
33+
const areRequiredOptionsValid = (packageJsonObject: PackageJson | any, patterns: string[]): boolean =>
3234
(!patterns && !isPackageJsonObjectValid(packageJsonObject)) ||
3335
(patterns && (packageJsonObject || isPackageJsonObjectValid(packageJsonObject)));
3436

3537
/**
3638
* Filters results to only include errors.
3739
*
38-
* @param {LintResult[]} results The results to filter.
39-
* @returns {LintResult[]} The filtered results.
40+
* @param results The results to filter.
41+
* @returns The filtered results.
4042
*/
41-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
42-
const getErrorResults = (results) => {
43+
const getErrorResults = (results: PackageJsonFileLintingResult[]): PackageJsonFileLintingResult[] => {
4344
const filtered = [];
4445

4546
results.forEach((result) => {
@@ -60,16 +61,6 @@ const getErrorResults = (results) => {
6061
return filtered;
6162
};
6263

63-
/**
64-
* CLIEngine configuration object
65-
*
66-
* @typedef {Object} NpmPackageJsonLint
67-
* @property {string} configFile The configuration file to use.
68-
* @property {string} cwd The value to use for the current working directory.
69-
* @property {boolean} useConfigFiles False disables use of .npmpackagejsonlintrc.json files, npmpackagejsonlint.config.js files, and npmPackageJsonLintConfig object in package.json file.
70-
* @property {Object<string,*>} rules An object of rules to use.
71-
*/
72-
7364
export interface NpmPackageJsonLintOptions {
7465
cwd?: string;
7566
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -88,10 +79,6 @@ export interface NpmPackageJsonLintOptions {
8879
fix?: boolean;
8980
}
9081

91-
/**
92-
* Public CLIEngine class
93-
* @class
94-
*/
9582
export class NpmPackageJsonLint {
9683
cwd: string;
9784

@@ -117,7 +104,7 @@ export class NpmPackageJsonLint {
117104

118105
/**
119106
* constructor
120-
* @param {NpmPackageJsonLint} options The options for the CLIEngine.
107+
* @param options An instance of the {@link NpmPackageJsonLintOptions} options object.
121108
* @constructor
122109
*/
123110
constructor(options: NpmPackageJsonLintOptions) {
@@ -154,11 +141,9 @@ export class NpmPackageJsonLint {
154141
/**
155142
* Runs the linter using the config specified in the constructor
156143
*
157-
* @returns {LinterResult} The results {@link LinterResult} from linting a collection of package.json files.
158-
* @memberof NpmPackageJsonLint
144+
* @returns The results {@link OverallLintingResult} from linting a collection of package.json files.
159145
*/
160-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
161-
lint() {
146+
lint(): OverallLintingResult {
162147
debug('Starting lint');
163148

164149
if (areRequiredOptionsValid(this.packageJsonObject, this.patterns)) {
@@ -168,7 +153,7 @@ export class NpmPackageJsonLint {
168153
}
169154

170155
const ignorer = getIgnorer(this.cwd, this.ignorePath);
171-
let linterOutput;
156+
let linterOutput: OverallLintingResult;
172157

173158
if (this.patterns) {
174159
debug('Linting using patterns');

0 commit comments

Comments
 (0)