-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
yml/file-extension
rule (#188)
* feat: add `yml/file-extension` rule * Create fluffy-grapes-raise.md
- Loading branch information
Showing
9 changed files
with
188 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"eslint-plugin-yml": minor | ||
--- | ||
|
||
feat: add `yml/file-extension` rule |
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
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,60 @@ | ||
--- | ||
pageClass: "rule-details" | ||
sidebarDepth: 0 | ||
title: "yml/file-extension" | ||
description: "enforce YAML file extension" | ||
--- | ||
|
||
# yml/file-extension | ||
|
||
> enforce YAML file extension | ||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> | ||
|
||
## :book: Rule Details | ||
|
||
This rule aims to enforce YAML file extension. | ||
|
||
<eslint-code-block file-name="example.yaml"> | ||
|
||
<!-- eslint-skip --> | ||
|
||
```yaml | ||
# ✓ GOOD | ||
# filename is `example.yaml` | ||
|
||
# eslint yml/file-extension: 'error' | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block file-name="example.yaml"> | ||
|
||
<!-- eslint-skip --> | ||
|
||
```yaml | ||
# ✗ BAD | ||
# filename is `example.yml` | ||
|
||
# eslint yml/file-extension: 'error' | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
```yaml | ||
yml/file-extension: | ||
- error | ||
- extension: yaml # or 'yml' | ||
caseSensitive: true | ||
``` | ||
- `extension` ... The extension you want to enforce. Default is `"yaml"`. | ||
- `caseSensitive` ... Specify `true` to enforce lowercase extension. Default is `true`. | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/ota-meshi/eslint-plugin-yml/blob/master/src/rules/file-extension.ts) | ||
- [Test source](https://github.com/ota-meshi/eslint-plugin-yml/blob/master/tests/src/rules/file-extension.ts) | ||
- [Test fixture sources](https://github.com/ota-meshi/eslint-plugin-yml/tree/master/tests/fixtures/rules/file-extension) |
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,60 @@ | ||
import path from "path"; | ||
import { createRule } from "../utils"; | ||
|
||
export default createRule("file-extension", { | ||
meta: { | ||
docs: { | ||
description: "enforce YAML file extension", | ||
categories: [], | ||
extensionRule: false, | ||
layout: false, | ||
}, | ||
schema: [ | ||
{ | ||
type: "object", | ||
properties: { | ||
extension: { | ||
enum: ["yaml", "yml"], | ||
}, | ||
caseSensitive: { | ||
type: "boolean", | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
messages: { | ||
unexpected: `Expected extension '{{expected}}' but used extension '{{actual}}'.`, | ||
}, | ||
type: "suggestion", | ||
}, | ||
create(context) { | ||
if (!context.parserServices.isYAML) { | ||
return {}; | ||
} | ||
const expected: string = context.options[0]?.extension || "yaml"; | ||
const caseSensitive: string = context.options[0]?.caseSensitive ?? true; | ||
|
||
return { | ||
Program(node) { | ||
const filename = context.getFilename(); | ||
const actual = path.extname(filename); | ||
if ( | ||
(caseSensitive ? actual : actual.toLocaleLowerCase()) === | ||
`.${expected}` | ||
) { | ||
return; | ||
} | ||
context.report({ | ||
node, | ||
loc: node.loc.start, | ||
messageId: "unexpected", | ||
data: { | ||
expected: `.${expected}`, | ||
actual, | ||
}, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
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,58 @@ | ||
import { RuleTester } from "eslint"; | ||
import rule from "../../../src/rules/file-extension"; | ||
|
||
const tester = new RuleTester({ | ||
parser: require.resolve("yaml-eslint-parser"), | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
}, | ||
}); | ||
|
||
tester.run("file-extension", rule as any, { | ||
valid: [ | ||
{ | ||
filename: "test.yaml", | ||
code: "a: b", | ||
}, | ||
{ | ||
filename: "test.yml", | ||
code: "a: b", | ||
options: [{ extension: "yml" }], | ||
}, | ||
{ | ||
filename: "test.yaml", | ||
code: "a: b", | ||
options: [{ extension: "yaml" }], | ||
}, | ||
{ | ||
filename: "test.YAML", | ||
code: "a: b", | ||
options: [{ extension: "yaml", caseSensitive: false }], | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
filename: "test.yml", | ||
code: "a: b", | ||
errors: ["Expected extension '.yaml' but used extension '.yml'."], | ||
}, | ||
{ | ||
filename: "test.yaml", | ||
code: "a: b", | ||
options: [{ extension: "yml" }], | ||
errors: ["Expected extension '.yml' but used extension '.yaml'."], | ||
}, | ||
{ | ||
filename: "test.yml", | ||
code: "a: b", | ||
options: [{ extension: "yaml" }], | ||
errors: ["Expected extension '.yaml' but used extension '.yml'."], | ||
}, | ||
{ | ||
filename: "test.YAML", | ||
code: "a: b", | ||
options: [{ extension: "yaml" }], | ||
errors: ["Expected extension '.yaml' but used extension '.YAML'."], | ||
}, | ||
], | ||
}); |
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