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.MethodReassignment] = "Access to undefined field"; 92 LinterConfig.nodeDesc[FaultID.MultipleStaticBlocks] = "Multiple static blocks"; 93 LinterConfig.nodeDesc[FaultID.ThisType] = "\"this\" type"; 94 LinterConfig.nodeDesc[FaultID.IntefaceExtendDifProps] = "Extends same properties with different types"; 95 LinterConfig.nodeDesc[FaultID.StructuralIdentity] = "Use of type structural identity"; 96 LinterConfig.nodeDesc[FaultID.TypeOnlyImport] = "Type-only imports"; 97 LinterConfig.nodeDesc[FaultID.TypeOnlyExport] = "Type-only exports"; 98 LinterConfig.nodeDesc[FaultID.DefaultImport] = "Default import declarations"; 99 LinterConfig.nodeDesc[FaultID.ExportAssignment] = "Export assignments (export = ..)"; 100 LinterConfig.nodeDesc[FaultID.ImportAssignment] = "Import assignments (import = ..)"; 101 LinterConfig.nodeDesc[FaultID.GenericCallNoTypeArgs] = "Generic calls without type arguments"; 102 LinterConfig.nodeDesc[FaultID.ParameterProperties] = "Parameter properties in constructor"; 103 LinterConfig.nodeDesc[FaultID.InstanceofUnsupported] = "Left-hand side of \"instanceof\" is wrong"; 104 LinterConfig.nodeDesc[FaultID.ShorthandAmbientModuleDecl] = "Shorthand ambient module declaration"; 105 LinterConfig.nodeDesc[FaultID.WildcardsInModuleName] = "Wildcards in module name"; 106 LinterConfig.nodeDesc[FaultID.UMDModuleDefinition] = "UMD module definition"; 107 LinterConfig.nodeDesc[FaultID.NewTarget] = "\"new.target\" meta-property"; 108 LinterConfig.nodeDesc[FaultID.DefiniteAssignment] = "Definite assignment assertion"; 109 LinterConfig.nodeDesc[FaultID.Prototype] = "Prototype assignment"; 110 LinterConfig.nodeDesc[FaultID.GlobalThis] = "Use of globalThis"; 111 LinterConfig.nodeDesc[FaultID.UtilityType] = "Standard Utility types"; 112 LinterConfig.nodeDesc[FaultID.PropertyDeclOnFunction] = "Property declaration on function"; 113 LinterConfig.nodeDesc[FaultID.FunctionApplyBindCall] = "Invoking methods of function objects"; 114 LinterConfig.nodeDesc[FaultID.ConstAssertion] = "\"as const\" assertion"; 115 LinterConfig.nodeDesc[FaultID.ImportAssertion] = "Import assertion"; 116 LinterConfig.nodeDesc[FaultID.SpreadOperator] = "Spread operation"; 117 LinterConfig.nodeDesc[FaultID.LimitedStdLibApi] = "Limited standard library API"; 118 LinterConfig.nodeDesc[FaultID.ErrorSuppression] = "Error suppression annotation"; 119 LinterConfig.nodeDesc[FaultID.StrictDiagnostic] = "Strict diagnostic"; 120 LinterConfig.nodeDesc[FaultID.UnsupportedDecorators] = "Unsupported decorators"; 121 LinterConfig.nodeDesc[FaultID.ImportAfterStatement] = "Import declaration after other declaration or statement"; 122 LinterConfig.nodeDesc[FaultID.EsObjectType] = '"ESObject" type'; 123 LinterConfig.nodeDesc[FaultID.EsObjectAssignment] = '"ESObject" type assignment'; 124 LinterConfig.nodeDesc[FaultID.EsObjectAccess] = '"ESObject" access'; 125 126 // LinterConfig.initTsSyntaxKindNames(); 127 } 128 129 /* 130 private static initTsSyntaxKindNames(): void { 131 const keys = Object.keys(SyntaxKind); 132 const values = Object.values(SyntaxKind); 133 134 for (let i = 0; i < values.length; i++) { 135 const val = values[i]; 136 const kindNum = typeof val === "string" ? parseInt(val) : val; 137 if (kindNum && !LinterConfig.tsSyntaxKindNames[kindNum]) { 138 LinterConfig.tsSyntaxKindNames[kindNum] = keys[i]; 139 } 140 } 141 } 142*/ 143 // must detect terminals during parsing 144 static terminalTokens: Set<SyntaxKind> = new Set([ 145 SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken, SyntaxKind.OpenParenToken, 146 SyntaxKind.CloseParenToken, SyntaxKind.OpenBracketToken, SyntaxKind.CloseBracketToken, 147 SyntaxKind.DotToken, SyntaxKind.DotDotDotToken, SyntaxKind.SemicolonToken, SyntaxKind.CommaToken, 148 SyntaxKind.QuestionDotToken, SyntaxKind.LessThanToken, SyntaxKind.LessThanSlashToken, 149 SyntaxKind.GreaterThanToken, SyntaxKind.LessThanEqualsToken, SyntaxKind.GreaterThanEqualsToken, 150 SyntaxKind.EqualsEqualsToken, SyntaxKind.ExclamationEqualsToken, SyntaxKind.EqualsEqualsEqualsToken, 151 SyntaxKind.ExclamationEqualsEqualsToken, SyntaxKind.EqualsGreaterThanToken, SyntaxKind.PlusToken, 152 SyntaxKind.MinusToken, SyntaxKind.AsteriskToken, SyntaxKind.AsteriskAsteriskToken, 153 SyntaxKind.SlashToken, SyntaxKind.PercentToken, SyntaxKind.PlusPlusToken, SyntaxKind.MinusMinusToken, 154 SyntaxKind.LessThanLessThanToken, SyntaxKind.GreaterThanGreaterThanToken, 155 SyntaxKind.GreaterThanGreaterThanGreaterThanToken, SyntaxKind.AmpersandToken, SyntaxKind.BarToken, 156 SyntaxKind.CaretToken, SyntaxKind.ExclamationToken, SyntaxKind.TildeToken, 157 SyntaxKind.AmpersandAmpersandToken, SyntaxKind.BarBarToken, SyntaxKind.QuestionQuestionToken, 158 SyntaxKind.QuestionToken, SyntaxKind.ColonToken, SyntaxKind.AtToken, SyntaxKind.BacktickToken, 159 SyntaxKind.EqualsToken, SyntaxKind.PlusEqualsToken, SyntaxKind.MinusEqualsToken, 160 SyntaxKind.AsteriskEqualsToken, SyntaxKind.AsteriskAsteriskEqualsToken, SyntaxKind.SlashEqualsToken, 161 SyntaxKind.PercentEqualsToken, SyntaxKind.LessThanLessThanEqualsToken, 162 SyntaxKind.GreaterThanGreaterThanEqualsToken, SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken, 163 SyntaxKind.AmpersandEqualsToken, SyntaxKind.BarEqualsToken, SyntaxKind.CaretEqualsToken, 164 SyntaxKind.EndOfFileToken, SyntaxKind.SingleLineCommentTrivia, 165 SyntaxKind.MultiLineCommentTrivia, SyntaxKind.NewLineTrivia, SyntaxKind.WhitespaceTrivia, 166 SyntaxKind.ShebangTrivia, /* We detect and preserve #! on the first line */ SyntaxKind.ConflictMarkerTrivia, 167 ]); 168 169 // tokens which can be reported without additional parsing 170 static incrementOnlyTokens: ESMap<SyntaxKind , FaultID> = new Map([ 171 [SyntaxKind.AnyKeyword, FaultID.AnyType], [SyntaxKind.SymbolKeyword, FaultID.SymbolType], 172 [SyntaxKind.ThisType, FaultID.ThisType], 173 [SyntaxKind.ComputedPropertyName, FaultID.ComputedPropertyName], 174 [SyntaxKind.TypeQuery, FaultID.TypeQuery], 175 [SyntaxKind.DeleteExpression, FaultID.DeleteOperator], 176 [SyntaxKind.RegularExpressionLiteral, FaultID.RegexLiteral], 177 [SyntaxKind.TypePredicate, FaultID.IsOperator], [SyntaxKind.YieldExpression, FaultID.YieldExpression], 178 [SyntaxKind.IndexSignature, FaultID.IndexMember], [SyntaxKind.WithStatement, FaultID.WithStatement], 179 [SyntaxKind.IndexedAccessType, FaultID.IndexedAccessType],[SyntaxKind.UnknownKeyword, FaultID.UnknownType], 180 [SyntaxKind.InKeyword, FaultID.InOperator], [SyntaxKind.CallSignature, FaultID.CallSignature], 181 [SyntaxKind.IntersectionType, FaultID.IntersectionType], 182 [SyntaxKind.TypeLiteral, FaultID.ObjectTypeLiteral], [SyntaxKind.ConstructorType, FaultID.ConstructorFuncs], 183 // [SyntaxKind.ConstructSignature, FaultID.ConstructorType], 184 [SyntaxKind.PrivateIdentifier, FaultID.PrivateIdentifier], 185 [SyntaxKind.ConditionalType, FaultID.ConditionalType], [SyntaxKind.MappedType, FaultID.MappedType], 186 [SyntaxKind.JsxElement, FaultID.JsxElement], [SyntaxKind.JsxSelfClosingElement, FaultID.JsxElement], 187 [SyntaxKind.ImportEqualsDeclaration, FaultID.ImportAssignment], 188 [SyntaxKind.NamespaceExportDeclaration, FaultID.UMDModuleDefinition], 189 ]); 190} 191 192}