【发布时间】:2019-02-24 12:40:25
【问题描述】:
当我在 npm 存储库中找到 ejs-lint 模块时,我试图在 EJS 模板中查找语法错误。我正在尝试使用它来识别我的模板代码中的语法错误。模块 https://www.npmjs.com/package/ejs-lint 的 npm 文档(在 API 部分下方显示了用法)。我正在尝试通过它运行我的脚本。 linting 脚本的代码:
/*
* For checking ejs scripts
*/
const ejs = require('ejs');
const path = require('path');
const fs = require('fs');
const syntax_checker = require('syntax-error');
const ejslint = require('ejs-lint');
function _template_loader (name){
/*
loads a template and passs it to the given callback
*/
return fs.readFileSync(__dirname+'/views/'+name+'.html', 'utf-8');
}
const _filename = 'index';
(function(){
// main checker
let mainnav_opts = {
langoptions: [
{
href: '#',
langcode: 'en',
langname: 'English',
},
]
};
let jumbo_opts = {
renderParticlesNum : 10
};
let index_opts = {
langoptions: [
{
href: '#',
langcode: 'en',
langname: 'English',
},
],
renderParticlesNum: 10,
};
let index_parsed = ejslint(_template_loader(_filename), index_opts);
let jumbo_parsed = ejslint(_template_loader(_filename), jumbo_opts);
let mainnav_parsed = ejslint(_template_loader(_filename), mainnav_opts);
console.log('Index: '+ (!index_parsed ? 'Passed' : 'Failed'));
console.log('Mainnavbar: '+ (!mainnav_parsed ? 'Passed' : 'Failed'));
console.log('Jumbotron: '+ (!jumbo_parsed ? 'Passed' : 'Failed'));
})();
根据文档,ejslint 函数应该返回一个 node-syntax-error 对象(来自 syntax-error 模块)。但是,我只是变得不确定。这是否意味着没有语法错误(我仍然在页面中遇到一些错误)?
编辑:为了使它更好,我包含了一个 ejs 部分,我认为它会导致错误:
<div id='indexjumbtrn' class='jumbotron'>
<div id='jumbo-bg-container'>
<!--
@ ejs : server sends the number of particles to create and render based on
some initial client heuristics
-->
<% var anim_win_width = document.getElementById('jumbo-bg-container').style.width; %>
<% var anim_win_height = document.getElementById('jumbo-bg-container').style.height; %>
<% function fillpart(){ %>
<% /* fills a particle color randomly */ %>
<% var r = parseInt(Math.floor(Math.random()*255+1)); %>
<% var g = parseInt(Math.floor(Math.random()*255+1)); %>
<% var b = parseInt(Math.floor(Math.random()*255+1)); %>
<% var a = parseFloat((Math.random()*(0.89-0.73)) + (0.73)).toPrecision(2); %>
<% return 'rgba(' + r + ',' + g + ',' + b + ',' + a +')'; %>
<% } %>
<% function randpos(mn, mx){ %>
<% return ((Math.random()*(mx-mn))+mn); %>
<% } %>
<object id='svgobj'>
<svg xmlns='http://www.w3.org/TR/SVG11' viewBox='0 0 450 450' id='dotbox'
width=<%= anim_win_width+'px' %> height=<%= anim_win_height+'px' %>>
<% if(renderParticlesNum !== undefined){ %>
<% var parts = renderParticlesNum; %>
<% parts.forEach(function(p){ %>
<% var fillv = ''+fillpart(); %>
<% var cx = parseInt(randpos(0, anim_win_width)); %>
<% var cy = parseInt(randpos(0, anim_win_height)); %>
<% var r = parseInt(randpos(10, 16)); %>
<% var partid = 'part-'+r(0, 120); %>
<g style='margin:5% 5% 5% 5%;' fill='white' width=24 height=24 id=<%= partid %>> >
<circle class='particle' fill=<%= fillv %> cx=<%= cx %>
cy=<%= cy %> r=<%= r %>></circle>
</g>
<% }); %>
<% } %>
</svg>
</object>
</div>
<div id='create-form' class='container'>
<h2 id='jumbotag'>Tag goes here</h2>
<p><a class='btn btn-primary' id='jumbobtn' href=<%= jumbolink %>>Try Now</a></p>
</div>
</div>
【问题讨论】:
-
“这是否意味着没有语法错误(我仍然在页面中遇到一些错误)?” - 您遇到什么错误?通过快速阅读,我没有看到任何 syntax 错误。
-
你好,错误实际上是在ejs模板代码中,而不是这里的sn-p。而且,仍然存在的另一个错误是 TypeError(如果我是对的,那是因为我在传递给模板的渲染上下文对象中缺少一个值)。那么,澄清一下,ejslint 函数的 undefined 返回值是否意味着模板代码中没有错误?
标签: javascript ejs