From 7c01a4686668dd9f6aa7d2d82c01145dbf876adf Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Thu, 2 Nov 2017 09:39:22 -0600 Subject: [PATCH] Correctly set file path for discovered tag + tests --- src/compiler/taglib-loader/scanTagsDir.js | 31 ++++++++++--------- .../components/bar/index.marko | 0 .../components/baz.marko | 0 .../code-generator-only/code-generator.js | 17 ++++++++++ .../components/foo/code-generator.js | 17 ++++++++++ .../components/foo/marko-tag.json | 4 +++ .../expected.js | 27 ++++++++++++++++ .../template.marko | 1 + .../createCompileContext-getTagDef/test.js | 28 +++++++++++++++++ 9 files changed, 111 insertions(+), 14 deletions(-) create mode 100644 test/autotests/api-compiler/createCompileContext-getTagDef/components/bar/index.marko create mode 100644 test/autotests/api-compiler/createCompileContext-getTagDef/components/baz.marko create mode 100644 test/autotests/api-compiler/createCompileContext-getTagDef/components/code-generator-only/code-generator.js create mode 100644 test/autotests/api-compiler/createCompileContext-getTagDef/components/foo/code-generator.js create mode 100644 test/autotests/api-compiler/createCompileContext-getTagDef/components/foo/marko-tag.json create mode 100644 test/autotests/api-compiler/createCompileContext-getTagDef/expected.js create mode 100644 test/autotests/api-compiler/createCompileContext-getTagDef/template.marko create mode 100644 test/autotests/api-compiler/createCompileContext-getTagDef/test.js diff --git a/src/compiler/taglib-loader/scanTagsDir.js b/src/compiler/taglib-loader/scanTagsDir.js index 56821be678..ff3509d6e0 100644 --- a/src/compiler/taglib-loader/scanTagsDir.js +++ b/src/compiler/taglib-loader/scanTagsDir.js @@ -81,7 +81,7 @@ function findAndSetFile(tagDef, tagDirname) { if(path) { tagDef[type] = path; - return true; + return path; } } } @@ -127,39 +127,42 @@ module.exports = function scanTagsDir(tagsConfigPath, tagsConfigDirname, dir, ta let tagName; let tagDef = null; let tagDirname; - let tagJsonPath; + let tagFilePath; let ext = nodePath.extname(childFilename); if (ext === '.marko') { tagName = childFilename.slice(0, 0 - ext.length); tagDirname = dir; tagDef = createDefaultTagDef(); - tagDef.template = nodePath.join(dir, childFilename); + debugger; + tagDef.template = tagFilePath = nodePath.join(dir, childFilename); } else { tagName = prefix + childFilename; tagDirname = nodePath.join(dir, childFilename); - tagJsonPath = nodePath.join(tagDirname, 'marko-tag.json'); + tagFilePath = nodePath.join(tagDirname, 'marko-tag.json'); let hasTagJson = false; - if (fs.existsSync(tagJsonPath)) { + if (fs.existsSync(tagFilePath)) { hasTagJson = true; // marko-tag.json exists in the directory, use that as the tag definition try { - tagDef = JSON.parse(stripJsonComments(fs.readFileSync(tagJsonPath, fsReadOptions))); + tagDef = JSON.parse(stripJsonComments(fs.readFileSync(tagFilePath, fsReadOptions))); } catch(e) { - throw new Error('Unable to parse JSON file at path "' + tagJsonPath + '". Error: ' + e); + throw new Error('Unable to parse JSON file at path "' + tagFilePath + '". Error: ' + e); } } else { - tagJsonPath = null; + tagFilePath = null; tagDef = createDefaultTagDef(); } if (!hasFile(tagDef)) { - let fileWasSet = findAndSetFile(tagDef, tagDirname); - if(!fileWasSet) { + let setFile = findAndSetFile(tagDef, tagDirname); + if(setFile) { + tagFilePath = tagFilePath || setFile; + } else { if (hasTagJson) { - throw new Error('Invalid tag file: ' + tagJsonPath + '. Neither a renderer or a template was found for tag. ' + JSON.stringify(tagDef, null, 2)); + throw new Error('Invalid tag file: ' + tagFilePath + '. Neither a renderer or a template was found for tag. ' + JSON.stringify(tagDef, null, 2)); } else { // Skip this directory... there doesn't appear to be anything in it continue; @@ -178,13 +181,13 @@ module.exports = function scanTagsDir(tagsConfigPath, tagsConfigDirname, dir, ta let tagDependencyChain; - if (tagJsonPath) { - tagDependencyChain = dependencyChain.append(tagJsonPath); + if (tagFilePath) { + tagDependencyChain = dependencyChain.append(tagFilePath); } else { tagDependencyChain = dependencyChain.append(tagDirname); } - let tag = new types.Tag(tagJsonPath || tagDirname); + let tag = new types.Tag(tagFilePath || tagDirname); loaders.loadTagFromProps(tag, tagDef, tagDependencyChain); tag.name = tag.name || tagName; taglib.addTag(tag); diff --git a/test/autotests/api-compiler/createCompileContext-getTagDef/components/bar/index.marko b/test/autotests/api-compiler/createCompileContext-getTagDef/components/bar/index.marko new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/autotests/api-compiler/createCompileContext-getTagDef/components/baz.marko b/test/autotests/api-compiler/createCompileContext-getTagDef/components/baz.marko new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/autotests/api-compiler/createCompileContext-getTagDef/components/code-generator-only/code-generator.js b/test/autotests/api-compiler/createCompileContext-getTagDef/components/code-generator-only/code-generator.js new file mode 100644 index 0000000000..320e6a7926 --- /dev/null +++ b/test/autotests/api-compiler/createCompileContext-getTagDef/components/code-generator-only/code-generator.js @@ -0,0 +1,17 @@ +const marked = require('marked'); + +function removeIndentation (str) { + var indentMatches = /\s*\n(\s+)/.exec(str); + if (indentMatches) { + var indent = indentMatches[1]; + str = str.replace(new RegExp('^' + indent, 'mg'), ''); + } + return str; +} + +module.exports = function (el, codegen) { + var bodyText = removeIndentation(el.bodyText); + var builder = codegen.builder; + var html = marked(bodyText); + return builder.html(builder.literal(html)); +}; diff --git a/test/autotests/api-compiler/createCompileContext-getTagDef/components/foo/code-generator.js b/test/autotests/api-compiler/createCompileContext-getTagDef/components/foo/code-generator.js new file mode 100644 index 0000000000..320e6a7926 --- /dev/null +++ b/test/autotests/api-compiler/createCompileContext-getTagDef/components/foo/code-generator.js @@ -0,0 +1,17 @@ +const marked = require('marked'); + +function removeIndentation (str) { + var indentMatches = /\s*\n(\s+)/.exec(str); + if (indentMatches) { + var indent = indentMatches[1]; + str = str.replace(new RegExp('^' + indent, 'mg'), ''); + } + return str; +} + +module.exports = function (el, codegen) { + var bodyText = removeIndentation(el.bodyText); + var builder = codegen.builder; + var html = marked(bodyText); + return builder.html(builder.literal(html)); +}; diff --git a/test/autotests/api-compiler/createCompileContext-getTagDef/components/foo/marko-tag.json b/test/autotests/api-compiler/createCompileContext-getTagDef/components/foo/marko-tag.json new file mode 100644 index 0000000000..1632660eae --- /dev/null +++ b/test/autotests/api-compiler/createCompileContext-getTagDef/components/foo/marko-tag.json @@ -0,0 +1,4 @@ +{ + "body": "static-text", + "preserve-whitespace": true +} diff --git a/test/autotests/api-compiler/createCompileContext-getTagDef/expected.js b/test/autotests/api-compiler/createCompileContext-getTagDef/expected.js new file mode 100644 index 0000000000..86fbee0b10 --- /dev/null +++ b/test/autotests/api-compiler/createCompileContext-getTagDef/expected.js @@ -0,0 +1,27 @@ +"use strict"; + +var marko_template = module.exports = require("marko/src/vdom").t(), + components_helpers = require("marko/src/components/helpers"), + marko_registerComponent = components_helpers.rc, + marko_componentType = marko_registerComponent("/marko-test$1.0.0/autotests/api-compiler/compileFileForBrowser.js/template.marko", function() { + return module.exports; + }), + marko_renderer = components_helpers.r, + marko_defineComponent = components_helpers.c; + +function render(input, out, __component, component, state) { + var data = input; + + out.t("Hello "); + + out.t(data.name); + + out.t("!"); +} + +marko_template._ = marko_renderer(render, { + ___implicit: true, + ___type: marko_componentType + }); + +marko_template.Component = marko_defineComponent({}, marko_template._); diff --git a/test/autotests/api-compiler/createCompileContext-getTagDef/template.marko b/test/autotests/api-compiler/createCompileContext-getTagDef/template.marko new file mode 100644 index 0000000000..4b66737517 --- /dev/null +++ b/test/autotests/api-compiler/createCompileContext-getTagDef/template.marko @@ -0,0 +1 @@ +-- Hello ${data.name}! \ No newline at end of file diff --git a/test/autotests/api-compiler/createCompileContext-getTagDef/test.js b/test/autotests/api-compiler/createCompileContext-getTagDef/test.js new file mode 100644 index 0000000000..106c54e280 --- /dev/null +++ b/test/autotests/api-compiler/createCompileContext-getTagDef/test.js @@ -0,0 +1,28 @@ +var path = require('path'); + +exports.check = function(marko, markoCompiler, expect, helpers, done) { + var compiler = require('marko/compiler'); + var templatePath = path.join(__dirname, 'template.marko'); + + var compileContext = compiler.createCompileContext(templatePath); + + var tagDef; + + tagDef = compileContext.getTagDef('foo'); + expect(tagDef.filePath).to.equal(path.join(__dirname, 'components/foo/marko-tag.json')); + expect(tagDef.dir).to.equal(path.join(__dirname, 'components/foo')); + + tagDef = compileContext.getTagDef('bar'); + expect(tagDef.filePath).to.equal(path.join(__dirname, 'components/bar/index.marko')); + expect(tagDef.dir).to.equal(path.join(__dirname, 'components/bar')); + + tagDef = compileContext.getTagDef('baz'); + expect(tagDef.filePath).to.equal(path.join(__dirname, 'components/baz.marko')); + expect(tagDef.dir).to.equal(path.join(__dirname, 'components')); + + tagDef = compileContext.getTagDef('code-generator-only'); + expect(tagDef.filePath).to.equal(path.join(__dirname, 'components/code-generator-only/code-generator.js')); + expect(tagDef.dir).to.equal(path.join(__dirname, 'components/code-generator-only')); + + done(); +};