mirror of https://gitee.com/bigwinds/arangodb
Foxx: Source Code Transformer is done
This commit is contained in:
parent
071290b36d
commit
f0a2aeef4a
|
@ -319,6 +319,7 @@ SHELL_SERVER_ONLY = \
|
|||
@top_srcdir@/js/common/tests/shell-foxx-base-middleware.js \
|
||||
@top_srcdir@/js/common/tests/shell-foxx-template-middleware.js \
|
||||
@top_srcdir@/js/common/tests/shell-foxx-format-middleware.js \
|
||||
@top_srcdir@/js/common/tests/shell-foxx-transformer.js \
|
||||
@top_srcdir@/js/common/tests/shell-graph-traversal.js \
|
||||
@top_srcdir@/js/common/tests/shell-graph-algorithms.js \
|
||||
@top_srcdir@/js/common/tests/shell-graph-measurement.js \
|
||||
|
|
|
@ -0,0 +1,162 @@
|
|||
require("internal").flushModuleCache();
|
||||
|
||||
var jsunity = require("jsunity"),
|
||||
transform = require("org/arangodb/foxx/transformer").transform,
|
||||
Transformer = require("org/arangodb/foxx/transformer").Transformer;
|
||||
|
||||
// High Level Spec Suite for the transform Function
|
||||
function TransformSpec () {
|
||||
var testFileWithoutJSDoc, testFileWithJSDoc, testFileWithJSDocTransformed;
|
||||
|
||||
return {
|
||||
setUp: function () {
|
||||
testFileWithoutJSDoc = [
|
||||
"(function() {",
|
||||
" // normal comment",
|
||||
" var x = 1;",
|
||||
" /* long description",
|
||||
" * test",
|
||||
" */",
|
||||
"}());"
|
||||
].join("\n");
|
||||
|
||||
testFileWithJSDoc = [
|
||||
"(function() {",
|
||||
" /** long description",
|
||||
" * test",
|
||||
" */",
|
||||
" var x = 2;",
|
||||
"}());"
|
||||
].join("\n");
|
||||
|
||||
testFileWithJSDocTransformed = [
|
||||
"(function() {",
|
||||
"appContext.comment(\"long description\");",
|
||||
"appContext.comment(\"test\");",
|
||||
"appContext.comment(\"\");",
|
||||
" var x = 2;",
|
||||
"}());"
|
||||
].join("\n");
|
||||
},
|
||||
|
||||
testDoesntChangeFileWithoutJSDocComments: function () {
|
||||
assertEqual(transform(testFileWithoutJSDoc), testFileWithoutJSDoc);
|
||||
},
|
||||
|
||||
testTransformsFileWithJSDocComments: function () {
|
||||
assertEqual(transform(testFileWithJSDoc), testFileWithJSDocTransformed);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Low level Spec Suite for the Transform prototype
|
||||
function TransformerSpec () {
|
||||
var i, result, testFileWithSingleJSDoc, testFileWithJSDocAndMultiline, transformedLineOne;
|
||||
|
||||
return {
|
||||
setUp: function () {
|
||||
testFileWithSingleJSDoc = [
|
||||
"(function() {",
|
||||
" /** long description",
|
||||
" * test",
|
||||
" */",
|
||||
" var x = 2;",
|
||||
"}());"
|
||||
].join("\n");
|
||||
|
||||
transformedLineOne = [
|
||||
"(function() {",
|
||||
"appContext.comment(\"long description\");",
|
||||
" * test",
|
||||
" */",
|
||||
" var x = 2;",
|
||||
"}());"
|
||||
].join("\n");
|
||||
|
||||
testFileWithMultiline = [
|
||||
"(function() {",
|
||||
" /* long description",
|
||||
" * test",
|
||||
" */",
|
||||
" var x = 2;",
|
||||
"}());"
|
||||
].join("\n");
|
||||
|
||||
testFileWithJSDocAndMultiline = [
|
||||
"(function() {",
|
||||
" /** long description",
|
||||
" * test",
|
||||
" */",
|
||||
" var x = 2;",
|
||||
" /* long comment",
|
||||
" * test",
|
||||
" */",
|
||||
"}());"
|
||||
].join("\n");
|
||||
},
|
||||
|
||||
testSearchForFirstJSDocStart: function () {
|
||||
transformer = new Transformer(testFileWithSingleJSDoc);
|
||||
|
||||
for (i = 0; i < 1; i++) {
|
||||
transformer.searchNext();
|
||||
}
|
||||
|
||||
assertEqual(transformer.getCurrentLineNumber(), 1);
|
||||
},
|
||||
|
||||
testContinueInJSDoc: function () {
|
||||
transformer = new Transformer(testFileWithSingleJSDoc);
|
||||
|
||||
for (i = 0; i < 2; i++) {
|
||||
transformer.searchNext();
|
||||
}
|
||||
|
||||
assertEqual(transformer.getCurrentLineNumber(), 2);
|
||||
},
|
||||
|
||||
testStopAtTheEndOfJSDoc: function () {
|
||||
transformer = new Transformer(testFileWithSingleJSDoc);
|
||||
|
||||
for (i = 0; i < 4; i++) {
|
||||
transformer.searchNext();
|
||||
}
|
||||
|
||||
assertUndefined(transformer.getCurrentLineNumber());
|
||||
},
|
||||
|
||||
testDoNotIncludeNormalMultiline: function () {
|
||||
transformer = new Transformer(testFileWithMultiline);
|
||||
|
||||
for (i = 0; i < 1; i++) {
|
||||
transformer.searchNext();
|
||||
}
|
||||
|
||||
assertUndefined(transformer.getCurrentLineNumber());
|
||||
},
|
||||
|
||||
testDoNotIncludeNonJSDocComments: function () {
|
||||
transformer = new Transformer(testFileWithJSDocAndMultiline);
|
||||
|
||||
for (i = 0; i < 4; i++) {
|
||||
transformer.searchNext();
|
||||
}
|
||||
|
||||
assertUndefined(transformer.getCurrentLineNumber());
|
||||
},
|
||||
|
||||
testConvertLine: function () {
|
||||
transformer = new Transformer(testFileWithSingleJSDoc);
|
||||
|
||||
transformer.searchNext();
|
||||
transformer.convertLine();
|
||||
|
||||
assertEqual(transformer.result(), transformedLineOne);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
jsunity.run(TransformSpec);
|
||||
jsunity.run(TransformerSpec);
|
||||
|
||||
return jsunity.done();
|
|
@ -0,0 +1,148 @@
|
|||
/*jslint indent: 2, nomen: true, maxlen: 120 */
|
||||
/*global module, require, exports */
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Foxx application
|
||||
///
|
||||
/// @file
|
||||
///
|
||||
/// DISCLAIMER
|
||||
///
|
||||
/// Copyright 2013 triagens GmbH, Cologne, Germany
|
||||
///
|
||||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
/// you may not use this file except in compliance with the License.
|
||||
/// You may obtain a copy of the License at
|
||||
///
|
||||
/// http://www.apache.org/licenses/LICENSE-2.0
|
||||
///
|
||||
/// Unless required by applicable law or agreed to in writing, software
|
||||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
/// See the License for the specific language governing permissions and
|
||||
/// limitations under the License.
|
||||
///
|
||||
/// Copyright holder is triAGENS GmbH, Cologne, Germany
|
||||
///
|
||||
/// @author Lucas Dohmen
|
||||
/// @author Copyright 2013, triAGENS GmbH, Cologne, Germany
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
var Transformer,
|
||||
transform,
|
||||
ArrayIterator,
|
||||
extend = require("underscore").extend;
|
||||
|
||||
ArrayIterator = function (arr) {
|
||||
this.array = arr;
|
||||
this.currentLineNumber = -1;
|
||||
};
|
||||
|
||||
extend(ArrayIterator.prototype, {
|
||||
next: function () {
|
||||
this.currentLineNumber += 1;
|
||||
return this.array[this.currentLineNumber];
|
||||
},
|
||||
|
||||
current: function () {
|
||||
return this.array[this.currentLineNumber];
|
||||
},
|
||||
|
||||
hasNext: function () {
|
||||
return this.currentLineNumber < (this.array.length - 1);
|
||||
},
|
||||
|
||||
replaceWith: function (newLine) {
|
||||
this.array[this.currentLineNumber] = newLine;
|
||||
},
|
||||
|
||||
entireString: function () {
|
||||
return this.array.join("\n");
|
||||
},
|
||||
|
||||
getCurrentLineNumber: function () {
|
||||
if (this.hasNext()) {
|
||||
return this.currentLineNumber;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Transformer = function (input) {
|
||||
this.iterator = new ArrayIterator(input.split("\n"));
|
||||
this.inJSDoc = false;
|
||||
};
|
||||
|
||||
extend(Transformer.prototype, {
|
||||
result: function () {
|
||||
return this.iterator.entireString();
|
||||
},
|
||||
|
||||
convert: function () {
|
||||
while (this.searchNext()) {
|
||||
this.convertLine();
|
||||
}
|
||||
return this;
|
||||
},
|
||||
|
||||
searchNext: function () {
|
||||
while (this.iterator.hasNext()) {
|
||||
if (this.isJSDoc(this.iterator.next())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
convertLine: function () {
|
||||
this.iterator.replaceWith(
|
||||
"appContext.comment(\"" +
|
||||
this.stripComment(this.iterator.current()) +
|
||||
"\");"
|
||||
);
|
||||
},
|
||||
|
||||
getCurrentLineNumber: function () {
|
||||
return this.iterator.getCurrentLineNumber();
|
||||
},
|
||||
|
||||
// helper
|
||||
|
||||
stripComment: function (str) {
|
||||
return str.match(/^\s*\/?\*\*?\/?\s*(.*)$/)[1];
|
||||
},
|
||||
|
||||
isJSDoc: function (str) {
|
||||
var matched;
|
||||
|
||||
if (this.inJSDoc && str.match(/^\s*\*/)) {
|
||||
matched = true;
|
||||
if (str.match(/^\s*\*\//)) {
|
||||
this.inJSDoc = false;
|
||||
}
|
||||
} else if ((!this.inJSDoc) && str.match(/^\s*\/\*\*/)) {
|
||||
matched = true;
|
||||
this.inJSDoc = true;
|
||||
}
|
||||
|
||||
return matched;
|
||||
}
|
||||
});
|
||||
|
||||
transform = function (input) {
|
||||
var transformer = new Transformer(input);
|
||||
return transformer.convert().result();
|
||||
};
|
||||
|
||||
// Only Exported for Tests, please use `transform`
|
||||
exports.Transformer = Transformer;
|
||||
|
||||
// transform(str) returns the transformed String
|
||||
exports.transform = transform;
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// --SECTION-- END-OF-FILE
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
/// Local Variables:
|
||||
/// mode: outline-minor
|
||||
/// outline-regexp: "/// @brief\\|/// @addtogroup\\|/// @page\\|// --SECTION--\\|/// @\\}\\|/\\*jslint"
|
||||
/// End:
|
Loading…
Reference in New Issue