-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
850cc2f
commit 5a0d446
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import type { AST as JsonAST } from "jsonc-eslint-parser"; | ||
|
||
import { createRule } from "../createRule"; | ||
import { isString } from "../utils/predicates"; | ||
|
||
export const rule = createRule({ | ||
create(context) { | ||
const objectFields = [ | ||
"peerDependencies", | ||
"scripts", | ||
"dependencies", | ||
"devDependencies", | ||
]; | ||
const arrayFields = ["files"]; | ||
|
||
return { | ||
"Program > JSONExpressionStatement > JSONObjectExpression"( | ||
node: JsonAST.JSONObjectExpression, | ||
) { | ||
function getRange( | ||
properties: JsonAST.JSONProperty[], | ||
property: JsonAST.JSONProperty, | ||
index: number, | ||
): [number, number] { | ||
const isLastProperty = properties.length - 1 === index; | ||
// if the property is last, we should remove ',' before this property | ||
const start = isLastProperty | ||
? properties.slice(-2)[0].range[1] | ||
: property.range[0]; | ||
// if the property isn't last, we should remove ',' after this property | ||
const end = property.range[1] + (isLastProperty ? 0 : 1); | ||
return [start, end]; | ||
} | ||
|
||
node.properties.forEach((property, index) => { | ||
if ( | ||
property.key.type === "JSONLiteral" && | ||
isString(property.key.value) && | ||
objectFields.includes(property.key.value) && | ||
property.value.type === "JSONObjectExpression" && | ||
!property.value.properties.length | ||
) { | ||
context.report({ | ||
data: { | ||
property: property.key.value, | ||
}, | ||
fix(fixer) { | ||
return fixer.removeRange( | ||
getRange(node.properties, property, index), | ||
); | ||
}, | ||
loc: property.loc, | ||
messageId: "emptyFields", | ||
}); | ||
} else if ( | ||
property.key.type === "JSONLiteral" && | ||
isString(property.key.value) && | ||
arrayFields.includes(property.key.value) && | ||
property.value.type === "JSONArrayExpression" && | ||
!property.value.elements.length | ||
) { | ||
context.report({ | ||
data: { | ||
property: property.key.value, | ||
}, | ||
fix(fixer) { | ||
return fixer.removeRange( | ||
getRange(node.properties, property, index), | ||
); | ||
}, | ||
loc: property.loc, | ||
messageId: "emptyFields", | ||
}); | ||
} | ||
}); | ||
}, | ||
}; | ||
}, | ||
meta: { | ||
docs: { | ||
category: "Best Practices", | ||
description: "Remove empty fields", | ||
recommended: true, | ||
}, | ||
hasSuggestions: true, | ||
messages: { | ||
emptyFields: 'Should remove empty "{{property}}"', | ||
}, | ||
fixable: "whitespace", | ||
schema: [], | ||
type: "suggestion", | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters