Skip to content

Add first-attribute-linebreak rule #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2021
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ These rules relate to style guidelines, and are therefore quite subjective:

| Rule ID | Description | |
|:--------|:------------|:---|
| [@ota-meshi/svelte/first-attribute-linebreak](https://ota-meshi.github.io/eslint-plugin-svelte/rules/first-attribute-linebreak.html) | enforce the location of first attribute | :wrench: |
| [@ota-meshi/svelte/html-quotes](https://ota-meshi.github.io/eslint-plugin-svelte/rules/html-quotes.html) | enforce quotes style of HTML attributes | :wrench: |
| [@ota-meshi/svelte/indent](https://ota-meshi.github.io/eslint-plugin-svelte/rules/indent.html) | enforce consistent indentation | :wrench: |
| [@ota-meshi/svelte/max-attributes-per-line](https://ota-meshi.github.io/eslint-plugin-svelte/rules/max-attributes-per-line.html) | enforce the maximum number of attributes per line | :wrench: |
Expand Down
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ These rules relate to style guidelines, and are therefore quite subjective:

| Rule ID | Description | |
|:--------|:------------|:---|
| [@ota-meshi/svelte/first-attribute-linebreak](./first-attribute-linebreak.md) | enforce the location of first attribute | :wrench: |
| [@ota-meshi/svelte/html-quotes](./html-quotes.md) | enforce quotes style of HTML attributes | :wrench: |
| [@ota-meshi/svelte/indent](./indent.md) | enforce consistent indentation | :wrench: |
| [@ota-meshi/svelte/max-attributes-per-line](./max-attributes-per-line.md) | enforce the maximum number of attributes per line | :wrench: |
Expand Down
79 changes: 79 additions & 0 deletions docs/rules/first-attribute-linebreak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
pageClass: "rule-details"
sidebarDepth: 0
title: "@ota-meshi/svelte/first-attribute-linebreak"
description: "enforce the location of first attribute"
---

# @ota-meshi/svelte/first-attribute-linebreak

> enforce the location of first attribute

- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge>
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.

## :book: Rule Details

This rule aims to enforce a consistent location for the first attribute.

<eslint-code-block fix>

<!-- prettier-ignore-start -->
<!--eslint-skip-->

```svelte
<script>
/* eslint @ota-meshi/svelte/first-attribute-linebreak: "error" */
</script>

<!-- ✓ GOOD -->
<input type="checkbox" />
<button
type="button"
on:click={click} />
<button type="button" on:click={click} />

<!-- ✗ BAD -->
<input
type="checkbox" />
<button type="button"
on:click={click} />
<button
type="button" on:click={click} />
```

<!-- prettier-ignore-end -->

</eslint-code-block>

## :wrench: Options

```json
{
"@ota-meshi/svelte/first-attribute-linebreak": [
"error",
{
"multiline": "below", // or "beside"
"singleline": "beside" // "below"
}
]
}
```

- `multiline` ... The location of the first attribute when the attributes span multiple lines. Default is `"below"`.
- `"below"` ... Requires a newline before the first attribute.
- `"beside"` ... Disallows a newline before the first attribute.
- `singleline` ... The location of the first attribute when the attributes on single line. Default is `"beside"`.
- `"below"` ... Requires a newline before the first attribute.
- `"beside"` ... Disallows a newline before the first attribute.

## :couple: Related Rules

- [@ota-meshi/svelte/max-attributes-per-line]

[@ota-meshi/svelte/max-attributes-per-line]: ./max-attributes-per-line.md

## :mag: Implementation

- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/first-attribute-linebreak.ts)
- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/first-attribute-linebreak.ts)
8 changes: 7 additions & 1 deletion docs/rules/max-attributes-per-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,14 @@ There is a configurable number of attributes that are acceptable in one-line cas
}
```

- `singleline` ... The number of maximum attributes per line when the opening tag is in a single line. Default is `1`.
- `multiline` ... The number of maximum attributes per line when the opening tag is in multiple lines. Default is `1`.
- `singleline` ... The number of maximum attributes per line when the opening tag is in a single line. Default is `1`.

## :couple: Related Rules

- [@ota-meshi/svelte/first-attribute-linebreak]

[@ota-meshi/svelte/first-attribute-linebreak]: ./first-attribute-linebreak.md

## :rocket: Version

Expand Down
84 changes: 84 additions & 0 deletions src/rules/first-attribute-linebreak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import type { AST } from "svelte-eslint-parser"
import { createRule } from "../utils"

export default createRule("first-attribute-linebreak", {
meta: {
docs: {
description: "enforce the location of first attribute",
category: "Stylistic Issues",
recommended: false,
},
fixable: "whitespace",
schema: [
{
type: "object",
properties: {
multiline: { enum: ["below", "beside"] },
singleline: { enum: ["below", "beside"] },
},
additionalProperties: false,
},
],
messages: {
expected: "Expected a linebreak before this attribute.",
unexpected: "Expected no linebreak before this attribute.",
},
type: "layout",
},
create(context) {
const multiline: "below" | "beside" =
context.options[0]?.multiline || "below"
const singleline: "below" | "beside" =
context.options[0]?.singleline || "beside"
const sourceCode = context.getSourceCode()

/**
* Report attribute
*/
function report(
firstAttribute: AST.SvelteStartTag["attributes"][number],
location: "below" | "beside",
) {
context.report({
node: firstAttribute,
messageId: location === "beside" ? "unexpected" : "expected",
fix(fixer) {
const prevToken = sourceCode.getTokenBefore(firstAttribute, {
includeComments: true,
})!
return fixer.replaceTextRange(
[prevToken.range[1], firstAttribute.range[0]],
location === "beside" ? " " : "\n",
)
},
})
}

return {
SvelteStartTag(node) {
const firstAttribute = node.attributes[0]
if (!firstAttribute) return

const lastAttribute = node.attributes[node.attributes.length - 1]

const location =
firstAttribute.loc.start.line === lastAttribute.loc.end.line
? singleline
: multiline

if (location === "beside") {
if (
node.parent.name.loc!.end.line === firstAttribute.loc.start.line
) {
return
}
} else {
if (node.parent.name.loc!.end.line < firstAttribute.loc.start.line) {
return
}
}
report(firstAttribute, location)
},
}
},
})
2 changes: 2 additions & 0 deletions src/utils/rules.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { RuleModule } from "../types"
import buttonHasType from "../rules/button-has-type"
import commentDirective from "../rules/comment-directive"
import firstAttributeLinebreak from "../rules/first-attribute-linebreak"
import htmlQuotes from "../rules/html-quotes"
import indent from "../rules/indent"
import maxAttributesPerLine from "../rules/max-attributes-per-line"
Expand All @@ -20,6 +21,7 @@ import system from "../rules/system"
export const rules = [
buttonHasType,
commentDirective,
firstAttributeLinebreak,
htmlQuotes,
indent,
maxAttributesPerLine,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"options": [{ "multiline": "below", "singleline": "below" }]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"message": "Expected a linebreak before this attribute.",
"line": 9,
"column": 9
},
{
"message": "Expected a linebreak before this attribute.",
"line": 13,
"column": 8
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script>
function click() {}
</script>

<!-- prettier-ignore -->
<input
type="checkbox">
<!-- prettier-ignore -->
<button type="button"
on:click={click} />

<!-- prettier-ignore -->
<input type="checkbox">
<!-- prettier-ignore -->
<button
on:click={
click
} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<script>
function click() {}
</script>

<!-- prettier-ignore -->
<input
type="checkbox">
<!-- prettier-ignore -->
<button
type="button"
on:click={click} />

<!-- prettier-ignore -->
<input
type="checkbox">
<!-- prettier-ignore -->
<button
on:click={
click
} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"options": [{ "multiline": "beside", "singleline": "beside" }]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"message": "Expected no linebreak before this attribute.",
"line": 7,
"column": 3
},
{
"message": "Expected no linebreak before this attribute.",
"line": 16,
"column": 3
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script>
function click() {}
</script>

<!-- prettier-ignore -->
<input
type="checkbox">
<!-- prettier-ignore -->
<button type="button"
on:click={click} />

<!-- prettier-ignore -->
<input type="checkbox">
<!-- prettier-ignore -->
<button
on:click={
click
} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script>
function click() {}
</script>

<!-- prettier-ignore -->
<input type="checkbox">
<!-- prettier-ignore -->
<button type="button"
on:click={click} />

<!-- prettier-ignore -->
<input type="checkbox">
<!-- prettier-ignore -->
<button on:click={
click
} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"message": "Expected no linebreak before this attribute.",
"line": 7,
"column": 3
},
{
"message": "Expected a linebreak before this attribute.",
"line": 9,
"column": 9
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script>
function click() {}
</script>

<!-- prettier-ignore -->
<input
type="checkbox">
<!-- prettier-ignore -->
<button type="button"
on:click={click} />

<!-- prettier-ignore -->
<input type="checkbox">
<!-- prettier-ignore -->
<button
on:click={
click
} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script>
function click() {}
</script>

<!-- prettier-ignore -->
<input type="checkbox">
<!-- prettier-ignore -->
<button
type="button"
on:click={click} />

<!-- prettier-ignore -->
<input type="checkbox">
<!-- prettier-ignore -->
<button
on:click={
click
} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"options": [{ "multiline": "below", "singleline": "below" }]
}
Loading