Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/small-dancers-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@naverpay/eslint-plugin": minor
---

auto fix peer-deps-in-dev-deps

PR: [auto fix peer-deps-in-dev-deps](https://github.com/NaverPayDev/code-style/pull/103)
32 changes: 28 additions & 4 deletions packages/eslint-plugin/lib/rules/peer-deps-in-dev-deps.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'path'
import {getIndentationLength} from '../utils/getIndentationLength.js'

/**
* @type {import('eslint').Rule.RuleModule}
Expand All @@ -10,7 +10,7 @@ export default {
description: 'Ensure that all peerDependencies are also declared in devDependencies.',
recommended: false,
},
fixable: null,
fixable: 'code',
schema: [],
messages: {
missingInDevDeps: "'{{packageName}}' is declared in peerDependencies but not in devDependencies.",
Expand All @@ -20,7 +20,7 @@ export default {
create(context) {
return {
Program(node) {
const filename = path.basename(context.getFilename())
const filename = context.filename.replace(context.cwd, '')
if (filename !== 'package.json') {
return
}
Expand All @@ -46,13 +46,37 @@ export default {
(property) => property.key.value === 'devDependencies',
)

for (const [depName] of Object.entries(peerDeps)) {
for (const [depName, depVersion] of Object.entries(peerDeps)) {
if (!Object.prototype.hasOwnProperty.call(devDeps, depName)) {
context.report({
node,
messageId: 'missingInDevDeps',
data: {packageName: depName},
loc: devDepsNode?.loc || peerDepsNode?.loc,
fix(fixer) {
const s = ' '.repeat(getIndentationLength(sourceCode.getText()))
const ss = s.repeat(2)
Comment on lines +57 to +58
Copy link
Contributor

Choose a reason for hiding this comment

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

ㅋㅋㅋㅋ 스페이스 군요 ㅋㅋ

const newDevDep = `"${depName}": "${depVersion}"`

if (devDepsNode) {
if (devDepsNode.value.properties.length > 0) {
const lastDevDep =
devDepsNode.value.properties[devDepsNode.value.properties.length - 1]

return fixer.insertTextAfter(lastDevDep, `,\n${ss}${newDevDep}`)
} else {
return fixer.replaceText(
devDepsNode,
`"devDependencies": {\n${ss}${newDevDep}\n${s}}`,
)
}
} else {
return fixer.insertTextBefore(
peerDepsNode,
`"devDependencies": {\n${ss}${newDevDep}\n${s}},\n${s}`,
)
}
},
})
}
}
Expand Down
13 changes: 13 additions & 0 deletions packages/eslint-plugin/lib/utils/getIndentationLength.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function getIndentationLength(jsonString) {
const lines = jsonString.split('\n')

for (const line of lines) {
const trimmedLine = line.trim()
if (trimmedLine.startsWith('"')) {
const leadingWhitespace = line.match(/^\s*/)[0]
return leadingWhitespace.length
}
}

return 2
}