Skip to content
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

feat: add astro/valid-compile rule #130

Merged
merged 4 commits into from
Oct 31, 2022
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
5 changes: 5 additions & 0 deletions .changeset/five-lions-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-astro": minor
---

feat: add `astro/valid-compile` rule
5 changes: 5 additions & 0 deletions .changeset/purple-pumpkins-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-astro": minor
---

BREAKING: add `astro/valid-compile` rule to recommended config
5 changes: 5 additions & 0 deletions .changeset/silly-berries-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-astro": minor
---

feat: update astro-eslint-parser to v0.9
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ These rules relate to possible syntax or logic errors in Astro component code:
| [astro/no-deprecated-astro-fetchcontent](https://ota-meshi.github.io/eslint-plugin-astro/rules/no-deprecated-astro-fetchcontent/) | disallow using deprecated `Astro.fetchContent()` | :star::wrench: |
| [astro/no-deprecated-astro-resolve](https://ota-meshi.github.io/eslint-plugin-astro/rules/no-deprecated-astro-resolve/) | disallow using deprecated `Astro.resolve()` | :star: |
| [astro/no-unused-define-vars-in-style](https://ota-meshi.github.io/eslint-plugin-astro/rules/no-unused-define-vars-in-style/) | disallow unused `define:vars={...}` in `style` tag | :star: |
| [astro/valid-compile](https://ota-meshi.github.io/eslint-plugin-astro/rules/valid-compile/) | disallow warnings when compiling. | :star: |

## Security Vulnerability

Expand Down
1 change: 1 addition & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ These rules relate to possible syntax or logic errors in Astro component code:
| [astro/no-deprecated-astro-fetchcontent](./rules/no-deprecated-astro-fetchcontent.md) | disallow using deprecated `Astro.fetchContent()` | :star::wrench: |
| [astro/no-deprecated-astro-resolve](./rules/no-deprecated-astro-resolve.md) | disallow using deprecated `Astro.resolve()` | :star: |
| [astro/no-unused-define-vars-in-style](./rules/no-unused-define-vars-in-style.md) | disallow unused `define:vars={...}` in `style` tag | :star: |
| [astro/valid-compile](./rules/valid-compile.md) | disallow warnings when compiling. | :star: |

## Security Vulnerability

Expand Down
48 changes: 48 additions & 0 deletions docs/rules/valid-compile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: "astro/valid-compile"
description: "disallow warnings when compiling."
---

# astro/valid-compile

> disallow warnings when compiling.

- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge>
- :gear: This rule is included in `"plugin:astro/recommended"`.

## :book: Rule Details

This rule uses `@astrojs/compiler` to check the source code.

<ESLintCodeBlock>

<!--eslint-skip-->

```astro
---
/* eslint astro/valid-compile: "error" */
---

{/* ✗ BAD */}
<p set:html="string">string</p>
```

</ESLintCodeBlock>

## :wrench: Options

Nothing.

## :books: Further Reading

## :couple: Related Rules

- [astro/no-conflict-set-directives]

[astro/no-conflict-set-directives]: ./no-conflict-set-directives.md

## :mag: Implementation

- [Rule source](https://github.com/ota-meshi/eslint-plugin-astro/blob/main/src/rules/valid-compile.ts)
- [Test source](https://github.com/ota-meshi/eslint-plugin-astro/blob/main/tests/src/rules/valid-compile.ts)
- [Test fixture sources](https://github.com/ota-meshi/eslint-plugin-astro/tree/main/tests/fixtures/rules/valid-compile)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"homepage": "https://ota-meshi.github.io/eslint-plugin-astro/",
"dependencies": {
"@typescript-eslint/types": "^5.25.0",
"astro-eslint-parser": "^0.7.0",
"astro-eslint-parser": "^0.9.0",
"eslint-utils": "^3.0.0",
"postcss": "^8.4.14",
"postcss-selector-parser": "^6.0.10",
Expand Down
1 change: 1 addition & 0 deletions src/configs/recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export = {
"astro/no-deprecated-astro-fetchcontent": "error",
"astro/no-deprecated-astro-resolve": "error",
"astro/no-unused-define-vars-in-style": "error",
"astro/valid-compile": "error",
},
}
36 changes: 36 additions & 0 deletions src/rules/valid-compile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { createRule } from "../utils"

export default createRule("valid-compile", {
meta: {
docs: {
description: "disallow warnings when compiling.",
category: "Possible Errors",
recommended: true,
},
schema: [],
messages: {},
type: "problem",
},
create(context) {
if (!context.parserServices.getAstroResult) {
return {}
}
const diagnostics = context.parserServices.getAstroResult().diagnostics

return {
Program() {
for (const { text, code, location, severity } of diagnostics) {
if (severity === 2 /* Warning */) {
context.report({
loc: {
start: location,
end: location,
},
message: `${text} [${code}]`,
})
}
}
},
}
},
})
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
Scope,
SourceCode as ESLintSourceCode,
} from "eslint"
import type { AST } from "astro-eslint-parser"
import type { AST, parseForESLint } from "astro-eslint-parser"
import type * as ESTree from "estree"
import type {
ASTNode,
Expand Down Expand Up @@ -121,7 +121,7 @@ export type RuleContext = {
}
parserPath: string
parserOptions: Linter.ParserOptions
parserServices: { isAstro?: boolean }
parserServices: Partial<ReturnType<typeof parseForESLint>["services"]>

getAncestors(): ASTNode[]

Expand Down
2 changes: 2 additions & 0 deletions src/utils/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import preferClassListDirective from "../rules/prefer-class-list-directive"
import preferObjectClassList from "../rules/prefer-object-class-list"
import preferSplitClassList from "../rules/prefer-split-class-list"
import semi from "../rules/semi"
import validCompile from "../rules/valid-compile"
import { buildA11yRules } from "../a11y"

export const rules = [
Expand All @@ -26,5 +27,6 @@ export const rules = [
preferObjectClassList,
preferSplitClassList,
semi,
validCompile,
...buildA11yRules(),
] as RuleModule[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"message": "set:html directive will overwrite child nodes. [2006]",
"line": 5,
"column": 5
},
{
"message": "set:html directive will overwrite child nodes. [2006]",
"line": 7,
"column": 5
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
let string = "Foo"
---

<p set:html="string">string</p>
<p set:html={string}>{string}</p>
<p set:html={string}>string</p>
<p set:html="string">{string}</p>
<button set:html="string">
<div></div>
</button>
<button set:html="string">
<div>Foo</div>{string}
</button>
<p set:html="string">
<!-- comment -->{string}
</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"message": "set:text directive will overwrite child nodes. [2006]",
"line": 5,
"column": 5
},
{
"message": "set:text directive will overwrite child nodes. [2006]",
"line": 7,
"column": 5
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
let string = "Foo"
---

<p set:text="string">string</p>
<p set:text={string}>{string}</p>
<p set:text={string}>string</p>
<p set:text="string">{string}</p>
<button set:text="string">
<div></div>
</button>
<button set:text="string">
<div>Foo</div>{string}
</button>
<p set:text="string">
<!-- comment -->{string}
</p>
11 changes: 11 additions & 0 deletions tests/fixtures/rules/valid-compile/valid/child-input.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
let string = "Foo"
---

<p></p>
<p>string</p>
<p>{string}</p>
<p>
<!-- comment -->{string}
</p>
<div><p></p></div>
12 changes: 12 additions & 0 deletions tests/src/rules/valid-compile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { RuleTester } from "eslint"
import rule from "../../../src/rules/valid-compile"
import { loadTestCases } from "../../utils/utils"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
sourceType: "module",
},
})

tester.run("valid-compile", rule as any, loadTestCases("valid-compile"))