From d853828f50a65a46b45db0ddef4cbd6de9261afe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20B=C5=82a=C5=BCejewicz=20=28Peter=20Blazejewicz=29?= Date: Tue, 31 Mar 2020 00:14:25 +0200 Subject: [PATCH] feat(karma): update constants export details (#42992) * feat(karma): update constants export details - move Constatns to separate file - export as named re-export from main module - add missing type for 'PORT' - should be `number` OR `string` depending on the source of this contant. If read from ENV it will be a string always. - add missing documentation - move LOG types to use string literal types to use in the values in typechecks and intelllisense - tests updated https://github.com/karma-runner/karma/blob/master/lib/constants.js Thanks! * Resolve pull request comments: - constants import changed - re-export redefined - contants module reshape to comply with native module details. Using namespace import and named import for constant required TSLint config update to allow named imports: see: palantir/tslint#4524 /cc @43081j Thanks! * Refine import details as per PR comment /cc @43081j --- types/karma/index.d.ts | 31 +++-------------------------- types/karma/karma-tests.ts | 19 ++++++++++++++++++ types/karma/lib/constants.d.ts | 36 ++++++++++++++++++++++++++++++++++ types/karma/tslint.json | 12 +++++++++++- 4 files changed, 69 insertions(+), 29 deletions(-) create mode 100644 types/karma/lib/constants.d.ts diff --git a/types/karma/index.d.ts b/types/karma/index.d.ts index 6eccea0881acd4..bef0e79f0a1d15 100644 --- a/types/karma/index.d.ts +++ b/types/karma/index.d.ts @@ -14,7 +14,10 @@ import Promise = require('bluebird'); import https = require('https'); import { Appender } from 'log4js'; import { EventEmitter } from 'events'; +import * as constants from './lib/constants'; +import { VERSION } from './lib/constants'; +export { constants, VERSION }; /** * `start` method is deprecated since 0.13. It will be removed in 0.14. * Please use @@ -27,37 +30,9 @@ import { EventEmitter } from 'events'; * @deprecated */ export const server: DeprecatedServer; - export const runner: Runner; export const stopper: Stopper; -export const VERSION: string; -export const constants: Constants; - -export interface Constants { - VERSION: string; - DEFAULT_PORT: number; - DEFAULT_HOSTNAME: string; - DEFAULT_LISTEN_ADDR: string; - LOG_DISABLE: string; - LOG_ERROR: string; - LOG_WARN: string; - LOG_INFO: string; - LOG_DEBUG: string; - LOG_LOG: string; - LOG_PRIORITIES: string[]; - COLOR_PATTERN: string; - NO_COLOR_PATTERN: string; - CONSOLE_APPENDER: { - type: string; - layout: { - type: string; - pattern: string; - }; - }; - EXIT_CODE: string; -} - export namespace launcher { class Launcher { static generateId(): string; diff --git a/types/karma/karma-tests.ts b/types/karma/karma-tests.ts index 498497370795cf..b0a330cb923e9f 100644 --- a/types/karma/karma-tests.ts +++ b/types/karma/karma-tests.ts @@ -196,3 +196,22 @@ karma.config.parseConfig('karma.conf.js', { singleRun: true, restartOnFileChange: true, }); + +// constants +karma.VERSION; // $ExpectType string +karma.constants.VERSION; // $ExpectType string +karma.constants.DEFAULT_PORT; // $ExpectType string | number +karma.constants.DEFAULT_HOSTNAME; // $ExpectType string +karma.constants.DEFAULT_LISTEN_ADDR; // $ExpectType string +karma.constants.LOG_DISABLE; // $ExpectType "OFF" +karma.constants.LOG_ERROR; // $ExpectType "ERROR" +karma.constants.LOG_WARN; // $ExpectType "WARN" +karma.constants.LOG_INFO; // $ExpectType "INFO" +karma.constants.LOG_DEBUG; // $ExpectType "DEBUG" +karma.constants.LOG_LOG; // $ExpectType "LOG" +karma.constants.LOG_PRIORITIES; // $ExpectType ["OFF", "ERROR", "WARN", "LOG", "INFO", "DEBUG"] +karma.constants.COLOR_PATTERN; // $ExpectType string +karma.constants.NO_COLOR_PATTERN; // $ExpectType string +karma.constants.CONSOLE_APPENDER; // $ExpectType { type: string; layout: { type: string; pattern: string; }; } +karma.constants.EXIT_CODE; // $ExpectType string +karma.constants.LOG_PRIORITIES[5] === 'DEBUG'; diff --git a/types/karma/lib/constants.d.ts b/types/karma/lib/constants.d.ts new file mode 100644 index 00000000000000..8cd14682fba6cd --- /dev/null +++ b/types/karma/lib/constants.d.ts @@ -0,0 +1,36 @@ +/** The current version of karma */ +export const VERSION: string; +/** The default port used for the karma server */ +export const DEFAULT_PORT: string | number; +/** The default hostname used for the karma server */ +export const DEFAULT_HOSTNAME: string; +/** The default listen address used for the karma server */ +export const DEFAULT_LISTEN_ADDR: string; +/** The value for disabling logs */ +export const LOG_DISABLE: 'OFF'; +/** The value for the log `error` level */ +export const LOG_ERROR: 'ERROR'; +/** The value for the log `warn` level */ +export const LOG_WARN: 'WARN'; +/** The value for the log `info` level */ +export const LOG_INFO: 'INFO'; +/** The value for the log `debug` level */ +export const LOG_DEBUG: 'DEBUG'; +export const LOG_LOG: 'LOG'; +/** An array of log levels in descending order, i.e. LOG_DISABLE, LOG_ERROR, LOG_WARN, LOG_LOG, LOG_INFO, and LOG_DEBUG */ +export const LOG_PRIORITIES: ['OFF', 'ERROR', 'WARN', 'LOG', 'INFO', 'DEBUG']; + +/** The default color pattern for log output */ +export const COLOR_PATTERN: string; +/** The default pattern for log output without color */ +export const NO_COLOR_PATTERN: string; +/** The default console appender */ +export const CONSOLE_APPENDER: { + type: string; + layout: { + type: string; + pattern: string; + }; +}; +/** The exit code */ +export const EXIT_CODE: string; diff --git a/types/karma/tslint.json b/types/karma/tslint.json index 3db14f85eaf7b9..e27ae39541c4f3 100644 --- a/types/karma/tslint.json +++ b/types/karma/tslint.json @@ -1 +1,11 @@ -{ "extends": "dtslint/dt.json" } +{ + "extends": "dtslint/dt.json", + "rules": { + "no-duplicate-imports": [ + true, + { + "allow-namespace-imports": true + } + ] + } +}