comment-variables 0.0.2 → 0.0.4

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.
@@ -11,6 +11,10 @@ const config = {
11
11
  levelThreeAlso: "Also level three here.",
12
12
  levelThreeToo: "This too is level three.",
13
13
  // test: "LEVELONE#LEVELTWO#LEVELTHREE", // errors
14
+ [`level$Three#First
15
+ whitespace`]: `This is level three
16
+ with whitespaces. `,
17
+ testing: 2,
14
18
  },
15
19
  },
16
20
  };
@@ -90,7 +90,7 @@ export const resolveImportPath = (
90
90
  /* getSourceCodeFromFilePath */
91
91
 
92
92
  // ESLint configs language options
93
- const typeScriptAndJSXCompatible = {
93
+ export const typeScriptAndJSXCompatible = {
94
94
  // for compatibility with .ts and .tsx
95
95
  parser: tseslint.parser,
96
96
  // for compatibility with JSX
package/index.js CHANGED
@@ -7,11 +7,14 @@ import fs from "fs";
7
7
  import { ESLint } from "eslint";
8
8
 
9
9
  import { runWithConfig } from "./run-with-config.js";
10
- import { findAllImports } from "./find-all-imports.js";
10
+ import {
11
+ findAllImports,
12
+ typeScriptAndJSXCompatible,
13
+ } from "./find-all-imports.js";
11
14
 
12
15
  const cwd = process.cwd();
13
16
 
14
- // ENSURES THE CLI TOOL ONLY RUN IN FOLDER THAT POSSESS A package.json FILE AND A .git FOLDER.
17
+ // ENSURES THE CLI TOOL ONLY RUN IN FOLDERS THAT POSSESS A package.json FILE AND A .git FOLDER.
15
18
 
16
19
  const hasPackageJson = fs.existsSync(path.join(cwd, "package.json"));
17
20
  if (!hasPackageJson) {
@@ -166,10 +169,7 @@ async function resolveCommentsInProject(fileGlobs = allJSTSFileGlobs) {
166
169
  {
167
170
  files: fileGlobs,
168
171
  ignores: [...configIgnores, ...knownIgnores], // 🚫 Ensure config isn't linted
169
- languageOptions: {
170
- ecmaVersion: "latest",
171
- sourceType: "module",
172
- },
172
+ languageOptions: typeScriptAndJSXCompatible,
173
173
  plugins: {
174
174
  "js-comments": {
175
175
  rules: {
@@ -233,17 +233,6 @@ const makeReverseJsCommentsRule = (reversedFlattenedConfig) => {
233
233
  resolvedValue,
234
234
  commentKey,
235
235
  ] of sortedReversedFlattenedConfig) {
236
- // if (fixedText.includes(resolvedValue)) {
237
- // fixedText = fixedText.replaceAll(
238
- // resolvedValue,
239
- // `$COMMENT#${commentKey}`
240
- // );
241
- // modified = true;
242
- // }
243
- // }
244
-
245
- // if (modified) {
246
-
247
236
  const pattern = new RegExp(
248
237
  `(?<=\\s|^)${escapeRegex(resolvedValue)}(?=\\s|$)`,
249
238
  "g"
@@ -288,10 +277,7 @@ async function compressCommentsInProject(fileGlobs = allJSTSFileGlobs) {
288
277
  {
289
278
  files: fileGlobs,
290
279
  ignores: [...configIgnores, ...knownIgnores], // 🚫 Ensure config isn't linted
291
- languageOptions: {
292
- ecmaVersion: "latest",
293
- sourceType: "module",
294
- },
280
+ languageOptions: typeScriptAndJSXCompatible,
295
281
  plugins: {
296
282
  "js-comments": {
297
283
  rules: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "comment-variables",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "",
5
5
  "bin": {
6
6
  "jscomments": "./index.js",
@@ -15,7 +15,7 @@ function flattenConfig(
15
15
  normalizedPath = currentPath
16
16
  .map((k) => k.toUpperCase())
17
17
  .join("#")
18
- .replaceAll(" ", "_"); // spaces are replaced by underscores
18
+ .replace(/\s/g, "_"); // whitespaces are replaced by underscores
19
19
 
20
20
  if (typeof value === "string") {
21
21
  if (map[normalizedPath]) {
@@ -59,13 +59,7 @@ function flattenConfig(
59
59
  return { flattenedConfig, reversedFlattenedConfig };
60
60
  }
61
61
 
62
- export async function runWithConfig(rawConfigPath) {
63
- // const __filename = fileURLToPath(import.meta.url);
64
- // const __dirname = dirname(__filename);
65
-
66
- // const configPath = join(__dirname, rawConfigPath);
67
- const configPath = rawConfigPath;
68
-
62
+ export async function runWithConfig(configPath) {
69
63
  // Step 1: Check if config file exists
70
64
  if (!existsSync(configPath)) {
71
65
  console.warn("No config file found. Exiting gracefully.");
@@ -82,13 +76,59 @@ export async function runWithConfig(rawConfigPath) {
82
76
  return null;
83
77
  }
84
78
 
85
- const RecursiveObject = z.lazy(() =>
86
- z.record(z.union([z.string(), RecursiveObject]))
87
- );
79
+ const RecursiveObject = z
80
+ .lazy(() =>
81
+ z.record(
82
+ z.any().superRefine((val, ctx) => {
83
+ if (typeof val === "string") {
84
+ return;
85
+ }
86
+
87
+ if (typeof val === "object" && val !== null && !Array.isArray(val)) {
88
+ const parsed = RecursiveObject.safeParse(val);
89
+ if (!parsed.success) {
90
+ for (const issue of parsed.error.issues) {
91
+ ctx.addIssue({
92
+ ...issue,
93
+ path: [...ctx.path, ...issue.path], // proper path propagation
94
+ });
95
+ }
96
+ }
97
+ return;
98
+ }
99
+
100
+ ctx.addIssue({
101
+ code: z.ZodIssueCode.custom,
102
+ message: `Value \`${val}\` of type "${typeof val}" should be a string or a nested object.`,
103
+ path: ctx.path,
104
+ });
105
+ })
106
+ )
107
+ )
108
+ .superRefine((obj, ctx) => {
109
+ for (const key of Object.keys(obj)) {
110
+ if (key.includes("$")) {
111
+ ctx.addIssue({
112
+ code: z.ZodIssueCode.custom,
113
+ message: `Key "${key}" should not include the "$" character.`,
114
+ path: [key],
115
+ });
116
+ }
117
+ if (key.includes("#")) {
118
+ ctx.addIssue({
119
+ code: z.ZodIssueCode.custom,
120
+ message: `Key "${key}" should not include the "#" character.`,
121
+ path: [key],
122
+ });
123
+ }
124
+ }
125
+ });
126
+
88
127
  const result = RecursiveObject.safeParse(config);
89
128
 
90
129
  if (!result.success) {
91
130
  console.warn("Config could not pass validation from zod.");
131
+ result.error.errors.map((e) => console.log(e.message));
92
132
  return null;
93
133
  }
94
134