eslint-plugin-interface-to-type 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,23 @@
1
+ ---
2
+ name: Bug Report
3
+ about: Report a bug to help us improve
4
+ title: "[BUG] "
5
+ labels: bug
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Describe the bug**
11
+ A clear and concise description of what the bug is.
12
+
13
+ **To Reproduce**
14
+ Steps to reproduce the behavior:
15
+ 1. Go to '...'
16
+ 2. Run '...'
17
+ 3. See error
18
+
19
+ **Expected behavior**
20
+ A clear and concise description of what you expected to happen.
21
+
22
+ **Additional context**
23
+ Add any other context about the problem here.
@@ -0,0 +1,17 @@
1
+ ---
2
+ name: Feature Request
3
+ about: Suggest a new feature
4
+ title: "[FEATURE] "
5
+ labels: enhancement
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Describe the feature you'd like**
11
+ A clear and concise description of the feature.
12
+
13
+ **Describe alternatives you've considered**
14
+ A clear and concise description of any alternative solutions or features you've considered.
15
+
16
+ **Additional context**
17
+ Add any other context or screenshots about the feature request here.
@@ -0,0 +1,10 @@
1
+ # Security Policy
2
+
3
+ ## Reporting a Vulnerability
4
+ If you discover a security vulnerability in this project, please report it by creating a new issue in this repository.
5
+
6
+ When creating the issue:
7
+ - Clearly describe the vulnerability.
8
+ - Include any relevant details or steps to reproduce the issue.
9
+
10
+ We will review and address all reported vulnerabilities as quickly as possible. Thank you for helping to improve the security of this project.
@@ -0,0 +1,11 @@
1
+
2
+ ### Description
3
+ Describe the changes you made and why.
4
+
5
+ ### Related Issues
6
+ Closes #[issue number]
7
+
8
+ ### Checklist
9
+ - [ ] I have tested the changes locally.
10
+ - [ ] I have updated documentation where necessary.
11
+ - [ ] I have ensured my code follows the style guidelines of this project.
@@ -0,0 +1,35 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+ We as members, contributors, and leaders pledge to make participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socioeconomic status, nationality, personal appearance, race, caste, color, religion, or sexual identity and orientation.
5
+
6
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
7
+
8
+ ## Our Standards
9
+ Examples of behavior that contributes to a positive environment for our community include:
10
+ - Demonstrating empathy and kindness toward other people.
11
+ - Being respectful of differing opinions, viewpoints, and experiences.
12
+ - Giving and gracefully accepting constructive feedback.
13
+ - Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience.
14
+ - Focusing on what is best not just for us as individuals, but for the overall community.
15
+
16
+ Examples of unacceptable behavior include:
17
+ - The use of sexualized language or imagery, and sexual attention or advances of any kind.
18
+ - Trolling, insulting or derogatory comments, and personal or political attacks.
19
+ - Public or private harassment.
20
+ - Publishing others' private information, such as a physical or email address, without their explicit permission.
21
+ - Other conduct which could reasonably be considered inappropriate in a professional setting.
22
+
23
+ ## Enforcement Responsibilities
24
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
25
+
26
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
27
+
28
+ ## Scope
29
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
30
+
31
+ ## Enforcement
32
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by creating an issue in this repository. All complaints will be reviewed and investigated promptly and fairly.
33
+
34
+ ## Attribution
35
+ This Code of Conduct is adapted from the [Contributor Covenant](https://www.contributor-covenant.org), version 2.1, available at https://www.contributor-covenant.org/version/2/1/code_of_conduct.html.
@@ -0,0 +1,48 @@
1
+ # Contributing to eslint-plugin-interface-to-type
2
+
3
+ Thank you for considering contributing to this project! Here are some guidelines to help you get started.
4
+
5
+ ---
6
+
7
+ ## How to Contribute
8
+
9
+ 1. **Report Bugs:**
10
+ - Use the [Issues](https://github.com/janszotkowski/eslint-plugin-interface-to-type/issues) tab to report any bugs or problems you encounter.
11
+ - Clearly describe the problem, steps to reproduce it, and your environment.
12
+
13
+ 2. **Request Features:**
14
+ - Use the same [Issues](https://github.com/janszotkowski/eslint-plugin-interface-to-type/issues) tab to suggest new features or enhancements.
15
+
16
+ 3. **Submit Changes:**
17
+ - Create a pull request for any changes you'd like to see in the project.
18
+ - Include a clear description of your changes and reference any relevant issues.
19
+
20
+ ---
21
+
22
+ ## Development Setup
23
+
24
+ 1. **Clone the repository:**
25
+ ```bash
26
+ git clone https://github.com/janszotkowski/eslint-plugin-interface-to-type.git
27
+ cd eslint-plugin-interface-to-type
28
+ ```
29
+
30
+ 2. **Install dependencies:**
31
+ ```bash
32
+ npm install
33
+ ```
34
+
35
+ 3. **Run tests:**
36
+ ```bash
37
+ npm test
38
+ ```
39
+
40
+ ---
41
+
42
+ ## Guidelines for Contributions
43
+
44
+ - Ensure all changes are thoroughly tested.
45
+ - Include clear and descriptive commit messages.
46
+ - Update documentation if your changes affect usage.
47
+
48
+ Thank you for contributing and helping improve this project!
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Jan Szotkowski
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
 
2
2
  # eslint-plugin-interface-to-type
3
3
 
4
- [![npm version](https://img.shields.io/npm/v/eslint-plugin-interface-to-type.svg)](https://www.npmjs.com/package/eslint-plugin-interface-to-type)
5
- [![downloads](https://img.shields.io/npm/dm/eslint-plugin-interface-to-type.svg)](https://www.npmjs.com/package/eslint-plugin-interface-to-type)
4
+ [![npm version](https://img.shields.io/npm/v/eslint-plugin-interface-to-type.svg)](https://www.npmjs.com/package/eslint-plugin-interface-to-type)
5
+ [![downloads](https://img.shields.io/npm/dm/eslint-plugin-interface-to-type.svg)](https://www.npmjs.com/package/eslint-plugin-interface-to-type)
6
6
 
7
7
  An ESLint plugin that enforces the use of `type` aliases instead of `interface` declarations in TypeScript.
8
8
 
@@ -66,6 +66,32 @@ type User = {
66
66
 
67
67
  ---
68
68
 
69
+ ## Why `interface` can be risky
70
+
71
+ One of the reasons to prefer `type` over `interface` is the potential for unexpected behavior with `interface` merging. In TypeScript, when two `interface` declarations share the same name, they are automatically merged into a single declaration. This behavior, known as [Declaration Merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html), can introduce subtle bugs and unintended behaviors in your codebase.
72
+
73
+ ### Example of Declaration Merging
74
+
75
+ ```typescript
76
+ interface User {
77
+ id: number;
78
+ }
79
+
80
+ interface User {
81
+ name: string;
82
+ }
83
+
84
+ // Resulting type
85
+ interface User {
86
+ id: number;
87
+ name: string;
88
+ }
89
+ ```
90
+
91
+ While this behavior can sometimes be useful, it often leads to confusion and makes code harder to maintain, especially in large or collaborative projects. Using `type` avoids this risk entirely, as `type` declarations with the same name will result in a compilation error.
92
+
93
+ ---
94
+
69
95
  ## Autofix
70
96
 
71
97
  This rule supports ESLint's `--fix` option, which can automatically convert `interface` declarations to equivalent `type` aliases.
@@ -13,7 +13,7 @@ module.exports = {
13
13
  return {
14
14
  TSInterfaceDeclaration(node) {
15
15
  const interfaceName = node.id.name;
16
- const sourceCode = context.getSourceCode();
16
+ const sourceCode = context.sourceCode || context.getSourceCode();
17
17
  const typeParams = node.typeParameters ? sourceCode.getText(node.typeParameters) : '';
18
18
 
19
19
  const bodyStart = node.body.range[0];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-interface-to-type",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "mocha tests/**/*.test.js"
@@ -19,13 +19,13 @@
19
19
  "url": "https://github.com/janszotkowski/eslint-plugin-interface-to-type"
20
20
  },
21
21
  "description": "An ESLint plugin to enforce the use of type over interface in TypeScript.",
22
- "dependencies": {},
23
22
  "devDependencies": {
24
- "@typescript-eslint/eslint-plugin": "^5.62.0",
25
- "@typescript-eslint/parser": "^5.62.0",
26
- "chai": "^5.1.2",
27
- "eslint": "^8.57.1",
23
+ "@typescript-eslint/eslint-plugin": "^8.54.0",
24
+ "@typescript-eslint/parser": "^8.54.0",
25
+ "chai": "^6.2.2",
26
+ "eslint": "^10.0.0",
28
27
  "eslint-rule-tester": "^2.0.0",
29
- "mocha": "^10.8.2"
28
+ "mocha": "^11.7.5",
29
+ "typescript": "^5.9.3"
30
30
  }
31
31
  }
@@ -0,0 +1,85 @@
1
+ const { RuleTester } = require("eslint");
2
+ const rule = require("../../../lib/rules/prefer-type-over-interface");
3
+ const parser = require("@typescript-eslint/parser");
4
+
5
+ // Mock context pro simulaci starších verzí ESLintu (před v9)
6
+ const mockContextV8 = {
7
+ getSourceCode: () => ({
8
+ getText: (node) => {
9
+ if (!node) return "mock source code";
10
+ if (node.type === "TSTypeParameterDeclaration") return "<T>";
11
+ if (node.type === "TSInterfaceBody") return "{ prop: string }";
12
+ if (node.name === "Parent") return "Parent";
13
+ return "getText result";
14
+ }
15
+ }),
16
+ report: () => {}
17
+ };
18
+
19
+ // Mock context pro ESLint v9/v10
20
+ const mockContextV10 = {
21
+ sourceCode: {
22
+ getText: (node) => {
23
+ if (!node) return "mock source code";
24
+ return "getText result";
25
+ }
26
+ },
27
+ report: () => {}
28
+ };
29
+
30
+ describe("Kompatibilita verzí ESLint", () => {
31
+ it("mělo by fungovat s context.getSourceCode() (pro ESLint < 9)", () => {
32
+ const ruleInstance = rule.create(mockContextV8);
33
+ const node = {
34
+ id: { name: "Test" },
35
+ body: { range: [0, 10] },
36
+ typeParameters: { type: "TSTypeParameterDeclaration" }
37
+ };
38
+ // Nemělo by vyhodit chybu
39
+ ruleInstance.TSInterfaceDeclaration(node);
40
+ });
41
+
42
+ it("mělo by fungovat s context.sourceCode (pro ESLint >= 9)", () => {
43
+ const ruleInstance = rule.create(mockContextV10);
44
+ const node = {
45
+ id: { name: "Test" },
46
+ body: { range: [0, 10] },
47
+ typeParameters: { type: "TSTypeParameterDeclaration" }
48
+ };
49
+ // Nemělo by vyhodit chybu
50
+ ruleInstance.TSInterfaceDeclaration(node);
51
+ });
52
+ });
53
+
54
+ const ruleTester = new RuleTester({
55
+ languageOptions: {
56
+ parser: parser,
57
+ ecmaVersion: 2020,
58
+ sourceType: "module",
59
+ },
60
+ });
61
+
62
+ ruleTester.run("prefer-type-over-interface-v10-compatibility", rule, {
63
+ valid: [
64
+ {
65
+ code: "type MyType = { field: string; };",
66
+ },
67
+ ],
68
+ invalid: [
69
+ {
70
+ code: "interface MyInterface { field: string; }",
71
+ errors: [{ message: "Interface 'MyInterface' can be replaced by a type." }],
72
+ output: "type MyInterface = { field: string; };",
73
+ },
74
+ {
75
+ code: "interface MyInterface<T> { field: T; }",
76
+ errors: [{ message: "Interface 'MyInterface' can be replaced by a type." }],
77
+ output: "type MyInterface<T> = { field: T; };",
78
+ },
79
+ {
80
+ code: "interface MyInterface extends Parent { field: string; }",
81
+ errors: [{ message: "Interface 'MyInterface' can be replaced by a type." }],
82
+ output: "type MyInterface = Parent & { field: string; };",
83
+ },
84
+ ],
85
+ });
@@ -1,9 +1,10 @@
1
1
  const { RuleTester } = require("eslint");
2
2
  const rule = require("../../../lib/rules/prefer-type-over-interface");
3
+ const parser = require("@typescript-eslint/parser");
3
4
 
4
5
  const ruleTester = new RuleTester({
5
- parser: require.resolve("@typescript-eslint/parser"),
6
- parserOptions: {
6
+ languageOptions: {
7
+ parser: parser,
7
8
  ecmaVersion: 2020,
8
9
  sourceType: "module",
9
10
  },