@open-xchange/linter-presets 1.1.1 → 1.2.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## `1.2.0` – 2025-Jan-09
4
+
5
+ - change: (ESLint) move support for native decorators in JS files into own environment `env.decorators`
6
+ - removed: (ESLint) support for TypeScript's experimental decorators
7
+
3
8
  ## `1.1.1` – 2025-Jan-08
4
9
 
5
10
  - fix: (ESLint) remove obsolete package `eslint-import-resolver-typescript`
@@ -1,6 +1,5 @@
1
1
  import eslintJs from "@eslint/js";
2
- import babelParser from "@babel/eslint-parser";
3
- import { JS_GLOB, SRC_GLOB, VUE_GLOB, extGlob } from "../shared/env-utils.js";
2
+ import { SRC_GLOB, VUE_GLOB, extGlob } from "../shared/env-utils.js";
4
3
  import { builtinRestrictedRules } from "../shared/restricted.js";
5
4
  // functions ==================================================================
6
5
  /**
@@ -39,23 +38,6 @@ export default function base(languageConfig) {
39
38
  languageOptions("commonjs", "cjs", "cts"),
40
39
  // ECMA version and module type for *.js and *.ts files
41
40
  languageOptions(languageConfig.sourceType, "js", "jsx", "ts", "tsx"),
42
- // ECMA decorators via Babel plugin
43
- ...(languageConfig.nativeDecorators ? [{
44
- files: JS_GLOB,
45
- languageOptions: {
46
- parser: babelParser,
47
- parserOptions: {
48
- requireConfigFile: false,
49
- babelOptions: {
50
- babelrc: false,
51
- configFile: false,
52
- plugins: [
53
- ["@babel/plugin-proposal-decorators", { version: "2023-11" }],
54
- ],
55
- },
56
- },
57
- },
58
- }] : []),
59
41
  // configure linter rules
60
42
  {
61
43
  files: [...SRC_GLOB, ...VUE_GLOB],
@@ -129,7 +111,7 @@ export default function base(languageConfig) {
129
111
  radix: "error",
130
112
  "symbol-description": "error",
131
113
  // built-in restricted items
132
- ...builtinRestrictedRules(languageConfig),
114
+ ...builtinRestrictedRules(),
133
115
  },
134
116
  },
135
117
  ];
@@ -0,0 +1,21 @@
1
+ import type { Linter } from "eslint";
2
+ import type { EnvBaseOptions } from "../shared/env-utils.js";
3
+ /**
4
+ * Configuration options for the environment preset "env.decorators".
5
+ */
6
+ export interface EnvDecoratorsOptions extends EnvBaseOptions {
7
+ }
8
+ /**
9
+ * Adds support for native decorators in JavaScript source files.
10
+ *
11
+ * Wraps the following packages:
12
+ * - `@babel/eslint-parser`
13
+ * - `@babel/plugin-proposal-decorators`
14
+ *
15
+ * @param envOptions
16
+ * Configuration options for the environment.
17
+ *
18
+ * @returns
19
+ * An array of configuration objects to be added to the flat configuration.
20
+ */
21
+ export default function decorators(envOptions: EnvBaseOptions): Linter.Config[];
@@ -0,0 +1,34 @@
1
+ import babelParser from "@babel/eslint-parser";
2
+ import { TS_GLOB, concatConfigs, createConfig, customRules } from "../shared/env-utils.js";
3
+ // functions ==================================================================
4
+ /**
5
+ * Adds support for native decorators in JavaScript source files.
6
+ *
7
+ * Wraps the following packages:
8
+ * - `@babel/eslint-parser`
9
+ * - `@babel/plugin-proposal-decorators`
10
+ *
11
+ * @param envOptions
12
+ * Configuration options for the environment.
13
+ *
14
+ * @returns
15
+ * An array of configuration objects to be added to the flat configuration.
16
+ */
17
+ export default function decorators(envOptions) {
18
+ return concatConfigs(createConfig(envOptions, {
19
+ ignores: TS_GLOB,
20
+ languageOptions: {
21
+ parser: babelParser,
22
+ parserOptions: {
23
+ requireConfigFile: false,
24
+ babelOptions: {
25
+ babelrc: false,
26
+ configFile: false,
27
+ plugins: [
28
+ ["@babel/plugin-proposal-decorators", { version: "2023-11" }],
29
+ ],
30
+ },
31
+ },
32
+ },
33
+ }), customRules(envOptions));
34
+ }
@@ -6,9 +6,10 @@ import jest from "./env/jest.js";
6
6
  import vitest from "./env/vitest.js";
