@omnimod/plugin-moment-to-dayjs 0.1.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/LICENSE +21 -0
- package/README.md +26 -0
- package/dist/index.d.mts +13 -0
- package/dist/index.mjs +139 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 salnika
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# @omnimod/plugin-moment-to-dayjs
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@omnimod/plugin-moment-to-dayjs)
|
|
4
|
+
[](https://github.com/Salnika/omnimod/actions/workflows/ci.yml)
|
|
5
|
+
[](../../LICENSE)
|
|
6
|
+
|
|
7
|
+
omnimod plugin that migrates Moment.js imports and call sites to Day.js.
|
|
8
|
+
|
|
9
|
+
## Use With The CLI
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
omnimod run moment-to-dayjs "src/**/*.{ts,tsx,js,jsx}"
|
|
13
|
+
omnimod run moment-to-dayjs "src/**/*.{ts,tsx,js,jsx}" --write
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Use As A Library
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { momentToDayjs } from "@omnimod/plugin-moment-to-dayjs";
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Notes
|
|
23
|
+
|
|
24
|
+
The plugin rewrites the default Moment binding and compatible call sites. APIs
|
|
25
|
+
that require Day.js plugins, such as duration or relative-time helpers, are
|
|
26
|
+
reported for follow-up.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region src/plugin.d.ts
|
|
2
|
+
type MomentToDayjsOptions = Record<string, unknown>;
|
|
3
|
+
/**
|
|
4
|
+
* Migrate Moment.js to Day.js. Rewrites the `moment` import to `dayjs`, renames
|
|
5
|
+
* the default binding (and all its call sites) to `dayjs`, and rewrites type-only
|
|
6
|
+
* `{ Moment }` imports to `{ Dayjs }`. Chained methods are API-mirrored by Day.js,
|
|
7
|
+
* so once the base identifier is renamed they carry over unchanged. APIs that
|
|
8
|
+
* Day.js only exposes through a plugin (`utc`, `duration`, `.fromNow()`, …) are
|
|
9
|
+
* flagged with a diagnostic and a `// TODO(omnimod)` comment naming the plugin.
|
|
10
|
+
*/
|
|
11
|
+
declare const momentToDayjs: import("@omnimod/core").Plugin<MomentToDayjsOptions, unknown>;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { type MomentToDayjsOptions, momentToDayjs as default, momentToDayjs };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { definePlugin, walk } from "@omnimod/core";
|
|
2
|
+
import { cast } from "@omnimod/plugin-utils";
|
|
3
|
+
//#region src/plugin.ts
|
|
4
|
+
/** Static `moment.<name>()` helpers that Day.js only exposes via a plugin. */
|
|
5
|
+
const STATIC_PLUGIN_APIS = {
|
|
6
|
+
utc: "the `utc` plugin (`import utc from \"dayjs/plugin/utc\"; dayjs.extend(utc)`)",
|
|
7
|
+
duration: "the `duration` plugin (`import duration from \"dayjs/plugin/duration\"; dayjs.extend(duration)`)",
|
|
8
|
+
locale: "explicit locale loading (`import \"dayjs/locale/<name>\"; dayjs.locale(\"<name>\")`)",
|
|
9
|
+
tz: "the `timezone` plugin (`import timezone from \"dayjs/plugin/timezone\"; dayjs.extend(timezone)`)"
|
|
10
|
+
};
|
|
11
|
+
/** Instance methods (`moment().<name>()`) that Day.js only exposes via a plugin. */
|
|
12
|
+
const INSTANCE_PLUGIN_APIS = {
|
|
13
|
+
fromNow: "the `relativeTime` plugin (`import relativeTime from \"dayjs/plugin/relativeTime\"; dayjs.extend(relativeTime)`)",
|
|
14
|
+
from: "the `relativeTime` plugin (`import relativeTime from \"dayjs/plugin/relativeTime\"; dayjs.extend(relativeTime)`)",
|
|
15
|
+
to: "the `relativeTime` plugin (`import relativeTime from \"dayjs/plugin/relativeTime\"; dayjs.extend(relativeTime)`)",
|
|
16
|
+
calendar: "the `calendar` plugin (`import calendar from \"dayjs/plugin/calendar\"; dayjs.extend(calendar)`)",
|
|
17
|
+
isBetween: "the `isBetween` plugin (`import isBetween from \"dayjs/plugin/isBetween\"; dayjs.extend(isBetween)`)"
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Migrate Moment.js to Day.js. Rewrites the `moment` import to `dayjs`, renames
|
|
21
|
+
* the default binding (and all its call sites) to `dayjs`, and rewrites type-only
|
|
22
|
+
* `{ Moment }` imports to `{ Dayjs }`. Chained methods are API-mirrored by Day.js,
|
|
23
|
+
* so once the base identifier is renamed they carry over unchanged. APIs that
|
|
24
|
+
* Day.js only exposes through a plugin (`utc`, `duration`, `.fromNow()`, …) are
|
|
25
|
+
* flagged with a diagnostic and a `// TODO(omnimod)` comment naming the plugin.
|
|
26
|
+
*/
|
|
27
|
+
const momentToDayjs = definePlugin({
|
|
28
|
+
name: "moment-to-dayjs",
|
|
29
|
+
description: "Migrate Moment.js to Day.js.",
|
|
30
|
+
include: ["**/*.{ts,tsx,js,jsx,mts,cts}"],
|
|
31
|
+
transform(file) {
|
|
32
|
+
const momentImport = findMomentImport(file.program);
|
|
33
|
+
if (!momentImport) return;
|
|
34
|
+
const { node, defaultLocal } = momentImport;
|
|
35
|
+
file.magic.update(momentImport.source.start, momentImport.source.end, "\"dayjs\"");
|
|
36
|
+
for (const spec of node.specifiers) {
|
|
37
|
+
if (spec.type !== "ImportSpecifier") continue;
|
|
38
|
+
const named = cast(spec);
|
|
39
|
+
const imported = named.imported;
|
|
40
|
+
if (imported.type !== "Identifier") continue;
|
|
41
|
+
if (cast(imported).name === "Moment") {
|
|
42
|
+
file.magic.update(imported.start, imported.end, "Dayjs");
|
|
43
|
+
if (named.local.name === "Moment" && named.local.start === imported.start) renameTypeReferences(file, node, "Moment", "Dayjs");
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (defaultLocal === "moment") {
|
|
47
|
+
for (const spec of node.specifiers) {
|
|
48
|
+
if (spec.type !== "ImportDefaultSpecifier") continue;
|
|
49
|
+
const local = cast(spec).local;
|
|
50
|
+
file.magic.update(local.start, local.end, "dayjs");
|
|
51
|
+
}
|
|
52
|
+
renameValueReferences(file, node, "moment", "dayjs");
|
|
53
|
+
}
|
|
54
|
+
if (defaultLocal) reportPluginApis(file, defaultLocal, defaultLocal === "moment" ? "dayjs" : defaultLocal, momentImport);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
/** Find the `import … from "moment"` declaration, capturing its default local. */
|
|
58
|
+
function findMomentImport(program) {
|
|
59
|
+
const body = program.body ?? [];
|
|
60
|
+
for (const stmt of body) {
|
|
61
|
+
if (stmt.type !== "ImportDeclaration") continue;
|
|
62
|
+
const imp = cast(stmt);
|
|
63
|
+
if (imp.source.value !== "moment") continue;
|
|
64
|
+
let defaultLocal = null;
|
|
65
|
+
for (const spec of imp.specifiers) if (spec.type === "ImportDefaultSpecifier") defaultLocal = cast(spec).local.name;
|
|
66
|
+
return {
|
|
67
|
+
node: imp,
|
|
68
|
+
defaultLocal,
|
|
69
|
+
source: {
|
|
70
|
+
start: imp.source.start,
|
|
71
|
+
end: imp.source.end
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
/** True when `node`'s span lies inside the import declaration (skip those). */
|
|
78
|
+
function insideImport(importNode, node) {
|
|
79
|
+
return node.start >= importNode.start && node.end <= importNode.end;
|
|
80
|
+
}
|
|
81
|
+
/** Rewrite value-position `Identifier`/`JSXIdentifier` references from → to. */
|
|
82
|
+
function renameValueReferences(file, importNode, from, to) {
|
|
83
|
+
walk(file.program, (node) => {
|
|
84
|
+
if (node.type !== "Identifier" && node.type !== "JSXIdentifier") return;
|
|
85
|
+
if (insideImport(importNode, node)) return;
|
|
86
|
+
if (cast(node).name !== from) return;
|
|
87
|
+
file.magic.update(node.start, node.end, to);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/** Rewrite type-position `Identifier` references (e.g. `Moment` annotations). */
|
|
91
|
+
function renameTypeReferences(file, importNode, from, to) {
|
|
92
|
+
walk(file.program, (node) => {
|
|
93
|
+
if (node.type !== "Identifier") return;
|
|
94
|
+
if (insideImport(importNode, node)) return;
|
|
95
|
+
if (cast(node).name !== from) return;
|
|
96
|
+
file.magic.update(node.start, node.end, to);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Report (once each) any plugin-only Moment API used on the local binding.
|
|
101
|
+
* `origLocal` matches the untouched AST; `renamedBase` is what the code now reads.
|
|
102
|
+
*/
|
|
103
|
+
function reportPluginApis(file, origLocal, renamedBase, momentImport) {
|
|
104
|
+
const seenStatic = /* @__PURE__ */ new Set();
|
|
105
|
+
const seenInstance = /* @__PURE__ */ new Set();
|
|
106
|
+
walk(file.program, (node) => {
|
|
107
|
+
if (node.type !== "MemberExpression") return;
|
|
108
|
+
const member = cast(node);
|
|
109
|
+
if (member.computed || member.property.type !== "Identifier") return;
|
|
110
|
+
const prop = cast(member.property).name;
|
|
111
|
+
if (member.object.type === "Identifier" && cast(member.object).name === origLocal && prop in STATIC_PLUGIN_APIS && !insideImport(momentImport.node, node)) {
|
|
112
|
+
if (!seenStatic.has(prop)) {
|
|
113
|
+
seenStatic.add(prop);
|
|
114
|
+
emitTodo(file, node, `\`${renamedBase}.${prop}()\``, STATIC_PLUGIN_APIS[prop]);
|
|
115
|
+
}
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (prop in INSTANCE_PLUGIN_APIS && !seenInstance.has(prop)) {
|
|
119
|
+
seenInstance.add(prop);
|
|
120
|
+
emitTodo(file, node, `\`.${prop}()\``, INSTANCE_PLUGIN_APIS[prop]);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
/** Report a diagnostic and prepend a `// TODO(omnimod)` line above the statement. */
|
|
125
|
+
function emitTodo(file, node, label, requirement) {
|
|
126
|
+
const message = `${label} needs ${requirement} in Day.js.`;
|
|
127
|
+
file.report({
|
|
128
|
+
message,
|
|
129
|
+
severity: "warn"
|
|
130
|
+
});
|
|
131
|
+
const lineStart = statementLineStart(file.source, node.start);
|
|
132
|
+
file.magic.appendLeft(lineStart, `// TODO(omnimod): ${label} — add ${requirement}\n`);
|
|
133
|
+
}
|
|
134
|
+
/** Offset of the start of the source line containing `offset`. */
|
|
135
|
+
function statementLineStart(source, offset) {
|
|
136
|
+
return source.lastIndexOf("\n", offset - 1) + 1;
|
|
137
|
+
}
|
|
138
|
+
//#endregion
|
|
139
|
+
export { momentToDayjs as default, momentToDayjs };
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@omnimod/plugin-moment-to-dayjs",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "omnimod plugin: migrate Moment.js to Day.js.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"codemod",
|
|
7
|
+
"dayjs",
|
|
8
|
+
"migration",
|
|
9
|
+
"moment",
|
|
10
|
+
"omnimod-plugin"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://salnika.github.io/omnimod/",
|
|
13
|
+
"bugs": "https://github.com/salnika/omnimod/issues",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": "salnika",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/salnika/omnimod.git",
|
|
19
|
+
"directory": "packages/plugin-moment-to-dayjs"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": "./dist/index.mjs",
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@omnimod/core": "0.1.0",
|
|
34
|
+
"@omnimod/plugin-utils": "0.1.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/node": "^25.6.2",
|
|
38
|
+
"@typescript/native-preview": "7.0.0-dev.20260509.2",
|
|
39
|
+
"typescript": "^6.0.3",
|
|
40
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@0.2.2",
|
|
41
|
+
"vite-plus": "0.2.2"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "vp pack",
|
|
45
|
+
"dev": "vp pack --watch",
|
|
46
|
+
"test": "vp test",
|
|
47
|
+
"check": "vp check"
|
|
48
|
+
}
|
|
49
|
+
}
|