Skip to content

Commit

Permalink
fix(eslint-plugin): allow any template literal (#661)
Browse files Browse the repository at this point in the history
Co-authored-by: Anton Krylov <adkrylov@avito.ru>
  • Loading branch information
pivaszbs and Anton Krylov authored Oct 10, 2023
1 parent 7bac17f commit 5539cdd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/eslint-plugin/src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Identifier, Literal, Node, ImportDeclaration, VariableDeclarator } from 'estree'
import type { Identifier, Literal, Node, ImportDeclaration, VariableDeclarator, TemplateLiteral } from 'estree'

export function isIdentifier(node: Node): node is Identifier {
return node?.type === 'Identifier'
Expand All @@ -8,6 +8,10 @@ export function isLiteral(node: any): node is Literal {
return node && 'type' in node && node.type === 'Literal'
}

export function isTemplateLiteral(node: any): node is TemplateLiteral {
return node && 'type' in node && node.type === 'TemplateLiteral'
}

export interface ExtractConfig {
from: ImportDeclaration
packageName: string
Expand Down
22 changes: 19 additions & 3 deletions packages/eslint-plugin/src/rules/atom-rule.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { Rule } from 'eslint'
import type { CallExpression, Identifier, Literal, Node } from 'estree'
import type { CallExpression, Identifier, Literal, Node, TemplateLiteral } from 'estree'
import {
extractAssignedVariableName,
extractImportDeclaration,
isLiteral,
isTemplateLiteral,
traverseBy,
} from '../lib'

Expand Down Expand Up @@ -51,7 +52,6 @@ export const atomRule: Rule.RuleModule = {
})

const atomName = extractAssignedVariableName(atomVariable)

if (!atomName) {
return
}
Expand Down Expand Up @@ -81,7 +81,23 @@ export const atomRule: Rule.RuleModule = {
}

function validAtomVariable(node: CallExpression, correctName: string) {
return isLiteral(node.arguments[1]) && node.arguments[1].value === correctName
if (isLiteral(node.arguments[1])) {
return validateLiteral(node.arguments[1], correctName);
}

if (isTemplateLiteral(node.arguments[1])) {
return validateTemplateLiteral(node.arguments[1], correctName);
}

return false;
}

function validateLiteral(node: Literal, correctName: string) {
return node.value === correctName;
}

function validateTemplateLiteral(node: TemplateLiteral, correctName: string) {
return true;
}

function reportUnCorrectName(config: {
Expand Down
3 changes: 3 additions & 0 deletions packages/eslint-plugin/src/tests/rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const tester = new RuleTester({

tester.run('reatom/atom-rule', atomRule, {
valid: [
{
code: "import { atom } from '@reatom/framework'; const count = 'count';const countAtom = atom(0, `${count}Atom`);",
},
{
code: `
import { atom } from '@reatom/framework'
Expand Down

1 comment on commit 5539cdd

@vercel
Copy link

@vercel vercel bot commented on 5539cdd Oct 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.