Skip to content

Commit

Permalink
refactor: simplify conditional statements and extract reusable constants
Browse files Browse the repository at this point in the history
- consolidate return statements in tagged template expressions
- export reusable rule name constant
- improve readability by combining related logical checks
  • Loading branch information
wilfriedago committed Aug 29, 2024
1 parent 5d04e82 commit a2efd82
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 41 deletions.
14 changes: 7 additions & 7 deletions src/rules/indent-unindent.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { unindent } from '@antfu/utils'
import { createEslintRule } from '../utils'

export const RULE_NAME = 'indent-unindent'
export type MessageIds = 'indent-unindent'
export type Options = [{
indent?: number
tags?: string[]
}]

export default createEslintRule<Options, MessageIds>({
name: 'indent-unindent',
name: RULE_NAME,
meta: {
type: 'layout',
docs: {
Expand Down Expand Up @@ -48,12 +49,11 @@ export default createEslintRule<Options, MessageIds>({
return {
TaggedTemplateExpression(node) {
const id = node.tag
if (!id || id.type !== 'Identifier')
return
if (!tags.includes(id.name))
return
if (node.quasi.quasis.length !== 1)
return

if (!id || id.type !== 'Identifier') return
if (!tags.includes(id.name)) return
if (node.quasi.quasis.length !== 1) return

const quasi = node.quasi.quasis[0]
const value = quasi.value.raw
const lineStartIndex = context.sourceCode.getIndexFromLoc({
Expand Down
3 changes: 1 addition & 2 deletions src/rules/no-import-dist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ export default createEslintRule<Options, MessageIds>({
defaultOptions: [],
create: (context) => {
function isDist(path: string): boolean {
return Boolean((path.startsWith('.') && path.match(/\/dist(\/|$)/)))
|| path === 'dist'
return Boolean((path.startsWith('.') && path.match(/\/dist(\/|$)/))) || path === 'dist'
}

return {
Expand Down
11 changes: 3 additions & 8 deletions src/rules/no-import-node-modules-by-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,14 @@ export default createEslintRule<Options, MessageIds>({
return {
'ImportDeclaration': (node) => {
if (node.source.value.includes('/node_modules/')) {
context.report({
node,
messageId: 'noImportNodeModulesByPath',
})
context.report({ node, messageId: 'noImportNodeModulesByPath' })
}
},
'CallExpression[callee.name="require"]': (node: any) => {
const value = node.arguments[0]?.value

if (typeof value === 'string' && value.includes('/node_modules/')) {
context.report({
node,
messageId: 'noImportNodeModulesByPath',
})
context.report({ node, messageId: 'noImportNodeModulesByPath' })
}
},
}
Expand Down
33 changes: 9 additions & 24 deletions src/rules/top-level-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,18 @@ export default createEslintRule<Options, MessageIds>({
create: (context) => {
return {
VariableDeclaration(node) {
if (node.parent.type !== 'Program' && node.parent.type !== 'ExportNamedDeclaration')
return
if (node.parent.type !== 'Program' && node.parent.type !== 'ExportNamedDeclaration') return

if (node.declarations.length !== 1)
return
if (node.kind !== 'const')
return
if (node.declare)
return
if (node.declarations.length !== 1) return
if (node.kind !== 'const') return
if (node.declare) return

const declaration = node.declarations[0]

if (declaration.init?.type !== 'ArrowFunctionExpression')
return
if (declaration.id?.type !== 'Identifier')
return
if (declaration.id.typeAnnotation)
return
if (
declaration.init.body.type !== 'BlockStatement'
&& declaration.id?.loc.start.line === declaration.init?.body.loc.end.line
) {
return
}
if (declaration.init?.type !== 'ArrowFunctionExpression') return
if (declaration.id?.type !== 'Identifier') return
if (declaration.id.typeAnnotation) return
if (declaration.init.body.type !== 'BlockStatement' && declaration.id?.loc.start.line === declaration.init?.body.loc.end.line) return

const arrowFn = declaration.init
const body = declaration.init.body
Expand Down Expand Up @@ -75,10 +63,7 @@ export default createEslintRule<Options, MessageIds>({
const textAsync = arrowFn.async ? 'async ' : ''

const final = `${textAsync}function ${textName} ${textGeneric}(${textArgs})${textTypeReturn} ${textBody}`
// console.log({
// input: code.slice(node.range[0], node.range[1]),
// output: final,
// })

return fixer.replaceTextRange([node.range[0], node.range[1]], final)
},
})
Expand Down

0 comments on commit a2efd82

Please sign in to comment.