Skip to content

Commit

Permalink
feat(packages/eslint-plugin-sui): Make decorator-deprecated-remark-me…
Browse files Browse the repository at this point in the history
…thod runs on classes
  • Loading branch information
oriolpuig committed Jul 24, 2024
1 parent aa22a8d commit 5b9ee47
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,72 @@
*/
'use strict'

const dedent = require('string-dedent')

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

function getElementName(node, {isAClass, isAMethod, isArrowFunction}) {
if (isAClass) {
const className = node.id?.name ?? 'UnknownClass'
return `class ${className}`
}

if (isArrowFunction) {
const methodNode = node.parent
const classNode = methodNode?.parent?.parent
const className = classNode.id?.name ?? 'UnknownClass'
const methodName = methodNode.key?.name ?? 'UnknownMethod'

return `method ${className}.${methodName}`
}

if (isAMethod) {
const classNode = node.parent?.parent
const className = classNode.id?.name ?? 'UnknownClass'
const methodName = node.key?.name ?? 'UnknownMethod'

return `method ${className}.${methodName}`
}

return 'unknown'
}

function getDecoratorsNode(node, {isAClass, isAMethod, isArrowFunction}) {
if (isAClass) {
return node.decorators
}

if (isArrowFunction) {
const methodNode = node.parent
return methodNode.decorators ?? []
}

if (isAMethod) {
return node.decorators ?? []
}

return []
}

function remarkElement(node, {isAClass, isAMethod, isArrowFunction}) {
if (isAClass) {
return node.id
}

if (isArrowFunction) {
const methodNode = node.parent
return methodNode.key
}

if (isAMethod) {
return node.key
}

return node
}

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
Expand All @@ -18,34 +80,45 @@ module.exports = {
},
fixable: 'code',
schema: [],
messages: {}
messages: {
remarkWarningMessage: dedent`
The {{methodName}} is marked as a deprecated.
`
}
},
create: function (context) {
// TODO: Check using decorator in a Class.
function highlightNode(node) {
const isAClass = node.type === 'ClassDeclaration'
const isArrowFunction = node.type === 'ArrowFunctionExpression'
const isAMethod = node.type === 'MethodDefinition'

return {
MethodDefinition(node) {
// Method
const method = node
const nodeName = getElementName(node, {isAClass, isAMethod, isArrowFunction})
const decorators = getDecoratorsNode(node, {isAClass, isAMethod, isArrowFunction})
const hasDecorators = decorators?.length > 0

// Method decorators
const methodDecorators = method.decorators
const hasDecorators = methodDecorators?.length > 0
// Get the @Deprecated() decorator from node decorators
const deprecatedDecoratorNode =
hasDecorators && decorators?.find(decorator => decorator?.expression?.callee?.name === 'Deprecated')

if (!hasDecorators) return
if (!deprecatedDecoratorNode) return
console.log(nodeName, deprecatedDecoratorNode)

// Get the @Deprecated() decorator from method
const deprecatedDecoratorNode =
hasDecorators && methodDecorators?.find(decorator => decorator?.expression?.callee?.name === 'Deprecated')
const nodeToRemark = remarkElement(node, {isAClass, isAMethod, isArrowFunction})

if (!deprecatedDecoratorNode) return
// RULE: Mark method with a warning
context.report({
node: nodeToRemark,
messageId: 'remarkWarningMessage',
data: {
methodName: nodeName
}
})
}

// RULE: Mark method with a warning
context.report({
node: method.key,
message: 'This method is marked as a deprecated.'
})
}
return {
ClassDeclaration: highlightNode,
ArrowFunctionExpression: highlightNode,
MethodDefinition: highlightNode
}
}
}
4 changes: 3 additions & 1 deletion packages/sui-lint/eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ module.exports = {
rules: {
'sui/factory-pattern': RULES.WARNING,
'sui/serialize-deserialize': RULES.WARNING,
'sui/decorators': RULES.WARNING
'sui/decorators': RULES.WARNING,
'sui/decorator-deprecated': RULES.ERROR,
'sui/decorator-deprecated-remark-method': RULES.WARNING
}
},
{
Expand Down

0 comments on commit 5b9ee47

Please sign in to comment.