Permalink
Browse files

Change comment protocol. #10

  • Loading branch information...
1 parent 96a2537 commit ddc4cd5523bd19f94168d7337c0274b3001eb9a5 @Constellation Constellation committed Jul 17, 2012
View
75 escodegen.js
@@ -359,11 +359,20 @@
return base + stmt;
}
- function adjustMultilineComment(value) {
- var array, i, len, line, j, ch, spaces;
+ function calculateSpaces(code) {
+ var result = code.match(/(?:^|\r\n|[\r\n])([^\r\n]*)$/);
+ if (result) {
+ return result[1].length;
+ } else {
+ return undefined;
+ }
+ }
+
+ function adjustMultilineComment(value, specialBase) {
+ var array, i, len, line, j, ch, spaces, previousBase;
- spaces = Number.MAX_VALUE;
array = value.split(/\r\n|[\r\n]/);
+ spaces = Number.MAX_VALUE;
// first line doesn't have indentation
for (i = 1, len = array.length; i < len; i += 1) {
@@ -377,27 +386,46 @@
}
}
- if (spaces % 2 === 1) {
- // /*
- // *
- // */
- // If spaces are odd number, above pattern is considered.
- // We waste 1 space.
- spaces -= 1;
+ if (typeof specialBase !== 'undefined') {
+ // pattern like
+ // {
+ // var t = 20; /*
+ // * this is comment
+ // */
+ // }
+ previousBase = base;
+ if (array[1][spaces] === '*') {
+ specialBase += ' ';
+ }
+ base = specialBase;
+ } else {
+ if (spaces % 2 === 1) {
+ // /*
+ // *
+ // */
+ // If spaces are odd number, above pattern is considered.
+ // We waste 1 space.
+ spaces -= 1;
+ }
+ previousBase = base;
}
+
for (i = 1, len = array.length; i < len; i += 1) {
array[i] = addIndent(array[i].slice(spaces));
}
+
+ base = previousBase;
+
return array.join('\n');
}
- function generateComment(comment) {
+ function generateComment(comment, specialBase) {
if (comment.type === 'Line') {
// Esprima always produce last line comment with LineTerminator
return '//' + comment.value;
}
if (extra.format.indent.adjustMultilineComment && /[\n\r]/.test(comment.value)) {
- return adjustMultilineComment('/*' + comment.value + '*/');
+ return adjustMultilineComment('/*' + comment.value + '*/', specialBase);
}
return '/*' + comment.value + '*/';
}
@@ -792,7 +820,7 @@
}
function generateStatement(stmt, option) {
- var i, len, result, previousBase, comment, save, ret, node, allowIn;
+ var i, len, result, previousBase, comment, save, ret, node, allowIn, tailingToStatement, specialBase;
allowIn = true;
if (option) {
@@ -1193,10 +1221,27 @@
}
if (stmt.trailingComments) {
+ tailingToStatement = !endsWithLineTerminator(result);
+ specialBase = stringRepeat(' ', calculateSpaces(base + result + indent));
for (i = 0, len = stmt.trailingComments.length; i < len; i += 1) {
comment = stmt.trailingComments[i];
- result += addIndent(generateComment(comment));
- if (!endsWithLineTerminator(result)) {
+ if (tailingToStatement) {
+ // We assume target like following script
+ //
+ // var t = 20; /**
+ // * This is comment of t
+ // */
+ if (i === 0) {
+ // first case
+ result += indent;
+ } else {
+ result += specialBase;
+ }
+ result += generateComment(comment, specialBase);
+ } else {
+ result += addIndent(generateComment(comment));
+ }
+ if (i !== len - 1 && !endsWithLineTerminator(result)) {
result += '\n';
}
}
View
3 test/comment/empty-line-comment.expected.js
@@ -1,5 +1,4 @@
function test() {
// Leading to EmptyStatement
- ;
- // Trailing to EmptyStatement
+ ; // Trailing to EmptyStatement
}
View
3 test/comment/if-block-line-comment.expected.js
@@ -2,5 +2,4 @@ if (cond)
// Leading to if-block
{
print('hello');
- }
- // Trailing to if-block
+ } // Trailing to if-block
View
2 test/comment/if-block-line-comment.js
@@ -2,4 +2,4 @@ if (cond)
// Leading to if-block
{
print('hello');
-} // Trailing to if-block
+} // Trailing to if-block
View
3 test/comment/if-empty-line-comment.expected.js
@@ -1,4 +1,3 @@
if (cond)
// Leading to EmptyStatement
- ;
- // Trailing to EmptyStatement
+ ; // Trailing to EmptyStatement
View
3 test/comment/if-empty-line-comment.js
@@ -1,4 +1,3 @@
if (cond)
// Leading to EmptyStatement
- ;
-// Trailing to EmptyStatement
+ ; // Trailing to EmptyStatement
View
3 test/comment/if-line-comment.expected.js
@@ -2,6 +2,5 @@ function test() {
// Leading if statement
if (cond) {
print('hello');
- }
- // Trailing if-block statement
+ } // Trailing if-block statement
}
View
43 test/comment/simple-a-lot-of-line-comment.expected.js
@@ -20,25 +20,24 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
function test() {
-}
-// Copyright (C) 2012 Yusuke Suzuki <utatane.tea@gmail.com>
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above copyright
-// notice, this list of conditions and the following disclaimer in the
-// documentation and/or other materials provided with the distribution.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-// ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
-// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
-// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
-// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+} // Copyright (C) 2012 Yusuke Suzuki <utatane.tea@gmail.com>
+ //
+ // Redistribution and use in source and binary forms, with or without
+ // modification, are permitted provided that the following conditions are met:
+ //
+ // * Redistributions of source code must retain the above copyright
+ // notice, this list of conditions and the following disclaimer.
+ // * Redistributions in binary form must reproduce the above copyright
+ // notice, this list of conditions and the following disclaimer in the
+ // documentation and/or other materials provided with the distribution.
+ //
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+ // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
47 test/comment/simple-a-lot-of-multi-comment.expected.js
@@ -22,27 +22,26 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
function test() {
-}
-/*
- Copyright (C) 2012 Yusuke Suzuki <utatane.tea@gmail.com>
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
-
- * Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+} /*
+ Copyright (C) 2012 Yusuke Suzuki <utatane.tea@gmail.com>
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
View
3 test/comment/simple-line-comment.expected.js
@@ -1,3 +1,2 @@
// Leading
-var i = 20;
-// Trailing
+var i = 20; // Trailing
View
17 test/comment/simple-multi-comment.expected.js
@@ -7,13 +7,12 @@ function test() {
* Leading comment 2
*
*/
- var i = 20;
- /*
- * Trailing comment
- */
- /*
- *
- * Trailing comment 2
- *
- */
+ var i = 20; /*
+ * Trailing comment
+ */
+ /*
+ *
+ * Trailing comment 2
+ *
+ */
}
View
17 test/comment/variable-declarator-trailing-comment.expected.js
@@ -0,0 +1,17 @@
+{
+ var t = 20; /*
+ * This is trailing comment
+ */
+}
+{
+ var tt = 20; /*
+ * This is trailing comment
+ */
+}
+{
+ {
+ var t = 20; /*
+ * This is trailing comment
+ */
+ }
+}
View
16 test/comment/variable-declarator-trailing-comment.js
@@ -0,0 +1,16 @@
+{
+ var t = 20; /*
+ * This is trailing comment
+ */
+}
+
+{
+ var tt = 20; /*
+ * This is trailing comment
+ */
+}
+{{
+ var t = 20; /*
+ * This is trailing comment
+ */
+}}
View
24 test/identity.js
@@ -75,11 +75,12 @@
];
function testIdentity(code) {
- var expected, tree, actual, options, StringObject, err;
+ var expected, tree, actual, options, commentOptions, commentTree, StringObject, err;
// alias, so that JSLint does not complain.
StringObject = String;
+ // once
options = {
comment: false,
range: false,
@@ -99,6 +100,27 @@
if (expected !== actual) {
throw new NotMatchingError(expected, actual);
}
+
+ // second, attachComments
+ commentOptions = {
+ comment: true,
+ range: true,
+ loc: false,
+ tokens: true,
+ raw: false
+ };
+
+ try {
+ commentTree = esprima.parse(code, commentOptions);
+ commentTree = escodegen.attachComments(commentTree, commentTree.comments, commentTree.tokens);
+ tree = esprima.parse(escodegen.generate(commentTree), options);
+ actual = JSON.stringify(tree, adjustRegexLiteral, 4);
+ } catch (e) {
+ throw new NotMatchingError(expected, e.toString());
+ }
+ if (expected !== actual) {
+ throw new NotMatchingError(expected, actual);
+ }
}
total = 0;
View
179 test/test.js
@@ -8636,101 +8636,6 @@ data = {
start: { line: 1, column: 0 },
end: { line: 1, column: 7 }
}
- },
-
- '1 = 42': {
- type: 'ExpressionStatement',
- expression: {
- type: 'AssignmentExpression',
- operator: '=',
- left: {
- type: 'Literal',
- value: 1,
- raw: '1',
- range: [0, 1],
- loc: {
- start: { line: 1, column: 0 },
- end: { line: 1, column: 1 }
- }
- },
- right: {
- type: 'Literal',
- value: 42,
- raw: '42',
- range: [4, 6],
- loc: {
- start: { line: 1, column: 4 },
- end: { line: 1, column: 6 }
- }
- },
- range: [0, 6],
- loc: {
- start: { line: 1, column: 0 },
- end: { line: 1, column: 6 }
- }
- },
- range: [0, 6],
- loc: {
- start: { line: 1, column: 0 },
- end: { line: 1, column: 6 }
- }
- },
-
- '(1 + 1) = 42': {
- type: 'ExpressionStatement',
- expression: {
- type: 'AssignmentExpression',
- operator: '=',
- left: {
- type: 'BinaryExpression',
- operator: '+',
- left: {
- type: 'Literal',
- value: 1,
- raw: '1',
- range: [1, 2],
- loc: {
- start: { line: 1, column: 1 },
- end: { line: 1, column: 2 }
- }
- },
- right: {
- type: 'Literal',
- value: 1,
- raw: '1',
- range: [5, 6],
- loc: {
- start: { line: 1, column: 5 },
- end: { line: 1, column: 6 }
- }
- },
- range: [0, 7],
- loc: {
- start: { line: 1, column: 0 },
- end: { line: 1, column: 7 }
- }
- },
- right: {
- type: 'Literal',
- value: 42,
- raw: '42',
- range: [10, 12],
- loc: {
- start: { line: 1, column: 10 },
- end: { line: 1, column: 12 }
- }
- },
- range: [0, 12],
- loc: {
- start: { line: 1, column: 0 },
- end: { line: 1, column: 12 }
- }
- },
- range: [0, 12],
- loc: {
- start: { line: 1, column: 0 },
- end: { line: 1, column: 12 }
- }
}
},
@@ -11053,88 +10958,6 @@ data = {
}
},
- 'for((1 + 1) in list) process(x);': {
- type: 'ForInStatement',
- left: {
- type: 'BinaryExpression',
- operator: '+',
- left: {
- type: 'Literal',
- value: 1,
- raw: '1',
- range: [5, 6],
- loc: {
- start: { line: 1, column: 5 },
- end: { line: 1, column: 6 }
- }
- },
- right: {
- type: 'Literal',
- value: 1,
- raw: '1',
- range: [9, 10],
- loc: {
- start: { line: 1, column: 9 },
- end: { line: 1, column: 10 }
- }
- },
- range: [4, 11],
- loc: {
- start: { line: 1, column: 4 },
- end: { line: 1, column: 11 }
- }
- },
- right: {
- type: 'Identifier',
- name: 'list',
- range: [15, 19],
- loc: {
- start: { line: 1, column: 15 },
- end: { line: 1, column: 19 }
- }
- },
- body: {
- type: 'ExpressionStatement',
- expression: {
- type: 'CallExpression',
- callee: {
- type: 'Identifier',
- name: 'process',
- range: [21, 28],
- loc: {
- start: { line: 1, column: 21 },
- end: { line: 1, column: 28 }
- }
- },
- 'arguments': [{
- type: 'Identifier',
- name: 'x',
- range: [29, 30],
- loc: {
- start: { line: 1, column: 29 },
- end: { line: 1, column: 30 }
- }
- }],
- range: [21, 31],
- loc: {
- start: { line: 1, column: 21 },
- end: { line: 1, column: 31 }
- }
- },
- range: [21, 32],
- loc: {
- start: { line: 1, column: 21 },
- end: { line: 1, column: 32 }
- }
- },
- each: false,
- range: [0, 32],
- loc: {
- start: { line: 1, column: 0 },
- end: { line: 1, column: 32 }
- }
- },
-
'for (var x in list) process(x);': {
type: 'ForInStatement',
left: {
@@ -15029,7 +14852,7 @@ if (typeof window !== 'undefined') {
failures.forEach(function (failure) {
console.log(failure);
console.error(failure.source + ': Expected\n ' +
- failure.expected.split('\n').join('\n ') +
+ failure.source.split('\n').join('\n ') +
'\nto match\n ' + failure.actual);
});
} else {

0 comments on commit ddc4cd5

Please sign in to comment.