7
7
  import codecept from "./env/codecept.js";
8
8
  import react from "./env/react.js";
9
- import project from "./env/project.js";
10
9
  import eslint from "./env/eslint.js";
10
+ import project from "./env/project.js";
11
11
  import tsconfig from "./env/tsconfig.js";
12
+ import decorators from "./env/decorators.js";
12
13
  /**
13
14
  * Configuration options for linting the entire project with ESLint.
14
15
  */
@@ -50,9 +51,10 @@ export declare const env: {
50
51
  vitest: typeof vitest;
51
52
  codecept: typeof codecept;
52
53
  react: typeof react;
53
- project: typeof project;
54
54
  eslint: typeof eslint;
55
+ project: typeof project;
55
56
  tsconfig: typeof tsconfig;
57
+ decorators: typeof decorators;
56
58
  };
57
59
  /**
58
60
  * Generates ESLint configuration objects targeting the source files in the
@@ -17,9 +17,10 @@ import jest from "./env/jest.js";
17
17
  import vitest from "./env/vitest.js";
18
18
  import codecept from "./env/codecept.js";
19
19
  import react from "./env/react.js";
20
- import project from "./env/project.js";
21
20
  import eslint from "./env/eslint.js";
21
+ import project from "./env/project.js";
22
22
  import tsconfig from "./env/tsconfig.js";
23
+ import decorators from "./env/decorators.js";
23
24
  // environment presets ========================================================
24
25
  /**
25
26
  * A collection with all available environment presets.
@@ -31,9 +32,10 @@ export const env = {
31
32
  vitest,
32
33
  codecept,
33
34
  react,
34
- project,
35
35
  eslint,
36
+ project,
36
37
  tsconfig,
38
+ decorators,
37
39
  };
38
40
  // functions ==================================================================
39
41
  /**
@@ -59,7 +61,6 @@ export function configure(options) {
59
61
  const languageConfig = {
60
62
  ecmaVersion: "latest",
61
63
  sourceType: "module",
62
- nativeDecorators: false,
63
64
  ...options?.language,
64
65
  };
65
66
  // resolve dependency configuration options
@@ -21,14 +21,6 @@ export interface LanguageOptions {
21
21
  * @default "module"
22
22
  */
23
23
  sourceType?: "module" | "commonjs";
24
- /**
25
- * Whether to support native ES decorators in JavaScript code (via Babel
26
- * plugin). Does not affect TypeScript code which uses an own parser aware
27
- * of the decorator syntax.
28
- *
29
- * @default false
30
- */
31
- nativeDecorators?: boolean;
32
24
  }
33
25
  export type LanguageConfig = DeepRequired<LanguageOptions>;
34
26
  /**
@@ -1,5 +1,5 @@
1
1
  import type { Linter } from "eslint";
2
- import type { LanguageOptions, EnvFilesOptions, EnvBaseOptions } from "../shared/env-utils.js";
2
+ import type { EnvFilesOptions, EnvBaseOptions } from "../shared/env-utils.js";
3
3
  /**
4
4
  * Configuration for a single banned global or import.
5
5
  */
@@ -46,15 +46,6 @@ export interface EnvRestrictedOverride extends EnvFilesOptions, EnvRestrictedIte
46
46
  * banned globals, imports, properties, and syntax constructs.
47
47
  */
