comment-variables 1.0.0 → 1.1.1

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
@@ -12,7 +12,7 @@ npm install -g comment-variables
12
12
 
13
13
  ## Commands
14
14
 
15
- **`comment-variables` (aliases `jscomments`/`comvar`) comes with three commands in this initial release:**
15
+ **`comment-variables` (aliases `jscomments`/`comvar`) comes with four commands in this initial release:**
16
16
 
17
17
  ```
18
18
  comment-variables
@@ -20,6 +20,12 @@ comment-variables
20
20
 
21
21
  Interacts with your root `comments.config.js` file's default exported object to print all the parameters you need to be aware of before running `compress` or `resolve`. Also acts as a dry run validation check. If no error is printed, it means you can run `compress` or `resolve` safely, as long as the printed parameters correspond to what you've expected from your defined config. (Additionally creates a resolved version of your config data as a JSON file. If no configuration file is found, a tutorial mode is triggered, generating a template config file for you.)
22
22
 
23
+ ```
24
+ comment-variables placeholders
25
+ ```
26
+
27
+ Creates Comment Variables placeholders right next to the single sources of truth where Comment Variables are defined. (See in config example below.)
28
+
23
29
  ```
24
30
  comment-variables compress
25
31
  ```
@@ -34,45 +40,63 @@ Scans your line and block comments for `$COMMENT#*` placeholders (like `$COMMENT
34
40
 
35
41
  _The `compress` and `resolve` commands make each other entirely reversible._
36
42
 
37
- **New command: `comment-variables placeholders`**
43
+ ## Flags
44
+
45
+ **The CLI tool comes with a single flag:**
38
46
 
39
47
  ```
40
- comment-variables placeholders
48
+ comment-variables --config <your-config.js>
41
49
  ```
42
50
 
43
- Creates Comment Variables placeholders right next to the single sources of truth where Comment Variables are defined. (See in config example below.)
51
+ Passes a different file as your config file instead of the default root `comments.config.js` file (like `comment-variables --config your-config.js`), through a path relative to the root of your project.
44
52
 
45
- ## Flags
53
+ _The --config flag can be composed with any of the commands:_
54
+
55
+ ```
56
+ comment-variables --config your-config.js
57
+ comment-variables compress --config your-config.js
58
+ comment-variables resolve --config your-config.js
59
+ comment-variables placeholders --config your-config.js
60
+ ```
46
61
 
47
- **The CLI tool also comes with three flags initially:**
62
+ ## Settings
63
+
64
+ **The config object requires the following settings:**
48
65
 
49
66
  ```
50
- comment-variables --config <your-config.js>
67
+ data:
51
68
  ```
52
69
 
53
- Passes a different file as your config file instead of the default root `comments.config.js` file (like `comment-variables --config your-config.js`), through a path relative to the root of your project.
70
+ Your dedicated object defining your Comment Variables through nested key-value pairs of string literals.
54
71
 
55
72
  ```
56
- --lint-config-imports is now part of the config at the `lintConfigImports` key
73
+ ignores:
57
74
  ```
58
75
 
59
- By default, `comment-variables` excludes your config file and all the (JavaScript/TypeScript) files it recursively imports. This config option cancels this mechanism, linting config imports. (The config file however still remains excluded from linting.)
76
+ Your dedicated array defining your files and folders to be ignored by the `compress` and `resolve` commands through strings of paths relative to the root of your project.
77
+
78
+ ## Options
79
+
80
+ **The config object supports the following options:**
60
81
 
61
82
  ```
62
- --my-ignores-only is now part of the config at the `myIgnoresOnly` key
83
+ lintConfigImports:
63
84
  ```
64
85
 
65
- By default, `comment-variables` includes a preset list of ignored folders (`"node_modules"`, `".next"`, `".react-router"`...). This config option cancels this mechanism so that you can have full control over your ignored files and folders.
86
+ By default, `comment-variables` excludes your config file and all the (JavaScript/TypeScript) files it recursively imports. Passing `true` to this config option cancels this mechanism, linting config imports. (The config file however still remains excluded from linting.)
66
87
 
