Skip to content

Commit

Permalink
Correctly set file path for discovered tag + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-steele-idem committed Nov 7, 2017
1 parent fc77a1a commit 7c01a46
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/compiler/taglib-loader/scanTagsDir.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function findAndSetFile(tagDef, tagDirname) {

if(path) {
tagDef[type] = path;
return true;
return path;
}
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -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));
};
Original file line number Diff line number Diff line change
@@ -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));
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"body": "static-text",
"preserve-whitespace": true
}
Original file line number Diff line number Diff line change
@@ -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._);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- Hello ${data.name}!
28 changes: 28 additions & 0 deletions test/autotests/api-compiler/createCompileContext-getTagDef/test.js
Original file line number Diff line number Diff line change
@@ -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();
};

0 comments on commit 7c01a46

Please sign in to comment.