• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2023 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
16namespace ts {
17//import * as ts from 'typescript';
18import FaultID = Problems.FaultID;
19
20export class LinterConfig {
21  static nodeDesc: string[] = [];
22
23  // The SyntaxKind enum defines additional elements at the end of the enum
24  // that serve as markers (FirstX/LastX). Those elements are initialized
25  // with indices of the previously defined elements. As result, the enum
26  // may return incorrect name for a certain kind index (e.g. 'FirstStatement'
27  // instead of 'VariableStatement').
28  // The following code creates a map with correct syntax kind names.
29  // It can be used when need to print name of syntax kind of certain
30  // AST node in diagnostic messages.
31  static tsSyntaxKindNames: string[] = [];
32
33  // Use static init method, as TypeScript 4.2 doesn't support static blocks.
34  static initStatic(): void {
35    // Set the feature descriptions (for the output).
36    LinterConfig.nodeDesc[FaultID.AnyType] = "\"any\" type";
37    LinterConfig.nodeDesc[FaultID.SymbolType] = "\"symbol\" type";
38    LinterConfig.nodeDesc[FaultID.ObjectLiteralNoContextType] = "Object literals with no context Class or Interface type";
39    LinterConfig.nodeDesc[FaultID.ArrayLiteralNoContextType] = "Array literals with no context Array type";
40    LinterConfig.nodeDesc[FaultID.ComputedPropertyName] = "Computed properties";
41    LinterConfig.nodeDesc[FaultID.LiteralAsPropertyName] = "String or integer literal as property name";
42    LinterConfig.nodeDesc[FaultID.TypeQuery] = "\"typeof\" operations";
43    LinterConfig.nodeDesc[FaultID.RegexLiteral] = "regex literals";
44    LinterConfig.nodeDesc[FaultID.IsOperator] = "\"is\" operations";
45    LinterConfig.nodeDesc[FaultID.DestructuringParameter] = "destructuring parameters";
46    LinterConfig.nodeDesc[FaultID.YieldExpression] = "\"yield\" operations";
47    LinterConfig.nodeDesc[FaultID.InterfaceMerging] = "merging interfaces";
48    LinterConfig.nodeDesc[FaultID.EnumMerging] = "merging enums";
49    LinterConfig.nodeDesc[FaultID.InterfaceExtendsClass] = "interfaces inherited from classes";
50    LinterConfig.nodeDesc[FaultID.IndexMember] = "index members";
51    LinterConfig.nodeDesc[FaultID.WithStatement] = "\"with\" statements";
52    LinterConfig.nodeDesc[FaultID.ThrowStatement] = "\"throw\" statements with expression of wrong type";
53    LinterConfig.nodeDesc[FaultID.IndexedAccessType] = "Indexed access type";
54    LinterConfig.nodeDesc[FaultID.UnknownType] = "\"unknown\" type";
55    LinterConfig.nodeDesc[FaultID.ForInStatement] = "\"for-In\" statements";
56    LinterConfig.nodeDesc[FaultID.InOperator] = "\"in\" operations";
57    LinterConfig.nodeDesc[FaultID.ImportFromPath] = "imports from path";
58    LinterConfig.nodeDesc[FaultID.FunctionExpression] = "function expressions";
59    LinterConfig.nodeDesc[FaultID.IntersectionType] = "intersection types and type literals";
60    LinterConfig.nodeDesc[FaultID.ObjectTypeLiteral] = "Object type literals";
61    LinterConfig.nodeDesc[FaultID.CommaOperator] = "comma operator";
62    LinterConfig.nodeDesc[FaultID.LimitedReturnTypeInference] = "Functions with limited return type inference";
63    LinterConfig.nodeDesc[FaultID.LambdaWithTypeParameters] = "Lambda function with type parameters";
64    LinterConfig.nodeDesc[FaultID.ClassExpression] = "Class expressions";
65    LinterConfig.nodeDesc[FaultID.DestructuringAssignment] = "Destructuring assignments";
66    LinterConfig.nodeDesc[FaultID.DestructuringDeclaration] = "Destructuring variable declarations";
67    LinterConfig.nodeDesc[FaultID.VarDeclaration] = "\"var\" declarations";
68    LinterConfig.nodeDesc[FaultID.CatchWithUnsupportedType] = "\"catch\" clause with unsupported exception type";
69    LinterConfig.nodeDesc[FaultID.DeleteOperator] = "\"delete\" operations";
70    LinterConfig.nodeDesc[FaultID.DeclWithDuplicateName] = "Declarations with duplicate name";
71    LinterConfig.nodeDesc[FaultID.UnaryArithmNotNumber] = "Unary arithmetics with not-numeric values";
72    LinterConfig.nodeDesc[FaultID.ConstructorType] = "Constructor type";
73    LinterConfig.nodeDesc[FaultID.ConstructorFuncs] = "Constructor function type is not supported";
74    LinterConfig.nodeDesc[FaultID.ConstructorIface] = "Construct signatures are not supported in interfaces";
75    LinterConfig.nodeDesc[FaultID.CallSignature] = "Call signatures";
76    LinterConfig.nodeDesc[FaultID.TypeAssertion] = "Type assertion expressions";
77    LinterConfig.nodeDesc[FaultID.PrivateIdentifier] = "Private identifiers (with \"#\" prefix)";
78    LinterConfig.nodeDesc[FaultID.LocalFunction] = "Local function declarations";
79    LinterConfig.nodeDesc[FaultID.ConditionalType] = "Conditional type";
80    LinterConfig.nodeDesc[FaultID.MappedType] = "Mapped type";
81    LinterConfig.nodeDesc[FaultID.NamespaceAsObject] = "Namespaces used as objects";
82    LinterConfig.nodeDesc[FaultID.ClassAsObject] = "Class used as object";
83    LinterConfig.nodeDesc[FaultID.NonDeclarationInNamespace] = "Non-declaration statements in namespaces";
84    LinterConfig.nodeDesc[FaultID.GeneratorFunction] = "Generator functions";
85    LinterConfig.nodeDesc[FaultID.FunctionContainsThis] = "Functions containing \"this\"";
86    LinterConfig.nodeDesc[FaultID.PropertyAccessByIndex] = "property access by index";
87    LinterConfig.nodeDesc[FaultID.JsxElement] = "JSX Elements";
88    LinterConfig.nodeDesc[FaultID.EnumMemberNonConstInit] = "Enum members with non-constant initializer";
89    LinterConfig.nodeDesc[FaultID.ImplementsClass] = "Class type mentioned in \"implements\" clause";
90    LinterConfig.nodeDesc[FaultID.NoUndefinedPropAccess] = "Access to undefined field";
91    LinterConfig.nodeDesc[FaultID.MultipleStaticBlocks] = "Multiple static blocks";
92    LinterConfig.nodeDesc[FaultID.ThisType] = "\"this\" type";
93    LinterConfig.nodeDesc[FaultID.IntefaceExtendDifProps] = "Extends same properties with different types";
94    LinterConfig.nodeDesc[FaultID.StructuralIdentity] = "Use of type structural identity";
95    LinterConfig.nodeDesc[FaultID.DefaultImport] = "Default import declarations";
96    LinterConfig.nodeDesc[FaultID.ExportAssignment] = "Export assignments (export = ..)";
97    LinterConfig.nodeDesc[FaultID.ImportAssignment] = "Import assignments (import = ..)";
98    LinterConfig.nodeDesc[FaultID.GenericCallNoTypeArgs] = "Generic calls without type arguments";
99    LinterConfig.nodeDesc[FaultID.ParameterProperties] = "Parameter properties in constructor";
100    LinterConfig.nodeDesc[FaultID.InstanceofUnsupported] = "Left-hand side of \"instanceof\" is wrong";
101    LinterConfig.nodeDesc[FaultID.ShorthandAmbientModuleDecl] = "Shorthand ambient module declaration";
102    LinterConfig.nodeDesc[FaultID.WildcardsInModuleName] = "Wildcards in module name";
103    LinterConfig.nodeDesc[FaultID.UMDModuleDefinition] = "UMD module definition";
104    LinterConfig.nodeDesc[FaultID.NewTarget] = "\"new.target\" meta-property";
105    LinterConfig.nodeDesc[FaultID.DefiniteAssignment] = "Definite assignment assertion";
106    LinterConfig.nodeDesc[FaultID.Prototype] = "Prototype assignment";
107    LinterConfig.nodeDesc[FaultID.GlobalThis] = "Use of globalThis";
108    LinterConfig.nodeDesc[FaultID.UtilityType] = "Standard Utility types";
109    LinterConfig.nodeDesc[FaultID.PropertyDeclOnFunction] = "Property declaration on function";
110    LinterConfig.nodeDesc[FaultID.FunctionApplyBindCall] = "Invoking methods of function objects";
111    LinterConfig.nodeDesc[FaultID.ConstAssertion] = "\"as const\" assertion";
112    LinterConfig.nodeDesc[FaultID.ImportAssertion] = "Import assertion";
113    LinterConfig.nodeDesc[FaultID.SpreadOperator] = "Spread operation";
114    LinterConfig.nodeDesc[FaultID.LimitedStdLibApi] = "Limited standard library API";
115    LinterConfig.nodeDesc[FaultID.ErrorSuppression] = "Error suppression annotation";
116    LinterConfig.nodeDesc[FaultID.StrictDiagnostic] = "Strict diagnostic";
117    LinterConfig.nodeDesc[FaultID.UnsupportedDecorators] = "Unsupported decorators";
118    LinterConfig.nodeDesc[FaultID.ImportAfterStatement] = "Import declaration after other declaration or statement";
119    LinterConfig.nodeDesc[FaultID.EsObjectType] = 'Restricted "ESObject" type';
120  }
121
122  /*
123  private static initTsSyntaxKindNames(): void {
124    const keys = Object.keys(SyntaxKind);
125    const values = Object.values(SyntaxKind);
126
127    for (let i = 0; i < values.length; i++) {
128      const val = values[i];
129      const kindNum = typeof val === "string" ? parseInt(val) : val;
130      if (kindNum && !LinterConfig.tsSyntaxKindNames[kindNum]) {
131        LinterConfig.tsSyntaxKindNames[kindNum] = keys[i];
132      }
133    }
134  }
135*/
136  // must detect terminals during parsing
137  static terminalTokens: Set<SyntaxKind> = new Set([
138    SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken, SyntaxKind.OpenParenToken,
139    SyntaxKind.CloseParenToken, SyntaxKind.OpenBracketToken, SyntaxKind.CloseBracketToken,
140    SyntaxKind.DotToken, SyntaxKind.DotDotDotToken, SyntaxKind.SemicolonToken, SyntaxKind.CommaToken,
141    SyntaxKind.QuestionDotToken, SyntaxKind.LessThanToken, SyntaxKind.LessThanSlashToken,
142    SyntaxKind.GreaterThanToken, SyntaxKind.LessThanEqualsToken, SyntaxKind.GreaterThanEqualsToken,
143    SyntaxKind.EqualsEqualsToken, SyntaxKind.ExclamationEqualsToken, SyntaxKind.EqualsEqualsEqualsToken,
144    SyntaxKind.ExclamationEqualsEqualsToken, SyntaxKind.EqualsGreaterThanToken, SyntaxKind.PlusToken,
145    SyntaxKind.MinusToken, SyntaxKind.AsteriskToken, SyntaxKind.AsteriskAsteriskToken,
146    SyntaxKind.SlashToken, SyntaxKind.PercentToken, SyntaxKind.PlusPlusToken, SyntaxKind.MinusMinusToken,
147    SyntaxKind.LessThanLessThanToken, SyntaxKind.GreaterThanGreaterThanToken,
148    SyntaxKind.GreaterThanGreaterThanGreaterThanToken, SyntaxKind.AmpersandToken, SyntaxKind.BarToken,
149    SyntaxKind.CaretToken, SyntaxKind.ExclamationToken, SyntaxKind.TildeToken,
150    SyntaxKind.AmpersandAmpersandToken, SyntaxKind.BarBarToken, SyntaxKind.QuestionQuestionToken,
151    SyntaxKind.QuestionToken, SyntaxKind.ColonToken, SyntaxKind.AtToken, SyntaxKind.BacktickToken,
152    SyntaxKind.EqualsToken, SyntaxKind.PlusEqualsToken, SyntaxKind.MinusEqualsToken,
153    SyntaxKind.AsteriskEqualsToken, SyntaxKind.AsteriskAsteriskEqualsToken, SyntaxKind.SlashEqualsToken,
154    SyntaxKind.PercentEqualsToken, SyntaxKind.LessThanLessThanEqualsToken,
155    SyntaxKind.GreaterThanGreaterThanEqualsToken, SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken,
156    SyntaxKind.AmpersandEqualsToken, SyntaxKind.BarEqualsToken, SyntaxKind.CaretEqualsToken,
157    SyntaxKind.EndOfFileToken, SyntaxKind.SingleLineCommentTrivia,
158    SyntaxKind.MultiLineCommentTrivia, SyntaxKind.NewLineTrivia, SyntaxKind.WhitespaceTrivia,
159    SyntaxKind.ShebangTrivia, /* We detect and preserve #! on the first line */ SyntaxKind.ConflictMarkerTrivia,
160  ]);
161
162  // tokens which can be reported without additional parsing
163  static incrementOnlyTokens: ESMap<SyntaxKind , FaultID> = new Map([
164    [SyntaxKind.AnyKeyword, FaultID.AnyType], [SyntaxKind.SymbolKeyword, FaultID.SymbolType],
165    [SyntaxKind.ThisType, FaultID.ThisType],
166    [SyntaxKind.TypeQuery, FaultID.TypeQuery],
167    [SyntaxKind.DeleteExpression, FaultID.DeleteOperator],
168    [SyntaxKind.RegularExpressionLiteral, FaultID.RegexLiteral],
169    [SyntaxKind.TypePredicate, FaultID.IsOperator], [SyntaxKind.YieldExpression, FaultID.YieldExpression],
170    [SyntaxKind.IndexSignature, FaultID.IndexMember], [SyntaxKind.WithStatement, FaultID.WithStatement],
171    [SyntaxKind.IndexedAccessType, FaultID.IndexedAccessType],[SyntaxKind.UnknownKeyword, FaultID.UnknownType],
172    [SyntaxKind.InKeyword, FaultID.InOperator], [SyntaxKind.CallSignature, FaultID.CallSignature],
173    [SyntaxKind.IntersectionType, FaultID.IntersectionType],
174    [SyntaxKind.TypeLiteral, FaultID.ObjectTypeLiteral], [SyntaxKind.ConstructorType, FaultID.ConstructorFuncs],
175    [SyntaxKind.PrivateIdentifier, FaultID.PrivateIdentifier],
176    [SyntaxKind.ConditionalType, FaultID.ConditionalType], [SyntaxKind.MappedType, FaultID.MappedType],
177    [SyntaxKind.JsxElement, FaultID.JsxElement], [SyntaxKind.JsxSelfClosingElement, FaultID.JsxElement],
178    [SyntaxKind.ImportEqualsDeclaration, FaultID.ImportAssignment],
179    [SyntaxKind.NamespaceExportDeclaration, FaultID.UMDModuleDefinition],
180  ]);
181}
182
183}