Skip to content

Commit

Permalink
2.10.14: new JS keyword logic (#1461)
Browse files Browse the repository at this point in the history
  • Loading branch information
larshp authored Sep 5, 2024
1 parent 307d9dc commit 0375cac
Show file tree
Hide file tree
Showing 19 changed files with 207 additions and 45 deletions.
18 changes: 9 additions & 9 deletions packages/cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@abaplint/transpiler-cli",
"version": "2.10.11",
"version": "2.10.14",
"description": "Transpiler - Command Line Interface",
"funding": "https://github.com/sponsors/larshp",
"bin": {
Expand All @@ -26,7 +26,7 @@
"author": "abaplint",
"license": "MIT",
"devDependencies": {
"@abaplint/transpiler": "^2.10.11",
"@abaplint/transpiler": "^2.10.14",
"@types/glob": "^8.1.0",
"glob": "=7.2.0",
"@types/progress": "^2.0.7",
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@abaplint/runtime",
"version": "2.10.11",
"version": "2.10.14",
"description": "Transpiler - Runtime",
"main": "build/src/index.js",
"typings": "build/src/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions packages/transpiler/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/transpiler/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@abaplint/transpiler",
"version": "2.10.11",
"version": "2.10.14",
"description": "Transpiler",
"main": "build/src/index.js",
"typings": "build/src/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/transpiler/src/expressions/field_chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class FieldChainTranspiler implements IExpressionTranspiler {
if (c.get() instanceof Expressions.SourceField
|| c.get() instanceof Expressions.Field) {
const name = traversal.prefixAndName(c.getFirstToken(), filename).replace("~", "$");
ret.append(Traversal.escapeNamespace(name)!, c, traversal);
ret.append(Traversal.prefixVariable(Traversal.escapeNamespace(name)!), c, traversal);
} else if (c instanceof Nodes.ExpressionNode && c.get() instanceof Expressions.SourceFieldSymbol) {
ret.appendChunk(new FieldSymbolTranspiler().transpile(c, traversal));
} else if (c instanceof Nodes.ExpressionNode && c.get() instanceof Expressions.ClassName) {
Expand Down
2 changes: 1 addition & 1 deletion packages/transpiler/src/expressions/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class TargetTranspiler implements IExpressionTranspiler {

if (c.get() instanceof Expressions.TargetField) {
const prefix = traversal.prefixAndName(c.getFirstToken()).replace("~", "$");
ret.append(Traversal.escapeNamespace(prefix)!, c, traversal);
ret.append(Traversal.prefixVariable(Traversal.escapeNamespace(prefix)!), c, traversal);
} else if (c.get() instanceof Expressions.ClassName) {
const name = traversal.lookupClassOrInterface(c.getFirstToken().getStr(), c.getFirstToken());
ret.append(name, c, traversal);
Expand Down
12 changes: 8 additions & 4 deletions packages/transpiler/src/keywords.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as abaplint from "@abaplint/core";

// https://www.w3schools.com/js/js_reserved.asp
export const defaultKeywords: string[] = [
export const DEFAULT_KEYWORDS: string[] = [
"abstract", "arguments", "await",
"break", "byte", "catch",
"char", "class", "const", "continue",
// "char",
"class", "const", "continue",
"debugger", "default", "do",
"double", "else", "enum", "eval",
"export", "extends", "false", "final",
Expand All @@ -17,9 +18,9 @@ export const defaultKeywords: string[] = [
"switch", "synchronized", "this",
"throw", "throws", "transient", "true",
"try", "typeof", "var", "void",
"delete",
"volatile", "while", "yield"];
// "with"
// "delete"

/** Replaces javascript keywords in ABAP source code, in-memory only */
export class Keywords {
Expand All @@ -29,11 +30,14 @@ export class Keywords {
if (keywords !== undefined) {
this.keywords = keywords;
} else {
this.keywords = defaultKeywords;
this.keywords = DEFAULT_KEYWORDS;
}
}

public handle(reg: abaplint.IRegistry) {
if (this.keywords.length === 0) {
return;
}
reg.parse();

for (const o of reg.getObjects()) {
Expand Down
2 changes: 1 addition & 1 deletion packages/transpiler/src/statements/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class DataTranspiler implements IStatementTranspiler {

const ret = new Chunk()
.appendString("let ")
.append(found.getName().toLowerCase(), token, traversal)
.append(Traversal.prefixVariable(Traversal.escapeNamespace(found.getName().toLowerCase())), token, traversal)
.appendString(" = " + new TranspileTypes().toType(found.getType()))
.append(";", node.getLastToken(), traversal)
.append(value, node.getLastToken(), traversal);
Expand Down
2 changes: 1 addition & 1 deletion packages/transpiler/src/statements/end_method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class EndMethodTranspiler implements IStatementTranspiler {
for (const n in vars) {
const identifier = vars[n];
if (identifier.getMeta().includes(abaplint.IdentifierMeta.MethodReturning)) {
returning += "return " + n.toLowerCase() + ";\n";
returning += "return " + Traversal.prefixVariable(n.toLowerCase()) + ";\n";
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/transpiler/src/statements/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export class FormTranspiler implements IStatementTranspiler {
const ret = new Chunk("async function " + name + "(INPUT) {");
const params: string[] = [];
for (const p of def?.getChangingParameters() || []) {
params.push(`let ${p.getName()} = INPUT.${p.getName()};`);
params.push(`let ${Traversal.prefixVariable(p.getName())} = INPUT.${p.getName()};`);
}
for (const p of def?.getTablesParameters() || []) {
params.push(`let ${p.getName()} = INPUT.${p.getName()};`);
params.push(`let ${Traversal.prefixVariable(p.getName())} = INPUT.${p.getName()};`);
}
for (const p of def?.getUsingParameters() || []) {
params.push(`let ${p.getName()} = INPUT.${p.getName()};`);
params.push(`let ${Traversal.prefixVariable(p.getName())} = INPUT.${p.getName()};`);
}
if (params.length > 0) {
ret.appendString("\n" + params.join("\n"));
Expand Down
27 changes: 14 additions & 13 deletions packages/transpiler/src/statements/method_implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class MethodImplementationTranspiler implements IStatementTranspiler {
for (const n in vars) {
const identifier = vars[n];
const varName = n.toLowerCase();
const varPrefixed = Traversal.prefixVariable(varName);
if (identifier.getMeta().includes(abaplint.IdentifierMeta.MethodImporting)
|| identifier.getMeta().includes(abaplint.IdentifierMeta.MethodChanging)
|| identifier.getMeta().includes(abaplint.IdentifierMeta.EventParameter)
Expand All @@ -52,50 +53,50 @@ export class MethodImplementationTranspiler implements IStatementTranspiler {

const type = identifier.getType();
if (identifier.getMeta().includes(abaplint.IdentifierMeta.MethodExporting)) {
after += `let ${varName} = ${unique}?.${varName} || ${new TranspileTypes().toType(identifier.getType())};\n`;
after += `let ${varPrefixed} = ${unique}?.${varName} || ${new TranspileTypes().toType(identifier.getType())};\n`;
} else if (identifier.getMeta().includes(abaplint.IdentifierMeta.MethodImporting)
&& parameterDefault === undefined
&& passByValue === false
&& isOptional === false
&& type.isGeneric() === false) {

after += `let ${varName} = ${unique}?.${varName};\n`;
after += `let ${varPrefixed} = ${unique}?.${varName};\n`;
if (identifier.getType().getQualifiedName() !== undefined && identifier.getType().getQualifiedName() !== "") {
after += `if (${varName}?.getQualifiedName === undefined || ${varName}.getQualifiedName() !== "${identifier.getType().getQualifiedName()?.toUpperCase()}") { ${varName} = undefined; }\n`;
after += `if (${varPrefixed}?.getQualifiedName === undefined || ${varPrefixed}.getQualifiedName() !== "${identifier.getType().getQualifiedName()?.toUpperCase()}") { ${varPrefixed} = undefined; }\n`;
}
after += `if (${varName} === undefined) { ${varName} = ${new TranspileTypes().toType(identifier.getType())}.set(${unique}.${varName}); }\n`;
after += `if (${varPrefixed} === undefined) { ${varPrefixed} = ${new TranspileTypes().toType(identifier.getType())}.set(${unique}.${varName}); }\n`;

} else if (identifier.getMeta().includes(abaplint.IdentifierMeta.MethodImporting)
&& type.isGeneric() === true) {
if (isOptional === true) {
after += `let ${varName} = ${unique}?.${varName} || ${new TranspileTypes().toType(identifier.getType())};\n`;
after += `let ${varPrefixed} = ${unique}?.${varName} || ${new TranspileTypes().toType(identifier.getType())};\n`;
} else {
after += `let ${varName} = ${unique}?.${varName};\n`;
after += `let ${varPrefixed} = ${unique}?.${varName};\n`;
}

if (type instanceof abaplint.BasicTypes.NumericGenericType) {
after += `if (${varName}.constructor.name === "Character") {
${varName} = ${new TranspileTypes().toType(identifier.getType())};
${varName}.set(${unique}?.${varName});
after += `if (${varPrefixed}.constructor.name === "Character") {
${varPrefixed} = ${new TranspileTypes().toType(identifier.getType())};
${varPrefixed}.set(${unique}?.${varName});
}\n`;
}

} else if (identifier.getMeta().includes(abaplint.IdentifierMeta.MethodImporting)
&& type.isGeneric() === false) {
after += new TranspileTypes().declare(identifier) + "\n";
// note: it might be nessesary to do a type conversion, eg char is passed to xstring parameter
after += "if (" + unique + " && " + unique + "." + varName + ") {" + varName + ".set(" + unique + "." + varName + ");}\n";
after += "if (" + unique + " && " + unique + "." + varName + ") {" + varPrefixed + ".set(" + unique + "." + varName + ");}\n";
} else {
after += new TranspileTypes().declare(identifier) + "\n";
after += "if (" + unique + " && " + unique + "." + varName + ") {" + varName + " = " + unique + "." + varName + ";}\n";
after += "if (" + unique + " && " + unique + "." + varName + ") {" + varPrefixed + " = " + unique + "." + varName + ";}\n";
}

if (parameterDefault) {
const val = this.buildDefault(parameterDefault, traversal, methodDef?.getFilename());
if (passByValue === true || identifier.getMeta().includes(abaplint.IdentifierMeta.MethodChanging)) {
after += "if (" + unique + " === undefined || " + unique + "." + varName + " === undefined) {" + varName + ".set(" + val + ");}\n";
after += "if (" + unique + " === undefined || " + unique + "." + varName + " === undefined) {" + varPrefixed + ".set(" + val + ");}\n";
} else {
after += "if (" + unique + " === undefined || " + unique + "." + varName + " === undefined) {" + varName + " = " + val + ";}\n";
after += "if (" + unique + " === undefined || " + unique + "." + varName + " === undefined) {" + varPrefixed + " = " + val + ";}\n";
}
}
} else if (identifier.getMeta().includes(abaplint.IdentifierMeta.MethodReturning)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/transpiler/src/statements/return.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class ReturnTranspiler implements IStatementTranspiler {
for (const n in vars) {
const identifier = vars[n];
if (identifier.getMeta().includes(abaplint.IdentifierMeta.MethodReturning)) {
extra = " " + n.toLowerCase();
extra = " " + Traversal.prefixVariable(n.toLowerCase());
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/transpiler/src/transpile_types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as abaplint from "@abaplint/core";
import {Traversal} from "./traversal";

const featureHexUInt8 = false;

Expand All @@ -7,7 +8,7 @@ export class TranspileTypes {

public declare(t: abaplint.TypedIdentifier): string {
const type = t.getType();
return "let " + t.getName().toLowerCase() + " = " + this.toType(type) + ";";
return "let " + Traversal.prefixVariable(t.getName().toLowerCase()) + " = " + this.toType(type) + ";";
}

public declareStaticSkipVoid(pre: string, t: abaplint.TypedIdentifier): string {
Expand Down
9 changes: 9 additions & 0 deletions packages/transpiler/src/traversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {ISpaghettiScopeNode} from "@abaplint/core";
import {Chunk} from "./chunk";
import {ConstantTranspiler} from "./expressions";
import {ITranspilerOptions} from "./types";
import {DEFAULT_KEYWORDS} from "./keywords";

export class Traversal {
private readonly spaghetti: abaplint.ISpaghettiScope;
Expand All @@ -31,6 +32,14 @@ export class Traversal {
return name?.replace(/\//g, "$");
}

public static prefixVariable(name: string | undefined) {
// TODO: performace, make this a hash lookup?,
if (DEFAULT_KEYWORDS.some(k => k === name)) {
return "$" + name;
}
return name + "";
}

public getCurrentObject(): abaplint.ABAPObject {
return this.obj;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/transpiler/src/validation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Issue, IRegistry, Config, IConfig, Version} from "@abaplint/core";
import {defaultKeywords} from "./keywords";
import {DEFAULT_KEYWORDS} from "./keywords";
import {ITranspilerOptions, UnknownTypesEnum} from "./types";

export const config: IConfig = {
Expand Down Expand Up @@ -125,7 +125,7 @@ export class Validation {

config.rules["forbidden_identifier"]["check"] = ["^unique\\d+$"];
if (this.options?.keywords === undefined) {
for (const d of defaultKeywords) {
for (const d of DEFAULT_KEYWORDS) {
const add = "^" + d + "$";
config.rules["forbidden_identifier"]["check"].push(add);
}
Expand Down
Loading

0 comments on commit 0375cac

Please sign in to comment.