@travetto/eslint 3.0.0-rc.15 → 3.0.0-rc.17

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.
package/README.md CHANGED
@@ -6,4 +6,175 @@
6
6
  **Install: @travetto/eslint**
7
7
  ```bash
8
8
  npm install @travetto/eslint
9
+
10
+ # or
11
+
12
+ yarn add @travetto/eslint
13
+ ```
14
+
15
+ [ESLint](https://eslint.org/) is the standard for linting [Typescript](https://typescriptlang.org) and [Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript) code. This module provides some standard linting patterns and the ability to create custom rules. Due to the fact that the framework supports both [CommonJS](https://nodejs.org/api/modules.html) and [Ecmascript Module](https://nodejs.org/api/esm.html) formats, a novel solution was required to allow [ESLint](https://eslint.org/) to load [Ecmascript Module](https://nodejs.org/api/esm.html) files.
16
+
17
+ **Note**: The [ESLint](https://eslint.org/) has introduced [a new configuration format](https://eslint.org/blog/2022/08/new-config-system-part-3/) which allows for [Ecmascript Module](https://nodejs.org/api/esm.html) files.
18
+
19
+ ## CLI - Register
20
+ In a new project, the first thing that will need to be done, post installation, is to create the eslint configuration file.
21
+
22
+ **Terminal: Registering the Configuration**
23
+ ```bash
24
+ $ trv lint:register
25
+
26
+ Wrote eslint config to <workspace-root>/eslint.config.js
27
+ ```
28
+
29
+ This is the file the linter will use, and any other tooling (e.g. IDEs).
30
+
31
+ **Code: Sample configuration**
32
+ ```javascript
33
+ process.env.TRV_MANIFEST = './.trv_output/node_modules/@travetto/eslint';
34
+ const { buildConfig } = require('./.trv_output/node_modules/@travetto/eslint/support/bin/eslint-config.js');
35
+ const { RootIndex } = require('./.trv_output/node_modules/@travetto/manifest/__index__.js');
36
+ const pluginFiles = RootIndex.findSupport({ filter: f => /support\/eslint[.]/.test(f) });
37
+ const plugins = pluginFiles.map(x => require(x.outputFile));
38
+ const config = buildConfig(plugins);
39
+ module.exports = config;
40
+ ```
41
+
42
+ The output is tied to whether or not you are using the [CommonJS](https://nodejs.org/api/modules.html) or [Ecmascript Module](https://nodejs.org/api/esm.html) format.
43
+
44
+ ## CLI - Lint
45
+
46
+ Once installed, using the linter is as simple as invoking it via the cli:
47
+
48
+ **Terminal: Running the Linter**
49
+ ```bash
50
+ npx trv lint
51
+ ```
52
+
53
+ Or pointing your IDE to reference the registered configuration file.
54
+
55
+ ## Custom Rules
56
+ It can be seen in the sample configuration, that the configuration is looking for files with the pattern of `support/eslint/.*`
57
+
58
+ These files will follow a given pattern of:
59
+
60
+ **Code: Custom Rule Shape**
61
+ ```typescript
62
+ import type eslint from 'eslint';
63
+
64
+ export type TrvEslintPlugin = {
65
+ name: string;
66
+ rules: Record<string, {
67
+ defaultLevel?: string | boolean | number;
68
+ create(context: eslint.Rule.RuleContext): eslint.Rule.RuleListener;
69
+ }>;
70
+ };
71
+ ```
72
+
73
+ An example plugin is used in the [Travetto](https://travetto.dev) framework for enforcing import patterns:
74
+
75
+ **Code: Import Order Rule**
76
+ ```typescript
77
+ import type eslint from 'eslint';
78
+ import { Program, BaseExpression, Expression } from 'estree';
79
+
80
+ import type { TrvEslintPlugin } from '@travetto/eslint';
81
+
82
+ const groupTypeMap = {
83
+ node: ['node', 'travetto', 'local'],
84
+ travetto: ['travetto', 'local'],
85
+ local: ['local'],
86
+ };
87
+
88
+ interface TSAsExpression extends BaseExpression {
89
+ type: 'TSAsExpression';
90
+ expression: Expression;
91
+ }
92
+
93
+ declare module 'estree' {
94
+ interface ExpressionMap {
95
+ TSAsExpression: TSAsExpression;
96
+ }
97
+ }
98
+
99
+ export const ImportOrder: TrvEslintPlugin = {
100
+ name: '@travetto-import',
101
+ rules: {
102
+ order: {
103
+ defaultLevel: 'error',
104
+ create(context) {
105
+ function check({ body }: Program): void {
106
+ let groupType: (keyof typeof groupTypeMap) | undefined;
107
+ let groupSize = 0;
108
+ let contiguous = false;
109
+ let prev: eslint.AST.Program['body'][number] | undefined;
110
+
111
+ for (const node of body) {
112
+
113
+ let from: string | undefined;
114
+
115
+ if (node.type === 'ImportDeclaration') {
116
+ if (node.source?.value && typeof node.source.value === 'string') {
117
+ from = node.source.value;
118
+ }
119
+ } else if (node.type === 'VariableDeclaration' && node.kind === 'const') {
120
+ const [decl] = node.declarations;
121
+ let call: Expression | undefined;
122
+ const initType = decl?.init?.type;
123
+ if (initType === 'CallExpression') {
124
+ call = decl.init;
125
+ } else if (initType === 'TSAsExpression') { // tslint support
126
+ call = decl.init.expression;
127
+ }
128
+ if (
129
+ call?.type === 'CallExpression' && call.callee.type === 'Identifier' &&
130
+ call.callee.name === 'require' && call.arguments[0].type === 'Literal'
131
+ ) {
132
+ const arg1 = call.arguments[0];
133
+ if (arg1.value && typeof arg1.value === 'string') {
134
+ from = arg1.value;
135
+ }
136
+ }
137
+ }
138
+
139
+ if (!from) {
140
+ continue;
141
+ }
142
+
143
+ const lineType: typeof groupType = /^@travetto/.test(from) ? 'travetto' : /^[^.]/.test(from) ? 'node' : 'local';
144
+
145
+ if (/module\/[^/]+\/doc\//.test(context.getFilename()) && lineType === 'local' && from.startsWith('..')) {
146
+ context.report({ message: 'Doc does not support parent imports', node });
147
+ }
148
+
149
+ if (groupType && !groupTypeMap[groupType].includes(lineType)) {
150
+ context.report({ message: `Invalid transition from ${groupType} to ${lineType}`, node });
151
+ }
152
+
153
+ if (groupType === lineType) {
154
+ groupSize += 1;
155
+ } else if (((node.loc?.end.line ?? 0) - (prev?.loc?.end.line ?? 0)) > 1) {
156
+ // Newlines
157
+ contiguous = false;
158
+ groupSize = 0;
159
+ }
160
+
161
+ if (groupSize === 0) { // New group, who dis
162
+ groupSize = 1;
163
+ groupType = lineType;
164
+ } else if (groupType === lineType && !contiguous) { // Contiguous same
165
+ // Do nothing
166
+ } else if (groupSize === 1) { // Contiguous diff, count 1
167
+ contiguous = true;
168
+ groupType = lineType;
169
+ } else { // Contiguous diff, count > 1
170
+ context.report({ message: `Invalid contiguous groups ${groupType} and ${lineType}`, node });
171
+ }
172
+ prev = node;
173
+ }
174
+ }
175
+ return { Program: check };
176
+ }
177
+ }
178
+ }
179
+ };
9
180
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/eslint",
3
- "version": "3.0.0-rc.15",
3
+ "version": "3.0.0-rc.17",
4
4
  "description": "ES Linting Rules",
5
5
  "keywords": [
6
6
  "eslint",
@@ -23,7 +23,7 @@
23
23
  "directory": "module/eslint"
24
24
  },
25
25
  "dependencies": {
26
- "@travetto/manifest": "^3.0.0-rc.16",
26
+ "@travetto/manifest": "^3.0.0-rc.18",
27
27
  "@types/eslint": "^8.21.1",
28
28
  "@typescript-eslint/eslint-plugin": "^5.52.0",
29
29
  "@typescript-eslint/parser": "^5.52.0",
@@ -31,7 +31,7 @@
31
31
  "eslint-plugin-unused-imports": "^2.0.0"
32
32
  },
33
33
  "peerDependencies": {
34
- "@travetto/cli": "^3.0.0-rc.21"
34
+ "@travetto/cli": "^3.0.0-rc.24"
35
35
  },
36
36
  "peerDependenciesMeta": {
37
37
  "@travetto/cli": {
@@ -1,7 +1,9 @@
1
+ import type eslint from 'eslint';
2
+
1
3
  export type TrvEslintPlugin = {
2
4
  name: string;
3
5
  rules: Record<string, {
4
6
  defaultLevel?: string | boolean | number;
5
- create: Function;
7
+ create(context: eslint.Rule.RuleContext): eslint.Rule.RuleListener;
6
8
  }>;
7
9
  };