@spinnaker/eslint-plugin 0.0.0-main-2

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.
Files changed (61) hide show
  1. package/.eslintignore +3 -0
  2. package/CHANGELOG.md +96 -0
  3. package/LICENSE.txt +203 -0
  4. package/README.md +259 -0
  5. package/babel.config.js +3 -0
  6. package/base.config.js +86 -0
  7. package/create-rule.js +67 -0
  8. package/eslint-plugin.ts +47 -0
  9. package/index.js +6 -0
  10. package/newrule.sh +88 -0
  11. package/none.config.js +18 -0
  12. package/package.json +55 -0
  13. package/rules/api-deprecation.spec.ts +79 -0
  14. package/rules/api-deprecation.ts +254 -0
  15. package/rules/api-no-slashes.spec.ts +84 -0
  16. package/rules/api-no-slashes.ts +148 -0
  17. package/rules/api-no-unused-chaining.spec.ts +26 -0
  18. package/rules/api-no-unused-chaining.ts +47 -0
  19. package/rules/import-from-alias-not-npm.spec.ts +22 -0
  20. package/rules/import-from-alias-not-npm.ts +53 -0
  21. package/rules/import-from-npm-not-alias.spec.ts +24 -0
  22. package/rules/import-from-npm-not-alias.ts +56 -0
  23. package/rules/import-from-npm-not-relative.spec.ts +22 -0
  24. package/rules/import-from-npm-not-relative.ts +57 -0
  25. package/rules/import-from-presentation-not-core.spec.ts +44 -0
  26. package/rules/import-from-presentation-not-core.ts +106 -0
  27. package/rules/import-relative-within-subpackage.spec.ts +51 -0
  28. package/rules/import-relative-within-subpackage.ts +71 -0
  29. package/rules/import-sort.spec.ts +85 -0
  30. package/rules/import-sort.ts +280 -0
  31. package/rules/migrate-to-mock-http-client.spec.ts +78 -0
  32. package/rules/migrate-to-mock-http-client.ts +122 -0
  33. package/rules/ng-no-component-class.spec.ts +45 -0
  34. package/rules/ng-no-component-class.ts +68 -0
  35. package/rules/ng-no-module-export.spec.ts +26 -0
  36. package/rules/ng-no-module-export.ts +117 -0
  37. package/rules/ng-no-require-angularjs.spec.ts +27 -0
  38. package/rules/ng-no-require-angularjs.ts +94 -0
  39. package/rules/ng-no-require-module-deps.spec.ts +33 -0
  40. package/rules/ng-no-require-module-deps.ts +211 -0
  41. package/rules/ng-strictdi.spec.ts +100 -0
  42. package/rules/ng-strictdi.ts +304 -0
  43. package/rules/prefer-promise-like.spec.ts +75 -0
  44. package/rules/prefer-promise-like.ts +108 -0
  45. package/rules/react2angular-with-error-boundary.spec.ts +29 -0
  46. package/rules/react2angular-with-error-boundary.ts +118 -0
  47. package/rules/rest-prefer-static-strings-in-initializer.spec.ts +34 -0
  48. package/rules/rest-prefer-static-strings-in-initializer.ts +89 -0
  49. package/template/template-rule.spec.ts +17 -0
  50. package/template/template-rule.ts +21 -0
  51. package/test.eslintrc +20 -0
  52. package/test_rule_against_deck_source.sh +18 -0
  53. package/tsconfig.json +17 -0
  54. package/utils/angular-rule/angular-rule.js +302 -0
  55. package/utils/angular-rule/false-values.js +6 -0
  56. package/utils/angular-rule/utils.js +624 -0
  57. package/utils/import-aliases.mock.ts +17 -0
  58. package/utils/import-aliases.ts +91 -0
  59. package/utils/mockModule.js +15 -0
  60. package/utils/ruleTester.js +8 -0
  61. package/utils/utils.ts +90 -0
