Skip to content

Suggest simplifications for overzealous shifts #59519

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

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39831,6 +39831,28 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
if (leftOk && rightOk) {
checkAssignmentOperator(resultType);
switch (operator) {
case SyntaxKind.LessThanLessThanToken:
case SyntaxKind.LessThanLessThanEqualsToken:
case SyntaxKind.GreaterThanGreaterThanToken:
case SyntaxKind.GreaterThanGreaterThanEqualsToken:
case SyntaxKind.GreaterThanGreaterThanGreaterThanToken:
case SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken:
const rhsEval = evaluate(right);
if (typeof rhsEval.value === "number" && Math.abs(rhsEval.value) >= 32) {
errorOrSuggestion(
isEnumMember(walkUpParenthesizedExpressions(right.parent.parent)), // elevate from suggestion to error within an enum member
errorNode || operatorToken,
Diagnostics.This_operation_can_be_simplified_This_shift_is_identical_to_0_1_2,
getTextOfNode(left),
tokenToString(operator),
rhsEval.value % 32,
);
}
break;
default:
break;
}
}
return resultType;
}
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -6420,6 +6420,10 @@
"category": "Message",
"code": 6806
},
"This operation can be simplified. This shift is identical to `{0} {1} {2}`.": {
"category": "Error",
"code": 6807
},

"one of:": {
"category": "Message",
Expand Down
12 changes: 9 additions & 3 deletions src/harness/compilerImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export class CompilationResult {
}
}

export function compileFiles(host: fakes.CompilerHost, rootFiles: string[] | undefined, compilerOptions: ts.CompilerOptions, typeScriptVersion?: string): CompilationResult {
export function compileFiles(host: fakes.CompilerHost, rootFiles: string[] | undefined, compilerOptions: ts.CompilerOptions, typeScriptVersion?: string, captureSuggestions?: boolean): CompilationResult {
if (compilerOptions.project || !rootFiles || rootFiles.length === 0) {
const project = readProject(host.parseConfigHost, compilerOptions.project, compilerOptions);
if (project) {
Expand All @@ -265,11 +265,17 @@ export function compileFiles(host: fakes.CompilerHost, rootFiles: string[] | und
// and if the test is running `skipLibCheck` - an indicator that we want the tets to run quickly - skip the before/after error comparison, too
const skipErrorComparison = ts.length(rootFiles) >= 100 || (!!compilerOptions.skipLibCheck && !!compilerOptions.declaration);
const preProgram = !skipErrorComparison ? ts.createProgram({ rootNames: rootFiles || [], options: { ...compilerOptions, configFile: compilerOptions.configFile, traceResolution: false }, host, typeScriptVersion }) : undefined;
const preErrors = preProgram && ts.getPreEmitDiagnostics(preProgram);
let preErrors = preProgram && ts.getPreEmitDiagnostics(preProgram);
if (preProgram && captureSuggestions) {
preErrors = ts.concatenate(preErrors, ts.flatMap(preProgram.getSourceFiles(), f => preProgram.getSuggestionDiagnostics(f)));
}

const program = ts.createProgram({ rootNames: rootFiles || [], options: compilerOptions, host, typeScriptVersion });
const emitResult = program.emit();
const postErrors = ts.getPreEmitDiagnostics(program);
let postErrors = ts.getPreEmitDiagnostics(program);
if (captureSuggestions) {
postErrors = ts.concatenate(postErrors, ts.flatMap(program.getSourceFiles(), f => program.getSuggestionDiagnostics(f)));
}
const longerErrors = ts.length(preErrors) > postErrors.length ? preErrors : postErrors;
const shorterErrors = longerErrors === preErrors ? postErrors : preErrors;
const errors = preErrors && (preErrors.length !== postErrors.length) ? [
Expand Down
4 changes: 3 additions & 1 deletion src/harness/harnessIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ export namespace Compiler {
baselineFile?: string;
libFiles?: string;
noTypesAndSymbols?: boolean;
captureSuggestions?: boolean;
}

// Additional options not already in ts.optionDeclarations
Expand All @@ -303,6 +304,7 @@ export namespace Compiler {
{ name: "fullEmitPaths", type: "boolean", defaultValueDescription: false },
{ name: "noCheck", type: "boolean", defaultValueDescription: false },
{ name: "reportDiagnostics", type: "boolean", defaultValueDescription: false }, // used to enable error collection in `transpile` baselines
{ name: "captureSuggestions", type: "boolean", defaultValueDescription: false }, // Adds suggestion diagnostics to error baselines
];

let optionsIndex: Map<string, ts.CommandLineOption>;
Expand Down Expand Up @@ -428,7 +430,7 @@ export namespace Compiler {

ts.assign(options, ts.convertToOptionsWithAbsolutePaths(options, path => ts.getNormalizedAbsolutePath(path, currentDirectory)));
const host = new fakes.CompilerHost(fs, options);
const result = compiler.compileFiles(host, programFileNames, options, typeScriptVersion);
const result = compiler.compileFiles(host, programFileNames, options, typeScriptVersion, harnessSettings?.captureSuggestions === "true");
result.symlinks = symlinks;
(result as CompileFilesResult).repeat = newOptions => compileFiles(inputFiles, otherFiles, { ...harnessSettings, ...newOptions }, compilerOptions, originalCurrentDirectory, symlinks);
return result as CompileFilesResult;
Expand Down
Loading