Skip to content

Commit 3dd465f

Browse files
committed
feat(ChangedPayload): Add "remove-ChangedPayload"
1 parent 012d84e commit 3dd465f

File tree

6 files changed

+111
-0
lines changed

6 files changed

+111
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { UseCase, ChangedPayload } from "almin";
2+
3+
export class ExampleUseCase extends UseCase {
4+
execute() {
5+
this.context.useCase(new ChangedPayload()).execute();
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { UseCase } from "almin";
2+
3+
export class ExampleUseCase extends UseCase {
4+
execute() {
5+
this.context.useCase({ type: "ChangedPayload" }).execute();
6+
}
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { Context, ChangedPayload } from "almin";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { Context } from "almin";
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// MIT © 2017 azu
2+
"use strict";
3+
const defineTest = require('jscodeshift/dist/testUtils').defineTest;
4+
const tests = [
5+
'import',
6+
'dispatch',
7+
];
8+
describe('remove-ChangedPayload', () => {
9+
tests.forEach(test =>
10+
defineTest(
11+
__dirname,
12+
'remove-ChangedPayload',
13+
{},
14+
`remove-ChangedPayload/${ test }`
15+
)
16+
);
17+
});

scripts/remove-ChangedPayload.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// MIT © 2017 azu
2+
"use strict";
3+
const isSourceAlmin = source => {
4+
if (!source) {
5+
return false;
6+
}
7+
return source.type === "Literal" && source.value === "almin";
8+
};
9+
const isChangedPayload = source => {
10+
if (!source) {
11+
return false;
12+
}
13+
return source.type === "Identifier" && source.name === "ChangedPayload";
14+
};
15+
/**
16+
* If found { ChangedPayload }, return ImportSpecifier
17+
* @param path
18+
* @returns {boolean}
19+
*/
20+
const isChangedPayloadImportSpecifier = path => {
21+
if (!path.node) {
22+
return false;
23+
}
24+
if (path.node.type !== "ImportSpecifier") {
25+
return false;
26+
}
27+
// from "almin"
28+
const parent = path.parent;
29+
if (!isSourceAlmin(parent.node.source)) {
30+
return false;
31+
}
32+
// import { ChangedPayload }
33+
return isChangedPayload(path.node.imported);
34+
};
35+
36+
function isNewExpressionChangedPayload(path) {
37+
const node = path.node;
38+
if (!node || node.type !== "NewExpression") {
39+
return false;
40+
}
41+
const expression = node;
42+
console.log("expression", expression);
43+
return (
44+
expression &&
45+
expression.type === "NewExpression" &&
46+
expression.callee &&
47+
expression.callee.type === "Identifier" &&
48+
expression.callee.name === "ChangedPayload"
49+
);
50+
}
51+
52+
module.exports = function transformer(file, api) {
53+
const j = api.jscodeshift;
54+
// Remove `import { ChangedPayload } from "almin"
55+
return (
56+
j(
57+
j(file.source)
58+
.find(j.ImportSpecifier)
59+
.filter(path => {
60+
return isChangedPayloadImportSpecifier(path);
61+
})
62+
.remove()
63+
.toSource()
64+
)
65+
// Rewrite new ChangedPayload() => { type: "ChangedPayload" }
66+
.find(j.NewExpression)
67+
.filter(path => {
68+
let newExpressionChangedPayload = isNewExpressionChangedPayload(path);
69+
console.log(newExpressionChangedPayload);
70+
return newExpressionChangedPayload;
71+
})
72+
.replaceWith(path => {
73+
return `{ type: "ChangedPayload" }`;
74+
R;
75+
})
76+
.toSource()
77+
);
78+
};

0 commit comments

Comments
 (0)