diff --git a/compiler/util/adjustIndent.js b/compiler/util/adjustIndent.js new file mode 100644 index 000000000..c6421932f --- /dev/null +++ b/compiler/util/adjustIndent.js @@ -0,0 +1,69 @@ +var splitLinesRegExp = /\r?\n/; +var initialIndentationRegExp = /^\s+/; + +// var initialSpaceMatches = /^\s+/.exec(text); +// if (initialSpaceMatches) { +// var indentMatches = /\n[^\n]*$/.exec(initialSpaceMatches[0]); +// if (indentMatches) { +// var indentRegExp = new RegExp(indentMatches[0].replace(/\n/g, '\\n'), 'g'); +// text = text.replace(indentRegExp, '\n'); +// } +// text = text.replace(/^\s*/, '').replace(/\s*$/, ''); +// this.setText(text); +// } +function adjustIndent(str, newIndentation) { + if (!str) { + return str; + } + + var lines = str.split(splitLinesRegExp); + var initialIndentationMatches = initialIndentationRegExp.exec(lines[0]); + + var indentation = initialIndentationMatches ? initialIndentationMatches[0] : ''; + if (!indentation && !newIndentation) { + return str; + } + + lines.forEach((line, i) => { + if (line.startsWith(indentation)) { + line = line.substring(indentation.length); + } + + lines[i] = line; + }); + + return newIndentation ? + newIndentation + lines.join('\n' + newIndentation) : + lines.join('\n'); +} + +// function adjustIndent(str, newIndentation) { +// if (!str) { +// return str; +// } +// +// var lines = str.split(splitLinesRegExp); +// var initialIndentationMatches = initialIndentationRegExp.exec(lines[0]); +// +// var indentation = initialIndentationMatches ? initialIndentationMatches[0] : ''; +// if (!indentation && !newIndentation) { +// return str; +// } +// +// var result = ''; +// +// lines.forEach((line, i) => { +// if (line.startsWith(indentation)) { +// line = line.substring(indentation.length); +// } +// +// if (newIndentation) { +// line = newIndentation + line; +// } +// +// result += line + '\n'; +// }); +// +// return result; +// } +module.exports = adjustIndent; \ No newline at end of file diff --git a/test/adjustIndent-test.js b/test/adjustIndent-test.js new file mode 100644 index 000000000..2576bfb17 --- /dev/null +++ b/test/adjustIndent-test.js @@ -0,0 +1,25 @@ +'use strict'; +var chai = require('chai'); +chai.config.includeStack = true; +var path = require('path'); +var adjustIndent = require('../compiler/util/adjustIndent'); +var autotest = require('./autotest'); +var fs = require('fs'); + +describe('compiler/util/adjustIndent', function() { + var autoTestDir = path.join(__dirname, 'fixtures/adjustIndent/autotest'); + + autotest.scanDir( + autoTestDir, + function run(dir) { + var inputPath = path.join(dir, 'input.txt'); + var testSettings = require(path.join(dir, 'test.js')); + var input = fs.readFileSync(inputPath, { encoding: 'utf8' }); + var newIndentation = testSettings.newIndentation; + var output = adjustIndent(input, newIndentation); + return output; + }, + { + compareExtension: '.txt' + }); +}); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/bad-indent/actual.txt b/test/fixtures/adjustIndent/autotest/bad-indent/actual.txt new file mode 100644 index 000000000..5af2954ba --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/bad-indent/actual.txt @@ -0,0 +1,3 @@ + var foo = require('foo'); + var bar = require('bar'); + var baz = require('baz'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/bad-indent/expected.txt b/test/fixtures/adjustIndent/autotest/bad-indent/expected.txt new file mode 100644 index 000000000..5af2954ba --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/bad-indent/expected.txt @@ -0,0 +1,3 @@ + var foo = require('foo'); + var bar = require('bar'); + var baz = require('baz'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/bad-indent/input.txt b/test/fixtures/adjustIndent/autotest/bad-indent/input.txt new file mode 100644 index 000000000..a05184db3 --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/bad-indent/input.txt @@ -0,0 +1,3 @@ + var foo = require('foo'); +var bar = require('bar'); + var baz = require('baz'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/bad-indent/test.js b/test/fixtures/adjustIndent/autotest/bad-indent/test.js new file mode 100644 index 000000000..1174036bb --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/bad-indent/test.js @@ -0,0 +1 @@ +exports.newIndentation = ' '; \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/empty/actual.txt b/test/fixtures/adjustIndent/autotest/empty/actual.txt new file mode 100644 index 000000000..e69de29bb diff --git a/test/fixtures/adjustIndent/autotest/empty/expected.txt b/test/fixtures/adjustIndent/autotest/empty/expected.txt new file mode 100644 index 000000000..e69de29bb diff --git a/test/fixtures/adjustIndent/autotest/empty/input.txt b/test/fixtures/adjustIndent/autotest/empty/input.txt new file mode 100644 index 000000000..e69de29bb diff --git a/test/fixtures/adjustIndent/autotest/empty/test.js b/test/fixtures/adjustIndent/autotest/empty/test.js new file mode 100644 index 000000000..91f4d398e --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/empty/test.js @@ -0,0 +1 @@ +exports.newIndentation = ' '; \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/increase-indent/actual.txt b/test/fixtures/adjustIndent/autotest/increase-indent/actual.txt new file mode 100644 index 000000000..ab84318e5 --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/increase-indent/actual.txt @@ -0,0 +1,2 @@ + var foo = require('foo'); + var bar = require('bar'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/increase-indent/expected.txt b/test/fixtures/adjustIndent/autotest/increase-indent/expected.txt new file mode 100644 index 000000000..ab84318e5 --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/increase-indent/expected.txt @@ -0,0 +1,2 @@ + var foo = require('foo'); + var bar = require('bar'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/increase-indent/input.txt b/test/fixtures/adjustIndent/autotest/increase-indent/input.txt new file mode 100644 index 000000000..f9c033403 --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/increase-indent/input.txt @@ -0,0 +1,2 @@ + var foo = require('foo'); + var bar = require('bar'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/increase-indent/test.js b/test/fixtures/adjustIndent/autotest/increase-indent/test.js new file mode 100644 index 000000000..91f4d398e --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/increase-indent/test.js @@ -0,0 +1 @@ +exports.newIndentation = ' '; \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/no-indent/actual.txt b/test/fixtures/adjustIndent/autotest/no-indent/actual.txt new file mode 100644 index 000000000..5af2954ba --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/no-indent/actual.txt @@ -0,0 +1,3 @@ + var foo = require('foo'); + var bar = require('bar'); + var baz = require('baz'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/no-indent/expected.txt b/test/fixtures/adjustIndent/autotest/no-indent/expected.txt new file mode 100644 index 000000000..5af2954ba --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/no-indent/expected.txt @@ -0,0 +1,3 @@ + var foo = require('foo'); + var bar = require('bar'); + var baz = require('baz'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/no-indent/input.txt b/test/fixtures/adjustIndent/autotest/no-indent/input.txt new file mode 100644 index 000000000..cd328cc09 --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/no-indent/input.txt @@ -0,0 +1,3 @@ +var foo = require('foo'); +var bar = require('bar'); +var baz = require('baz'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/no-indent/test.js b/test/fixtures/adjustIndent/autotest/no-indent/test.js new file mode 100644 index 000000000..1174036bb --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/no-indent/test.js @@ -0,0 +1 @@ +exports.newIndentation = ' '; \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/remove-indent/actual.txt b/test/fixtures/adjustIndent/autotest/remove-indent/actual.txt new file mode 100644 index 000000000..c4ea2d57d --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/remove-indent/actual.txt @@ -0,0 +1,2 @@ +var foo = require('foo'); +var bar = require('bar'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/remove-indent/expected.txt b/test/fixtures/adjustIndent/autotest/remove-indent/expected.txt new file mode 100644 index 000000000..c4ea2d57d --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/remove-indent/expected.txt @@ -0,0 +1,2 @@ +var foo = require('foo'); +var bar = require('bar'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/remove-indent/input.txt b/test/fixtures/adjustIndent/autotest/remove-indent/input.txt new file mode 100644 index 000000000..f9c033403 --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/remove-indent/input.txt @@ -0,0 +1,2 @@ + var foo = require('foo'); + var bar = require('bar'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/remove-indent/test.js b/test/fixtures/adjustIndent/autotest/remove-indent/test.js new file mode 100644 index 000000000..971ed64a6 --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/remove-indent/test.js @@ -0,0 +1 @@ +exports.newIndentation = ''; \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/tab-indent/actual.txt b/test/fixtures/adjustIndent/autotest/tab-indent/actual.txt new file mode 100644 index 000000000..652d77bad --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/tab-indent/actual.txt @@ -0,0 +1,3 @@ + var foo = require('foo'); + var bar = require('bar'); + var baz = require('baz'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/tab-indent/expected.txt b/test/fixtures/adjustIndent/autotest/tab-indent/expected.txt new file mode 100644 index 000000000..652d77bad --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/tab-indent/expected.txt @@ -0,0 +1,3 @@ + var foo = require('foo'); + var bar = require('bar'); + var baz = require('baz'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/tab-indent/input.txt b/test/fixtures/adjustIndent/autotest/tab-indent/input.txt new file mode 100644 index 000000000..f5eb70b1a --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/tab-indent/input.txt @@ -0,0 +1,3 @@ + var foo = require('foo'); + var bar = require('bar'); + var baz = require('baz'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/tab-indent/test.js b/test/fixtures/adjustIndent/autotest/tab-indent/test.js new file mode 100644 index 000000000..91f4d398e --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/tab-indent/test.js @@ -0,0 +1 @@ +exports.newIndentation = ' '; \ No newline at end of file diff --git a/test/fixtures/compiler/autotest/template-init/expected.js b/test/fixtures/compiler/autotest/template-init/expected.js new file mode 100644 index 000000000..65111b3c0 --- /dev/null +++ b/test/fixtures/compiler/autotest/template-init/expected.js @@ -0,0 +1,43 @@ +function create(__helpers) { + var str = __helpers.s, + empty = __helpers.e, + notEmpty = __helpers.ne, + escapeXml = __helpers.x, + forEach = __helpers.f; + + return function render(data, out) { + out.w("Hello " + + escapeXml(data.name) + + "! "); + + if (notEmpty(data.colors)) { + out.w("