From 14cdca3bb083111a5a21c802c26307bfe7c6535b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Zugmeyer?= Date: Mon, 28 Feb 2022 11:21:18 +0100 Subject: [PATCH 1/5] =?UTF-8?q?=F0=9F=9A=A8=20[RUMF-1171]=20add=20lint=20t?= =?UTF-8?q?o=20make=20sure=20we=20use=20`const=20enum`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .eslintrc.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.eslintrc.js b/.eslintrc.js index 6548f1fc69..8d04779f9a 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -59,6 +59,13 @@ module.exports = { 'no-extra-bind': 'error', 'no-new-func': 'error', 'no-new-wrappers': 'error', + 'no-restricted-syntax': [ + 'error', + { + selector: 'TSEnumDeclaration:not([const=true])', + message: 'When possible, use `const enum` as it produces less code when transpiled.', + }, + ], 'no-return-await': 'error', 'no-sequences': 'error', 'no-template-curly-in-string': 'error', From 2874ba1f4dc90f24d189057d5ebb1ebb05baefdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Zugmeyer?= Date: Mon, 28 Feb 2022 11:22:13 +0100 Subject: [PATCH 2/5] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20[RUMF-1171]=20use=20co?= =?UTF-8?q?nst=20enums=20everywhere?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/domain/internalMonitoring/internalMonitoring.ts | 2 +- packages/core/src/domain/session/sessionManager.spec.ts | 2 +- packages/core/src/domain/session/sessionStore.spec.ts | 2 +- packages/core/src/tools/error.ts | 2 +- packages/core/src/tools/utils.ts | 4 ++-- packages/logs/src/domain/logsSessionManager.ts | 2 +- packages/rum-core/src/domain/assembly.ts | 2 +- packages/rum-core/src/domain/lifeCycle.ts | 2 +- packages/rum-core/src/domain/rumSessionManager.ts | 4 ++-- packages/rum-core/src/rawRumEvent.types.ts | 6 +++--- 10 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/core/src/domain/internalMonitoring/internalMonitoring.ts b/packages/core/src/domain/internalMonitoring/internalMonitoring.ts index f058be3e78..7a05f430f7 100644 --- a/packages/core/src/domain/internalMonitoring/internalMonitoring.ts +++ b/packages/core/src/domain/internalMonitoring/internalMonitoring.ts @@ -7,7 +7,7 @@ import type { Configuration } from '../configuration' import { computeStackTrace } from '../tracekit' import { startMonitoringBatch } from './startMonitoringBatch' -enum StatusType { +const enum StatusType { info = 'info', error = 'error', } diff --git a/packages/core/src/domain/session/sessionManager.spec.ts b/packages/core/src/domain/session/sessionManager.spec.ts index 28bf90f4a9..da389dccfe 100644 --- a/packages/core/src/domain/session/sessionManager.spec.ts +++ b/packages/core/src/domain/session/sessionManager.spec.ts @@ -10,7 +10,7 @@ import { startSessionManager, stopSessionManager, VISIBILITY_CHECK_DELAY } from import { SESSION_TIME_OUT_DELAY, SESSION_EXPIRATION_DELAY } from './sessionStore' import { SESSION_COOKIE_NAME } from './sessionCookieStore' -enum FakeTrackingType { +const enum FakeTrackingType { NOT_TRACKED = 'not-tracked', TRACKED = 'tracked', } diff --git a/packages/core/src/domain/session/sessionStore.spec.ts b/packages/core/src/domain/session/sessionStore.spec.ts index 9dea7ccf5d..54f921f5dd 100644 --- a/packages/core/src/domain/session/sessionStore.spec.ts +++ b/packages/core/src/domain/session/sessionStore.spec.ts @@ -6,7 +6,7 @@ import type { SessionStore } from './sessionStore' import { startSessionStore, SESSION_EXPIRATION_DELAY, SESSION_TIME_OUT_DELAY } from './sessionStore' import { SESSION_COOKIE_NAME } from './sessionCookieStore' -enum FakeTrackingType { +const enum FakeTrackingType { TRACKED = 'tracked', NOT_TRACKED = 'not-tracked', } diff --git a/packages/core/src/tools/error.ts b/packages/core/src/tools/error.ts index a91e9f25c1..d57f3c951d 100644 --- a/packages/core/src/tools/error.ts +++ b/packages/core/src/tools/error.ts @@ -29,7 +29,7 @@ export const ErrorSource = { SOURCE: 'source', } as const -export enum ErrorHandling { +export const enum ErrorHandling { HANDLED = 'handled', UNHANDLED = 'unhandled', } diff --git a/packages/core/src/tools/utils.ts b/packages/core/src/tools/utils.ts index 32f3509a00..97cdee7dc0 100644 --- a/packages/core/src/tools/utils.ts +++ b/packages/core/src/tools/utils.ts @@ -38,7 +38,7 @@ export const enum DOM_EVENT { PAUSE = 'pause', } -export enum ResourceType { +export const enum ResourceType { DOCUMENT = 'document', XHR = 'xhr', BEACON = 'beacon', @@ -51,7 +51,7 @@ export enum ResourceType { OTHER = 'other', } -export enum RequestType { +export const enum RequestType { FETCH = ResourceType.FETCH, XHR = ResourceType.XHR, } diff --git a/packages/logs/src/domain/logsSessionManager.ts b/packages/logs/src/domain/logsSessionManager.ts index 4993295331..4a2f75a09f 100644 --- a/packages/logs/src/domain/logsSessionManager.ts +++ b/packages/logs/src/domain/logsSessionManager.ts @@ -12,7 +12,7 @@ export type LogsSession = { id?: string // session can be tracked without id } -export enum LoggerTrackingType { +export const enum LoggerTrackingType { NOT_TRACKED = '0', TRACKED = '1', } diff --git a/packages/rum-core/src/domain/assembly.ts b/packages/rum-core/src/domain/assembly.ts index 7f9d8d4273..a049c2ca1c 100644 --- a/packages/rum-core/src/domain/assembly.ts +++ b/packages/rum-core/src/domain/assembly.ts @@ -34,7 +34,7 @@ import type { RumConfiguration } from './configuration' // replaced at build time declare const __BUILD_ENV__SDK_VERSION__: string -enum SessionType { +const enum SessionType { SYNTHETICS = 'synthetics', USER = 'user', CI_TEST = 'ci_test', diff --git a/packages/rum-core/src/domain/lifeCycle.ts b/packages/rum-core/src/domain/lifeCycle.ts index 43c0e2fdab..0c9965b729 100644 --- a/packages/rum-core/src/domain/lifeCycle.ts +++ b/packages/rum-core/src/domain/lifeCycle.ts @@ -7,7 +7,7 @@ import type { RequestCompleteEvent, RequestStartEvent } from './requestCollectio import type { AutoAction, AutoActionCreatedEvent } from './rumEventsCollection/action/trackActions' import type { ViewEvent, ViewCreatedEvent, ViewEndedEvent } from './rumEventsCollection/view/trackViews' -export enum LifeCycleEventType { +export const enum LifeCycleEventType { PERFORMANCE_ENTRIES_COLLECTED, AUTO_ACTION_CREATED, AUTO_ACTION_COMPLETED, diff --git a/packages/rum-core/src/domain/rumSessionManager.ts b/packages/rum-core/src/domain/rumSessionManager.ts index fd5e395af9..582b99f239 100644 --- a/packages/rum-core/src/domain/rumSessionManager.ts +++ b/packages/rum-core/src/domain/rumSessionManager.ts @@ -16,12 +16,12 @@ export type RumSession = { hasLitePlan: boolean } -export enum RumSessionPlan { +export const enum RumSessionPlan { LITE = 1, REPLAY = 2, } -export enum RumTrackingType { +export const enum RumTrackingType { NOT_TRACKED = '0', // Note: the "tracking type" value (stored in the session cookie) does not match the "session // plan" value (sent in RUM events). This is expected, and was done to keep retrocompatibility diff --git a/packages/rum-core/src/rawRumEvent.types.ts b/packages/rum-core/src/rawRumEvent.types.ts index 568b28a9e8..9533540579 100644 --- a/packages/rum-core/src/rawRumEvent.types.ts +++ b/packages/rum-core/src/rawRumEvent.types.ts @@ -9,7 +9,7 @@ import type { } from '@datadog/browser-core' import type { RumSessionPlan } from './domain/rumSessionManager' -export enum RumEventType { +export const enum RumEventType { ACTION = 'action', ERROR = 'error', LONG_TASK = 'long_task', @@ -110,7 +110,7 @@ export interface InForegroundPeriod { duration: ServerDuration } -export enum ViewLoadingType { +export const enum ViewLoadingType { INITIAL_LOAD = 'initial_load', ROUTE_CHANGE = 'route_change', } @@ -157,7 +157,7 @@ export interface RawRumActionEvent { } } -export enum ActionType { +export const enum ActionType { CLICK = 'click', CUSTOM = 'custom', } From dbdc0a58e996764ce27e6abc2c09795b7baae3c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Zugmeyer?= Date: Mon, 28 Feb 2022 11:44:26 +0100 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=90=9B=20fix=20`disallowEnumExports`?= =?UTF-8?q?=20rule=20when=20exporting=20aliased=20enum?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- eslint-local-rules/disallowEnumExports.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/eslint-local-rules/disallowEnumExports.js b/eslint-local-rules/disallowEnumExports.js index cdc57c2c6d..327bad0c36 100644 --- a/eslint-local-rules/disallowEnumExports.js +++ b/eslint-local-rules/disallowEnumExports.js @@ -62,16 +62,27 @@ module.exports = { const moduleExports = checker.getExportsOfModule(moduleSymbol) for (const symbol of moduleExports) { - if (isEnum(symbol)) { + if (isEnum(symbol, checker)) { context.report(node, `Cannot export enum ${symbol.getName()}`) } } }, } - }, -} -function isEnum(symbol) { - // eslint-disable-next-line no-bitwise - return symbol.getFlags() & SymbolFlags.Enum + function isEnum(symbol) { + const flags = symbol.getFlags() + + // eslint-disable-next-line no-bitwise + if (flags & SymbolFlags.Enum) { + return true + } + + // eslint-disable-next-line no-bitwise + if (flags & SymbolFlags.Alias) { + return isEnum(checker.getAliasedSymbol(symbol)) + } + + return false + } + }, } From ccff3fbba27e07fc7aab58c1739fa58e9c3fcb10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Zugmeyer?= Date: Mon, 28 Feb 2022 11:54:43 +0100 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=90=9B=20use=20a=20plain=20object=20f?= =?UTF-8?q?or=20`IncrementalSource`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/rum/src/domain/record/types.ts | 48 +++++++++++++------------ 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/packages/rum/src/domain/record/types.ts b/packages/rum/src/domain/record/types.ts index 00d2dd8ef5..34217d2287 100644 --- a/packages/rum/src/domain/record/types.ts +++ b/packages/rum/src/domain/record/types.ts @@ -2,52 +2,54 @@ import type { DefaultPrivacyLevel } from '@datadog/browser-core' import type { FocusRecord, RawRecord, VisualViewportRecord } from '../../types' import type { MutationController } from './mutationObserver' -export enum IncrementalSource { - Mutation = 0, - MouseMove = 1, - MouseInteraction = 2, - Scroll = 3, - ViewportResize = 4, - Input = 5, - TouchMove = 6, - MediaInteraction = 7, - StyleSheetRule = 8, - // CanvasMutation = 9, - // Font = 10, -} +export const IncrementalSource = { + Mutation: 0, + MouseMove: 1, + MouseInteraction: 2, + Scroll: 3, + ViewportResize: 4, + Input: 5, + TouchMove: 6, + MediaInteraction: 7, + StyleSheetRule: 8, + // CanvasMutation : 9, + // Font : 10, +} as const + +export type IncrementalSource = typeof IncrementalSource[keyof typeof IncrementalSource] export type MutationData = { - source: IncrementalSource.Mutation + source: typeof IncrementalSource.Mutation } & MutationPayload export interface MousemoveData { - source: IncrementalSource.MouseMove | IncrementalSource.TouchMove + source: typeof IncrementalSource.MouseMove | typeof IncrementalSource.TouchMove positions: MousePosition[] } export type MouseInteractionData = { - source: IncrementalSource.MouseInteraction + source: typeof IncrementalSource.MouseInteraction } & MouseInteractionParam export type ScrollData = { - source: IncrementalSource.Scroll + source: typeof IncrementalSource.Scroll } & ScrollPosition export type ViewportResizeData = { - source: IncrementalSource.ViewportResize + source: typeof IncrementalSource.ViewportResize } & ViewportResizeDimention export type InputData = { - source: IncrementalSource.Input + source: typeof IncrementalSource.Input id: number } & InputState export type MediaInteractionData = { - source: IncrementalSource.MediaInteraction + source: typeof IncrementalSource.MediaInteraction } & MediaInteractionParam export type StyleSheetRuleData = { - source: IncrementalSource.StyleSheetRule + source: typeof IncrementalSource.StyleSheetRule } & StyleSheetRuleParam export type IncrementalData = @@ -158,7 +160,7 @@ export type MutationCallBack = (m: MutationPayload) => void export type MousemoveCallBack = ( p: MousePosition[], - source: IncrementalSource.MouseMove | IncrementalSource.TouchMove + source: typeof IncrementalSource.MouseMove | typeof IncrementalSource.TouchMove ) => void export interface MousePosition { @@ -248,7 +250,7 @@ export type VisualViewportResizeCallback = (data: VisualViewportRecord['data']) export type ListenerHandler = () => void export type HookResetter = () => void -export enum NodeType { +export const enum NodeType { Document, DocumentType, Element, From 714894a6eebc481e64051ad33b7cd39e32e34fa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Zugmeyer?= Date: Mon, 28 Feb 2022 16:39:11 +0100 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=90=9B=20in=20e2e=20tests,=20import?= =?UTF-8?q?=20RUM=20replay=20types=20from=20`src`=20instead=20of=20`cjs`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After the previous commit replacing the `IncrementalSource` enum, typechecking is failing because we are using types coming from both `src` (via the `@datadog/browser-rum/test/utils` import) and `cjs` (other imports). I'm not sure why it fails now. --- test/e2e/lib/types/serverEvents.ts | 2 +- test/e2e/scenario/recorder/recorder.scenario.ts | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/test/e2e/lib/types/serverEvents.ts b/test/e2e/lib/types/serverEvents.ts index 885125fc2e..03f61ecdbb 100644 --- a/test/e2e/lib/types/serverEvents.ts +++ b/test/e2e/lib/types/serverEvents.ts @@ -1,5 +1,5 @@ import type { RumActionEvent, RumErrorEvent, RumEvent, RumResourceEvent, RumViewEvent } from '@datadog/browser-rum' -import type { Segment } from '@datadog/browser-rum/cjs/types' +import type { Segment } from '@datadog/browser-rum/src/types' export interface ServerInternalMonitoringMessage { message: string diff --git a/test/e2e/scenario/recorder/recorder.scenario.ts b/test/e2e/scenario/recorder/recorder.scenario.ts index a63815bc63..20b73c913f 100644 --- a/test/e2e/scenario/recorder/recorder.scenario.ts +++ b/test/e2e/scenario/recorder/recorder.scenario.ts @@ -1,7 +1,8 @@ -import type { CreationReason, Segment } from '@datadog/browser-rum/cjs/types' -import { IncrementalSource, RecordType } from '@datadog/browser-rum/cjs/types' -import type { InputData, StyleSheetRuleData } from '@datadog/browser-rum/cjs/domain/record/types' -import { NodeType } from '@datadog/browser-rum/cjs/domain/record/types' +import type { CreationReason, Segment } from '@datadog/browser-rum/src/types' +import { IncrementalSource, RecordType } from '@datadog/browser-rum/src/types' +import type { InputData, StyleSheetRuleData } from '@datadog/browser-rum/src/domain/record/types' +import { NodeType } from '@datadog/browser-rum/src/domain/record/types' + import type { RumInitConfiguration } from '@datadog/browser-rum-core' import { DefaultPrivacyLevel } from '@datadog/browser-rum'