Skip to content

Commit

Permalink
Update to ESLint 9 (versatica#1436)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibc authored Aug 5, 2024
1 parent 5d445e3 commit e5ed2c3
Show file tree
Hide file tree
Showing 42 changed files with 855 additions and 606 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const eslintConfig = {
'no-undef': 2,
'no-unmodified-loop-condition': 2,
'no-unreachable': 2,
'no-unused-vars': [1, { vars: 'all', args: 'after-used' }],
'no-unused-vars': [2, { vars: 'all', args: 'after-used' }],
'no-use-before-define': 0,
'no-useless-call': 2,
'no-useless-computed-key': 2,
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### NEXT

- CI: Support Node 22 ([PR #1434](https://github.com/versatica/mediasoup/pull/1434)).
- Update ESLint to version 9 ([PR #1435](https://github.com/versatica/mediasoup/pull/1435)).

### 3.14.9

Expand Down Expand Up @@ -864,7 +865,7 @@ Migrate `npm-scripts.js` to `npm-scripts.mjs` (ES Module) ([PR #1093](https://gi
### 3.6.6

- Update `usrsctp` library.
- Update ESlint and TypeScript related dependencies.
- Update ESLint and TypeScript related dependencies.

### 3.6.5

Expand Down
185 changes: 185 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import eslint from '@eslint/js';
import tsEslint from 'typescript-eslint';
import jestEslint from 'eslint-plugin-jest';
import prettierRecommendedEslint from 'eslint-plugin-prettier/recommended';
import globals from 'globals';

const config = tsEslint.config(
{
languageOptions: {
sourceType: 'module',
globals: { ...globals.node },
},
linterOptions: {
noInlineConfig: false,
reportUnusedDisableDirectives: 'error',
},
},
eslint.configs.recommended,
{
rules: {
'constructor-super': 2,
curly: [2, 'all'],
// Unfortunatelly `curly` does not apply to blocks in `switch` cases so
// this is needed.
'no-restricted-syntax': [
2,
{
selector: 'SwitchCase > *.consequent[type!="BlockStatement"]',
message: 'Switch cases without blocks are disallowed',
},
],
'guard-for-in': 2,
'newline-after-var': 2,
'newline-before-return': 2,
'no-alert': 2,
'no-caller': 2,
'no-case-declarations': 2,
'no-catch-shadow': 2,
'no-class-assign': 2,
'no-console': 2,
'no-const-assign': 2,
'no-debugger': 2,
'no-dupe-args': 2,
'no-dupe-keys': 2,
'no-duplicate-case': 2,
'no-div-regex': 2,
'no-empty': [2, { allowEmptyCatch: true }],
'no-empty-pattern': 2,
'no-eval': 2,
'no-extend-native': 2,
'no-ex-assign': 2,
'no-extra-bind': 2,
'no-extra-boolean-cast': 2,
'no-extra-label': 2,
'no-fallthrough': 2,
'no-func-assign': 2,
'no-global-assign': 2,
'no-implicit-coercion': 2,
'no-implicit-globals': 2,
'no-inner-declarations': 2,
'no-invalid-regexp': 2,
'no-invalid-this': 2,
'no-irregular-whitespace': 2,
'no-lonely-if': 2,
'no-multi-str': 2,
'no-native-reassign': 2,
'no-negated-in-lhs': 2,
'no-new': 2,
'no-new-func': 2,
'no-new-wrappers': 2,
'no-obj-calls': 2,
'no-proto': 2,
'no-prototype-builtins': 0,
'no-redeclare': 2,
'no-regex-spaces': 2,
'no-restricted-imports': 2,
'no-return-assign': 2,
'no-self-assign': 2,
'no-self-compare': 2,
'no-sequences': 2,
'no-shadow': 2,
'no-shadow-restricted-names': 2,
'no-sparse-arrays': 2,
'no-this-before-super': 2,
'no-throw-literal': 2,
'no-undef': 2,
'no-unmodified-loop-condition': 2,
'no-unreachable': 2,
'no-unused-vars': [
2,
{ vars: 'all', args: 'after-used', caughtErrors: 'none' },
],
'no-use-before-define': 0,
'no-useless-call': 2,
'no-useless-computed-key': 2,
'no-useless-concat': 2,
'no-useless-rename': 2,
'no-var': 2,
'object-curly-newline': 0,
'prefer-const': 2,
'prefer-rest-params': 2,
'prefer-spread': 2,
'prefer-template': 2,
'spaced-comment': [2, 'always'],
strict: 2,
'valid-typeof': 2,
yoda: 2,
},
},
// NOTE: We need to apply this only to .ts files (and not to .mjs files).
...tsEslint.configs.recommendedTypeChecked.map(item => ({
...item,
files: ['node/src/**/*.ts'],
})),
// NOTE: We need to apply this only to .ts files (and not to .mjs files).
...tsEslint.configs.stylisticTypeChecked.map(item => ({
...item,
files: ['node/src/**/*.ts'],
})),
{
name: 'mediasoup .ts files',
files: ['node/src/**/*.ts'],
languageOptions: {
parserOptions: {
projectService: true,
project: 'tsconfig.json',
},
},
rules: {
'@typescript-eslint/consistent-generic-constructors': [
2,
'type-annotation',
],
'@typescript-eslint/dot-notation': 0,
'@typescript-eslint/no-unused-vars': [
2,
{
vars: 'all',
args: 'after-used',
caughtErrors: 'none',
ignoreRestSiblings: false,
},
],
// We want to use `type` instead of `interface`.
'@typescript-eslint/consistent-type-definitions': 0,
// Sorry, we need many `any` usage.
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/no-unsafe-member-access': 0,
'@typescript-eslint/no-unsafe-assignment': 0,
'@typescript-eslint/no-unsafe-call': 0,
'@typescript-eslint/no-unsafe-return': 0,
'@typescript-eslint/no-unsafe-argument': 0,
'@typescript-eslint/consistent-indexed-object-style': 0,
'@typescript-eslint/no-empty-function': 0,
'@typescript-eslint/restrict-template-expressions': 0,
'@typescript-eslint/no-duplicate-type-constituents': [
2,
{ ignoreUnions: true },
],
},
},
{
name: 'mediasoup .ts test files',
...jestEslint.configs['flat/recommended'],
files: ['node/src/test/**/*.ts'],
rules: {
...jestEslint.configs['flat/recommended'].rules,
'jest/no-disabled-tests': 2,
'jest/prefer-expect-assertions': 0,
'@typescript-eslint/no-unnecessary-type-assertion': 0,
},
},
prettierRecommendedEslint
);

// console.log('*** config:***\n', config);

// console.log(
// '---tsEslint.configs.strictTypeChecked: %o',
// tsEslint.configs.strictTypeChecked
// );

// process.exit(1);

export default config;
9 changes: 6 additions & 3 deletions node/src/Channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ export class Channel extends EnhancedEventEmitter {

let msgStart = 0;

// eslint-disable-next-line no-constant-condition
while (true) {
const readLen = this.#recvBuffer.length - msgStart;

Expand Down Expand Up @@ -320,7 +319,11 @@ export class Channel extends EnhancedEventEmitter {
);
}

this.#nextId < 4294967295 ? ++this.#nextId : (this.#nextId = 1);
if (this.#nextId < 4294967295) {
++this.#nextId;
} else {
this.#nextId = 1;
}

const id = this.#nextId;

Expand Down Expand Up @@ -408,7 +411,7 @@ export class Channel extends EnhancedEventEmitter {

if (!sent) {
logger.error(
`received response does not match any sent request [id:${response.id}]`
`received response does not match any sent request [id:${response.id()}]`
);

return;
Expand Down
17 changes: 7 additions & 10 deletions node/src/Consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ export class Consumer<
this.#producerPaused = producerPaused;
this.#score = score;
this.#preferredLayers = preferredLayers;
this.#appData = appData || ({} as ConsumerAppData);
this.#appData = appData ?? ({} as ConsumerAppData);

this.handleWorkerNotifications();
}
Expand Down Expand Up @@ -556,7 +556,7 @@ export class Consumer<
/**
* Get Consumer stats.
*/
async getStats(): Promise<Array<ConsumerStat | ProducerStat>> {
async getStats(): Promise<(ConsumerStat | ProducerStat)[]> {
logger.debug('getStats()');

const response = await this.#channel.request(
Expand Down Expand Up @@ -642,7 +642,7 @@ export class Consumer<
FbsConsumer.ConsumerLayers.createConsumerLayers(
builder,
spatialLayer,
temporalLayer !== undefined ? temporalLayer : null
temporalLayer ?? null
);
const requestOffset =
FbsConsumer.SetPreferredLayersRequest.createSetPreferredLayersRequest(
Expand All @@ -669,10 +669,7 @@ export class Consumer<
if (status.preferredLayers) {
preferredLayers = {
spatialLayer: status.preferredLayers.spatialLayer,
temporalLayer:
status.preferredLayers.temporalLayer !== null
? status.preferredLayers.temporalLayer
: undefined,
temporalLayer: status.preferredLayers.temporalLayer ?? undefined,
};
}
}
Expand Down Expand Up @@ -835,7 +832,7 @@ export class Consumer<

data!.body(notification);

const score: ConsumerScore = notification!.score()!.unpack();
const score: ConsumerScore = notification.score()!.unpack();

this.#score = score;

Expand All @@ -848,7 +845,7 @@ export class Consumer<
}

case Event.CONSUMER_LAYERS_CHANGE: {
const notification = new FbsConsumer.LayersChangeNotification()!;
const notification = new FbsConsumer.LayersChangeNotification();

data!.body(notification);

Expand Down Expand Up @@ -1194,6 +1191,6 @@ function parseConsumerDumpResponse(

function parseConsumerStats(
binary: FbsConsumer.GetStatsResponse
): Array<ConsumerStat | ProducerStat> {
): (ConsumerStat | ProducerStat)[] {
return utils.parseVector(binary, 'stats', parseRtpStreamStats);
}
2 changes: 1 addition & 1 deletion node/src/DataConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export class DataConsumer<
this.#paused = paused;
this.#dataProducerPaused = dataProducerPaused;
this.#subchannels = subchannels;
this.#appData = appData || ({} as DataConsumerAppData);
this.#appData = appData ?? ({} as DataConsumerAppData);

this.handleWorkerNotifications();
}
Expand Down
2 changes: 1 addition & 1 deletion node/src/DataProducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class DataProducer<
this.#data = data;
this.#channel = channel;
this.#paused = paused;
this.#appData = appData || ({} as DataProducerAppData);
this.#appData = appData ?? ({} as DataProducerAppData);

this.handleWorkerNotifications();
}
Expand Down
8 changes: 5 additions & 3 deletions node/src/DirectTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class DirectTransport<
DirectTransportObserver
> {
// DirectTransport data.
// eslint-disable-next-line no-unused-private-class-members
readonly #data: DirectTransportData;

/**
Expand Down Expand Up @@ -178,14 +179,15 @@ export class DirectTransport<
*
* @override
*/
// eslint-disable-next-line @typescript-eslint/require-await
async connect(): Promise<void> {
logger.debug('connect()');
}

/**
* @override
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/require-await
async setMaxIncomingBitrate(bitrate: number): Promise<void> {
throw new UnsupportedError(
'setMaxIncomingBitrate() not implemented in DirectTransport'
Expand All @@ -195,7 +197,7 @@ export class DirectTransport<
/**
* @override
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/require-await
async setMaxOutgoingBitrate(bitrate: number): Promise<void> {
throw new UnsupportedError(
'setMaxOutgoingBitrate() not implemented in DirectTransport'
Expand All @@ -205,7 +207,7 @@ export class DirectTransport<
/**
* @override
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/require-await
async setMinOutgoingBitrate(bitrate: number): Promise<void> {
throw new UnsupportedError(
'setMinOutgoingBitrate() not implemented in DirectTransport'
Expand Down
2 changes: 1 addition & 1 deletion node/src/PipeTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ export class PipeTransport<
type: 'pipe' as ConsumerType,
};

const consumer = new Consumer<ConsumerAppData>({
const consumer: Consumer<ConsumerAppData> = new Consumer({
internal: {
...this.internal,
consumerId,
Expand Down
6 changes: 5 additions & 1 deletion node/src/Producer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export class Producer<
this.#data = data;
this.#channel = channel;
this.#paused = paused;
this.#appData = appData || ({} as ProducerAppData);
this.#appData = appData ?? ({} as ProducerAppData);

this.handleWorkerNotifications();
}
Expand Down Expand Up @@ -627,6 +627,10 @@ export function producerTypeToFbs(type: ProducerType): FbsRtpParameters.Type {
case 'svc': {
return FbsRtpParameters.Type.SVC;
}

default: {
throw new TypeError(`invalid ProducerType: ${type}`);
}
}
}

Expand Down
Loading

0 comments on commit e5ed2c3

Please sign in to comment.