-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add new use-client-directive rule
- Loading branch information
1 parent
e68297b
commit 744c368
Showing
7 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
packages/eslint-plugin-orbit-components/src/__fixtures__/file.ts
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 @@ | ||
// noop |
1 change: 1 addition & 0 deletions
1
packages/eslint-plugin-orbit-components/src/__fixtures__/react.tsx
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 @@ | ||
// noop |
9 changes: 9 additions & 0 deletions
9
packages/eslint-plugin-orbit-components/src/__tests__/tsconfig.json
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,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": true, | ||
"module": "ESNext", | ||
"noEmit": true, | ||
"moduleResolution": "node" | ||
}, | ||
"include": ["*.test.ts", "../__fixtures__"] | ||
} |
31 changes: 31 additions & 0 deletions
31
packages/eslint-plugin-orbit-components/src/__tests__/useClient.test.ts
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,31 @@ | ||
import dedent from "dedent"; | ||
import ruleTester from "../ruleTester"; | ||
import useClient from "../rules/useClient"; | ||
|
||
describe("use-client-directive", () => { | ||
ruleTester.run("use-client-directive", useClient, { | ||
valid: [ | ||
{ | ||
code: dedent` | ||
"use client"; | ||
import React from "react"; | ||
import styled from 'styled-components'; | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: dedent` | ||
import React from "react"; | ||
import styled from 'styled-components'; | ||
`, | ||
errors: [{ messageId: "error" }], | ||
output: dedent` | ||
"use client"; | ||
import React from "react"; | ||
import styled from 'styled-components'; | ||
`, | ||
}, | ||
], | ||
}); | ||
}); |
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,2 @@ | ||
export const RULE_URL = | ||
"https://github.com/kiwicom/orbit/tree/master/packages/eslint-plugin-orbit-components/docs/rules"; |
41 changes: 41 additions & 0 deletions
41
packages/eslint-plugin-orbit-components/src/rules/useClient.ts
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,41 @@ | ||
import { AST_NODE_TYPES, TSESTree as t, TSESLint } from "@typescript-eslint/utils"; | ||
import ruleCreator from "../utils/ruleCreator"; | ||
|
||
const useClient = ruleCreator({ | ||
name: "use-client-directive", | ||
meta: { | ||
type: "problem", | ||
fixable: "code", | ||
docs: { | ||
description: "Prevents missing 'use client' directive in Orbit components", | ||
}, | ||
messages: { | ||
error: "Missing 'use client' directive in Orbit components", | ||
}, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
|
||
create: context => { | ||
return { | ||
Program(node: t.Program) { | ||
if (node.sourceType !== "module") return; | ||
if (node.body.length === 0) return; | ||
|
||
const noDirective = node.body.some(n => n.type === AST_NODE_TYPES.ExpressionStatement); | ||
|
||
if (!noDirective) { | ||
context.report({ | ||
node, | ||
messageId: "error", | ||
fix: fixer => { | ||
return fixer.insertTextBefore(node.body[0], `"use client";\n`); | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); | ||
|
||
export default useClient; |
5 changes: 5 additions & 0 deletions
5
packages/eslint-plugin-orbit-components/src/utils/ruleCreator.ts
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 @@ | ||
import { ESLintUtils } from "@typescript-eslint/utils"; | ||
|
||
import { RULE_URL } from "../consts"; | ||
|
||
export default ESLintUtils.RuleCreator(name => `${RULE_URL}/${name}`); |