Skip to content

Commit 05dfab6

Browse files
committed
Refactored to support ts-eslint
1 parent 9fe40ce commit 05dfab6

File tree

18 files changed

+4775
-5720
lines changed

18 files changed

+4775
-5720
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ node_modules/
44
.vscode/
55
_build/bin
66
_build/obj
7-
packages/stencil-library/eslint-plugin

package-lock.json

Lines changed: 4567 additions & 5592 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# dnn-checkbox should not have any content inside itself
2+
3+
In earlier versions of dnn-elements, whatever content was inside dnn-checkbox would be pulled automatically into a label for the checkbox. This solution was not great as it prevented flexibility of having positioning between the label and the box or custom styling, etc. It couls also have been a accessibility problem.
4+
5+
To match the porper accessibility recommendations, this was obsolete and then removed.
6+
7+
Here is how you can fix old code:
8+
9+
## Old
10+
```jsx
11+
<dnn-checkbox
12+
onChecked={e => console.log(e.detail)}
13+
>
14+
This is my label
15+
</dnn-checkbox>
16+
```
17+
18+
## New
19+
```jsx
20+
<label>
21+
<dnn-checkbox onClick={e => console.log(e.detail)} />
22+
This is my label
23+
</label>
24+
```
25+
26+
You can also have the text before the checkbox if you prefer depending on the desired UI.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { rules } from "./rules";
2+
3+
const plugin = {
4+
configs: {
5+
get recommended() {
6+
return recommended;
7+
}
8+
}
9+
}
10+
const recommended = {
11+
plugins: {
12+
"dnn-elements": plugin,
13+
},
14+
rules,
15+
};
16+
17+
export default plugin;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { rule as noLabelSlotInCheckbox } from "./no-label-slot-in-checkbox";
2+
3+
export const rules = {
4+
"no-label-slot-in-checkbox": noLabelSlotInCheckbox,
5+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { RuleTester } from "@typescript-eslint/rule-tester";
2+
import { rule } from "./no-label-slot-in-checkbox";
3+
4+
const ruleTester = new RuleTester();
5+
6+
// Define a reusable configuration for JSX parser options
7+
const jsxParserOptions = {
8+
parserOptions: {
9+
ecmaFeatures: {
10+
jsx: true,
11+
},
12+
},
13+
};
14+
15+
ruleTester.run("no-label-slot-in-checkbox", rule, {
16+
valid: [
17+
{
18+
code: "<dnn-checkbox></dnn-checkbox>",
19+
languageOptions: jsxParserOptions,
20+
},
21+
],
22+
invalid: [
23+
{
24+
code: "<dnn-checkbox onClick={e => console.log(e)}>Something</dnn-checkbox>",
25+
languageOptions: jsxParserOptions,
26+
errors: [{ messageId: "noLabelSlotInCheckbox" }],
27+
output: "<label>\n<dnn-checkbox onClick={e => console.log(e)} />\nSomething\n</label>",
28+
},
29+
],
30+
});
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { createRule } from "../utils";
2+
3+
export const rule = createRule({
4+
name: "no-label-slot-in-checkbox",
5+
defaultOptions: [],
6+
meta: {
7+
docs: {
8+
description: "Disallow label slot in checkbox",
9+
recommended: true,
10+
url: "https://github.com/DNNCommunity/dnn-elements/releases/tag/v0.24.0",
11+
},
12+
type: "problem",
13+
messages: {
14+
noLabelSlotInCheckbox: "Label slot is not allowed in dnn-checkbox, wrap dnn-checkbox with a label instead."
15+
},
16+
fixable: "code",
17+
schema: [], // Ensure schema is defined as an empty array or with the appropriate schema definition
18+
},
19+
create(context) {
20+
return {
21+
JSXElement(node) {
22+
if (
23+
node.openingElement.name.type === "JSXIdentifier" &&
24+
node.openingElement.name.name === "dnn-checkbox"
25+
) {
26+
const innerContent = node.children
27+
.map(child => context.sourceCode.getText(child))
28+
.join("")
29+
.trim();
30+
31+
if (innerContent) {
32+
context.report({
33+
node,
34+
messageId: "noLabelSlotInCheckbox",
35+
fix: fixer => {
36+
const checkboxText = context.sourceCode.getText(node.openingElement);
37+
const selfClosing = checkboxText.replace(/>$/, " />");
38+
39+
const replacement = `<label>\n${selfClosing}\n${innerContent}\n</label>`;
40+
return fixer.replaceText(node, replacement);
41+
}
42+
});
43+
}
44+
}
45+
}
46+
};
47+
},
48+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { ESLintUtils } from "@typescript-eslint/utils";
2+
3+
export interface TypedLintingRuleDocs {
4+
description: string;
5+
recommended?: boolean;
6+
requiresTyepeChecking?: boolean;
7+
};
8+
9+
export const createRule = ESLintUtils.RuleCreator<TypedLintingRuleDocs>(
10+
name => `https://github.com/DNNCommunity/dnn-elements/tree/main/packages/stencil-library/eslint-plugin/docs/${name}.md`
11+
);

packages/stencil-library/src/eslint-plugin/tsconfig.json renamed to packages/stencil-library/eslint-plugin/tsconfig.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
{
22
"compilerOptions": {
33
"target": "es2019",
4-
"module": "commonjs",
5-
"moduleResolution": "Node",
6-
"outDir": "../../eslint-plugin",
4+
"module": "NodeNext",
5+
"moduleResolution": "nodenext",
6+
"outDir": "dist",
77
"strict": true,
88
"esModuleInterop": true,
99
"declaration": true,
1010
"skipLibCheck": true,
1111
"types": ["jest", "node", "estree-jsx"],
1212
"forceConsistentCasingInFileNames": true,
13-
"sourceMap": true
13+
"sourceMap": true,
14+
"isolatedModules":true,
15+
"jsx": "react",
16+
"jsxFactory": "h"
1417
},
15-
"include": ["src/index.ts"]
18+
"include": ["src/**/*.ts"]
1619
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
module.exports = {
22
preset: "ts-jest",
33
testEnvironment: "node",
4-
testMatch: ["<rootDir>/src/eslint-plugin/__tests__/**/*.tests.ts"],
4+
testMatch: ["<rootDir>/src/eslint-plugin/**/*.tests.ts"],
5+
transform: {
6+
"^.+\\.ts$": ["ts-jest", { tsconfig: "<rootDir>/src/eslint-plugin/tsconfig.json" }],
7+
}
58
}

0 commit comments

Comments
 (0)