【问题标题】:EJS Lint Object Return ValueEJS Lint 对象返回值
【发布时间】: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


【解决方案1】:

我刚才遇到了类似的问题,我相信当没有发现 linting 错误时,undefined 返回值是预期的。

正如你所说,ejs-lint 依赖于syntax-error 进行错误检查。 documentation for syntax-error 声明:

如果src 有语法错误,则返回一个可以打印或字符串化的错误对象err

如果src 中没有语法错误,则返回undefined

类似地,如果您故意将语法错误扔到您的 EJS 文件中作为健全性检查,您应该会得到一个错误对象。您也可以使用ejslint CLI 命令手动检查文件。

当我测试您发布的部分代码时,我得到了undefined

【讨论】:

    猜你喜欢
    • 2010-11-02
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    相关资源
    最近更新 更多