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

✨ feat(typescript): upgrade to 4.1 #39

Merged
merged 2 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['/node_modules/', '/fixtures/', 'util.ts', 'compiler.ts'],
testTimeout: 120 * 1000,
forceExit: true,
testPathIgnorePatterns: ['/node_modules/', '/fixtures/', 'util.ts', 'compiler.ts'],
coveragePathIgnorePatterns: ['/node_modules/', '.faas_debug_tmp', '__tests__'],
snapshotSerializers: ['jest-serializer-path', 'jest-snapshot-serializer-raw'],
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
"codecov": "^3.8.0",
"execa": "^4.0.3",
"husky": "^4.2.3",
"jest": "^25.1.0",
"jest": "^26.6.3",
"jest-serializer-path": "^0.1.15",
"jest-snapshot-serializer-raw": "^1.1.0",
"lerna": "latest",
"prettier": "^2.0.2",
"pretty-quick": "^3.1.0",
"ts-jest": "^25.2.1",
"typescript": "3"
"ts-jest": "^26.4.4",
"typescript": "^4.1.3"
},
"husky": {
"hooks": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"rootDir": "src",
"outDir": "dist",
"experimentalDecorators": true,
"emitDecoratorMetadata": true
"emitDecoratorMetadata": true,
"skipLibCheck": true
},
"include": ["./src/**/*.ts"]
}
1 change: 0 additions & 1 deletion packages/hooks-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"jest-snapshot-serializer-raw": "^1.1.0",
"memory-fs": "^0.5.0",
"ts-loader": "^8.0.11",
"typescript": "^3.9.3",
"webpack": "^4.42.1",
"webpack-chain": "^6.5.0"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"exclude": ["lib", "node_modules", "**/__tests__"],
"include": ["./src"]
"include": ["./src"],
"compilerOptions": {
"skipLibCheck": true
}
}
3 changes: 0 additions & 3 deletions packages/hooks-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
"type": "git",
"url": "git@github.com:midwayjs/hooks.git"
},
"devDependencies": {
"typescript": "^3.9.3"
},
"publishConfig": {
"access": "public"
},
Expand Down
3 changes: 1 addition & 2 deletions packages/hooks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"url": "git@github.com:midwayjs/hooks.git"
},
"devDependencies": {
"@midwayjs/faas": "2",
"typescript": "^3.9.3"
"@midwayjs/faas": "2"
},
"dependencies": {
"@midwayjs/hooks-request": "^1.0.2"
Expand Down
5 changes: 2 additions & 3 deletions packages/next-hooks-compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@midwayjs/fcli-plugin-invoke": "^1.2.29",
"@midwayjs/hooks": "^1.0.2",
"@midwayjs/hooks-shared": "^1.0.2",
"@midwayjs/mwcc": "^0.4.5",
"@midwayjs/mwcc": "^0.5.0",
"@midwayjs/serverless-invoke": "^1.2.12",
"@types/lodash": "^4.14.159",
"chalk": "^4.0.0",
Expand All @@ -36,8 +36,7 @@
"@types/debug": "^4.1.5",
"@types/jest": "^26.0.15",
"@types/path-is-inside": "^1.0.0",
"globby": "^11.0.0",
"typescript": "^3.9.3"
"globby": "^11.0.0"
},
"publishConfig": {
"access": "public"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ export const useAsyncArrow = async (name: string) => {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAsyncArrow = exports.useArrow = exports.useDemo = void 0;
exports.useDemo = function useDemo(name) { const _req_ctx_ = this; };
exports.useArrow = function useArrow(name) { const _req_ctx_ = this; exports.useDemo.bind(_req_ctx_)(name + '666'); };
exports.useAsyncArrow = async function useAsyncArrow(name) { const _req_ctx_ = this; await exports.useDemo.bind(_req_ctx_)(name + '666'); };
const useDemo = function useDemo(name) { const _req_ctx_ = this; };
exports.useDemo = useDemo;
const useArrow = function useArrow(name) { const _req_ctx_ = this; exports.useDemo.bind(_req_ctx_)(name + '666'); };
exports.useArrow = useArrow;
const useAsyncArrow = async function useAsyncArrow(name) { const _req_ctx_ = this; await exports.useDemo.bind(_req_ctx_)(name + '666'); };
exports.useAsyncArrow = useAsyncArrow;
//# sourceMappingURL=arrow.js.map
`;

Expand Down Expand Up @@ -212,15 +215,17 @@ export const useInner = async () => {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useInner = exports.useQuery = void 0;
exports.useQuery = function useQuery(id) { const _req_ctx_ = this; return ''; };
exports.useInner = async function useInner() {
const useQuery = function useQuery(id) { const _req_ctx_ = this; return ''; };
exports.useQuery = useQuery;
const useInner = async function useInner() {
const _req_ctx_ = this;
exports.useQuery.bind(_req_ctx_)('123');
const inner = () => {
console.log(exports.useQuery.bind(_req_ctx_)('123'));
};
inner();
};
exports.useInner = useInner;
//# sourceMappingURL=arrow-hook-ref-arrow-hook.js.map
`;

Expand All @@ -237,7 +242,8 @@ const functionExpression = function () {}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.hello = void 0;
exports.hello = function hello(name) { const _req_ctx_ = this; console.log(name); };
const hello = function hello(name) { const _req_ctx_ = this; console.log(name); };
exports.hello = hello;
const functionExpression = function () { };
//# sourceMappingURL=export-arrow.js.map
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"rootDir": "src",
"outDir": "dist",
"experimentalDecorators": true,
"emitDecoratorMetadata": true
"emitDecoratorMetadata": true,
"skipLibCheck": true
},
"include": ["./src/**/*.ts"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ export default {
const expression = node.expression

if (ts.isArrowFunction(expression)) {
return ts.createFunctionDeclaration(
return ts.factory.createFunctionDeclaration(
expression.decorators,
[
ts.createModifier(ts.SyntaxKind.ExportKeyword),
ts.createModifier(ts.SyntaxKind.DefaultKeyword),
ts.createModifier(ts.SyntaxKind.AsyncKeyword),
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword),
ts.factory.createModifier(ts.SyntaxKind.DefaultKeyword),
ts.factory.createModifier(ts.SyntaxKind.AsyncKeyword),
],
expression.asteriskToken,
ts.createIdentifier(DefaultKeyword),
ts.factory.createIdentifier(DefaultKeyword),
expression.typeParameters,
expression.parameters,
expression.type,
Expand All @@ -59,12 +59,12 @@ export default {
return node
}

return ts.updateFunctionDeclaration(
return ts.factory.updateFunctionDeclaration(
node,
node.decorators,
node.modifiers,
node.asteriskToken,
ts.createIdentifier(DefaultKeyword),
ts.factory.createIdentifier(DefaultKeyword),
node.typeParameters,
node.parameters,
node.type,
Expand All @@ -87,11 +87,13 @@ export default {
}

if (name) {
const body = ts.isBlock(node.body) ? node.body : ts.createBlock([ts.createReturn(node.body)])
return ts.createFunctionExpression(
const body = ts.isBlock(node.body)
? node.body
: ts.factory.createBlock([ts.factory.createReturnStatement(node.body)])
return ts.factory.createFunctionExpression(
node.modifiers,
node.asteriskToken,
ts.createIdentifier(name),
ts.factory.createIdentifier(name),
node.typeParameters,
node.parameters,
node.type,
Expand Down
19 changes: 13 additions & 6 deletions packages/next-hooks-compiler/src/plugin/create-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default {
addRoute(getSourceFilePath(node), parseFunctionConfig(node, functionName, isExportDefault))
}

return ts.updateFunctionDeclaration(
return ts.factory.updateFunctionDeclaration(
node,
node.decorators,
node.modifiers,
Expand Down Expand Up @@ -66,7 +66,7 @@ export default {
addRoute(getSourceFilePath(node), parseFunctionConfig(node, functionName, isExportDefault))
}

return ts.updateFunctionExpression(
return ts.factory.updateFunctionExpression(
node,
node.modifiers,
node.asteriskToken,
Expand Down Expand Up @@ -134,14 +134,21 @@ function createLambdaContext(block: ts.Block) {
return block
}

const expr = ts.createVariableStatement(
const expr = ts.factory.createVariableStatement(
undefined,
ts.createVariableDeclarationList(
[ts.createVariableDeclaration(ts.createIdentifier(HooksRequestContext), undefined, ts.createThis())],
ts.factory.createVariableDeclarationList(
[
ts.factory.createVariableDeclaration(
ts.factory.createIdentifier(HooksRequestContext),
undefined,
undefined,
ts.factory.createThis()
),
],
ts.NodeFlags.Const
)
)

const statements = [expr, ...block.statements]
return ts.createBlock(statements)
return ts.factory.createBlock(statements)
}
21 changes: 13 additions & 8 deletions packages/next-hooks-compiler/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ export function isHookName(s: string) {
}

export function isInsideLambdaOrHook(node: ts.Node) {
const topLevelParent = closetAncestorWhileKind(
node,
(kind, currentNode) => currentNode.parent.kind === ts.SyntaxKind.SourceFile
)
const topLevelParent = closetAncestorWhileKind(node, (ancestorKind, ancestorNode) => {
return ancestorNode.parent.kind === ts.SyntaxKind.SourceFile
// try {

// } catch (e) {
// console.log(node, ancestorNode)
// debugger
// }
})

const topLevelNode = getTopLevelNode(topLevelParent)
return isLambdaOrHook(topLevelNode, topLevelParent)
Expand Down Expand Up @@ -94,7 +99,7 @@ export function getTopLevelNameNode(node: ts.Node): ts.Identifier {
}

console.log('getTopLevelNameNode unsupported types ' + ts.SyntaxKind[node.kind])
return ts.createIdentifier('')
return ts.factory.createIdentifier('')
}

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

export function closetAncestor<T extends ts.Node = ts.Node>(node: ts.Node, kind: ts.SyntaxKind) {
return closetAncestorWhileKind(node, (currentKind) => currentKind === kind) as T
return closetAncestorWhileKind(node, (ancestorKind) => ancestorKind === kind) as T
}

export function closetAncestorWhileKind(
node: ts.Node,
condition: (kind: ts.SyntaxKind, currentNode?: ts.Node) => boolean
condition: (ancestorKind: ts.SyntaxKind, currentNode?: ts.Node) => boolean
) {
let parent = node.parent
while (parent != null) {
while (parent) {
if (condition(parent.kind, parent)) {
return parent
}
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"paths": {
"@midwayjs/*": ["./*/src"]
},
"composite": true
"composite": true,
"skipLibCheck": true
},
"exclude": ["dist", "lib", "node_modules", "**/__tests__"]
}
Loading