Skip to content

Commit 57ba77b

Browse files
committed
Avoid coercing to boolean where possible.
1 parent c078234 commit 57ba77b

34 files changed

+92
-90
lines changed

.babel.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function BabelModePlugin({ types }) {
5656
}
5757
})
5858

59-
return !! parent
59+
return parent !== void 0
6060
}
6161

6262
function isSimpleParameterList(params) {

esm.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ const jestEnvironmentHooks = { __proto__: null }
8888

8989
function jestTransform(content, filename, { cwd, testEnvironment }) {
9090

91-
if (! jestEnvironmentHooks[cwd]) {
91+
if (! has(jestEnvironmentHooks, cwd)) {
9292
jestEnvironmentHooks[cwd] = {
9393
compile: { __proto__: null },
9494
loader: makeRequireFunction(module)
@@ -130,7 +130,7 @@ function jestTransform(content, filename, { cwd, testEnvironment }) {
130130
runtimeName
131131
})
132132

133-
if (! jestEnvironmentHooks[cwd][testEnvironment]) {
133+
if (! has(jestEnvironmentHooks[cwd], testEnvironment)) {
134134
jestEnvironmentHooks[testEnvironment] = true
135135

136136
Environment.prototype.runScript = function (...args) {

src/acorn/parser/error-messages.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,15 @@ function init() {
141141
}
142142

143143
function unexpected(pos) {
144-
if (pos == null) {
144+
if (pos === void 0) {
145145
pos = this.start
146146
}
147147

148148
const message = this.type === tt.eof
149149
? UNEXPECTED_EOS
150150
: INVALID_OR_UNEXPECTED_TOKEN
151151

152-
this.raise(pos != null ? pos : this.start, message)
152+
this.raise(pos, message)
153153
}
154154

155155
return Plugin

src/builtin/console.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function init() {
120120
const Console = maskFunction(function (...args) {
121121
const target = new.target
122122

123-
if (! target) {
123+
if (target === void 0) {
124124
return Reflect.construct(Console, args)
125125
}
126126

src/caching-compiler.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function init() {
5656
const { map } = cache
5757
const meta = map[cacheName]
5858

59-
if (! meta) {
59+
if (meta === void 0) {
6060
return null
6161
}
6262

@@ -145,8 +145,8 @@ function init() {
145145
const { cacheName, cachePath } = options
146146
const result = compileAndCache(code, options)
147147

148-
if (! cacheName ||
149-
! cachePath ||
148+
if (cacheName === void 0 ||
149+
cachePath === void 0 ||
150150
! result.changed) {
151151
return result
152152
}
@@ -269,7 +269,7 @@ function init() {
269269
let scriptData
270270
let changed = false
271271

272-
if (! cachedData) {
272+
if (cachedData === void 0) {
273273
scriptData = useCreateCachedData
274274
? script.createCachedData()
275275
: script.cachedData
@@ -370,7 +370,7 @@ function init() {
370370
for (const cacheName in map) {
371371
const meta = map[cacheName]
372372

373-
if (! meta) {
373+
if (meta === void 0) {
374374
continue
375375
}
376376

src/env/get-options.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ function init() {
3030
let options = ESM_OPTIONS.trim()
3131

3232
if (isPath(options)) {
33-
options = readFile(resolve(options), "utf8")
33+
options = readFile(resolve(options), "utf8") || ""
3434

3535
if (options) {
3636
options = options.trim()
3737
}
3838
}
3939

40-
if (! options) {
40+
if (options === "") {
4141
return env.options = null
4242
}
4343

src/env/has-loader-value.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function init() {
2323
if (isPath(value)) {
2424
let thePath = value
2525

26-
if (! extname(thePath)) {
26+
if (extname(thePath) === "") {
2727
thePath += sep + "index.js"
2828
}
2929

src/hook/module.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function hook(Mod, parent) {
7070

7171
let parentPkg = Package.from(parent)
7272

73-
if (! parentPkg) {
73+
if (parentPkg === null) {
7474
parentPkg = Package.from(parent, OPTIONS || true)
7575
}
7676

@@ -126,7 +126,7 @@ function hook(Mod, parent) {
126126
entry.mtime = getMtime(filename)
127127
}
128128

129-
if (! cacheName) {
129+
if (cacheName === null) {
130130
cacheName =
131131
entry.cacheName = getCacheName(mtime, {
132132
cachePath,
@@ -157,7 +157,7 @@ function hook(Mod, parent) {
157157

158158
let { compileData } = entry
159159

160-
if (! compileData &&
160+
if (compileData === null &&
161161
cache.compile[cacheName] === null) {
162162
compileData = Compiler.from(entry)
163163

@@ -178,7 +178,7 @@ function hook(Mod, parent) {
178178
})
179179
}
180180

181-
if (! compileData &&
181+
if (compileData === null &&
182182
passthruMap.get(func)) {
183183
tryPassthru.call(this, func, args, pkg)
184184
} else {
@@ -202,7 +202,7 @@ function hook(Mod, parent) {
202202

203203
let passthru =
204204
typeof extCompiler === "function" &&
205-
! extCompiler[shared.symbol.mjs]
205+
! has(extCompiler, shared.symbol.mjs)
206206

207207
if (passthru &&
208208
extIsMJS) {

src/module/esm/import.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function esmImport(entry, request, setterArgsList, isExport) {
2424
const dependencySpecifiers = compileData && compileData.dependencySpecifiers
2525

2626
let child
27-
let childEntry
27+
let childEntry = null
2828

2929
if (dependencySpecifiers &&
3030
dependencySpecifiers[request] &&
@@ -48,7 +48,7 @@ function esmImport(entry, request, setterArgsList, isExport) {
4848

4949
const exported = tryRequire(request, entry)
5050

51-
if (! childEntry) {
51+
if (childEntry === null) {
5252
// Create the child entry for unresolved mocked requests.
5353
childEntry = getEntryFrom(request, exported)
5454
child = childEntry.module

src/module/esm/resolve-filename.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function resolveFilename(request, parent, isMain, options) {
103103

104104
const cached = cache.get(cacheKey)
105105

106-
if (cached) {
106+
if (cached !== void 0) {
107107
return cached
108108
}
109109
}
@@ -137,7 +137,7 @@ function resolveFilename(request, parent, isMain, options) {
137137
skipGlobalPaths = false
138138
}
139139

140-
let foundPath
140+
let foundPath = ""
141141

142142
if (! isPath &&
143143
(request.charCodeAt(0) === FORWARD_SLASH ||
@@ -146,13 +146,13 @@ function resolveFilename(request, parent, isMain, options) {
146146

147147
foundPath = getFilePathFromURL(parsed)
148148

149-
if (! foundPath &&
149+
if (foundPath.length === 0 &&
150150
parsed.protocol !== "file:" &&
151151
! localhostRegExp.test(request)) {
152152
throw new ERR_INVALID_PROTOCOL(parsed.protocol, "file:")
153153
}
154154

155-
if (foundPath) {
155+
if (foundPath.length > 0) {
156156
foundPath = _resolveFilename(foundPath, parent, isMain, options, emptyArray, emptyArray, true)
157157
}
158158
} else if (isPath) {
@@ -169,14 +169,14 @@ function resolveFilename(request, parent, isMain, options) {
169169

170170
foundPath = _resolveFilename(decoded, parent, isMain, options, fields, exts, skipGlobalPaths)
171171

172-
if (! foundPath &&
172+
if (foundPath.length === 0 &&
173173
Reflect.has(builtinLookup, decoded)) {
174174
cache.set(cacheKey, decoded)
175175
return decoded
176176
}
177177
}
178178

179-
if (foundPath) {
179+
if (foundPath.length > 0) {
180180
if (autoMode ||
181181
cjsPaths ||
182182
isMain ||
@@ -192,7 +192,7 @@ function resolveFilename(request, parent, isMain, options) {
192192

193193
foundPath = Module._resolveFilename(request, parent, isMain, options)
194194

195-
if (foundPath) {
195+
if (foundPath.length > 0) {
196196
if (cjsPaths) {
197197
cache.set(cacheKey, foundPath)
198198
return foundPath

src/module/esm/validate.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function resolveExportedStars(entry) {
7878
for (const request of compileData.exportedStars) {
7979
const childEntry = dependencySpecifiers[request].entry
8080

81-
if (! childEntry ||
81+
if (childEntry === null ||
8282
childEntry.builtin ||
8383
childEntry.type !== TYPE_ESM) {
8484
continue
@@ -121,7 +121,7 @@ function validateDependencies(entry) {
121121
exportedNames:childExportedNames
122122
} = dependencySpecifiers[request]
123123

124-
if (! childEntry ||
124+
if (childEntry === null ||
125125
childEntry.builtin) {
126126
continue
127127
}
@@ -192,7 +192,7 @@ function validateExportedName(entry, exportedName, seen) {
192192
for (const request of exportedStars) {
193193
const childEntry = dependencySpecifiers[request].entry
194194

195-
if (! childEntry ||
195+
if (childEntry === null ||
196196
childEntry.type !== TYPE_ESM) {
197197
throwExportMissing = false
198198
break

src/module/internal/compile.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ function compile(caller, entry, content, filename, fallback) {
7272

7373
let { compileData } = entry
7474

75-
if (! compileData) {
75+
if (compileData === null) {
7676
compileData = Compiler.from(entry)
7777

78-
if (! compileData ||
78+
if (compileData === null ||
7979
compileData.changed) {
8080
const { cacheName } = entry
8181
const { cjs } = options

src/module/internal/find-path.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ function findPath(request, paths, isMain, fields, exts) {
6464

6565
if (isAbs) {
6666
paths = [""]
67-
} else if (! paths || ! paths.length) {
67+
} else if (! Array.isArray(paths) ||
68+
paths.length === 0) {
6869
return ""
6970
}
7071

@@ -89,7 +90,8 @@ function findPath(request, paths, isMain, fields, exts) {
8990
: resolveSymlinks
9091

9192
for (let curPath of paths) {
92-
if (curPath &&
93+
if (typeof curPath === "string" &&
94+
curPath.length > 0 &&
9395
statFast(curPath) !== 1) {
9496
continue
9597
}
@@ -102,7 +104,7 @@ function findPath(request, paths, isMain, fields, exts) {
102104

103105
curPath = realpath(curPath)
104106

105-
if (! curPath) {
107+
if (curPath.length === 0) {
106108
continue
107109
}
108110
}
@@ -124,14 +126,14 @@ function findPath(request, paths, isMain, fields, exts) {
124126
isMJS(thePath)) {
125127
stat = statSync(thePath)
126128

127-
if (stat) {
129+
if (stat !== null) {
128130
rc = Reflect.apply(isFile, stat, []) ? 0 : 1
129131
}
130132
} else {
131133
rc = statFast(thePath)
132134
}
133135

134-
let filename
136+
let filename = ""
135137

136138
if (! trailingSlash) {
137139
// If a file.
@@ -141,7 +143,7 @@ function findPath(request, paths, isMain, fields, exts) {
141143
: thePath
142144
}
143145

144-
if (! filename) {
146+
if (filename.length === 0) {
145147
if (exts === void 0) {
146148
exts = keys(Module._extensions)
147149
}
@@ -152,7 +154,7 @@ function findPath(request, paths, isMain, fields, exts) {
152154

153155
// If a directory.
154156
if (rc === 1 &&
155-
! filename) {
157+
filename.length === 0) {
156158
if (exts === void 0) {
157159
exts = keys(Module._extensions)
158160
}
@@ -170,7 +172,7 @@ function findPath(request, paths, isMain, fields, exts) {
170172
tryExtensions(resolve(thePath, "index"), exts, isMain)
171173
}
172174

173-
if (filename) {
175+
if (filename.length > 0) {
174176
cache.set(cacheKey, filename)
175177
return filename
176178
}
@@ -212,7 +214,7 @@ function tryFilename(filename, isMain) {
212214
isMJS(filename)) {
213215
stat = statSync(filename)
214216

215-
if (stat) {
217+
if (stat !== null) {
216218
rc = Reflect.apply(isFile, stat, []) ? 0 : 1
217219
}
218220
} else {
@@ -228,7 +230,7 @@ function tryFilename(filename, isMain) {
228230
: resolveSymlinks
229231

230232
if (useRealpath &&
231-
(! stat ||
233+
(stat === null ||
232234
stat.nlink > 1 ||
233235
isSymLink)) {
234236
return realpath(filename)
@@ -238,10 +240,10 @@ function tryFilename(filename, isMain) {
238240
}
239241

240242
function tryPackage(dirPath, fields, exts, isMain) {
241-
const json = readPackage(dirPath, fields)
243+
const json = readPackage(dirPath, fields) || ""
242244

243-
if (! json) {
244-
return ""
245+
if (json.length === 0) {
246+
return json
245247
}
246248

247249
for (const field of fields) {

src/module/internal/read-package.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ function init() {
2727
}
2828

2929
const jsonPath = dirPath + sep + "package.json"
30-
const jsonString = readFileFast(jsonPath, "utf8")
30+
const jsonString = readFileFast(jsonPath, "utf8") || ""
3131

32-
if (! jsonString ||
32+
if (jsonString.length === 0 ||
3333
(fieldsLength === 1 &&
3434
fields[0] === "main" &&
3535
! mainFieldRegExp.test(jsonString))) {

0 commit comments

Comments
 (0)