Skip to content

Commit

Permalink
tools,benchmark,lib,test: enable no-case-declarations lint rule
Browse files Browse the repository at this point in the history
PR-URL: #41385
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Tierney Cyren <hello@bnb.im>
Reviewed-By: Ricky Zhou <0x19951125@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
Trott authored and targos committed Jan 14, 2022
1 parent dcada80 commit 800640a
Show file tree
Hide file tree
Showing 26 changed files with 95 additions and 51 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ module.exports = {
}],
'new-parens': 'error',
'no-async-promise-executor': 'error',
'no-case-declarations': 'error',
'no-class-assign': 'error',
'no-confusing-arrow': 'error',
'no-const-assign': 'error',
Expand Down
6 changes: 4 additions & 2 deletions benchmark/zlib/inflate.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function main({ n, method, inputLen }) {
let i = 0;
switch (method) {
// Performs `n` single inflate operations
case 'inflate':
case 'inflate': {
const inflate = zlib.inflate;
bench.start();
(function next(err, result) {
Expand All @@ -25,14 +25,16 @@ function main({ n, method, inputLen }) {
inflate(chunk, next);
})();
break;
}
// Performs `n` single inflateSync operations
case 'inflateSync':
case 'inflateSync': {
const inflateSync = zlib.inflateSync;
bench.start();
for (; i < n; ++i)
inflateSync(chunk);
bench.end(n);
break;
}
default:
throw new Error('Unsupported inflate method');
}
Expand Down
3 changes: 2 additions & 1 deletion lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ Server.prototype.setTimeout = function setTimeout(msecs, callback) {

Server.prototype[EE.captureRejectionSymbol] = function(err, event, ...args) {
switch (event) {
case 'request':
case 'request': {
const { 1: res } = args;
if (!res.headersSent && !res.writableEnded) {
// Don't leak headers.
Expand All @@ -425,6 +425,7 @@ Server.prototype[EE.captureRejectionSymbol] = function(err, event, ...args) {
res.destroy();
}
break;
}
default:
net.Server.prototype[SymbolFor('nodejs.rejection')]
.apply(this, arguments);
Expand Down
24 changes: 16 additions & 8 deletions lib/internal/bootstrap/switches/is_main_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,22 @@ function createWritableStdioStream(fd) {
let stream;
// Note stream._type is used for test-module-load-list.js
switch (guessHandleType(fd)) {
case 'TTY':
case 'TTY': {
const tty = require('tty');
stream = new tty.WriteStream(fd);
stream._type = 'tty';
break;
}

case 'FILE':
case 'FILE': {
const SyncWriteStream = require('internal/fs/sync_write_stream');
stream = new SyncWriteStream(fd, { autoClose: false });
stream._type = 'fs';
break;
}

case 'PIPE':
case 'TCP':
case 'TCP': {
const net = require('net');

// If fd is already being used for the IPC channel, libuv will return
Expand All @@ -78,8 +80,9 @@ function createWritableStdioStream(fd) {

stream._type = 'pipe';
break;
}

default:
default: {
// Provide a dummy black-hole output for e.g. non-console
// Windows applications.
const { Writable } = require('stream');
Expand All @@ -88,6 +91,7 @@ function createWritableStdioStream(fd) {
cb();
}
});
}
}

// For supporting legacy API we put the FD here.
Expand Down Expand Up @@ -147,18 +151,20 @@ function getStdin() {
const fd = 0;

switch (guessHandleType(fd)) {
case 'TTY':
case 'TTY': {
const tty = require('tty');
stdin = new tty.ReadStream(fd);
break;
}

case 'FILE':
case 'FILE': {
const fs = require('fs');
stdin = new fs.ReadStream(null, { fd: fd, autoClose: false });
break;
}

case 'PIPE':
case 'TCP':
case 'TCP': {
const net = require('net');

// It could be that process has been started with an IPC channel
Expand All @@ -183,13 +189,15 @@ function getStdin() {
// Make sure the stdin can't be `.end()`-ed
stdin._writableState.ended = true;
break;
}

default:
default: {
// Provide a dummy contentless input for e.g. non-console
// Windows applications.
const { Readable } = require('stream');
stdin = new Readable({ read() {} });
stdin.push(null);
}
}

// For supporting legacy API we put the FD here.
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/crypto/aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function asyncAesGcmCipher(
const tagByteLength = MathFloor(tagLength / 8);
let tag;
switch (mode) {
case kWebCryptoCipherDecrypt:
case kWebCryptoCipherDecrypt: {
const slice = ArrayBufferIsView(data) ?
TypedArrayPrototypeSlice : ArrayBufferPrototypeSlice;
tag = slice(data, -tagByteLength);
Expand All @@ -206,6 +206,7 @@ function asyncAesGcmCipher(

data = slice(data, 0, -tagByteLength);
break;
}
case kWebCryptoCipherEncrypt:
tag = tagByteLength;
break;
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/crypto/webcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ async function importGenericSecretKey(

break;
}
case 'raw':
case 'raw': {
if (hasAnyNotIn(usagesSet, ['deriveKey', 'deriveBits'])) {
throw lazyDOMException(
`Unsupported key usage for a ${name} key`,
Expand All @@ -449,6 +449,7 @@ async function importGenericSecretKey(

const keyObject = createSecretKey(keyData);
return new InternalCryptoKey(keyObject, { name }, keyUsages, false);
}
}

throw lazyDOMException(
Expand Down
6 changes: 4 additions & 2 deletions lib/internal/error_serdes.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,23 @@ let deserialize;
function deserializeError(error) {
if (!deserialize) deserialize = require('v8').deserialize;
switch (error[0]) {
case kSerializedError:
case kSerializedError: {
const { constructor, properties } = deserialize(error.subarray(1));
const ctor = errors[constructor];
ObjectDefineProperty(properties, SymbolToStringTag, {
value: { value: 'Error', configurable: true },
enumerable: true
});
return ObjectCreate(ctor.prototype, properties);
}
case kSerializedObject:
return deserialize(error.subarray(1));
case kInspectedError:
case kInspectedError: {
const buf = Buffer.from(error.buffer,
error.byteOffset + 1,
error.byteLength - 1);
return buf.toString('utf8');
}
}
require('assert').fail('This should not happen');
}
Expand Down
15 changes: 10 additions & 5 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,28 +217,31 @@ const proxySocketHandler = {
case 'writable':
case 'destroyed':
return stream[prop];
case 'readable':
case 'readable': {
if (stream.destroyed)
return false;
const request = stream[kRequest];
return request ? request.readable : stream.readable;
case 'setTimeout':
}
case 'setTimeout': {
const session = stream.session;
if (session !== undefined)
return FunctionPrototypeBind(session.setTimeout, session);
return FunctionPrototypeBind(stream.setTimeout, stream);
}
case 'write':
case 'read':
case 'pause':
case 'resume':
throw new ERR_HTTP2_NO_SOCKET_MANIPULATION();
default:
default: {
const ref = stream.session !== undefined ?
stream.session[kSocket] : stream;
const value = ref[prop];
return typeof value === 'function' ?
FunctionPrototypeBind(value, ref) :
value;
}
}
},
getPrototypeOf(stream) {
Expand All @@ -258,23 +261,25 @@ const proxySocketHandler = {
case 'destroy':
stream[prop] = value;
return true;
case 'setTimeout':
case 'setTimeout': {
const session = stream.session;
if (session !== undefined)
session.setTimeout = value;
else
stream.setTimeout = value;
return true;
}
case 'write':
case 'read':
case 'pause':
case 'resume':
throw new ERR_HTTP2_NO_SOCKET_MANIPULATION();
default:
default: {
const ref = stream.session !== undefined ?
stream.session[kSocket] : stream;
ref[prop] = value;
return true;
}
}
}
};
Expand Down
15 changes: 10 additions & 5 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -867,14 +867,15 @@ const proxySocketHandler = {
case 'setKeepAlive':
case 'setNoDelay':
throw new ERR_HTTP2_NO_SOCKET_MANIPULATION();
default:
default: {
const socket = session[kSocket];
if (socket === undefined)
throw new ERR_HTTP2_SOCKET_UNBOUND();
const value = socket[prop];
return typeof value === 'function' ?
FunctionPrototypeBind(value, socket) :
value;
}
}
},
getPrototypeOf(session) {
Expand All @@ -901,12 +902,13 @@ const proxySocketHandler = {
case 'setKeepAlive':
case 'setNoDelay':
throw new ERR_HTTP2_NO_SOCKET_MANIPULATION();
default:
default: {
const socket = session[kSocket];
if (socket === undefined)
throw new ERR_HTTP2_SOCKET_UNBOUND();
socket[prop] = value;
return true;
}
}
}
};
Expand Down Expand Up @@ -1558,10 +1560,11 @@ class Http2Session extends EventEmitter {

[EventEmitter.captureRejectionSymbol](err, event, ...args) {
switch (event) {
case 'stream':
case 'stream': {
const stream = args[0];
stream.destroy(err);
break;
}
default:
this.destroy(err);
}
Expand Down Expand Up @@ -3184,7 +3187,7 @@ Http2Server.prototype[EventEmitter.captureRejectionSymbol] = function(
err, event, ...args) {

switch (event) {
case 'stream':
case 'stream': {
// TODO(mcollina): we might want to match this with what we do on
// the compat side.
const { 0: stream } = args;
Expand All @@ -3195,7 +3198,8 @@ Http2Server.prototype[EventEmitter.captureRejectionSymbol] = function(
stream.end();
}
break;
case 'request':
}
case 'request': {
const { 1: res } = args;
if (!res.headersSent && !res.finished) {
// Don't leak headers.
Expand All @@ -3208,6 +3212,7 @@ Http2Server.prototype[EventEmitter.captureRejectionSymbol] = function(
res.destroy();
}
break;
}
default:
ArrayPrototypeUnshift(args, err, event);
ReflectApply(net.Server.prototype[EventEmitter.captureRejectionSymbol],
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/repl/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,15 @@ function isRecoverableError(e, code) {
recoverable = true;
break;

case 'Unterminated string constant':
case 'Unterminated string constant': {
const token = StringPrototypeSlice(this.input,
this.lastTokStart, this.pos);
// See https://www.ecma-international.org/ecma-262/#sec-line-terminators
if (RegExpPrototypeTest(/\\(?:\r\n?|\n|\u2028|\u2029)$/,
token)) {
recoverable = true;
}
}
}
super.raise(pos, message);
}
Expand Down
Loading

0 comments on commit 800640a

Please sign in to comment.