Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(eslint-plugin): cleanup prefer-nullish-coalescing rule
- Loading branch information
Showing
8 changed files
with
244 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils'; | ||
/* | ||
* Supported: | ||
* Identifier Literal MemberExpression ThisExpression | ||
* Currently not supported: | ||
* ArrayExpression ArrayPattern ArrowFunctionExpression AssignmentExpression | ||
* AssignmentPattern AwaitExpression BinaryExpression BlockStatement | ||
* BreakStatement CallExpression CatchClause ChainExpression ClassBody | ||
* ClassDeclaration ClassExpression ConditionalExpression ContinueStatement | ||
* DebuggerStatement Decorator DoWhileStatement EmptyStatement | ||
* ExportAllDeclaration ExportDefaultDeclaration ExportNamedDeclaration | ||
* ExportSpecifier ExpressionStatement ForInStatement ForOfStatement | ||
* ForStatement FunctionDeclaration FunctionExpression IfStatement | ||
* ImportAttribute ImportDeclaration ImportDefaultSpecifier ImportExpression | ||
* ImportNamespaceSpecifier ImportSpecifier JSXAttribute JSXClosingElement | ||
* JSXClosingFragment JSXElement JSXEmptyExpression JSXExpressionContainer | ||
* JSXFragment JSXIdentifier JSXMemberExpression JSXNamespacedName | ||
* JSXOpeningElement JSXOpeningFragment JSXSpreadAttribute JSXSpreadChild | ||
* JSXText LabeledStatement LogicalExpression MetaProperty MethodDefinition | ||
* NewExpression ObjectExpression ObjectPattern PrivateIdentifier Program | ||
* Property PropertyDefinition RestElement ReturnStatement SequenceExpression | ||
* SpreadElement StaticBlock Super SwitchCase SwitchStatement | ||
* TaggedTemplateExpression TemplateElement TemplateLiteral ThrowStatement | ||
* TryStatement TSAbstractKeyword TSAbstractMethodDefinition | ||
* TSAbstractPropertyDefinition TSAnyKeyword TSArrayType TSAsExpression | ||
* TSAsyncKeyword TSBigIntKeyword TSBooleanKeyword TSCallSignatureDeclaration | ||
* TSClassImplements TSConditionalType TSConstructorType | ||
* TSConstructSignatureDeclaration TSDeclareFunction TSDeclareKeyword | ||
* TSEmptyBodyFunctionExpression TSEnumDeclaration TSEnumMember | ||
* TSExportAssignment TSExportKeyword TSExternalModuleReference | ||
* TSFunctionType TSImportEqualsDeclaration TSImportType TSIndexedAccessType | ||
* TSIndexSignature TSInferType TSInterfaceBody TSInterfaceDeclaration | ||
* TSInterfaceHeritage TSIntersectionType TSIntrinsicKeyword TSLiteralType | ||
* TSMappedType TSMethodSignature TSModuleBlock TSModuleDeclaration | ||
* TSNamedTupleMember TSNamespaceExportDeclaration TSNeverKeyword | ||
* TSNonNullExpression TSNullKeyword TSNumberKeyword TSObjectKeyword | ||
* TSOptionalType TSParameterProperty TSPrivateKeyword TSPropertySignature | ||
* TSProtectedKeyword TSPublicKeyword TSQualifiedName TSReadonlyKeyword | ||
* TSRestType TSStaticKeyword TSStringKeyword TSSymbolKeyword | ||
* TSTemplateLiteralType TSThisType TSTupleType TSTypeAliasDeclaration | ||
* TSTypeAnnotation TSTypeAssertion TSTypeLiteral TSTypeOperator | ||
* TSTypeParameter TSTypeParameterDeclaration TSTypeParameterInstantiation | ||
* TSTypePredicate TSTypeQuery TSTypeReference TSUndefinedKeyword TSUnionType | ||
* TSUnknownKeyword TSVoidKeyword UnaryExpression UpdateExpression | ||
* VariableDeclaration VariableDeclarator WhileStatement WithStatement | ||
* YieldExpression | ||
*/ | ||
|
||
export function isNodeEqual(a: TSESTree.Node, b: TSESTree.Node): boolean { | ||
if (a.type !== b.type) { | ||
return false; | ||
} | ||
if ( | ||
a.type === AST_NODE_TYPES.ThisExpression && | ||
b.type === AST_NODE_TYPES.ThisExpression | ||
) { | ||
return true; | ||
} | ||
if (a.type === AST_NODE_TYPES.Literal && b.type === AST_NODE_TYPES.Literal) { | ||
return a.value === b.value; | ||
} | ||
if ( | ||
a.type === AST_NODE_TYPES.Identifier && | ||
b.type === AST_NODE_TYPES.Identifier | ||
) { | ||
return a.name === b.name; | ||
} | ||
if ( | ||
a.type === AST_NODE_TYPES.MemberExpression && | ||
b.type === AST_NODE_TYPES.MemberExpression | ||
) { | ||
return ( | ||
isNodeEqual(a.property, b.property) && isNodeEqual(a.object, b.object) | ||
); | ||
} | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils'; | ||
|
||
export function isNull(i: TSESTree.Node): boolean { | ||
return i.type === AST_NODE_TYPES.Literal && i.value === null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils'; | ||
|
||
export function isUndefined(i: TSESTree.Node): boolean { | ||
return i.type === AST_NODE_TYPES.Identifier && i.name === 'undefined'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.