首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用类型记录-eslint验证类型记录文件的ejs模板?

如何使用类型记录-eslint验证类型记录文件的ejs模板?
EN

Stack Overflow用户
提问于 2019-06-01 01:36:25
回答 1查看 1.4K关注 0票数 1

我有一个类型记录文件的ejs模板,我需要用#jest编写一个带有#jest的单元测试来验证我的模板使用# typescript -eslint呈现的类型记录输出(不是tslint,因为它很快就会被废弃:https://medium.com/palantir/tslint-in-2019-1a144c2317a9)

因此,我需要在我的spec文件中要求类型转录-eslint模块,并通过以下代码运行已呈现的ejs的验证:

代码语言:javascript
运行
复制
it('should be valid typescript file', () => {
        return ejs.renderFile('my-ejs-template.ts.ejs', {}).then(async (view) => {
            const eslint = require('@typescript-eslint/eslint-plugin');
            // what is next ???
        });
    });

如有任何帮助,我们将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-30 15:52:20

首先,在devDependencies中安装以下模块:

  • @typescript-eslint/eslint-plugin
  • @typescript-eslint/解析器

其次,将模块eslint导入到规范文件中,如下所示:

代码语言:javascript
运行
复制
const eslint = require('eslint');

第三,创建一个解析类型记录文件的.eslintrc.js配置文件,如:

代码语言:javascript
运行
复制
module.exports = {
  env: {
    node: true,
    es6: true,
  },
  extends: ['plugin:@typescript-eslint/recommended'],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    sourceType: 'module',
    typescript: true,
    modules: true,
    ecmaVersion: 6,
    ecmaFeatures: {
      experimentalObjectRestSpread: true,
    },
  },
  plugins: ['@typescript-eslint'],
  rules: {
    indent: ['error', 4],
    'linebreak-style': ['error', 'unix'],
    quotes: ['error', 'single'],
    semi: ['error', 'always'],
  },
};

然后,在spec文件中导入上述配置文件,并创建一个函数以获得linter:

代码语言:javascript
运行
复制
const configTsEslint = require('.eslintrc-typescript');
const tsLinter = () => {
    const Linter = eslint.Linter;
    return new Linter();
};

最后,验证您的类型记录文件如下:

代码语言:javascript
运行
复制
it('should be valid typescript file', () => {
        return ejs.renderFile('my-ejs-template.ts.ejs', {}).then(async (view) => {
            const result = tsLinter().verifyAndFix(view);
            expect(result.messages).toStrictEqual([]);
            expect(result.fixed).toBe(true);
        });
    });

您可以添加由类型记录-eslint支持的规则。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56403252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档