-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add prefer-repository-shorthand rule
- Loading branch information
Showing
8 changed files
with
251 additions
and
10 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
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 |
---|---|---|
|
@@ -25,7 +25,9 @@ | |
"quickstart", | ||
"ruleset", | ||
"tsup", | ||
"vuejs", | ||
"wontfix", | ||
"yudai", | ||
"zetlan", | ||
"zetlen" | ||
] | ||
|
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,28 @@ | ||
# Enforce shorthand declaration for GitHub repository (`package-json/prefer-repository-shorthand`) | ||
|
||
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
## Rule Details | ||
|
||
This rule validates url for `repository` in a `package.json` and recommends a shortened version for GitHub repositories. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```json | ||
{ | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/vuejs/core" | ||
} | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```json | ||
{ | ||
"repository": "vuejs/core" | ||
} | ||
``` |
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
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,103 @@ | ||
import type ESTree from "estree"; | ||
import type { JSONLiteral } from "jsonc-eslint-parser/lib/parser/ast.js"; | ||
|
||
import { createRule } from "../createRule.js"; | ||
|
||
const isGitHubUrl = (url: string) => | ||
/^(?:git\+)?(?:ssh:\/\/git@|http?s:\/\/)?(?:www\.)?github\.com\/.*\/.*/.test( | ||
url, | ||
); | ||
|
||
const cleanGitHubUrl = (url: string) => | ||
url | ||
.replace( | ||
/^(?:git\+)?(?:ssh:\/\/git@|http?s:\/\/)?(?:www\.)?github\.com\//, | ||
"", | ||
) | ||
.replace(/\.git$/, ""); | ||
|
||
export default createRule({ | ||
create(context) { | ||
return { | ||
JSONProperty: (node) => { | ||
const message = | ||
"Use shorthand repository URL for GitHub repository"; | ||
if ( | ||
node.key.type === "JSONLiteral" && | ||
node.key.value === "repository" | ||
) { | ||
if (node.value.type === "JSONObjectExpression") { | ||
const { properties } = node.value; | ||
const typeProperty = properties.find( | ||
(property) => | ||
property.key.type === "JSONLiteral" && | ||
property.key.value === "type", | ||
); | ||
const urlProperty = properties.find( | ||
(property) => | ||
property.key.type === "JSONLiteral" && | ||
property.key.value === "url", | ||
); | ||
const directoryProperty = properties.find( | ||
(property) => | ||
property.key.type === "JSONLiteral" && | ||
property.key.value === "directory", | ||
); | ||
if ( | ||
!directoryProperty && | ||
typeProperty && | ||
typeProperty.value.type === "JSONLiteral" && | ||
typeProperty.value.value === "git" && | ||
urlProperty && | ||
urlProperty.value.type === "JSONLiteral" && | ||
typeof urlProperty.value.value === "string" && | ||
isGitHubUrl(urlProperty.value.value) | ||
) { | ||
context.report({ | ||
fix(fixer) { | ||
return fixer.replaceText( | ||
node.value as unknown as ESTree.Node, | ||
JSON.stringify( | ||
cleanGitHubUrl( | ||
( | ||
urlProperty.value as JSONLiteral | ||
).value as string, | ||
), | ||
), | ||
); | ||
}, | ||
message, | ||
node: node.value as unknown as ESTree.Node, | ||
}); | ||
} | ||
} | ||
|
||
if (node.value.type === "JSONLiteral") { | ||
const { value } = node.value; | ||
if (typeof value === "string" && isGitHubUrl(value)) { | ||
context.report({ | ||
fix(fixer) { | ||
return fixer.replaceText( | ||
node.value as unknown as ESTree.Node, | ||
JSON.stringify(cleanGitHubUrl(value)), | ||
); | ||
}, | ||
message, | ||
node: node.value as unknown as ESTree.Node, | ||
}); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
|
||
meta: { | ||
docs: { | ||
category: "Best Practices", | ||
description: "Enforce shorthand declaration for GitHub repository.", | ||
recommended: true, | ||
}, | ||
fixable: "code", | ||
}, | ||
}); |
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,107 @@ | ||
import rule from "../../rules/prefer-repository-shorthand.js"; | ||
import { ruleTester } from "./ruleTester.js"; | ||
|
||
ruleTester.run("prefer-repository-shorthand", rule, { | ||
invalid: [ | ||
{ | ||
code: `{ | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/JoshuaKGoldberg/create-typescript-app" | ||
} | ||
}`, | ||
errors: [ | ||
{ | ||
message: | ||
"Use shorthand repository URL for GitHub repository", | ||
type: "JSONObjectExpression", | ||
}, | ||
], | ||
filename: "package.json", | ||
output: `{ | ||
"repository": "JoshuaKGoldberg/create-typescript-app" | ||
}`, | ||
}, | ||
{ | ||
code: `{ | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/vuejs/core.git" | ||
} | ||
}`, | ||
errors: [ | ||
{ | ||
message: | ||
"Use shorthand repository URL for GitHub repository", | ||
type: "JSONObjectExpression", | ||
}, | ||
], | ||
filename: "package.json", | ||
output: `{ | ||
"repository": "vuejs/core" | ||
}`, | ||
}, | ||
{ | ||
code: `{ | ||
"repository": { | ||
"type": "git", | ||
"url": "git+ssh://git@github.com/yudai-nkt/eslint-plugin-uvu.git" | ||
} | ||
}`, | ||
errors: [ | ||
{ | ||
message: | ||
"Use shorthand repository URL for GitHub repository", | ||
type: "JSONObjectExpression", | ||
}, | ||
], | ||
filename: "package.json", | ||
output: `{ | ||
"repository": "yudai-nkt/eslint-plugin-uvu" | ||
}`, | ||
}, | ||
{ | ||
code: `{ | ||
"repository": "https://github.com/eslint/eslint" | ||
}`, | ||
errors: [ | ||
{ | ||
message: | ||
"Use shorthand repository URL for GitHub repository", | ||
type: "JSONLiteral", | ||
}, | ||
], | ||
filename: "package.json", | ||
output: `{ | ||
"repository": "eslint/eslint" | ||
}`, | ||
}, | ||
], | ||
valid: [ | ||
{ | ||
code: `{ | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/facebook/react.git", | ||
"directory": "packages/react" | ||
} | ||
}`, | ||
filename: "package.json", | ||
}, | ||
{ | ||
code: `{ | ||
"repository": "browserslist/browserslist" | ||
}`, | ||
filename: "package.json", | ||
}, | ||
{ | ||
code: `{ | ||
"repository": { | ||
"type": "git", | ||
"url": "https://gitlab.com/gitlab/gitlab" | ||
} | ||
}`, | ||
filename: "package.json", | ||
}, | ||
], | ||
}); |