Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.

Commit bbd9afc

Browse files
committed
✨ feat(typescript): upgrade to 4.1
1 parent 01294d4 commit bbd9afc

File tree

15 files changed

+1254
-2009
lines changed

15 files changed

+1254
-2009
lines changed

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module.exports = {
22
preset: 'ts-jest',
33
testEnvironment: 'node',
4-
testPathIgnorePatterns: ['/node_modules/', '/fixtures/', 'util.ts', 'compiler.ts'],
54
testTimeout: 120 * 1000,
65
forceExit: true,
6+
testPathIgnorePatterns: ['/node_modules/', '/fixtures/', 'util.ts', 'compiler.ts'],
77
coveragePathIgnorePatterns: ['/node_modules/', '.faas_debug_tmp', '__tests__'],
88
snapshotSerializers: ['jest-serializer-path', 'jest-snapshot-serializer-raw'],
99
}

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
"codecov": "^3.8.0",
2626
"execa": "^4.0.3",
2727
"husky": "^4.2.3",
28-
"jest": "^25.1.0",
28+
"jest": "^26.6.3",
2929
"jest-serializer-path": "^0.1.15",
3030
"jest-snapshot-serializer-raw": "^1.1.0",
3131
"lerna": "latest",
3232
"prettier": "^2.0.2",
3333
"pretty-quick": "^3.1.0",
34-
"ts-jest": "^25.2.1",
35-
"typescript": "3"
34+
"ts-jest": "^26.4.4",
35+
"typescript": "^4.1.3"
3636
},
3737
"husky": {
3838
"hooks": {
@@ -41,5 +41,8 @@
4141
},
4242
"workspaces": [
4343
"packages/*"
44-
]
44+
],
45+
"resolutions": {
46+
"@midwayjs/mwcc": "^0.5.0-nightly.0"
47+
}
4548
}

packages/faas-cli-plugin-midway-hooks/src/__tests__/fixtures/package/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"rootDir": "src",
55
"outDir": "dist",
66
"experimentalDecorators": true,
7-
"emitDecoratorMetadata": true
7+
"emitDecoratorMetadata": true,
8+
"skipLibCheck": true
89
},
910
"include": ["./src/**/*.ts"]
1011
}

packages/hooks-loader/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
"jest-snapshot-serializer-raw": "^1.1.0",
3636
"memory-fs": "^0.5.0",
3737
"ts-loader": "^8.0.11",
38-
"typescript": "^3.9.3",
3938
"webpack": "^4.42.1",
4039
"webpack-chain": "^6.5.0"
4140
},
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
22
"exclude": ["lib", "node_modules", "**/__tests__"],
3-
"include": ["./src"]
3+
"include": ["./src"],
4+
"compilerOptions": {
5+
"skipLibCheck": true
6+
}
47
}

packages/hooks-shared/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
"type": "git",
2020
"url": "[email protected]:midwayjs/hooks.git"
2121
},
22-
"devDependencies": {
23-
"typescript": "^3.9.3"
24-
},
2522
"publishConfig": {
2623
"access": "public"
2724
},