67
- _The --config flag can be composed with any of the commands:_
88
+ ```
89
+ myIgnoresOnly:
90
+ ```
91
+
92
+ By default, `comment-variables` includes a preset list of ignored folders (`"node_modules"`, `".next"`, `".react-router"`...). Passing `true` to this config option cancels this mechanism so that you can have full control over your ignored files and folders.
68
93
 
69
94
  ```
70
- comment-variables --config your-config.js
71
- comment-variables compress --config your-config.js
72
- comment-variables resolve --config your-config.js
73
- comment-variables placeholders --config your-config.js
95
+ composedVariablesExclusives:
74
96
  ```
75
97
 
98
+ In due time, you may end up creating Comment Variables that are exclusively meant to be used to create other Comment Variables – the latter classified as composed variables. Passing an array to this config option, comprised of the keys of these original comment variables (for example, if a Comment Variable placeholder is `$COMMENT#COMMENT` its related key is `COMMENT`), prevents these original comment variables from being affected by the `compress` and `resolve` commands.
99
+
76
100
  ## **`comments.config.js`**
77
101
 
78
102
  A root `comments.config.js` file looks like this. (This is the config file I'm using to manage my JavaScript comments in this library.)
@@ -142,11 +166,14 @@ const ignores = ["README.md", "generate.template.js", "generate.example.js"];
142
166
  const lintConfigImports = false; // can be omitted
143
167
  const myIgnoresOnly = false; // can be omitted
144
168
 
169
+ const composedVariablesExclusives = []; // can be omitted
170
+
145
171
  const config = {
146
172
  data,
147
173
  ignores,
148
174
  lintConfigImports,
149
175
  myIgnoresOnly,
176
+ composedVariablesExclusives,
150
177
  };
151
178
 
152
179
  export default config;
@@ -74,6 +74,8 @@ const data = {
74
74
  "The flattened config data, with `$COMMENT` placeholders as keys and actual comments as values." /* $COMMENT#JSDOC#PARAMS#FLATTENEDCONFIGDATA */,
75
75
  reversedFlattenedConfigData:
76
76
  "The reversed flattened config data, with actual comments as keys and `$COMMENT` placeholders as values." /* $COMMENT#JSDOC#PARAMS#REVERSEDFLATTENEDCONFIGDATA */,
77
+ composedVariablesExclusives:
78
+ "The array of comment variables keys (implying their aliases as well) exclusively used to craft composed variables, that should be ignored by both the `resolve` and the `compress` commands." /* $COMMENT#JSDOC#PARAMS#COMPOSEDVARIABLESEXCLUSIVES */,
77
79
  aliases_flattenedKeys:
78
80
  "The dictionary that connects aliases to their original flattened keys in case an encountered placeholder is actually an alias." /* $COMMENT#JSDOC#PARAMS#ALIASES_FLATTENEDKEYS */,
79
81
  ruleName:
@@ -110,16 +112,17 @@ const data = {
110
112
 
111
113
  const ignores = ["README.md", "generate.template.js", "generate.example.js"];
112
114
 
113
- // FORMER CLI FLAGS NOW TO BE INCLUDED INSIDE THE CONFIG ITSELF
114
-
115
115
  const lintConfigImports = false; // can be omitted
116
116
  const myIgnoresOnly = false; // can be omitted
117
117
 
118
+ const composedVariablesExclusives = []; // can be omitted
119
+
118
120
  const config = {
119
121
  data,
120
122
  ignores,
121
123
  lintConfigImports,
122
124
  myIgnoresOnly,
125
+ composedVariablesExclusives,
123
126
  };
124
127
 
125
128
  export default config;
@@ -82,6 +82,11 @@
82
82
  "key": "JSDOC#PARAMS#REVERSEDFLATTENEDCONFIGDATA",
83
83
  "placeholder": "$COMMENT#JSDOC#PARAMS#REVERSEDFLATTENEDCONFIGDATA"
84
84
  },
85
+ "composedVariablesExclusives": {
86
+ "value": "The array of comment variables keys (implying their aliases as well) exclusively used to craft composed variables, that should be ignored by both the `resolve` and the `compress` commands.",
87
+ "key": "JSDOC#PARAMS#COMPOSEDVARIABLESEXCLUSIVES",
88
+ "placeholder": "$COMMENT#JSDOC#PARAMS#COMPOSEDVARIABLESEXCLUSIVES"
89
+ },
85
90
  "aliases_flattenedKeys": {
86
91
  "value": "The dictionary that connects aliases to their original flattened keys in case an encountered placeholder is actually an alias.",
87
92
  "key": "JSDOC#PARAMS#ALIASES_FLATTENEDKEYS",
@@ -20,11 +20,14 @@ const ignores = [];
20
20
  const lintConfigImports = false; // can be omitted
21
21
  const myIgnoresOnly = false; // can be omitted
22
22
 
23
+ const composedVariablesExclusives = []; // can be omitted
24
+
23
25
  const config = {
24
26
  data,
25
27
  ignores,
26
28
  lintConfigImports,
27
29
  myIgnoresOnly,
30
+ composedVariablesExclusives,
28
31
  };
29
32
 
30
33
  export default config;
@@ -3,10 +3,10 @@ import path from "path";
3
3
 
4
4
  import { cwd } from "comment-variables-resolve-config";
5
5
 
6
- // rule names
7
- export const resolveRuleName = "resolve";
8
- export const compressRuleName = "compress";
9
- export const placeholdersRuleName = "placeholders"; // rule?
6
+ // rule names (now inside "comment-variables-resolve-config")
7
+ // export const resolveRuleName = "resolve";
8
+ // export const compressRuleName = "compress";
9
+ // export const placeholdersRuleName = "placeholders"; // rule?
10
10
 
11
11
  // to prevent accidental changes
12
12
  export const hasPackageJson = fs.existsSync(path.join(cwd, "package.json"));
@@ -1,4 +1,7 @@
1
- import { resolveRuleName, compressRuleName } from "../constants/bases.js";
1
+ import {
2
+ resolveRuleName,
3
+ compressRuleName,
4
+ } from "comment-variables-resolve-config";
2
5
 
3
6
  import makeResolveRule from "../rules/resolve.js";
4
7
  import makeCompressRule from "../rules/compress.js";
@@ -7,14 +7,18 @@ import {
7
7
  /**
8
8
  * The utility that creates the compress rule based on the reversed flattened config data, used to transform actual comments into `$COMMENT` placeholders.
9
9
  * @param {{[key: string]: string}} reversedFlattenedConfigData The reversed flattened config data, with actual comments as keys and `$COMMENT` placeholders as values.
10
+ * @param {string[]} composedVariablesExclusives The array of comment variables keys (implying their aliases as well) exclusively used to craft composed variables, that should be ignored by both the `resolve` and the `compress` commands.
10
11
  * @returns The compress rule based on the reversed flattened config data.
11
12
  */
12
- const makeRule = (reversedFlattenedConfigData) => {
13
+ const makeRule = (reversedFlattenedConfigData, composedVariablesExclusives) => {
13
14
  /** The whole `reversedFlattenedConfigData` turned from an object to an array of key-value arrays sorted by the descending length of each key to prevent partial replacements. */
14
15
  const sortedReversedFlattenedConfigData = Object.entries(
15
16
  reversedFlattenedConfigData
16
17
  ).sort(([a], [b]) => b.length - a.length);
17
18
 
19
+ // makes a set out of composed variables exclusives
20
+ const composedVariablesExclusivesSet = new Set(composedVariablesExclusives);
21
+
18
22
  /** @type {import('@typescript-eslint/utils').TSESLint.RuleModule<typeof placeholderMessageId, []>} */
19
23
  const rule = {
20
24
  meta: {
@@ -44,6 +48,10 @@ const makeRule = (reversedFlattenedConfigData) => {
44
48
  resolvedValue,
45
49
  commentKey,
46
50
  ] of sortedReversedFlattenedConfigData) {
51
+ // NEW
52
+ // if (composedVariablesExclusives.some((e) => commentKey === e))
53
+ if (composedVariablesExclusivesSet.has(commentKey)) continue;
54
+
47
55
  const pattern = makeIsolatedStringRegex(resolvedValue);
48
56
 
49
57
  fixedText = fixedText.replace(pattern, () => {
@@ -6,10 +6,18 @@ import {
6
6
  /**
7
7
  * The utility that creates the resolve rule based on the flattened config data, used to transform `$COMMENT` placeholders into actual comments.
8
8
  * @param {{[key: string]: string}} flattenedConfigData The flattened config data, with `$COMMENT` placeholders as keys and actual comments as values.
9
+ * @param {string[]} composedVariablesExclusives The array of comment variables keys (implying their aliases as well) exclusively used to craft composed variables, that should be ignored by both the `resolve` and the `compress` commands.
9
10
  * @param {{[key: string]: string}} aliases_flattenedKeys The dictionary that connects aliases to their original flattened keys in case an encountered placeholder is actually an alias.
10
11
  * @returns The resolve rule based on the flattened config data.
11
12
  */
12
- const makeRule = (flattenedConfigData, aliases_flattenedKeys) => {
13
+ const makeRule = (
14
+ flattenedConfigData,
15
+ composedVariablesExclusives,
16
+ aliases_flattenedKeys
17
+ ) => {
18
+ // makes a set out of composed variables exclusives
19
+ const composedVariablesExclusivesSet = new Set(composedVariablesExclusives);
20
+
13
21
  /** @type {import('@typescript-eslint/utils').TSESLint.RuleModule<typeof placeholderMessageId, []>} */
14
22
  const rule = {
15
23
  meta: {
@@ -43,10 +51,20 @@ const makeRule = (flattenedConfigData, aliases_flattenedKeys) => {
43
51
 
44
52
  for (const match of matches) {
45
53
  const fullMatch = match[0]; // e.g. $COMMENT#LEVELONE#LEVELTWO
46
- const key = match[1]; // e.g. LEVELONE#LEVELTWO
47
- const replacement =
48
- flattenedConfigData[key] || // original
49
- flattenedConfigData[aliases_flattenedKeys?.[key]]; // alias
54
+ const rawKey = match[1]; // e.g. LEVELONE#LEVELTWO
55
+ const key =
56
+ aliases_flattenedKeys?.[rawKey] || // alias
57
+ rawKey; // original
58
+ const replacement = flattenedConfigData[key];
59
+
60
+ // NEW
61
+ // The idea is that only comment variables... Okay.
62
+ // The issue is that having a pattern is way too powerful, and can lead to unplanned inconsistencies. It is true that doing it instance by instance, comment variable by comment variable, is painstaking. But it's the more secure in order to fix an issue that is essentially purely cosmetic.
63
+ // Also, focusing exclusively on comment variables and barring aliases (and composed) solves many issues at once and can be checked within resolveConfig. // Done.
64
+
65
+ // if (replacement && composedVariablesExclusives.some((e) => key === e))
66
+ if (replacement && composedVariablesExclusivesSet.has(key)) continue;
67
+
50
68
  if (replacement) {
51
69
  fixedText = fixedText.replace(fullMatch, () => replacement);
52
70
  hasValidFix = true;
@@ -5,13 +5,13 @@ import {
5
5
  $COMMENT,
6
6
  commentVariablesPluginName,
7
7
  extractRuleName,
8
+ resolveRuleName,
9
+ compressRuleName,
8
10
  typeScriptAndJSXCompatible,
9
11
  extractObjectStringLiteralValues,
10
12
  } from "comment-variables-resolve-config";
11
13
 
12
14
  import {
13
- resolveRuleName,
14
- compressRuleName,
15
15
  allJSTSFileGlobs,
16
16
  allMDFileGlobs,
17
17
  allMDVirtualJSTSFileGlobs,
@@ -25,6 +25,7 @@ import { ruleNames_makeRules } from "../constants/rules.js";
25
25
  * @param {typeof resolveRuleName | typeof compressRuleName} ruleName The name of the rule currently used. (Either `"resolve"` or `"compress"`.)
26
26
  * @param {string[]} ignores The array of paths and globs for the flow's ESLint instance to ignore.
27
27
  * @param {{[key: string]: string}} flattenedConfigData Either the flattened config data or the reversed flattened config data, since they share the same structure.
28
+ * @param {string[]} composedVariablesExclusives The array of comment variables keys (implying their aliases as well) exclusively used to craft composed variables, that should be ignored by both the `resolve` and the `compress` commands.
28
29
  * @param {Record<string, string> | undefined} aliases_flattenedKeys The dictionary that connects aliases to their original flattened keys in case an encountered placeholder is actually an alias.
29
30
  * @returns
30
31
  */
@@ -32,6 +33,7 @@ const coreCommentsFlow = async (
32
33
  ruleName,
33
34
  ignores,
34
35
  flattenedConfigData,
36
+ composedVariablesExclusives,
35
37
  aliases_flattenedKeys
36
38
  ) => {
37
39
  const eslint = new ESLint({
@@ -48,6 +50,7 @@ const coreCommentsFlow = async (
48
50
  rules: {
49
51
  [ruleName]: ruleNames_makeRules[ruleName](
50
52
  flattenedConfigData,
53
+ composedVariablesExclusives,
51
54
  aliases_flattenedKeys
52
55
  ),
53
56
  },
@@ -105,18 +108,21 @@ const coreCommentsFlow = async (
105
108
  * The flow that resolves `$COMMENT` placeholders into actual comments.
106
109
  * @param {string[]} ignores The array of paths and globs for the flow's ESLint instance to ignore.
107
110
  * @param {Record<string, string>} flattenedConfigData The flattened config data, with `$COMMENT` placeholders as keys and actual comments as values.
111
+ * @param {string[]} composedVariablesExclusives The array of comment variables keys (implying their aliases as well) exclusively used to craft composed variables, that should be ignored by both the `resolve` and the `compress` commands.
108
112
  * @param {Record<string, string>} aliases_flattenedKeys The dictionary that connects aliases to their original flattened keys in case an encountered placeholder is actually an alias.
109
113
  * @returns
110
114
  */
111
115
  export const resolveCommentsFlow = async (
112
116
  ignores,
113
117
  flattenedConfigData,
118
+ composedVariablesExclusives,
114
119
  aliases_flattenedKeys
115
120
  ) =>
116
121
  coreCommentsFlow(
117
122
  resolveRuleName,
118
123
  ignores,
119
124
  flattenedConfigData,
125
+ composedVariablesExclusives,
120
126
  aliases_flattenedKeys
121
127
  );
122
128
 
@@ -126,12 +132,20 @@ export const resolveCommentsFlow = async (
126
132
  * The flow that compresses actual comments into `$COMMENT` placeholders.
127
133
  * @param {string[]} ignores The array of paths and globs for the flow's ESLint instance to ignore.
128
134
  * @param {{[key: string]: string}} reversedFlattenedConfigData The reversed flattened config data, with actual comments as keys and `$COMMENT` placeholders as values.
135
+ * @param {string[]} composedVariablesExclusives The array of comment variables keys (implying their aliases as well) exclusively used to craft composed variables, that should be ignored by both the `resolve` and the `compress` commands.
129
136
  * @returns
130
137
  */
131
138
  export const compressCommentsFlow = async (
132
139
  ignores,
133
- reversedFlattenedConfigData
134
- ) => coreCommentsFlow(compressRuleName, ignores, reversedFlattenedConfigData);
140
+ reversedFlattenedConfigData,
141
+ composedVariablesExclusives
142
+ ) =>
143
+ coreCommentsFlow(
144
+ compressRuleName,
145
+ ignores,
146
+ reversedFlattenedConfigData,
147
+ composedVariablesExclusives
148
+ );
135
149
 
136
150
  /* placeholdersCommentsFlow */
137
151
 
package/library/index.js CHANGED
@@ -9,19 +9,16 @@ import resolveConfig, {
9
9
  defaultConfigFileName,
10
10
  templateFileName,
11
11
  exampleFileName,
12
+ resolveRuleName,
13
+ compressRuleName,
14
+ placeholdersRuleName,
12
15
  configFlag,
13
16
  cwd,
14
17
  knownIgnores,
15
18
  makeResolvedConfigData,
16
19
  } from "comment-variables-resolve-config";
17
20
 
18
- import {
19
- hasPackageJson,
20
- hasGitFolder,
21
- resolveRuleName,
22
- compressRuleName,
23
- placeholdersRuleName,
24
- } from "./_commons/constants/bases.js";
21
+ import { hasPackageJson, hasGitFolder } from "./_commons/constants/bases.js";
25
22
 
26
23
  import { exitDueToFailure, logError } from "./_commons/utilities/helpers.js";
27
24
  import {
@@ -128,6 +125,7 @@ const {
128
125
  rawConfigAndImportPaths,
129
126
  lintConfigImports,
130
127
  myIgnoresOnly,
128
+ composedVariablesExclusives,
131
129
  } = resolveConfigResults;
132
130
 
133
131
  skipDetails || console.log("Running with config:", config);
@@ -140,9 +138,14 @@ skipDetails ||
140
138
  skipDetails || console.log("Aliases are:", aliases_flattenedKeys);
141
139
  skipDetails || console.log("Config path is:", configPath);
142
140
  skipDetails || console.log("Passed ignores are:", passedIgnores);
143
- // NEW
144
141
  skipDetails || console.log("lintConfigImports is:", lintConfigImports);
145
142
  skipDetails || console.log("myIgnoresOnly are:", myIgnoresOnly);
143
+ // NEW
144
+ skipDetails ||
145
+ console.log(
146
+ "Composed variables exclusives are:",
147
+ composedVariablesExclusives
148
+ );
146
149
 
147
150
  // ADDRESSES THE --lint-config-imports FLAG (lintConfigImports, no longer a flag), GIVEN THAT THE FILES IMPORTED BY THE CONFIG ARE IGNORED BY DEFAULT.
148
151
 
@@ -195,12 +198,17 @@ switch (coreCommand) {
195
198
  await resolveCommentsFlow(
196
199
  ignores,
197
200
  flattenedConfigData,
201
+ composedVariablesExclusives,
198
202
  aliases_flattenedKeys
199
203
  );
200
204
  break;
201
205
  case compressRuleName:
202
206
  console.log(`Running ${compressRuleName}...`);
203
- await compressCommentsFlow(ignores, reversedFlattenedConfigData);
207
+ await compressCommentsFlow(
208
+ ignores,
209
+ reversedFlattenedConfigData,
210
+ composedVariablesExclusives
211
+ );
204
212
  break;
205
213
  case placeholdersRuleName:
206
214
  console.log(`Running ${placeholdersRuleName}...`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "comment-variables",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
4
4
  "description": "A CLI tool for configuring, managing and maintaining JavaScript comments as JavaScript variables.",
5
5
  "bin": {
6
6
  "comment-variables": "./library/index.js",
@@ -30,7 +30,7 @@
30
30
  "type": "module",
31
31
  "dependencies": {
32
32
  "@eslint/markdown": "^6.5.0",
33
- "comment-variables-resolve-config": "^1.13.2",
33
+ "comment-variables-resolve-config": "^1.14.1",
34
34
  "eslint": "^9.29.0"
35
35
  },
36
36
  "devDependencies": {