• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import * as ts from 'typescript';
17import type { CommandLineOptions } from '../lib/CommandLineOptions';
18import { formTscOptions } from './ts-compiler/FormTscOptions';
19import { logTscDiagnostic } from '../lib/utils/functions/LogTscDiagnostic';
20import type { LinterConfig } from '../lib/LinterConfig';
21import { TSCCompiledProgramWithDiagnostics } from '../lib/ts-diagnostics/TSCCompiledProgram';
22import { Logger } from '../lib/Logger';
23
24function compile(cmdOptions: CommandLineOptions, overrideCompilerOptions: ts.CompilerOptions): ts.Program {
25  const createProgramOptions = formTscOptions(cmdOptions, overrideCompilerOptions);
26  const program = ts.createProgram(createProgramOptions);
27  // Log Tsc errors if needed
28  if (cmdOptions.logTscErrors) {
29    const diagnostics = ts.getPreEmitDiagnostics(program);
30    logTscDiagnostic(diagnostics, Logger.info);
31  }
32  return program;
33}
34
35export function compileLintOptions(cmdOptions: CommandLineOptions): LinterConfig {
36  const strict = compile(cmdOptions, getOverrideCompilerOptions(true));
37  const nonStrict = compile(cmdOptions, getOverrideCompilerOptions(false));
38  return {
39    cmdOptions: cmdOptions,
40    tscCompiledProgram: new TSCCompiledProgramWithDiagnostics(strict, nonStrict, cmdOptions.inputFiles)
41  };
42}
43
44function getOverrideCompilerOptions(strict: boolean): ts.CompilerOptions {
45  return {
46    strict: false,
47    alwaysStrict: false,
48    noImplicitAny: false,
49    noImplicitThis: false,
50    strictBindCallApply: false,
51    useUnknownInCatchVariables: false,
52    strictNullChecks: strict,
53    strictFunctionTypes: strict,
54    strictPropertyInitialization: strict,
55    noImplicitReturns: strict
56  };
57}
58