packages/hooks/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
"url": "[email protected]:midwayjs/hooks.git"
2121
},
2222
"devDependencies": {
23-
"@midwayjs/faas": "2",
24-
"typescript": "^3.9.3"
23+
"@midwayjs/faas": "2"
2524
},
2625
"dependencies": {
2726
"@midwayjs/hooks-request": "^1.0.2"

packages/next-hooks-compiler/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"@midwayjs/fcli-plugin-invoke": "^1.2.29",
2121
"@midwayjs/hooks": "^1.0.2",
2222
"@midwayjs/hooks-shared": "^1.0.2",
23-
"@midwayjs/mwcc": "^0.4.5",
23+
"@midwayjs/mwcc": "^0.5.0-nightly.0",
2424
"@midwayjs/serverless-invoke": "^1.2.12",
2525
"@types/lodash": "^4.14.159",
2626
"chalk": "^4.0.0",
@@ -36,8 +36,7 @@
3636
"@types/debug": "^4.1.5",
3737
"@types/jest": "^26.0.15",
3838
"@types/path-is-inside": "^1.0.0",
39-
"globby": "^11.0.0",
40-
"typescript": "^3.9.3"
39+
"globby": "^11.0.0"
4140
},
4241
"publishConfig": {
4342
"access": "public"

packages/next-hooks-compiler/src/__tests__/__snapshots__/compile.test.ts.snap

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ export const useAsyncArrow = async (name: string) => {
1717
"use strict";
1818
Object.defineProperty(exports, "__esModule", { value: true });
1919
exports.useAsyncArrow = exports.useArrow = exports.useDemo = void 0;
20-
exports.useDemo = function useDemo(name) { const _req_ctx_ = this; };
21-
exports.useArrow = function useArrow(name) { const _req_ctx_ = this; exports.useDemo.bind(_req_ctx_)(name + '666'); };
22-
exports.useAsyncArrow = async function useAsyncArrow(name) { const _req_ctx_ = this; await exports.useDemo.bind(_req_ctx_)(name + '666'); };
20+
const useDemo = function useDemo(name) { const _req_ctx_ = this; };
21+
exports.useDemo = useDemo;
22+
const useArrow = function useArrow(name) { const _req_ctx_ = this; exports.useDemo.bind(_req_ctx_)(name + '666'); };
23+
exports.useArrow = useArrow;
24+
const useAsyncArrow = async function useAsyncArrow(name) { const _req_ctx_ = this; await exports.useDemo.bind(_req_ctx_)(name + '666'); };
25+
exports.useAsyncArrow = useAsyncArrow;
2326
//# sourceMappingURL=arrow.js.map
2427
`;
2528

@@ -212,15 +215,17 @@ export const useInner = async () => {
212215
"use strict";
213216
Object.defineProperty(exports, "__esModule", { value: true });
214217
exports.useInner = exports.useQuery = void 0;
215-
exports.useQuery = function useQuery(id) { const _req_ctx_ = this; return ''; };
216-
exports.useInner = async function useInner() {
218+
const useQuery = function useQuery(id) { const _req_ctx_ = this; return ''; };
219+
exports.useQuery = useQuery;
220+
const useInner = async function useInner() {
217221
const _req_ctx_ = this;
218222
exports.useQuery.bind(_req_ctx_)('123');
219223
const inner = () => {
220224
console.log(exports.useQuery.bind(_req_ctx_)('123'));
221225
};
222226
inner();
223227
};
228+
exports.useInner = useInner;
224229
//# sourceMappingURL=arrow-hook-ref-arrow-hook.js.map
225230
`;
226231

@@ -237,7 +242,8 @@ const functionExpression = function () {}
237242
"use strict";
238243
Object.defineProperty(exports, "__esModule", { value: true });
239244
exports.hello = void 0;
240-
exports.hello = function hello(name) { const _req_ctx_ = this; console.log(name); };
245+
const hello = function hello(name) { const _req_ctx_ = this; console.log(name); };
246+
exports.hello = hello;
241247
const functionExpression = function () { };
242248
//# sourceMappingURL=export-arrow.js.map
243249
`;

packages/next-hooks-compiler/src/__tests__/fixtures/with-controller/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"rootDir": "src",
55
"outDir": "dist",
66
"experimentalDecorators": true,
7-
"emitDecoratorMetadata": true
7+
"emitDecoratorMetadata": true,
8+
"skipLibCheck": true
89
},
910
"include": ["./src/**/*.ts"]
1011
}

packages/next-hooks-compiler/src/plugin/anonymous-function-to-named.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ export default {
2525
const expression = node.expression
2626

2727
if (ts.isArrowFunction(expression)) {
28-
return ts.createFunctionDeclaration(
28+
return ts.factory.createFunctionDeclaration(
2929
expression.decorators,
3030
[
31-
ts.createModifier(ts.SyntaxKind.ExportKeyword),
32-
ts.createModifier(ts.SyntaxKind.DefaultKeyword),
33-
ts.createModifier(ts.SyntaxKind.AsyncKeyword),
31+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword),
32+
ts.factory.createModifier(ts.SyntaxKind.DefaultKeyword),
33+
ts.factory.createModifier(ts.SyntaxKind.AsyncKeyword),
3434
],
3535
expression.asteriskToken,
36-
ts.createIdentifier(DefaultKeyword),
36+
ts.factory.createIdentifier(DefaultKeyword),
3737
expression.typeParameters,
3838
expression.parameters,
3939
expression.type,
@@ -59,12 +59,12 @@ export default {
5959
return node
6060
}
6161

62-
return ts.updateFunctionDeclaration(
62+
return ts.factory.updateFunctionDeclaration(
6363
node,
6464
node.decorators,
6565
node.modifiers,
6666
node.asteriskToken,
67-
ts.createIdentifier(DefaultKeyword),
67+
ts.factory.createIdentifier(DefaultKeyword),
6868
node.typeParameters,
6969
node.parameters,
7070
node.type,
@@ -87,11 +87,13 @@ export default {
8787
}
8888

8989
if (name) {
90-
const body = ts.isBlock(node.body) ? node.body : ts.createBlock([ts.createReturn(node.body)])
91-
return ts.createFunctionExpression(
90+
const body = ts.isBlock(node.body)
91+
? node.body
92+
: ts.factory.createBlock([ts.factory.createReturnStatement(node.body)])
93+
return ts.factory.createFunctionExpression(
9294
node.modifiers,
9395
node.asteriskToken,
94-
ts.createIdentifier(name),
96+
ts.factory.createIdentifier(name),
9597
node.typeParameters,
9698
node.parameters,
9799
node.type,

packages/next-hooks-compiler/src/plugin/create-lambda.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default {
3232
addRoute(getSourceFilePath(node), parseFunctionConfig(node, functionName, isExportDefault))
3333
}
3434

35-
return ts.updateFunctionDeclaration(
35+
return ts.factory.updateFunctionDeclaration(
3636
node,
3737
node.decorators,
3838
node.modifiers,
@@ -66,7 +66,7 @@ export default {
6666
addRoute(getSourceFilePath(node), parseFunctionConfig(node, functionName, isExportDefault))
6767
}
6868

69-
return ts.updateFunctionExpression(
69+
return ts.factory.updateFunctionExpression(
7070
node,
7171
node.modifiers,
7272
node.asteriskToken,
@@ -134,14 +134,21 @@ function createLambdaContext(block: ts.Block) {
134134
return block
135135
}
136136

137-
const expr = ts.createVariableStatement(
137+
const expr = ts.factory.createVariableStatement(
138138
undefined,
139-
ts.createVariableDeclarationList(
140-
[ts.createVariableDeclaration(ts.createIdentifier(HooksRequestContext), undefined, ts.createThis())],
139+
ts.factory.createVariableDeclarationList(
140+
[
141+
ts.factory.createVariableDeclaration(
142+
ts.factory.createIdentifier(HooksRequestContext),
143+
undefined,
144+
undefined,
145+
ts.factory.createThis()
146+
),
147+
],
141148
ts.NodeFlags.Const
142149
)
143150
)
144151

145152
const statements = [expr, ...block.statements]
146-
return ts.createBlock(statements)
153+
return ts.factory.createBlock(statements)
147154
}

packages/next-hooks-compiler/src/util.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ export function isHookName(s: string) {
2727
}
2828

2929
export function isInsideLambdaOrHook(node: ts.Node) {
30-
const topLevelParent = closetAncestorWhileKind(
31-
node,
32-
(kind, currentNode) => currentNode.parent.kind === ts.SyntaxKind.SourceFile
33-
)
30+
const topLevelParent = closetAncestorWhileKind(node, (ancestorKind, ancestorNode) => {
31+
return ancestorNode.parent.kind === ts.SyntaxKind.SourceFile
32+
// try {
33+
34+
// } catch (e) {
35+
// console.log(node, ancestorNode)
36+
// debugger
37+
// }
38+
})
3439

3540
const topLevelNode = getTopLevelNode(topLevelParent)
3641
return isLambdaOrHook(topLevelNode, topLevelParent)
@@ -94,7 +99,7 @@ export function getTopLevelNameNode(node: ts.Node): ts.Identifier {
9499
}
95100

96101
console.log('getTopLevelNameNode unsupported types ' + ts.SyntaxKind[node.kind])
97-
return ts.createIdentifier('')
102+
return ts.factory.createIdentifier('')
98103
}
99104

100105
export function isFunctionKind(kind: ts.SyntaxKind) {
@@ -108,15 +113,15 @@ export function isFunctionKind(kind: ts.SyntaxKind) {
108113
export type FunctionKind = ts.FunctionDeclaration | ts.ArrowFunction | ts.FunctionExpression
109114

110115
export function closetAncestor<T extends ts.Node = ts.Node>(node: ts.Node, kind: ts.SyntaxKind) {
111-
return closetAncestorWhileKind(node, (currentKind) => currentKind === kind) as T
116+
return closetAncestorWhileKind(node, (ancestorKind) => ancestorKind === kind) as T
112117
}
113118

114119
export function closetAncestorWhileKind(
115120
node: ts.Node,
116-
condition: (kind: ts.SyntaxKind, currentNode?: ts.Node) => boolean
121+
condition: (ancestorKind: ts.SyntaxKind, currentNode?: ts.Node) => boolean
117122
) {
118123
let parent = node.parent
119-
while (parent != null) {
124+
while (parent) {
120125
if (condition(parent.kind, parent)) {
121126
return parent
122127
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"paths": {
2222
"@midwayjs/*": ["./*/src"]
2323
},
24-
"composite": true
24+
"composite": true,
25+
"skipLibCheck": true
2526
},
2627
"exclude": ["dist", "lib", "node_modules", "**/__tests__"]
2728
}

0 commit comments

Comments
 (0)