|
| 1 | +/** |
| 2 | + * Copyright (c) 2021 NumberFour AG. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * NumberFour AG - Initial API and implementation |
| 10 | + */ |
| 11 | +package org.eclipse.n4js.transpiler.dts.transform; |
| 12 | + |
| 13 | +import static org.eclipse.n4js.transpiler.TranspilerBuilderBlocks._StringLiteral; |
| 14 | + |
| 15 | +import org.eclipse.n4js.n4JS.ExportDeclaration; |
| 16 | +import org.eclipse.n4js.n4JS.Expression; |
| 17 | +import org.eclipse.n4js.n4JS.Literal; |
| 18 | +import org.eclipse.n4js.n4JS.N4EnumDeclaration; |
| 19 | +import org.eclipse.n4js.n4JS.N4EnumLiteral; |
| 20 | +import org.eclipse.n4js.n4JS.ScriptElement; |
| 21 | +import org.eclipse.n4js.n4JS.StringLiteral; |
| 22 | +import org.eclipse.n4js.transpiler.Transformation; |
| 23 | +import org.eclipse.n4js.transpiler.im.SymbolTableEntry; |
| 24 | +import org.eclipse.n4js.transpiler.im.SymbolTableEntryOriginal; |
| 25 | +import org.eclipse.n4js.transpiler.utils.TranspilerUtils; |
| 26 | +import org.eclipse.n4js.ts.types.TEnumLiteral; |
| 27 | +import org.eclipse.n4js.utils.N4JSLanguageUtils; |
| 28 | +import org.eclipse.n4js.utils.N4JSLanguageUtils.EnumKind; |
| 29 | + |
| 30 | +/** |
| 31 | + * Transformer to add initializers to string-/number-based enums |
| 32 | + */ |
| 33 | +public class EnumAddMissingInitializersTransformation extends Transformation { |
| 34 | + |
| 35 | + @Override |
| 36 | + public void assertPreConditions() { |
| 37 | + // empty |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void assertPostConditions() { |
| 42 | + // empty |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void analyze() { |
| 47 | + // ignore |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void transform() { |
| 52 | + makeInferredTypesExplicit(); |
| 53 | + } |
| 54 | + |
| 55 | + private void makeInferredTypesExplicit() { |
| 56 | + for (ScriptElement rootElemRaw : getState().im.getScriptElements()) { |
| 57 | + ScriptElement rootElem = (rootElemRaw instanceof ExportDeclaration) |
| 58 | + ? ((ExportDeclaration) rootElemRaw).getExportedElement() |
| 59 | + : rootElemRaw; |
| 60 | + |
| 61 | + if (rootElem instanceof N4EnumDeclaration) { |
| 62 | + N4EnumDeclaration enumDecl = (N4EnumDeclaration) rootElem; |
| 63 | + EnumKind enumKind = N4JSLanguageUtils.getEnumKind(enumDecl); |
| 64 | + boolean isLiteralBased = enumKind != EnumKind.Normal; |
| 65 | + boolean isPartiallyInitialized = false; |
| 66 | + for (N4EnumLiteral literal : enumDecl.getLiterals()) { |
| 67 | + Expression valueExpr = literal.getValueExpression(); |
| 68 | + isPartiallyInitialized = isPartiallyInitialized |
| 69 | + || (valueExpr != null && valueExpr instanceof StringLiteral); |
| 70 | + |
| 71 | + if (valueExpr == null) { |
| 72 | + if (isPartiallyInitialized) { |
| 73 | + literal.setValueExpression(_StringLiteral(literal.getName())); |
| 74 | + } else if (isLiteralBased) { |
| 75 | + SymbolTableEntry propSTE = getState().steCache.mapNamedElement_2_STE.get(literal); |
| 76 | + TEnumLiteral tLiteral = (propSTE instanceof SymbolTableEntryOriginal) |
| 77 | + ? (TEnumLiteral) ((SymbolTableEntryOriginal) propSTE).getOriginalTarget() |
| 78 | + : null; |
| 79 | + Literal newLit = TranspilerUtils.enumLiteralToNumericOrStringLiteral(tLiteral); |
| 80 | + literal.setValueExpression(newLit); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments