@murky-web/simplelog 3.1.1 → 3.1.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.
@@ -1,4 +1,9 @@
1
- import { isConsoleObjectExpression, isLoggerLikeTarget } from "./utils.mjs";
1
+ import {
2
+ isConsoleObjectExpression,
3
+ isLoggerLikeTarget,
4
+ loggerNameHint,
5
+ oneLine
6
+ } from "./utils.mjs";
2
7
 
3
8
  export const noAdHocConsoleFallbackRule = {
4
9
  meta: {
@@ -10,13 +15,24 @@ export const noAdHocConsoleFallbackRule = {
10
15
  schema: [],
11
16
  messages: {
12
17
  noAdHocConsoleFallback:
13
- "Do not use console as an ad-hoc logger fallback or alias. Pass a SimpleLog logger or create one intentionally."
18
+ '`{{expression}}` logs through bare `console` whenever that branch is taken, so those lines skip the logger\'s level threshold, name and log file, and a call such as `.log(...)` has no `Logger` counterpart. Fix: `{{fix}}` with `import { Logger } from "@murky-web/simplelog/node";` (or `/bun`, `/web`), or make the logger a required parameter. Do not swap in `globalThis.console` or a `console` alias; both report too.'
14
19
  }
15
20
  },
16
21
  create(context) {
17
- function reportConsoleNode(node) {
22
+ function reportConsoleNode(node, owner) {
23
+ const { sourceCode } = context;
24
+ const text = sourceCode.getText(owner);
25
+ const start = node.range[0] - owner.range[0];
26
+ const end = node.range[1] - owner.range[0];
27
+ const replacement = `new Logger(${JSON.stringify(loggerNameHint(context))})`;
18
28
  context.report({
19
29
  messageId: "noAdHocConsoleFallback",
30
+ data: {
31
+ expression: oneLine(text),
32
+ fix: oneLine(
33
+ `${text.slice(0, start)}${replacement}${text.slice(end)}`
34
+ )
35
+ },
20
36
  node
21
37
  });
22
38
  }
@@ -28,20 +44,20 @@ export const noAdHocConsoleFallbackRule = {
28
44
  }
29
45
 
30
46
  if (isConsoleObjectExpression(node.left)) {
31
- reportConsoleNode(node.left);
47
+ reportConsoleNode(node.left, node);
32
48
  }
33
49
 
34
50
  if (isConsoleObjectExpression(node.right)) {
35
- reportConsoleNode(node.right);
51
+ reportConsoleNode(node.right, node);
36
52
  }
37
53
  },
38
54
  ConditionalExpression(node) {
39
55
  if (isConsoleObjectExpression(node.consequent)) {
40
- reportConsoleNode(node.consequent);
56
+ reportConsoleNode(node.consequent, node);
41
57
  }
42
58
 
43
59
  if (isConsoleObjectExpression(node.alternate)) {
44
- reportConsoleNode(node.alternate);
60
+ reportConsoleNode(node.alternate, node);
45
61
  }
46
62
  },
47
63
  VariableDeclarator(node) {
@@ -49,7 +65,7 @@ export const noAdHocConsoleFallbackRule = {
49
65
  isLoggerLikeTarget(node.id) &&
50
66
  isConsoleObjectExpression(node.init)
51
67
  ) {
52
- reportConsoleNode(node.init);
68
+ reportConsoleNode(node.init, node);
53
69
  }
54
70
  },
55
71
  AssignmentExpression(node) {
@@ -57,7 +73,7 @@ export const noAdHocConsoleFallbackRule = {
57
73
  isLoggerLikeTarget(node.left) &&
58
74
  isConsoleObjectExpression(node.right)
59
75
  ) {
60
- reportConsoleNode(node.right);
76
+ reportConsoleNode(node.right, node);
61
77
  }
62
78
  }
63
79
  };
@@ -1,9 +1,50 @@
1
1
  import {
2
+ CONSOLE_METHOD_TO_LOGGER_METHOD,
2
3
  isConsoleBindCall,
3
4
  isConsoleMethodReference,
4
- isConsoleObjectExpression
5
+ isConsoleObjectExpression,
6
+ oneLine
5
7
  } from "./utils.mjs";
6
8
 
9
+ function loggerMethodFor(consoleMethod) {
10
+ return CONSOLE_METHOD_TO_LOGGER_METHOD[consoleMethod] ?? "info";
11
+ }
12
+
13
+ /**
14
+ * The calls to write instead of the alias, from the alias' own names: `const {
15
+ * error } = console` becomes `logger.error(...)` in place of `error(...)`.
16
+ */
17
+ function describeReplacement(sourceCode, expression, target) {
18
+ if (isConsoleMethodReference(expression) || isConsoleBindCall(expression)) {
19
+ const reference = isConsoleBindCall(expression)
20
+ ? expression.callee.object
21
+ : expression;
22
+ const method = loggerMethodFor(reference.property.name);
23
+ return `\`logger.${method}(...)\` in place of \`${oneLine(sourceCode.getText(target))}(...)\``;
24
+ }
25
+
26
+ if (target?.type === "ObjectPattern") {
27
+ const calls = (target.properties ?? []).flatMap((property) => {
28
+ if (
29
+ property?.type !== "Property" ||
30
+ property.key?.type !== "Identifier" ||
31
+ property.value?.type !== "Identifier"
32
+ ) {
33
+ return [];
34
+ }
35
+
36
+ return [
37
+ `\`logger.${loggerMethodFor(property.key.name)}(...)\` in place of \`${property.value.name}(...)\``
38
+ ];
39
+ });
40
+ if (calls.length > 0) {
41
+ return calls.join(", ");
42
+ }
43
+ }
44
+
45
+ return `\`logger.<method>(...)\` in place of \`${oneLine(sourceCode.getText(target))}.<method>(...)\`, with \`log\` becoming \`info\` and \`trace\` becoming \`debug\``;
46
+ }
47
+
7
48
  export const noConsoleAliasRule = {
8
49
  meta: {
9
50
  type: "problem",
@@ -14,13 +55,18 @@ export const noConsoleAliasRule = {
14
55
  schema: [],
15
56
  messages: {
16
57
  noConsoleAlias:
17
- "Do not alias console or console methods. Use a SimpleLog logger instead."
58
+ "`{{alias}}` routes logging through `console` under another name, so `prefer-simplelog` no longer sees the calls and they skip the logger's level threshold, name and log file. Fix: delete it and call a SimpleLog logger: {{fix}}. Do not wrap `console` in an object or function of your own; that hides the same calls."
18
59
  }
19
60
  },
20
61
  create(context) {
21
- function report(node) {
62
+ function report(node, owner, target) {
63
+ const { sourceCode } = context;
22
64
  context.report({
23
65
  messageId: "noConsoleAlias",
66
+ data: {
67
+ alias: oneLine(sourceCode.getText(owner)),
68
+ fix: describeReplacement(sourceCode, node, target)
69
+ },
24
70
  node
25
71
  });
26
72
  }
@@ -28,17 +74,17 @@ export const noConsoleAliasRule = {
28
74
  return {
29
75
  VariableDeclarator(node) {
30
76
  if (isConsoleObjectExpression(node.init)) {
31
- report(node.init);
77
+ report(node.init, node, node.id);
32
78
  return;
33
79
  }
34
80
 
35
81
  if (isConsoleMethodReference(node.init)) {
36
- report(node.init);
82
+ report(node.init, node, node.id);
37
83
  return;
38
84
  }
39
85
 
40
86
  if (isConsoleBindCall(node.init)) {
41
- report(node.init);
87
+ report(node.init, node, node.id);
42
88
  return;
43
89
  }
44
90
 
@@ -46,22 +92,22 @@ export const noConsoleAliasRule = {
46
92
  node.id?.type === "ObjectPattern" &&
47
93
  isConsoleObjectExpression(node.init)
48
94
  ) {
49
- report(node.init);
95
+ report(node.init, node, node.id);
50
96
  }
51
97
  },
52
98
  AssignmentExpression(node) {
53
99
  if (isConsoleObjectExpression(node.right)) {
54
- report(node.right);
100
+ report(node.right, node, node.left);
55
101
  return;
56
102
  }
57
103
 
58
104
  if (isConsoleMethodReference(node.right)) {
59
- report(node.right);
105
+ report(node.right, node, node.left);
60
106
  return;
61
107
  }
62
108
 
63
109
  if (isConsoleBindCall(node.right)) {
64
- report(node.right);
110
+ report(node.right, node, node.left);
65
111
  }
66
112
  }
67
113
  };
@@ -1,4 +1,8 @@
1
- import { isInsideFunctionScope, isLoggerImportSource } from "./utils.mjs";
1
+ import {
2
+ isInsideFunctionScope,
3
+ isLoggerImportSource,
4
+ oneLine
5
+ } from "./utils.mjs";
2
6
 
3
7
  export const preferChildLoggerRule = {
4
8
  meta: {
@@ -10,7 +14,7 @@ export const preferChildLoggerRule = {
10
14
  schema: [],
11
15
  messages: {
12
16
  preferChildLogger:
13
- "Prefer logger.createChildLogger(...) over creating a new root Logger inside a nested function scope."
17
+ "`{{call}}` builds a new root logger on every call of the function around it, without a parent's name prefix or options (threshold, log file, colors). Fix: `logger.createChildLogger({{name}})` on the logger the caller passes in or the module's root logger, or `c.var.logger.createChildLogger({{name}})` inside a Hono handler."
14
18
  }
15
19
  },
16
20
  create(context) {
@@ -47,12 +51,21 @@ export const preferChildLoggerRule = {
47
51
  return;
48
52
  }
49
53
 
54
+ const [firstArgument] = node.arguments ?? [];
55
+ const data = {
56
+ call: oneLine(context.sourceCode.getText(node)),
57
+ name: firstArgument
58
+ ? oneLine(context.sourceCode.getText(firstArgument))
59
+ : '"child"'
60
+ };
61
+
50
62
  if (
51
63
  node.callee?.type === "Identifier" &&
52
64
  loggerConstructors.has(node.callee.name)
53
65
  ) {
54
66
  context.report({
55
67
  messageId: "preferChildLogger",
68
+ data,
56
69
  node: node.callee
57
70
  });
58
71
  return;
@@ -68,6 +81,7 @@ export const preferChildLoggerRule = {
68
81
  ) {
69
82
  context.report({
70
83
  messageId: "preferChildLogger",
84
+ data,
71
85
  node: node.callee.property
72
86
  });
73
87
  }
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  SIMPLELOG_LOG_METHODS,
3
- functionHasContextParam,
3
+ callWithCallee,
4
+ getContextParamName,
4
5
  getEnclosingFunction
5
6
  } from "./utils.mjs";
6
7
 
@@ -14,7 +15,7 @@ export const preferHonoContextLoggerRule = {
14
15
  schema: [],
15
16
  messages: {
16
17
  preferHonoContextLogger:
17
- "Prefer c.var.logger inside Hono handlers instead of calling a root logger directly."
18
+ '`{{call}}` logs through the root logger `{{logger}}` inside a Hono handler, so the line misses the request-scoped child logger and its request name. Fix: `{{fix}}`, with `app.use(createHonoLoggerMiddleware({ logger: {{logger}} }))` from `"@murky-web/simplelog/hono"` registered before the route.'
18
19
  }
19
20
  },
20
21
  create(context) {
@@ -99,12 +100,28 @@ export const preferHonoContextLoggerRule = {
99
100
  }
100
101
 
101
102
  const enclosingFunction = getEnclosingFunction(node);
102
- if (!functionHasContextParam(enclosingFunction)) {
103
+ const contextName = getContextParamName(enclosingFunction);
104
+ if (!contextName) {
103
105
  return;
104
106
  }
105
107
 
108
+ const loggerName = node.callee.object.name;
109
+ const method = node.callee.property.name;
106
110
  context.report({
107
111
  messageId: "preferHonoContextLogger",
112
+ data: {
113
+ call: callWithCallee(
114
+ context.sourceCode,
115
+ node,
116
+ `${loggerName}.${method}`
117
+ ),
118
+ fix: callWithCallee(
119
+ context.sourceCode,
120
+ node,
121
+ `${contextName}.var.logger.${method}`
122
+ ),
123
+ logger: loggerName
124
+ },
108
125
  node: node.callee.object
109
126
  });
110
127
  }
@@ -9,7 +9,7 @@ export const preferRuntimeEntryRule = {
9
9
  },
10
10
  schema: [],
11
11
  messages: {
12
- preferRuntimeEntry: `Prefer an explicit runtime entry over "${SIMPLELOG_PACKAGE}". Use a runtime-specific import like "@murky-web/simplelog/node", "@murky-web/simplelog/web", "@murky-web/simplelog/bun", or "@murky-web/simplelog/hono".`
12
+ preferRuntimeEntry: `\`"${SIMPLELOG_PACKAGE}"\` is the Node entry: it pulls \`node:fs\` into the bundle, which browsers and most edge runtimes do not have. Fix: \`"${SIMPLELOG_PACKAGE}/node"\` for the same Node logger, \`"${SIMPLELOG_PACKAGE}/bun"\` for Bun, or \`"${SIMPLELOG_PACKAGE}/web"\` for edge runtimes and browsers; \`createHonoLoggerMiddleware\` comes from \`"${SIMPLELOG_PACKAGE}/hono"\`.`
13
13
  }
14
14
  },
15
15
  create(context) {
@@ -1,11 +1,8 @@
1
- const CONSOLE_METHOD_TO_LOGGER_METHOD = {
2
- debug: "debug",
3
- error: "error",
4
- info: "info",
5
- log: "info",
6
- trace: "debug",
7
- warn: "warn"
8
- };
1
+ import {
2
+ callWithCallee,
3
+ CONSOLE_METHOD_TO_LOGGER_METHOD,
4
+ loggerNameHint
5
+ } from "./utils.mjs";
9
6
 
10
7
  export const preferSimplelogRule = {
11
8
  meta: {
@@ -17,7 +14,7 @@ export const preferSimplelogRule = {
17
14
  schema: [],
18
15
  messages: {
19
16
  preferSimplelog:
20
- "Prefer a SimpleLog logger over direct console.{{consoleMethod}}(). Use logger.{{loggerMethod}}(...) instead."
17
+ '`{{call}}` writes past SimpleLog, so the line skips the logger\'s level threshold, name and log file. Fix: `{{fix}}` on the module\'s logger (`import { Logger } from "@murky-web/simplelog/node"; const logger = new Logger("{{name}}");`, or the `/bun` or `/web` entry), or on `c.var.logger` inside a Hono handler. Do not alias `console` or add `// oxlint-disable` to keep the call.'
21
18
  }
22
19
  },
23
20
  create(context) {
@@ -53,8 +50,17 @@ export const preferSimplelogRule = {
53
50
  context.report({
54
51
  messageId: "preferSimplelog",
55
52
  data: {
56
- consoleMethod,
57
- loggerMethod
53
+ call: callWithCallee(
54
+ context.sourceCode,
55
+ node,
56
+ `console.${consoleMethod}`
57
+ ),
58
+ fix: callWithCallee(
59
+ context.sourceCode,
60
+ node,
61
+ `logger.${loggerMethod}`
62
+ ),
63
+ name: loggerNameHint(context)
58
64
  },
59
65
  node: node.callee.property
60
66
  });
@@ -1,3 +1,5 @@
1
+ import { loggerNameHint, oneLine } from "./utils.mjs";
2
+
1
3
  export const requireLoggerNameRule = {
2
4
  meta: {
3
5
  type: "problem",
@@ -8,7 +10,7 @@ export const requireLoggerNameRule = {
8
10
  schema: [],
9
11
  messages: {
10
12
  requireLoggerName:
11
- "Provide a non-empty logger name when creating a SimpleLog Logger."
13
+ "`{{call}}` has no name, so its lines print an empty logger name and cannot be told apart from other loggers' lines. Fix: `{{fix}}`, named after the module or service it logs for."
12
14
  }
13
15
  },
14
16
  create(context) {
@@ -60,10 +62,17 @@ export const requireLoggerNameRule = {
60
62
  return;
61
63
  }
62
64
 
65
+ const { sourceCode } = context;
66
+ const call = oneLine(sourceCode.getText(node));
67
+ const name = JSON.stringify(loggerNameHint(context));
63
68
  const [firstArgument] = node.arguments ?? [];
64
69
  if (!firstArgument) {
65
70
  context.report({
66
71
  messageId: "requireLoggerName",
72
+ data: {
73
+ call,
74
+ fix: `new ${sourceCode.getText(node.callee)}(${name})`
75
+ },
67
76
  node: node.callee
68
77
  });
69
78
  return;
@@ -74,8 +83,17 @@ export const requireLoggerNameRule = {
74
83
  typeof firstArgument.value === "string" &&
75
84
  firstArgument.value.trim().length === 0
76
85
  ) {
86
+ const start = firstArgument.range[0] - node.range[0];
87
+ const end = firstArgument.range[1] - node.range[0];
88
+ const text = sourceCode.getText(node);
77
89
  context.report({
78
90
  messageId: "requireLoggerName",
91
+ data: {
92
+ call,
93
+ fix: oneLine(
94
+ `${text.slice(0, start)}${name}${text.slice(end)}`
95
+ )
96
+ },
79
97
  node: firstArgument
80
98
  });
81
99
  }
package/oxlint/utils.mjs CHANGED
@@ -1,4 +1,12 @@
1
1
  const SIMPLELOG_PACKAGE = "@murky-web/simplelog";
2
+ const CONSOLE_METHOD_TO_LOGGER_METHOD = {
3
+ debug: "debug",
4
+ error: "error",
5
+ info: "info",
6
+ log: "info",
7
+ trace: "debug",
8
+ warn: "warn"
9
+ };
2
10
  const SIMPLELOG_LOG_METHODS = new Set([
3
11
  "debug",
4
12
  "error",
@@ -178,16 +186,49 @@ function getEnclosingFunction(node) {
178
186
  return null;
179
187
  }
180
188
 
181
- function functionHasContextParam(node) {
182
- if (!node) {
183
- return false;
189
+ /** The Hono context parameter's name (`c`, `ctx` or `context`), or `null`. */
190
+ function getContextParamName(node) {
191
+ for (const param of node?.params ?? []) {
192
+ const name = collectAssignedNames(param).find(
193
+ (candidate) =>
194
+ candidate === "c" ||
195
+ candidate === "ctx" ||
196
+ candidate === "context"
197
+ );
198
+ if (name) {
199
+ return name;
200
+ }
184
201
  }
185
202
 
186
- return (node.params ?? []).some((param) => {
187
- return collectAssignedNames(param).some(
188
- (name) => name === "c" || name === "ctx" || name === "context"
189
- );
190
- });
203
+ return null;
204
+ }
205
+
206
+ /** Source text on one line, for quoting code in a message. */
207
+ function oneLine(text) {
208
+ return text.replaceAll(/\s+/gu, " ");
209
+ }
210
+
211
+ /**
212
+ * The call with its callee replaced, so a message can hand back the fixed call.
213
+ * Arguments spread over several lines become `(…)`.
214
+ */
215
+ function callWithCallee(sourceCode, call, callee) {
216
+ const rest = sourceCode
217
+ .getText(call)
218
+ .slice(call.callee.range[1] - call.range[0]);
219
+ return `${callee}${rest.includes("\n") ? "(…)" : rest}`;
220
+ }
221
+
222
+ /**
223
+ * A logger name to suggest: the file's base name, or its directory's for an
224
+ * `index` file.
225
+ */
226
+ function loggerNameHint(context) {
227
+ const parts = (context.filename ?? context.getFilename?.() ?? "").split(
228
+ /[\\/]/u
229
+ );
230
+ const base = (parts.at(-1) ?? "").replace(/\..*$/u, "");
231
+ return (base === "index" ? parts.at(-2) : base) || "app";
191
232
  }
192
233
 
193
234
  function isTopLevelNode(node) {
@@ -195,7 +236,11 @@ function isTopLevelNode(node) {
195
236
  }
196
237
 
197
238
  export {
198
- functionHasContextParam,
239
+ callWithCallee,
240
+ CONSOLE_METHOD_TO_LOGGER_METHOD,
241
+ getContextParamName,
242
+ loggerNameHint,
243
+ oneLine,
199
244
  getEnclosingFunction,
200
245
  isBareSimplelogSource,
201
246
  isConsoleBindCall,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@murky-web/simplelog",
3
- "version": "3.1.1",
3
+ "version": "3.1.2",
4
4
  "private": false,
5
5
  "description": "A simple wrapper around console to make logging enjoyable again",
6
6
  "homepage": "https://github.com/MurkyTheMurloc/web_dev_tools/tree/main/packages/simplelog",
@@ -69,8 +69,8 @@
69
69
  "clean": "rm -rf dist",
70
70
  "lint": "oxlint -c ./.oxlintrc.jsonc --tsconfig ./tsconfig.json --type-aware src tsdown.config.ts",
71
71
  "lint:fix": "oxlint -c ./.oxlintrc.jsonc --tsconfig ./tsconfig.json --type-aware --fix src tsdown.config.ts",
72
- "format": "oxfmt -c ../../config/oxc/.oxfmtrc.jsonc src oxlint smoke tsdown.config.ts",
73
- "format:check": "oxfmt -c ../../config/oxc/.oxfmtrc.jsonc --check src oxlint smoke tsdown.config.ts",
72
+ "format": "oxfmt -c ../oxc-config/.oxfmtrc.jsonc src oxlint smoke tsdown.config.ts",
73
+ "format:check": "oxfmt -c ../oxc-config/.oxfmtrc.jsonc --check src oxlint smoke tsdown.config.ts",
74
74
  "smoke:oxlint": "bun ./smoke/oxlint/run-smoke.ts",
75
75
  "smoke:entries": "bun run build && bun ./smoke/entries/run.ts",
76
76
  "smoke:treeshake": "bun run build && bun ./smoke/treeshake/run.ts",