package/.eslintignore ADDED
@@ -0,0 +1,3 @@
1
+ # Built lib files
2
+ /**/*.spec.*
3
+ ./template/**/*
package/CHANGELOG.md ADDED
@@ -0,0 +1,96 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ ## [3.0.2](https://github.com/spinnaker/deck/compare/@spinnaker/eslint-plugin@3.0.1...@spinnaker/eslint-plugin@3.0.2) (2023-10-16)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * **publish:** set access config in deck libraries ([#10049](https://github.com/spinnaker/deck/issues/10049)) ([2a5ebe2](https://github.com/spinnaker/deck/commit/2a5ebe25662eeb9d41b5071749266bf9d6d51104))
12
+
13
+
14
+
15
+
16
+
17
+ ## [3.0.1](https://github.com/spinnaker/deck/compare/@spinnaker/eslint-plugin@3.0.0...@spinnaker/eslint-plugin@3.0.1) (2021-11-03)
18
+
19
+ **Note:** Version bump only for package @spinnaker/eslint-plugin
20
+
21
+
22
+
23
+
24
+
25
+ # [3.0.0](https://github.com/spinnaker/deck/compare/@spinnaker/eslint-plugin@1.1.1...@spinnaker/eslint-plugin@3.0.0) (2021-09-30)
26
+
27
+
28
+ ### Bug Fixes
29
+
30
+ * **eslint-plugin:** fix lodash typing error ([5b34420](https://github.com/spinnaker/deck/commit/5b3442034a2c3b9d2be14d9fa3bdbea40eddf223))
31
+
32
+
33
+ ### Features
34
+
35
+ * **eslint-plugin:** prefer type imports ([d46bd74](https://github.com/spinnaker/deck/commit/d46bd748375e549eb5e0ccacdfe20c3192a2c2a5))
36
+
37
+
38
+ ### BREAKING CHANGES
39
+
40
+ * **eslint-plugin:** Added consistent-type-imports rule as an 'error'. Run: eslint --fix
41
+
42
+ prefer: import type { IType } from 'somepackage';
43
+
44
+
45
+
46
+
47
+
48
+ # [2.0.0](https://github.com/spinnaker/deck/compare/@spinnaker/eslint-plugin@1.1.1...@spinnaker/eslint-plugin@2.0.0) (2021-09-30)
49
+
50
+
51
+ ### Features
52
+
53
+ * **eslint-plugin:** prefer type imports ([d46bd74](https://github.com/spinnaker/deck/commit/d46bd748375e549eb5e0ccacdfe20c3192a2c2a5))
54
+
55
+
56
+ ### BREAKING CHANGES
57
+
58
+ * **eslint-plugin:** Added consistent-type-imports rule as an 'error'. Run: eslint --fix
59
+
60
+ prefer: import type { IType } from 'somepackage';
61
+
62
+
63
+
64
+
65
+
66
+ ## [1.1.1](https://github.com/spinnaker/deck/compare/@spinnaker/eslint-plugin@1.1.0...@spinnaker/eslint-plugin@1.1.1) (2021-08-20)
67
+
68
+ **Note:** Version bump only for package @spinnaker/eslint-plugin
69
+
70
+
71
+
72
+
73
+
74
+ # [1.1.0](https://github.com/spinnaker/deck/compare/@spinnaker/eslint-plugin@1.0.14...@spinnaker/eslint-plugin@1.1.0) (2021-08-13)
75
+
76
+
77
+ ### Bug Fixes
78
+
79
+ * **eslint-plugin:** fix eslint violations inside eslint rules :inception: ([278a2f2](https://github.com/spinnaker/deck/commit/278a2f2393aa0dccf9a78d438951e60854a2ad13))
80
+ * **eslint-plugin:** update test_rule_against_deck_source.sh to allow colocated tests and checking packages/* ([26bff0b](https://github.com/spinnaker/deck/commit/26bff0be0cc6b12dfde7ade2851b30534ce23671))
81
+
82
+
83
+ ### Features
84
+
85
+ * **eslint-plugin:** add typescript support to eslint-plugin ([6f227b8](https://github.com/spinnaker/deck/commit/6f227b8f85a6a38aa9420f14bfbd8ece7a62cf41))
86
+
87
+
88
+
89
+
90
+
91
+ ## 1.0.14 (2021-07-17)
92
+
93
+
94
+ ### Bug Fixes
95
+
96
+ * **publishing:** Auto approve instead of adding "ready to merge" label ([51f536c](https://github.com/spinnaker/deck/commit/51f536c275e77854d8f173aeec86412ffbd66b6d))
package/LICENSE.txt ADDED
@@ -0,0 +1,203 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2014 Netflix
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
203
+
package/README.md ADDED
@@ -0,0 +1,259 @@
1
+ # @spinnaker/eslint-plugin
2
+
3
+ This package is an ESLint plugin containing:
4
+
5
+ - A base ESLint config
6
+ - Parser configured for typescript
7
+ - A set of default plugins, e.g. `react-hooks` plugin
8
+ - Recommended rule sets, e.g. `prettier/@typescript-eslint`
9
+ - Specific from the recommended rule sets are disabled
10
+ - Custom ESLint rules specific to Spinnaker
11
+
12
+ ### Use
13
+
14
+ To use the rules, create a `.eslintrc.js` containing:
15
+
16
+ ```js
17
+ module.exports = {
18
+ plugins: ['@spinnaker/eslint-plugin'],
19
+ extends: ['plugin:@spinnaker/base'],
20
+ };
21
+ ```
22
+
23
+ ## Creating a custom lint rule
24
+
25
+ This `yarn create-rule` command will:
26
+
27
+ - Scaffolds a sample rule
28
+ - Scaffolds a test for the sample rule
29
+ - Adds the rule to the plugin (`eslint-plugin.ts`)
30
+ - Adds the rule as an "error" to the plugin's base config `base.config.js`)
31
+
32
+ The rule should examine AST nodes to detect a lint violation.
33
+ Optionally, it can provide an automatic code fixer.
34
+
35
+ - Use https://astexplorer.net/ to generate the AST for a given code fragment.
36
+ - Set the Language to `Javascript` and the parser to `@typescript-eslint/parser`
37
+ - Paste a snippet of Javascript or Typescript, i.e. `API.one('foo/bar')`
38
+ - Using the interactive AST explorer, [write a rule](#write-a-rule) that detects the lint violation.
39
+ - [Write test cases](#test-a-rule) for valid and invalid input
40
+ - Optionally, [write a fixer](#write-a-fixer) and [write fixer tests](#test-a-fixer)
41
+ - [Add your rule to the plugin](#add-a-rule-to-the-plugin)
42
+
43
+ #### Write a rule
44
+
45
+ A rule file exports a Rule.RuleContext object.
46
+
47
+ ```ts
48
+ import { Rule } from 'eslint';
49
+ const rule: Rule.RuleModule = {
50
+ meta: {
51
+ type: 'problem',
52
+ docs: { description: `Rule Description` },
53
+ fixable: 'code',
54
+ },
55
+ create: function myRuleFunction(context: Rule.RuleContext) {
56
+ return {
57
+ // rule contents here
58
+ };
59
+ },
60
+ };
61
+ export default rule;
62
+ ```
63
+
64
+ > See: the [official docs](https://eslint.org/docs/developer-guide/working-with-rules#rule-basics) in a couple ways.
65
+ >
66
+ > Spinnaker rules can be written in Typescript instead of CommonJS
67
+
68
+ `myRuleFunction` is a callback that receives an [eslint context](https://eslint.org/docs/developer-guide/working-with-rules#the-context-object) and returns an object containing callbacks for AST node types.
69
+
70
+ Each callback will be called when the parser encounters a node of that type.
71
+ When a lint violation is detected, the callback should report it to the context object.
72
+
73
+ ```ts
74
+ import { Rule } from 'eslint';
75
+ import { SimpleLiteral } from 'estree';
76
+ // ...
77
+ function myRuleFunction(context: Rule.RuleContext) {
78
+ return {
79
+ // This callback is called whenever a 'Literal' node is encountered
80
+ Literal: function (literalNode: SimpleLiteral & Rule.NodeParentExtension) {
81
+ if (literalNode.raw.includes('JenkinsX')) {
82
+ // lint violation encountered; report it
83
+ const message = 'String literals may not include JenkinsX';
84
+ context.report({ node, message });
85
+ }
86
+ },
87
+ };
88
+ }
89
+ ```
90
+
91
+ > This example explicitly types the `context` and `literalNode` parameters, but these can be automatically inferred by Typescript
92
+
93
+ In addition to callbacks that trigger on a simple node type (`Literal` in the example above),
94
+ you can also trigger a callback using an [eslint selector](https://eslint.org/docs/developer-guide/selectors).
95
+
96
+ Think of an eslint selector as a CSS selector, but for an AST.
97
+ Selectors can reduce boilerplate while writing a rule, but more importantly they can potentially improve readability.
98
+
99
+ ```ts
100
+ // Using a selector
101
+ function myRuleFunction(context: Rule.RuleContext) {
102
+ return {
103
+ // Find an ExpressionStatement
104
+ // - that is a CallExpression
105
+ // - that has a callee object named 'React'
106
+ // - and has a callee property named 'useEffect'
107
+ "ExpressionStatement > CallExpression[callee.object.name = 'React'][callee.property.name = 'useEffect']"(
108
+ node: ExpressionStatement,
109
+ ) {
110
+ const message = 'Prefer bare useEffect() over React.useEffect()';
111
+ context.report({ node, message });
112
+ },
113
+ };
114
+ }
115
+
116
+ // Not using a selector
117
+ function myRuleFunction(context: Rule.RuleContext) {
118
+ return {
119
+ ExpressionStatement(node) {
120
+ const expression = node.expression;
121
+ if (
122
+ expression?.type === 'CallExpression' &&
123
+ expression.callee.type === 'MemberExpression' &&
124
+ expression.callee.object.name === 'React' &&
125
+ expression.callee.property.name === 'useEffect'
126
+ ) {
127
+ const message = 'Prefer bare useEffect() over React.useEffect()';
128
+ context.report({ node, message });
129
+ }
130
+ },
131
+ };
132
+ }
133
+ ```
134
+
135
+ > One downside of using eslint selectors is the node type is not automatically inferred in the callback.
136
+ > When using selectors, you should explicitly type the node parameter.
137
+
138
+ #### Test a rule
139
+
140
+ We run the tests using [Jest](http://jestjs.io/), but we do not use jest assertions.
141
+ Instead, we use the `RuleTester` API from eslint to define our assertions.
142
+
143
+ ```ts
144
+ import { ruleTester } from '../utils/ruleTester';
145
+ import { rule } from './my-cool-rule';
146
+
147
+ ruleTester.run('my-cool-rule', rule, {
148
+ valid: [
149
+ /** code that doesn't trigger the rule */
150
+ ],
151
+ invalid: [
152
+ /** code that triggers the rule */
153
+ ],
154
+ });
155
+ ```
156
+
157
+ Make sure to add at least one valid and one invalid test cases:
158
+
159
+ ```ts
160
+ ruleTester.run('my-cool-rule', rule, {
161
+ valid: [
162
+ {
163
+ code: 'var foo = "bar";',
164
+ },
165
+ ],
166
+ invalid: [
167
+ {
168
+ code: 'var foo = "JenkinsX";',
169
+ error: 'String literals may not include JenkinsX',
170
+ },
171
+ {
172
+ code: 'createTodo("learn more about JenkinsX foundations");',
173
+ error: 'String literals may not include JenkinsX',
174
+ },
175
+ ],
176
+ });
177
+ ```
178
+
179
+ Run the tests from `/packages/eslint-plugin`:
180
+
181
+ ```
182
+ ❯ yarn test
183
+ yarn run v1.22.4
184
+ $ jest
185
+ PASS test/my-cool-rule.spec.js
186
+
187
+ Test Suites: 1 passed, 1 total
188
+ Tests: 3 passed, 3 total
189
+ Snapshots: 0 total
190
+ Time: 1.095s
191
+ Ran all test suites.
192
+ ✨ Done in 1.69s.
193
+ ```
194
+
195
+ While writing tests, it's useful to run Jest in watch mode: `yarn test --watch`
196
+
197
+ If you need to debug your tests, run `yarn test:debug` and launch the Chrome Debugger
198
+ (enter `chrome://inspect` into the Chrome URL bar).
199
+
200
+ You can (and should) run your work-in-progress rule against the spinnaker OSS codebase:
201
+
202
+ ```shell
203
+ ./test_rule_against_deck_source.sh my-rule
204
+ ```
205
+
206
+ ### Write a fixer
207
+
208
+ Once your tests are passing, consider writing an auto-fixer.
209
+ Auto-fixers can be applied in downstream projects using `eslint --fix`.
210
+ An auto-fixer replaces AST nodes which violate the rule with non-violating code.
211
+
212
+ When reporting a lint violation for your rule, return a `fix` function.
213
+
214
+ ```ts
215
+ Literal(literalNode) {
216
+ if (literalNode.raw.includes('JenkinsX')) {
217
+ // lint violation encountered; report it
218
+ const message = 'String literals may not include JenkinsX';
219
+ const fix = (fixer) => {
220
+ const fixedValue = literalNode.value.replaceAll('JenkinsX', 'JengaX');
221
+ return fixer.replaceText(literalNode, '"' + fixedValue + '"');
222
+ }
223
+ context.report({ fix, node, message });
224
+ }
225
+ }
226
+ ```
227
+
228
+ Review the [fixer api docs](https://eslint.org/docs/developer-guide/working-with-rules#applying-fixes) for more details.
229
+
230
+ If you need to fix more than one thing for a given rule, you may return an array of fixes.
231
+
232
+ ```js
233
+ const fix = (fixer) => {
234
+ const fixedValue = literalNode.value.replaceAll('JenkinsX', 'JengaX');
235
+ return [
236
+ fixer.replaceText(literalNode, '"' + fixedValue '"'),
237
+ fixer.insertTextBefore(literalNode, `/* Jengafied */ `),
238
+ ]
239
+ }
240
+ ```
241
+
242
+ ### Test a fixer
243
+
244
+ The result of a fixer should be added to the tests.
245
+ Add an `output` key to all invalid test cases that can be auto-fixed.
246
+
247
+ ```js
248
+ invalid: [
249
+ {
250
+ code: 'var foo = "JenkinsX";',
251
+ error: 'String literals may not include JenkinsX',
252
+ output: 'var foo = /* Jengafied */ "JengaX";',
253
+ },
254
+ ];
255
+ ```
256
+
257
+ ### Publishing
258
+
259
+ After committing and pushing your new rule, bump the version in package.json (commit and push) and then run `npm publish` manually.
@@ -0,0 +1,3 @@
1
+ module.exports = {
2
+ presets: [['@babel/preset-env', { targets: { node: 'current' } }], '@babel/preset-typescript'],
3
+ };
package/base.config.js ADDED
@@ -0,0 +1,86 @@
1
+ module.exports = {
2
+ parser: '@typescript-eslint/parser',
3
+ parserOptions: { sourceType: 'module' },
4
+ plugins: ['@typescript-eslint', '@spinnaker/eslint-plugin', 'react-hooks'],
5
+ extends: ['eslint:recommended', 'prettier', 'prettier/@typescript-eslint', 'plugin:@typescript-eslint/recommended'],
6
+ rules: {
7
+ '@spinnaker/import-sort': 1,
8
+ '@spinnaker/api-deprecation': 2,
9
+ '@spinnaker/api-no-slashes': 2,
10
+ '@spinnaker/api-no-unused-chaining': 2,
11
+ '@spinnaker/import-from-alias-not-npm': 2,
12
+ '@spinnaker/import-from-npm-not-alias': 2,
13
+ '@spinnaker/import-from-npm-not-relative': 2,
14
+ '@spinnaker/import-from-presentation-not-core': 2,
15
+ '@spinnaker/import-relative-within-subpackage': 2,
16
+ '@spinnaker/migrate-to-mock-http-client': 2,
17
+ '@spinnaker/ng-no-component-class': 2,
18
+ '@spinnaker/ng-no-module-export': 2,
19
+ '@spinnaker/ng-no-require-angularjs': 2,
20
+ '@spinnaker/ng-no-require-module-deps': 2,
21
+ '@spinnaker/ng-strictdi': 2,
22
+ '@spinnaker/prefer-promise-like': 1,
23
+ '@spinnaker/react2angular-with-error-boundary': 2,
24
+ '@spinnaker/rest-prefer-static-strings-in-initializer': 2,
25
+ indent: 'off',
26
+ 'member-ordering': 'off',
27
+ 'no-console': ['error', { allow: ['warn', 'error'] }],
28
+ 'no-extra-boolean-cast': 'off',
29
+ 'no-prototype-builtins': 'off',
30
+ 'one-var': ['error', { initialized: 'never' }],
31
+ 'prefer-rest-params': 'off',
32
+ 'prefer-spread': 'off',
33
+ // turn back on if https://github.com/eslint/eslint/issues/11899 fixes false positives
34
+ 'require-atomic-updates': 'off',
35
+ 'react-hooks/rules-of-hooks': 'error',
36
+ // turn back on after addressing all violations
37
+ // 'react-hooks/exhaustive-deps': 'warn',
38
+ '@typescript-eslint/array-type': ['error', { default: 'array-simple' }],
39
+ '@typescript-eslint/ban-ts-ignore': 'off',
40
+ '@typescript-eslint/consistent-type-imports': ['error', { prefer: 'type-imports' }],
41
+ '@typescript-eslint/explicit-function-return-type': 'off',
42
+ '@typescript-eslint/explicit-member-accessibility': 'off',
43
+ '@typescript-eslint/indent': 'off',
44
+ '@typescript-eslint/interface-name-prefix': 'off',
45
+ '@typescript-eslint/no-case-declarations': 'off',
46
+ '@typescript-eslint/no-empty-function': 'off',
47
+ '@typescript-eslint/no-empty-interface': 'off',
48
+ '@typescript-eslint/no-explicit-any': 'off',
49
+ '@typescript-eslint/no-object-literal-type-assertion': 'off',
50
+ '@typescript-eslint/no-parameter-properties': 'off',
51
+ '@typescript-eslint/no-this-alias': 'off',
52
+ '@typescript-eslint/no-triple-slash-reference': 'off',
53
+ '@typescript-eslint/no-unused-vars': 'off',
54
+ '@typescript-eslint/no-use-before-define': 'off',
55
+ '@typescript-eslint/no-var-requires': 'off', // TODO: turn on once all code is using ES6 imports
56
+ '@typescript-eslint/triple-slash-reference': 'off',
57
+ '@typescript-eslint/explicit-module-boundary-types': 'off',
58
+ '@typescript-eslint/ban-types': 'off',
59
+ '@typescript-eslint/ban-ts-comment': 'off',
60
+ },
61
+ overrides: [
62
+ {
63
+ files: ['*.js', '*.jsx'],
64
+ rules: {
65
+ '@typescript-eslint/no-use-before-define': 'off',
66
+ },
67
+ },
68
+ {
69
+ files: ['*.ts', '*.tsx'],
70
+ rules: {
71
+ 'no-undef': 'off', // typescript already checks this
72
+ },
73
+ },
74
+ ],
75
+ env: {
76
+ browser: true,
77
+ node: true,
78
+ es6: true,
79
+ jasmine: true,
80
+ },
81
+ globals: {
82
+ angular: true,
83
+ $: true,
84
+ _: true,
85
+ },
86
+ };