From 8de6da01d6215e7176d6b5deb149b1908e2055d6 Mon Sep 17 00:00:00 2001 From: spaceface777 Date: Fri, 15 May 2020 15:55:49 +0200 Subject: [PATCH] jsgen: prefix JS keywords with 'v_' --- vlib/v/gen/js/js.v | 55 +++++---- vlib/v/gen/js/jsdoc.v | 7 +- vlib/v/gen/js/tests/js.js | 238 +++++++++++++++++++++----------------- vlib/v/gen/js/tests/js.v | 17 ++- 4 files changed, 179 insertions(+), 138 deletions(-) diff --git a/vlib/v/gen/js/js.v b/vlib/v/gen/js/js.v index c459409dba..2083fee71a 100644 --- a/vlib/v/gen/js/js.v +++ b/vlib/v/gen/js/js.v @@ -8,8 +8,12 @@ import term import v.util const ( - //TODO - js_reserved = ['delete', 'const', 'let', 'var', 'function', 'continue', 'break', 'switch', 'for', 'in', 'of', 'instanceof', 'typeof', 'do'] + // https://ecma-international.org/ecma-262/#sec-reserved-words + js_reserved = ['await', 'break', 'case', 'catch', 'class', 'const', 'continue', 'debugger', + 'default', 'delete', 'do', 'else', 'enum', 'export', 'extends', 'finally', 'for', 'function', + 'if', 'implements', 'import', 'in', 'instanceof', 'interface', 'let', 'new', 'package', 'private', + 'protected', 'public', 'return', 'static', 'super', 'switch', 'this', 'throw', 'try', 'typeof', + 'var', 'void', 'while', 'with', 'yield'] tabs = ['', '\t', '\t\t', '\t\t\t', '\t\t\t\t', '\t\t\t\t\t', '\t\t\t\t\t\t', '\t\t\t\t\t\t\t', '\t\t\t\t\t\t\t\t'] ) @@ -95,7 +99,6 @@ pub fn (g mut JsGen) enter_namespace(n string) { g.indents[g.namespace] = 0 g.out.writeln('const $n = (function () {') } - // else { g.out = g.namespaces[g.namespace] } @@ -107,10 +110,7 @@ pub fn (g mut JsGen) escape_namespace() { } pub fn (g mut JsGen) push_pub_var(s string) { - // Workaround until `m[key]< 0 && args[0].name == 'arg_1' for i, arg in args { + name := js_name(arg.name) is_varg := i == args.len - 1 && is_variadic if is_varg { - g.write('...$arg.name') + g.write('...$name') } else { - g.write(arg.name) + g.write(name) } // if its not the last argument if i < args.len - 1 { @@ -911,7 +921,6 @@ fn (g mut JsGen) gen_return_stmt(it ast.Return) { g.writeln(';') } - fn (g mut JsGen) enum_expr(node ast.Expr) { match node { ast.EnumVal { @@ -924,7 +933,7 @@ fn (g mut JsGen) enum_expr(node ast.Expr) { } fn (g mut JsGen) gen_struct_decl(node ast.StructDecl) { - g.writeln('class $node.name {') + g.writeln('class ${js_name(node.name)} {') g.inc_indent() g.writeln(g.doc.gen_ctor(node.fields)) g.writeln('constructor(values) {') @@ -980,7 +989,7 @@ fn (g mut JsGen) gen_ident(node ast.Ident) { } // TODO js_name - name := node.name + name := js_name(node.name) // TODO `is` // TODO handle optionals g.write(name) diff --git a/vlib/v/gen/js/jsdoc.v b/vlib/v/gen/js/jsdoc.v index 975f8087a5..1135434cbb 100644 --- a/vlib/v/gen/js/jsdoc.v +++ b/vlib/v/gen/js/jsdoc.v @@ -45,7 +45,7 @@ fn (mut d JsDoc) gen_typ(typ, name string) string { d.write('/**') d.write(' @type {$typ}') if name.len > 0 { - d.write(' - $name') + d.write(' - ${js_name(name)}') } d.write(' */') return d.out.str() @@ -77,10 +77,11 @@ fn (mut d JsDoc) gen_fn(it ast.FnDecl) string { } arg_type_name := d.gen.typ(arg.typ) is_varg := i == it.args.len - 1 && it.is_variadic + name := js_name(arg.name) if is_varg { - d.writeln('* @param {...$arg_type_name} $arg.name') + d.writeln('* @param {...$arg_type_name} $name') } else { - d.writeln('* @param {$arg_type_name} $arg.name') + d.writeln('* @param {$arg_type_name} $name') } } d.writeln('* @return {$type_name}') diff --git a/vlib/v/gen/js/tests/js.js b/vlib/v/gen/js/tests/js.js index c065a50483..f958962816 100644 --- a/vlib/v/gen/js/tests/js.js +++ b/vlib/v/gen/js/tests/js.js @@ -1,127 +1,147 @@ -// V_COMMIT_HASH 83289d7 -// V_CURRENT_COMMIT_HASH fc7e64b +// V_COMMIT_HASH d60233b +// V_CURRENT_COMMIT_HASH fc520d9 // Generated by the V compiler "use strict"; const CONSTANTS = Object.freeze({ /** @type {number} - i_am_a_const */ - i_am_a_const: 21214 + i_am_a_const: 21214, + /** @type {string} - v_super */ + v_super: "amazing keyword" }); +/* namespace: main */ +const main = (function () { class Companies { - /** - * @param {{google: number, amazon: boolean, yahoo: string}} values - values for this class fields - * @constructor - */ - constructor(values) { - this.google = values.google - this.amazon = values.amazon - this.yahoo = values.yahoo - } - - /** - * @return {number} - */ - method() { - const it = this; - const ss = new Companies({ - google: 2, - amazon: true, - yahoo: "hello" - }); - /** @type {[number, number]} */ - const [a, b] = hello(2, "google", "not google"); - /** @type {string} - glue */ - const glue = (a > 2 ? "more_glue" : a > 5 ? "more glueee" : "less glue"); - if (a !== 2) { + /** + * @param {{google: number, amazon: boolean, yahoo: string}} values - values for this class fields + * @constructor + */ + constructor(values) { + this.google = values.google + this.amazon = values.amazon + this.yahoo = values.yahoo } - return 0; + /** + * @return {number} + */ + method() { + const it = this; + const ss = new Companies({ + google: 2, + amazon: true, + yahoo: "hello" + }); + /** @type {[number, number]} */ + const [a, b] = hello(2, "google", "not google"); + /** @type {string} - glue */ + const glue = (a > 2 ? "more_glue" : a > 5 ? "more glueee" : "less glue"); + if (a !== 2) { + } + + return 0; + } } - -} -const POSITION = Object.freeze({ - GO_BACK: 0, - DONT_GO_BACK: 1, -}); - -/* program entry point */ -(async function() { - /** @type {string} - v */ - const v = "done"; - { - /** @type {string} - _ */ - const _ = "block"; - } - - /** @type {number} - pos */ - const pos = POSITION.GO_BACK; - /** @type {number} - dun */ - const dun = CONSTANTS.i_am_a_const * 20; - for (let i = 0; i < 10; i++) { - } - - for (let i = 0; i < "hello".length; ++i) { - let x = "hello"[i]; - } - - for (let x = 1; x < 10; ++x) { - } - - /** @type {number[]} - arr */ - const arr = [1, 2, 3, 4, 5]; - for (let tmp1 = 0; tmp1 < arr.length; ++tmp1) { - let a = arr[tmp1]; - } - - /** @type {Map} - ma */ - const ma = new Map([ - ["str", "done"], - ["ddo", "baba"] - ]); - for (let [m, n] of ma) { - /** @type {string} - iss */ - const iss = m; - } - - await new Promise(function(resolve){ - async(0, "hello"); - resolve(); + const POSITION = Object.freeze({ + go_back: 0, + dont_go_back: 1, }); -})(); - - -/** -* @param {number} num -* @param {string} def -* @return {void} -*/ -function async(num, def) { -} - - -/* [inline] */ -/** -* @param {number} game_on -* @param {...string} dummy -* @return {multi_return_int_int} -*/ -function hello(game_on, ...dummy) { - for (let tmp2 = 0; tmp2 < dummy.length; ++tmp2) { - let dd = dummy[tmp2]; - /** @type {string} - l */ - const l = dd; + /** + * @param {string} v_extends + * @param {number} v_instanceof + * @return {void} + */ + function v_class(v_extends, v_instanceof) { + /** @type {number} - v_delete */ + const v_delete = v_instanceof; } - (function defer() { - /** @type {string} - do */ - const do = "not"; + /* program entry point */ + (async function() { + /** @type {string} - v */ + const v = "done"; + { + /** @type {string} - _ */ + const _ = "block"; + } + + /** @type {number} - pos */ + const pos = POSITION.go_back; + /** @type {string} - v_debugger */ + const v_debugger = "JS keyword"; + /** @type {string} - v_await */ + const v_await = CONSTANTS.v_super + v_debugger; + /** @type {string} - v_finally */ + let v_finally = "implemented"; + /** @type {number} - dun */ + const dun = CONSTANTS.i_am_a_const * 20; + for (let i = 0; i < 10; i++) { + } + + for (let i = 0; i < "hello".length; ++i) { + let x = "hello"[i]; + } + + for (let x = 1; x < 10; ++x) { + } + + /** @type {number[]} - arr */ + const arr = [1, 2, 3, 4, 5]; + for (let _tmp1 = 0; _tmp1 < arr.length; ++_tmp1) { + let a = arr[_tmp1]; + } + + /** @type {Map} - ma */ + const ma = new Map([ + ["str", "done"], + ["ddo", "baba"] + ]); + for (let [m, n] of ma) { + /** @type {string} - iss */ + const iss = m; + } + + await new Promise(function(resolve){ + async(0, "hello"); + resolve(); + }); + })(); - return [game_on + 2, 221]; -} - - - + + /** + * @param {number} num + * @param {string} def + * @return {void} + */ + function async(num, def) { + } + + /* [inline] */ + /** + * @param {number} game_on + * @param {...string} dummy + * @return {multi_return_int_int} + */ + function hello(game_on, ...dummy) { + for (let _tmp2 = 0; _tmp2 < dummy.length; ++_tmp2) { + let dd = dummy[_tmp2]; + /** @type {string} - l */ + const l = dd; + } + + (function defer() { + /** @type {string} - v_do */ + const v_do = "not"; + })(); + return [game_on + 2, 221]; + } + + + /* module exports */ + return { + }; +})(); diff --git a/vlib/v/gen/js/tests/js.v b/vlib/v/gen/js/tests/js.v index 6c0863f579..719bf099ed 100644 --- a/vlib/v/gen/js/tests/js.v +++ b/vlib/v/gen/js/tests/js.v @@ -1,6 +1,7 @@ const ( i_am_a_const = 21214 + super = 'amazing keyword' ) struct Companies { @@ -10,17 +11,27 @@ struct Companies { } enum POSITION { - GO_BACK - DONT_GO_BACK + go_back + dont_go_back } +fn class(extends string, instanceof int) { + delete := instanceof +} + + fn main() { v := "done" { _ := "block" } - pos := POSITION.GO_BACK + pos := POSITION.go_back + + debugger := 'JS keyword' + // TODO: Implement interpolation + await := super + debugger + mut finally := 'implemented' dun := i_am_a_const * 20