mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Marko v3: Don't put quotes around property name if not necessary
This commit is contained in:
parent
7db74bf05b
commit
42b2a349fd
@ -7,6 +7,7 @@ const Identifier = require('./ast/Identifier');
|
||||
const ok = require('assert').ok;
|
||||
const Container = require('./ast/Container');
|
||||
const util = require('util');
|
||||
const isValidJavaScriptIdentifier = require('./util/isValidJavaScriptIdentifier');
|
||||
|
||||
class Slot {
|
||||
constructor(codegen, slotNode) {
|
||||
@ -495,8 +496,15 @@ class Generator {
|
||||
for (let i=0; i<keys.length; i++) {
|
||||
let k = keys[i];
|
||||
let v = value[k];
|
||||
|
||||
this.writeLineIndent();
|
||||
this.write(JSON.stringify(k) + ': ');
|
||||
|
||||
if (isValidJavaScriptIdentifier(k)) {
|
||||
this.write(k + ': ');
|
||||
} else {
|
||||
this.write(JSON.stringify(k) + ': ');
|
||||
}
|
||||
|
||||
if (v instanceof Node) {
|
||||
this.generateCode(v);
|
||||
} else {
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
var Node = require('./Node');
|
||||
const isValidJavaScriptIdentifier = require('../util/isValidJavaScriptIdentifier');
|
||||
const Node = require('./Node');
|
||||
|
||||
class Property extends Node {
|
||||
constructor(def) {
|
||||
@ -13,6 +13,13 @@ class Property extends Node {
|
||||
var key = this.key;
|
||||
var value = this.value;
|
||||
|
||||
if (key.type === 'Literal') {
|
||||
var propName = key.value;
|
||||
if (isValidJavaScriptIdentifier(propName)) {
|
||||
key = codegen.builder.identifier(propName);
|
||||
}
|
||||
}
|
||||
|
||||
codegen.generateCode(key);
|
||||
codegen.write(': ');
|
||||
codegen.generateCode(value);
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
var reservedWords = require('./javaScriptReservedWords');
|
||||
var varNameRegExp = /^[$A-Z_][0-9A-Z_$]*$/i;
|
||||
|
||||
module.exports = function isValidJavaScriptVarName(varName) {
|
||||
module.exports = function isValidJavaScriptIdentifier(varName) {
|
||||
if (reservedWords[varName]) {
|
||||
return false;
|
||||
}
|
||||
@ -2,8 +2,8 @@ var aString = "abc",
|
||||
aNumber = 123,
|
||||
aBoolean = false,
|
||||
anObject = {
|
||||
"foo": "bar",
|
||||
"dynamic": data.name
|
||||
foo: "bar",
|
||||
dynamic: data.name
|
||||
},
|
||||
anArray = [
|
||||
"foo",
|
||||
|
||||
@ -8,8 +8,8 @@ function create(__helpers) {
|
||||
|
||||
return function render(data, out) {
|
||||
test_body_function({
|
||||
"name": "World",
|
||||
"myBody": function myBody(foo, bar) {
|
||||
name: "World",
|
||||
myBody: function myBody(foo, bar) {
|
||||
out.w("This is the body content");
|
||||
}
|
||||
}, out);
|
||||
|
||||
@ -8,10 +8,10 @@ function create(__helpers) {
|
||||
|
||||
return function render(data, out) {
|
||||
test_import_var({
|
||||
"name": "World",
|
||||
"foo": data.foo,
|
||||
"bar": data.bar,
|
||||
"renderBody": function renderBody(out) {
|
||||
name: "World",
|
||||
foo: data.foo,
|
||||
bar: data.bar,
|
||||
renderBody: function renderBody(out) {
|
||||
out.w("This is the body content");
|
||||
}
|
||||
}, out);
|
||||
|
||||
@ -8,7 +8,7 @@ function create(__helpers) {
|
||||
|
||||
return function render(data, out) {
|
||||
test_hello({
|
||||
"name": "World"
|
||||
name: "World"
|
||||
}, out);
|
||||
};
|
||||
}
|
||||
|
||||
@ -10,18 +10,18 @@ function create(__helpers) {
|
||||
|
||||
return function render(data, out) {
|
||||
test_nested_tags_overlay({
|
||||
"header": "data.header"
|
||||
header: "data.header"
|
||||
}, out, 0, function renderBody(out, test_nested_tags_overlay0) {
|
||||
test_nested_tags_overlay_body({
|
||||
"className": "my-body",
|
||||
"renderBody": function renderBody(out) {
|
||||
className: "my-body",
|
||||
renderBody: function renderBody(out) {
|
||||
out.w("Body content");
|
||||
}
|
||||
}, out, test_nested_tags_overlay0);
|
||||
|
||||
test_nested_tags_overlay_footer({
|
||||
"className": "my-footer",
|
||||
"renderBody": function renderBody(out) {
|
||||
className: "my-footer",
|
||||
renderBody: function renderBody(out) {
|
||||
out.w("Footer content");
|
||||
}
|
||||
}, out, test_nested_tags_overlay0);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user