Skip to content

Commit 7d53c74

Browse files
committed
feat(eslint): enhance offset calculation for defineStyle formatting
1 parent 1889468 commit 7d53c74

File tree

1 file changed

+13
-3
lines changed
  • packages/eslint/src/rules/define-style

1 file changed

+13
-3
lines changed

packages/eslint/src/rules/define-style/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import prettier from '@prettier/sync'
22
import type { MessageIds, RuleOptions } from './types'
3+
import type { TSESTree } from '@typescript-eslint/utils'
34
import type { RuleModule } from '@typescript-eslint/utils/ts-eslint'
45

56
const rule: RuleModule<MessageIds, RuleOptions> = {
@@ -39,7 +40,6 @@ const rule: RuleModule<MessageIds, RuleOptions> = {
3940
node.callee.type === 'MemberExpression'
4041
? node.callee.object
4142
: node.callee
42-
const offset = callee.loc.start.column
4343
const parser =
4444
node.callee.type === 'MemberExpression' &&
4545
node.callee.property.type === 'Identifier'
@@ -61,8 +61,18 @@ const rule: RuleModule<MessageIds, RuleOptions> = {
6161
})
6262
}
6363

64-
const placeholder = ' '.repeat(offset + tabWidth)
65-
const result = `\n${placeholder}${formattedCss.slice(0, -1).replaceAll('\n', `\n${placeholder}`)}\n${' '.repeat(offset)}`
64+
const line = callee.loc.start.line
65+
function getOffset(node: TSESTree.Node) {
66+
if (node.parent?.loc.start.line === line) {
67+
return getOffset(node.parent)
68+
}
69+
return node.loc.start.column
70+
}
71+
const column = getOffset(callee)
72+
const placeholder = ' '.repeat(column + tabWidth)
73+
const result = `\n${placeholder}${formattedCss
74+
.slice(0, -1)
75+
.replaceAll('\n', `\n${placeholder}`)}\n${' '.repeat(column)}`
6676
if (result !== cssRaw) {
6777
context.report({
6878
node: arg,

0 commit comments

Comments
 (0)