Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡️ [RUMF-1171] prefer const enums #1364

Merged
merged 7 commits into from
Mar 2, 2022
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
23 changes: 17 additions & 6 deletions eslint-local-rules/disallowEnumExports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/domain/session/sessionManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/domain/session/sessionStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/tools/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const ErrorSource = {
SOURCE: 'source',
} as const

export enum ErrorHandling {
export const enum ErrorHandling {
HANDLED = 'handled',
UNHANDLED = 'unhandled',
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/tools/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const enum DOM_EVENT {
PAUSE = 'pause',
}

export enum ResourceType {
export const enum ResourceType {
DOCUMENT = 'document',
XHR = 'xhr',
BEACON = 'beacon',
Expand All @@ -51,7 +51,7 @@ export enum ResourceType {
OTHER = 'other',
}

export enum RequestType {
export const enum RequestType {
FETCH = ResourceType.FETCH,
XHR = ResourceType.XHR,
}
Expand Down
2 changes: 1 addition & 1 deletion packages/logs/src/domain/logsSessionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
Expand Down
2 changes: 1 addition & 1 deletion packages/rum-core/src/domain/assembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion packages/rum-core/src/domain/lifeCycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/rum-core/src/domain/rumSessionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions packages/rum-core/src/rawRumEvent.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -105,7 +105,7 @@ export interface InForegroundPeriod {
duration: ServerDuration
}

export enum ViewLoadingType {
export const enum ViewLoadingType {
INITIAL_LOAD = 'initial_load',
ROUTE_CHANGE = 'route_change',
}
Expand Down Expand Up @@ -152,7 +152,7 @@ export interface RawRumActionEvent {
}
}

export enum ActionType {
export const enum ActionType {
CLICK = 'click',
CUSTOM = 'custom',
}
Expand Down
48 changes: 25 additions & 23 deletions packages/rum/src/domain/record/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/lib/types/serverEvents.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 5 additions & 4 deletions test/e2e/scenario/recorder/recorder.scenario.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down