48
48
  export interface EnvRestrictedOption extends EnvRestrictedItems {
49
- /**
50
- * Whether the environment supports native decorators. In this case, it
51
- * will be required to replace TypeScript decorated `private` methods with
52
- * native `#private` methods. TypeScript's experimental decorators cannot
53
- * be used with native `#private` fields.
54
- *
55
- * @default false
56
- */
57
- nativeDecorators?: boolean;
58
49
  /**
59
50
  * Overrides for specific subsets of files in the environment. All
60
51
  * restricted items of overrides will be merged with the common restricted
@@ -76,13 +67,10 @@ export interface EnvRestrictedOptions extends EnvBaseOptions {
76
67
  /**
77
68
  * Creates restricted rules with built-in restricted items only.
78
69
  *
79
- * @param languageOptions
80
- * The language options passed with the Linter configuration.
81
- *
82
70
  * @returns
83
71
  * The rules dictionary with all needed restricted rules.
84
72
  */
85
- export declare function builtinRestrictedRules(languageOptions: LanguageOptions): Linter.RulesRecord;
73
+ export declare function builtinRestrictedRules(): Linter.RulesRecord;
86
74
  /**
87
75
  * Generates various "no-restricted-?" rules from the passed configuration.
88
76
  *
@@ -27,22 +27,19 @@ function createRulesRecord(generator) {
27
27
  /**
28
28
  * Merges built-in items and custom items for restricted rules.
29
29
  *
30
- * @param nativeDecorators
31
- * Whether to support native decorators in the code.
32
- *
33
30
  * @param restrictedItems
34
31
  * The custom items for restricted rules to be merged.
35
32
  *
36
33
  * @returns
37
34
  * The rules dictionary with all needed restricted rules.
38
35
  */
39
- function mergeRestrictedItems(nativeDecorators, ...restrictedItems) {
36
+ function mergeRestrictedItems(...restrictedItems) {
40
37
  const RESTRICTED_GLOBALS = [
41
38
  { name: "isFinite", message: "Use 'Number.isFinite' instead." },
42
39
  { name: "isNaN", message: "Use 'Number.isNaN' instead." },
43
40
  ];
44
41
  const RESTRICTED_SYNTAX = [
45
- { selector: ":matches(PropertyDefinition, MethodDefinition[kind!='constructor'])[accessibility='private']" + (nativeDecorators ? "" : "[decorators.length=0]"), message: "Use #private syntax instead." },
42
+ { selector: ":matches(PropertyDefinition, MethodDefinition[kind!='constructor'])[accessibility='private']", message: "Use #private syntax instead." },
46
43
  ];
47
44
  // restricted items for all files in the environment
48
45
  return {
@@ -56,15 +53,12 @@ function mergeRestrictedItems(nativeDecorators, ...restrictedItems) {
56
53
  /**
57
54
  * Creates restricted rules with built-in restricted items only.
58
55
  *
59
- * @param languageOptions
60
- * The language options passed with the Linter configuration.
61
- *
62
56
  * @returns
63
57
  * The rules dictionary with all needed restricted rules.
64
58
  */
65
- export function builtinRestrictedRules(languageOptions) {
59
+ export function builtinRestrictedRules() {
66
60
  // built-in restricted items
67
- const items = mergeRestrictedItems(!!languageOptions.nativeDecorators);
61
+ const items = mergeRestrictedItems();
68
62
  // generate the rules dictionary
69
63
  return createRulesRecord(key => items[key]);
70
64
  }
@@ -84,7 +78,7 @@ export function builtinRestrictedRules(languageOptions) {
84
78
  export function restrictedRulesConfig(envOptions, fixed) {
85
79
  const { restricted } = envOptions;
86
80
  // restricted items for all files in the environment
87
- const items = mergeRestrictedItems(!!restricted?.nativeDecorators, fixed, restricted);
81
+ const items = mergeRestrictedItems(fixed, restricted);
88
82
  // generate the configuration objects
89
83
  return concatConfigs(
90
84
  // base rules for all files in the environment
@@ -39,7 +39,6 @@ function configure(options?: ConfigureOptions): Linter.Config[]
39
39
  | `language` | `LanguageOptions` | | Configuration options for language and project setup. |
40
40
  | `language.ecmaVersion` | `number\|"latest"` | `latest` | The ECMAScript version to be used in the project (version or year). |
41
41
  | `language.sourceType` | `"module"\|"commonjs"` | `"module"` | Specifies how to treat `.js`, `.jsx`, `.ts`, and `.tsx` files. |
42
- | `language.nativeDecorators` | `boolean` | `false` | Whether to support native ES decorators in JavaScript code (via Babel plugin). Does not affect TypeScript code which uses an own parser aware of the decorator syntax. |
43
42
  | `packages` | `PackagesOptions` | | Configuration options for external package dependencies. |
44
43
  | `packages.allowed` | `string[]` | `[]` | Banned packages to be allowed in the project. |
45
44
  | `packages.banned` | `string[]` | `[]` | Additional packages to be banned in the project. |
@@ -101,3 +100,4 @@ The names of the environment presets are linked to detailed descriptions.
101
100
  | [`env.eslint`](./env/eslint.md) | Implementation of custom ESLint rules. |
102
101
  | [`env.project`](./env/project.md) | Project setup and module imports. |
103
102
  | [`env.tsconfig`](./env/tsconfig.md) | TypeScript project setup (per `tsconfig.json` file). Provided in case VSCode fails to use the new project service correctly. |
103
+ | [`env.decorators`](./env/decorators.md) | Support for native decorators in JavaScript source files. |
@@ -32,7 +32,6 @@ The option `restricted` is an object with the following properties:
32
32
  | `imports` | `EnvRestrictedName[]` | `[]` | Banned modules that must not be imported. Each entry is an object with string properties `name` and `message`. |
33
33
  | `properties` | `EnvRestrictedProperty[]` | `[]` | Banned object properties. Each entry is an object with string properties `object`, `property`, and `message`). |
34
34
  | `syntax` | `EnvRestrictedSyntax[]` | `[]` | Banned syntax constructs by [AST selectors](https://eslint.org/docs/latest/extend/selectors). Each entry is an object with string properties `selector` and `message`. |
35
- | `nativeDecorators` | `boolean` | `false` | Whether the environment supports native decorators. In this case, it will be required to replace decorated TypeScript `private` methods with native `#private` methods (TypeScript's experimental decorators cannot be used with native `#private` fields). |
36
35
  | `overrides` | `EnvRestrictedOverride[]` | `[]` | Overrides for specific subsets of files in the environment. Each entry is an object with the common properties `files` (required) and `ignores`, and the properties `globals`, `imports`, `properties`, and `syntax` to specify restricted items (see above). All restricted items of overrides will be merged with the common restricted items defined for the entire environment. |
37
36
 
38
37
  The environment `env.browser` already defines the following built-in restrictions:
@@ -0,0 +1,37 @@
1
+ # Environment `env.decorators`
2
+
3
+ Creates configuration objects to add support for native decorators to JavaScript source files.
4
+ Does not affect TypeScript source files which use an own parser aware of the decorator syntax.
5
+
6
+ Adds the following plugins:
7
+
8
+ - [@babel/eslint-parser](https://www.npmjs.com/package/@babel/eslint-parser)
9
+ - [@babel/plugin-proposal-decorators](https://babeljs.io/docs/babel-plugin-proposal-decorators)
10
+
11
+ ## Signature
12
+
13
+ ```ts
14
+ function decorators(options: EnvDecoratorsOptions): Linter.Config[]
15
+ ```
16
+
17
+ ## Options
18
+
19
+ | Name | Type | Default | Description |
20
+ | - | - | - | - |
21
+ | `files` | `string[]` | _required_ | Glob patterns for source files to be included. |
22
+ | `ignores` | `string[]` | `[]` | Glob patterns for source files matching `files` to be ignored. |
23
+ | `rules` | `Linter.RulesRecord` | `{}` | Additional linter rules to be added to the configuration. |
24
+
25
+ ## Example
26
+
27
+ ```js
28
+ // eslint.config.js
29
+ import { eslint } from "@open-xchange/linter-presets"
30
+
31
+ export default [
32
+ ...eslint.configure({ /* ... */ }),
33
+ ...eslint.env.decorators({
34
+ files: ["src/**/*.js"],
35
+ }),
36
+ ]
37
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-xchange/linter-presets",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "Configuration presets for ESLint and StyleLint",
5
5
  "repository": {
6
6
  "type": "git",