Skip to content

Commit 3d8981f

Browse files
sarvajeantross
authored andcommitted
Fix: Prune UserConfig data
Fix #2742 Close #2743
1 parent b486402 commit 3d8981f

File tree

3 files changed

+264
-24
lines changed

3 files changed

+264
-24
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_modules
44
fixtures
55
packages/hint-webpack-config/docs/is-valid.md
66
*.d.ts
7+
packages/hint/hint-report

packages/hint/src/lib/cli/analyze.ts

+63-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import * as isCI from 'is-ci';
66
import { default as ora } from 'ora';
77
import * as osLocale from 'os-locale';
88

9-
import { appInsights, configStore, debug as d, fs, logger, misc, network, npm } from '@hint/utils';
9+
import { appInsights, configStore, debug as d, fs, logger, misc, network, npm, ConnectorConfig, normalizeHints, HintsConfigObject, HintSeverity } from '@hint/utils';
10+
import { Problem, Severity } from '@hint/utils/dist/src/types/problems';
1011

1112
import {
1213
AnalyzerError,
@@ -17,11 +18,11 @@ import {
1718
UserConfig
1819
} from '../types';
1920
import { loadHintPackage } from '../utils/packages/load-hint-package';
20-
import { Problem, Severity } from '@hint/utils/dist/src/types/problems';
2121

2222
import { createAnalyzer, getUserConfig } from '../';
2323
import { Analyzer } from '../analyzer';
2424
import { AnalyzerErrorStatus } from '../enums/error-status';
25+
import { loadConfiguration } from '../utils';
2526

2627
const { getAsUris } = network;
2728
const { askQuestion, mergeEnvWithOptions } = misc;
@@ -77,6 +78,63 @@ or set the flag --tracking=on|off`;
7778
printFrame(message);
7879
};
7980

81+
const getHintsForTelemetry = (hints?: HintsConfigObject | (string | any)[]) => {
82+
if (!hints) {
83+
return null;
84+
}
85+
86+
const normalizedHints = normalizeHints(hints);
87+
const result = {} as HintsConfigObject;
88+
89+
for (const [hintId, severity] of Object.entries(normalizedHints)) {
90+
result[hintId] = typeof severity === 'string' ? severity : (severity as [HintSeverity, any])[0];
91+
}
92+
93+
return result;
94+
};
95+
96+
const getHintsFromConfiguration = (configName: string, parentConfigs: string[] = []) => {
97+
try {
98+
const configuration = loadConfiguration(configName, parentConfigs);
99+
100+
return {
101+
...getHintsFromConfigurations(configuration.extends, [configName, ...parentConfigs]), // eslint-disable-line no-use-before-define,@typescript-eslint/no-use-before-define
102+
...getHintsForTelemetry(configuration.hints)
103+
};
104+
} catch (e) { // If the configuration doesn't exists, ignore it and returns an empty object.
105+
return {};
106+
}
107+
};
108+
109+
const getHintsFromConfigurations = (configurations?: string[], parentConfigs: string[] = []): any => {
110+
if (!configurations || configurations.length === 0) {
111+
return {};
112+
}
113+
114+
const config = configurations[0];
115+
116+
return {
117+
...getHintsFromConfiguration(config, parentConfigs),
118+
...getHintsFromConfigurations(configurations.slice(1), parentConfigs)
119+
};
120+
};
121+
122+
const pruneUserConfig = (userConfig: UserConfig) => {
123+
return {
124+
browserslist: userConfig.browserslist,
125+
connector: userConfig.connector ? (userConfig.connector as ConnectorConfig).name || userConfig.connector : undefined,
126+
extends: userConfig.extends,
127+
formatters: userConfig.formatters,
128+
hints: {
129+
...getHintsFromConfigurations(userConfig.extends),
130+
...getHintsForTelemetry(userConfig.hints)
131+
},
132+
hintsTimeout: userConfig.hintsTimeout,
133+
language: userConfig.language,
134+
parsers: userConfig.parsers
135+
};
136+
};
137+
80138
/** Ask user if he wants to activate the telemetry or not. */
81139
const askForTelemetryConfirmation = async (userConfig: UserConfig) => {
82140
if (appInsights.isConfigured()) {
@@ -111,7 +169,7 @@ const askForTelemetryConfirmation = async (userConfig: UserConfig) => {
111169
appInsights.enable();
112170

113171
appInsights.trackEvent('SecondRun');
114-
appInsights.trackEvent('analyze', userConfig);
172+
appInsights.trackEvent('analyze', pruneUserConfig(userConfig));
115173

116174
return;
117175
}
@@ -332,7 +390,7 @@ export default async (actions: CLIOptions): Promise<boolean> => {
332390
return false;
333391
}
334392

335-
appInsights.trackEvent('analyze', userConfig!);
393+
appInsights.trackEvent('analyze', pruneUserConfig(userConfig));
336394

337395
const start = Date.now();
338396
let exitCode = 0;
@@ -369,6 +427,7 @@ export default async (actions: CLIOptions): Promise<boolean> => {
369427
updateCallback: undefined
370428
};
371429

430+
/* istanbul ignore else */
372431
if (!actions.debug) {
373432
analyzerOptions.updateCallback = (update) => {
374433
spinner.text = update.message;

0 commit comments

Comments
 (0)