Skip to content

Commit c511081

Browse files
anikethsahaevilebottnawi
authored andcommitted
refactor: added type checking using comments (#2107)
1 parent 4579775 commit c511081

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

lib/utils/addEntries.js

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
const webpack = require('webpack');
44
const createDomain = require('./createDomain');
55

6+
/**
7+
* A Entry, it can be of type string or string[] or Object<string | string[],string>
8+
* @typedef {(string[] | string | Object<string | string[],string>)} Entry
9+
*/
10+
11+
/**
12+
* Add entries Method
13+
* @param {?Object} config - Webpack config
14+
* @param {?Object} options - Dev-Server options
15+
* @param {?Object} server
16+
* @returns {void}
17+
*/
618
function addEntries(config, options, server) {
719
if (options.inline !== false) {
820
// we're stubbing the app in this method as it's static and doesn't require
@@ -15,21 +27,33 @@ function addEntries(config, options, server) {
1527
},
1628
};
1729

30+
/** @type {string} */
1831
const domain = createDomain(options, app);
32+
/** @type {string} */
1933
const sockHost = options.sockHost ? `&sockHost=${options.sockHost}` : '';
34+
/** @type {string} */
2035
const sockPath = options.sockPath ? `&sockPath=${options.sockPath}` : '';
36+
/** @type {string} */
2137
const sockPort = options.sockPort ? `&sockPort=${options.sockPort}` : '';
38+
/** @type {string} */
2239
const clientEntry = `${require.resolve(
2340
'../../client/'
2441
)}?${domain}${sockHost}${sockPath}${sockPort}`;
42+
43+
/** @type {(string[] | string)} */
2544
let hotEntry;
2645

2746
if (options.hotOnly) {
2847
hotEntry = require.resolve('webpack/hot/only-dev-server');
2948
} else if (options.hot) {
3049
hotEntry = require.resolve('webpack/hot/dev-server');
3150
}
32-
51+
/**
52+
* prependEntry Method
53+
* @param {Entry} originalEntry
54+
* @param {Entry} additionalEntries
55+
* @returns {Entry}
56+
*/
3357
const prependEntry = (originalEntry, additionalEntries) => {
3458
if (typeof originalEntry === 'function') {
3559
return () =>
@@ -39,6 +63,7 @@ function addEntries(config, options, server) {
3963
}
4064

4165
if (typeof originalEntry === 'object' && !Array.isArray(originalEntry)) {
66+
/** @type {Object<string,string>} */
4267
const clone = {};
4368

4469
Object.keys(originalEntry).forEach((key) => {
@@ -51,6 +76,7 @@ function addEntries(config, options, server) {
5176

5277
// in this case, entry is a string or an array.
5378
// make sure that we do not add duplicates.
79+
/** @type {Entry} */
5480
const entriesClone = additionalEntries.slice(0);
5581
[].concat(originalEntry).forEach((newEntry) => {
5682
if (!entriesClone.includes(newEntry)) {
@@ -60,21 +86,38 @@ function addEntries(config, options, server) {
6086
return entriesClone;
6187
};
6288

89+
/**
90+
*
91+
* Description of the option for checkInject method
92+
* @typedef {Function} checkInjectOptionsParam
93+
* @param {Object} _config - compilerConfig
94+
* @return {Boolean}
95+
*/
96+
97+
/**
98+
*
99+
* @param {Boolean | checkInjectOptionsParam} option - inject(Hot|Client) it is Boolean | fn => Boolean
100+
* @param {Object} _config
101+
* @param {Boolean} defaultValue
102+
* @return {Boolean}
103+
*/
63104
// eslint-disable-next-line no-shadow
64-
const checkInject = (option, config, defaultValue) => {
105+
const checkInject = (option, _config, defaultValue) => {
65106
if (typeof option === 'boolean') return option;
66-
if (typeof option === 'function') return option(config);
107+
if (typeof option === 'function') return option(_config);
67108
return defaultValue;
68109
};
69110

70111
// eslint-disable-next-line no-shadow
71112
[].concat(config).forEach((config) => {
113+
/** @type {Boolean} */
72114
const webTarget =
73115
config.target === 'web' ||
74116
config.target === 'webworker' ||
75117
config.target === 'electron-renderer' ||
76118
config.target === 'node-webkit' ||
77119
config.target == null;
120+
/** @type {Entry} */
78121
const additionalEntries = checkInject(
79122
options.injectClient,
80123
config,

package-lock.json

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

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"lint:prettier": "prettier \"{**/*,*}.{js,json,md,yml,css}\" --list-different",
1818
"lint:js": "eslint . --cache",
1919
"lint": "npm-run-all -l -p \"lint:**\"",
20+
"lint:type": "tsc --noEmit",
2021
"commitlint": "commitlint --from=master",
2122
"security": "npm audit",
2223
"test:only": "jest",
@@ -107,6 +108,7 @@
107108
"style-loader": "^0.23.1",
108109
"supertest": "^4.0.2",
109110
"tcp-port-used": "^1.0.1",
111+
"typescript": "^3.4.5",
110112
"url-loader": "^1.1.2",
111113
"webpack": "^4.38.0",
112114
"webpack-cli": "^3.3.6"

tsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2017",
4+
"module": "commonjs",
5+
"lib": ["es2017", "dom"],
6+
"allowJs": true,
7+
"checkJs": true,
8+
"noEmit": true,
9+
"strict": false,
10+
"noImplicitThis": true,
11+
"alwaysStrict": true,
12+
"types": ["node"],
13+
"esModuleInterop": true
14+
},
15+
"include": ["lib/utils/addEntries.js"]
16+
}

0 commit comments

Comments
 (0)