From 4469e0976bfee453643f43d3ec5f2ca98c11c772 Mon Sep 17 00:00:00 2001 From: kingwl Date: Fri, 9 Oct 2020 23:55:36 +0800 Subject: [PATCH] make linter happy --- src/compiler/checker.ts | 16 ++--- src/compiler/transformers/ts.ts | 69 +++++++++++-------- tests/baselines/reference/spreadEnum1.js | 25 +++++-- tests/baselines/reference/spreadEnum1.symbols | 17 ++++- tests/baselines/reference/spreadEnum1.types | 20 +++++- tests/cases/conformance/enums/spreadEnum1.ts | 6 +- 6 files changed, 101 insertions(+), 52 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2ada2656dfeaf..6153786984410 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1262,7 +1262,7 @@ namespace ts { } function combineSymbolTables(first: SymbolTable, second: SymbolTable): SymbolTable; - function combineSymbolTables(first: SymbolTable | undefined, second: SymbolTable | undefined): SymbolTable | undefined; + function combineSymbolTables(first: SymbolTable | undefined, second: SymbolTable | undefined): SymbolTable | undefined; function combineSymbolTables(first: SymbolTable | undefined, second: SymbolTable | undefined): SymbolTable | undefined { if (!first?.size) return second; if (!second?.size) return first; @@ -9472,7 +9472,7 @@ namespace ts { } } } - return memberTypeList + return memberTypeList; } function getDeclaredTypeOfEnum(symbol: Symbol): Type { @@ -10401,25 +10401,25 @@ namespace ts { } setStructuredTypeMembers(type, members, emptyArray, emptyArray, undefined, undefined); if (symbol.flags & SymbolFlags.Enum) { - const spreadEnumMembers: SpreadEnumMember[] = [] + const spreadEnumMembers: SpreadEnumMember[] = []; symbol.declarations.forEach(decl => { if (isEnumDeclaration(decl)) { decl.members.forEach(member => { if (isSpreadEnumMember(member)) { - spreadEnumMembers.push(member) + spreadEnumMembers.push(member); } - }) + }); } - }) + }); if (spreadEnumMembers.length) { spreadEnumMembers.forEach(member => { if (isIdentifier(member.name)) { - const sym = getResolvedSymbol(member.name) + const sym = getResolvedSymbol(member.name); if (sym.flags & SymbolFlags.Enum) { members = combineSymbolTables(members, getExportsOfSymbol(sym)); } } - }) + }); setStructuredTypeMembers(type, members, emptyArray, emptyArray, undefined, undefined); } } diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 3e2e789259915..387bce35636c1 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -2426,7 +2426,7 @@ namespace ts { const statements: Statement[] = []; startLexicalEnvironment(); - const members = map(node.members, transformEnumMember); + const members = map(node.members, transformEnumMemberLike); insertStatementsAfterStandardPrologue(statements, endLexicalEnvironment()); addRange(statements, members); @@ -2442,37 +2442,50 @@ namespace ts { * * @param member The enum member node. */ - function transformEnumMember(member: EnumMember): Statement { - // enums don't support computed properties - // we pass false as 'generateNameForComputedPropertyName' for a backward compatibility purposes - // old emitter always generate 'expression' part of the name as-is. - const name = getExpressionForPropertyName(member, /*generateNameForComputedPropertyName*/ false); - const valueExpression = transformEnumMemberDeclarationValue(member); - const innerAssignment = factory.createAssignment( - factory.createElementAccessExpression( - currentNamespaceContainerName, - name - ), - valueExpression - ); - const outerAssignment = valueExpression.kind === SyntaxKind.StringLiteral ? - innerAssignment : - factory.createAssignment( + function transformEnumMemberLike(member: EnumMemberLike): Statement { + if (isEnumMember(member)) { + // enums don't support computed properties + // we pass false as 'generateNameForComputedPropertyName' for a backward compatibility purposes + // old emitter always generate 'expression' part of the name as-is. + const name = getExpressionForPropertyName(member, /*generateNameForComputedPropertyName*/ false); + const valueExpression = transformEnumMemberDeclarationValue(member); + const innerAssignment = factory.createAssignment( factory.createElementAccessExpression( currentNamespaceContainerName, - innerAssignment + name ), - name + valueExpression ); - return setTextRange( - factory.createExpressionStatement( - setTextRange( - outerAssignment, - member - ) - ), - member - ); + const outerAssignment = valueExpression.kind === SyntaxKind.StringLiteral ? + innerAssignment : + factory.createAssignment( + factory.createElementAccessExpression( + currentNamespaceContainerName, + innerAssignment + ), + name + ); + return setTextRange( + factory.createExpressionStatement( + setTextRange( + outerAssignment, + member + ) + ), + member + ); + } + else { + return setTextRange( + factory.createExpressionStatement( + setTextRange( + emitHelpers().createAssignHelper([currentNamespaceContainerName, member.name as Identifier]), + member + ) + ), + member + ); + } } /** diff --git a/tests/baselines/reference/spreadEnum1.js b/tests/baselines/reference/spreadEnum1.js index f22528de6b111..ebd75c2b9e50f 100644 --- a/tests/baselines/reference/spreadEnum1.js +++ b/tests/baselines/reference/spreadEnum1.js @@ -14,10 +14,10 @@ declare let basic: BasicEvents; declare let adv: AdvEvents; -// adv = basic; +adv = basic; -// basic = BasicEvents.Start; -// basic = BasicEvents.Finish; +basic = BasicEvents.Start; +basic = BasicEvents.Finish; adv = AdvEvents.Start; adv = AdvEvents.Finish; @@ -28,6 +28,17 @@ adv = BasicEvents.Start; adv = BasicEvents.Finish; //// [spreadEnum1.js] +var __assign = (this && this.__assign) || function () { + __assign = Object.assign || function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; var BasicEvents; (function (BasicEvents) { BasicEvents["Start"] = "Start"; @@ -35,13 +46,13 @@ var BasicEvents; })(BasicEvents || (BasicEvents = {})); var AdvEvents; (function (AdvEvents) { - AdvEvents[AdvEvents["BasicEvents"] = void 0] = "BasicEvents"; + __assign(AdvEvents, BasicEvents); AdvEvents["Pause"] = "Pause"; AdvEvents["Resume"] = "Resume"; })(AdvEvents || (AdvEvents = {})); -// adv = basic; -// basic = BasicEvents.Start; -// basic = BasicEvents.Finish; +adv = basic; +basic = BasicEvents.Start; +basic = BasicEvents.Finish; adv = AdvEvents.Start; adv = AdvEvents.Finish; adv = AdvEvents.Pause; diff --git a/tests/baselines/reference/spreadEnum1.symbols b/tests/baselines/reference/spreadEnum1.symbols index 71f05a7bb32df..110b217867686 100644 --- a/tests/baselines/reference/spreadEnum1.symbols +++ b/tests/baselines/reference/spreadEnum1.symbols @@ -28,10 +28,21 @@ declare let adv: AdvEvents; >adv : Symbol(adv, Decl(spreadEnum1.ts, 13, 11)) >AdvEvents : Symbol(AdvEvents, Decl(spreadEnum1.ts, 3, 1)) -// adv = basic; +adv = basic; +>adv : Symbol(adv, Decl(spreadEnum1.ts, 13, 11)) +>basic : Symbol(basic, Decl(spreadEnum1.ts, 11, 11)) -// basic = BasicEvents.Start; -// basic = BasicEvents.Finish; +basic = BasicEvents.Start; +>basic : Symbol(basic, Decl(spreadEnum1.ts, 11, 11)) +>BasicEvents.Start : Symbol(BasicEvents.Start, Decl(spreadEnum1.ts, 0, 18)) +>BasicEvents : Symbol(BasicEvents, Decl(spreadEnum1.ts, 0, 0)) +>Start : Symbol(BasicEvents.Start, Decl(spreadEnum1.ts, 0, 18)) + +basic = BasicEvents.Finish; +>basic : Symbol(basic, Decl(spreadEnum1.ts, 11, 11)) +>BasicEvents.Finish : Symbol(BasicEvents.Finish, Decl(spreadEnum1.ts, 1, 20)) +>BasicEvents : Symbol(BasicEvents, Decl(spreadEnum1.ts, 0, 0)) +>Finish : Symbol(BasicEvents.Finish, Decl(spreadEnum1.ts, 1, 20)) adv = AdvEvents.Start; >adv : Symbol(adv, Decl(spreadEnum1.ts, 13, 11)) diff --git a/tests/baselines/reference/spreadEnum1.types b/tests/baselines/reference/spreadEnum1.types index 65a77a35b61b2..a8cc23ce09f95 100644 --- a/tests/baselines/reference/spreadEnum1.types +++ b/tests/baselines/reference/spreadEnum1.types @@ -32,10 +32,24 @@ declare let basic: BasicEvents; declare let adv: AdvEvents; >adv : AdvEvents -// adv = basic; +adv = basic; +>adv = basic : BasicEvents +>adv : AdvEvents +>basic : BasicEvents -// basic = BasicEvents.Start; -// basic = BasicEvents.Finish; +basic = BasicEvents.Start; +>basic = BasicEvents.Start : BasicEvents.Start +>basic : BasicEvents +>BasicEvents.Start : BasicEvents.Start +>BasicEvents : typeof BasicEvents +>Start : BasicEvents.Start + +basic = BasicEvents.Finish; +>basic = BasicEvents.Finish : BasicEvents.Finish +>basic : BasicEvents +>BasicEvents.Finish : BasicEvents.Finish +>BasicEvents : typeof BasicEvents +>Finish : BasicEvents.Finish adv = AdvEvents.Start; >adv = AdvEvents.Start : BasicEvents.Start diff --git a/tests/cases/conformance/enums/spreadEnum1.ts b/tests/cases/conformance/enums/spreadEnum1.ts index 962d3ded3be1a..4760e292c51cf 100644 --- a/tests/cases/conformance/enums/spreadEnum1.ts +++ b/tests/cases/conformance/enums/spreadEnum1.ts @@ -15,10 +15,10 @@ declare let basic: BasicEvents; declare let adv: AdvEvents; -// adv = basic; +adv = basic; -// basic = BasicEvents.Start; -// basic = BasicEvents.Finish; +basic = BasicEvents.Start; +basic = BasicEvents.Finish; adv = AdvEvents.Start; adv = AdvEvents.Finish;