简体   繁体   English

Eslint 内联注释以允许在 javascript 文件中使用双引号

[英]Eslint inline comment to allow double quotes in javascript file

I have some stub json data in a javascript file.我在 javascript 文件中有一些存根 json 数据。 I would like to preserve it as valid JSON with double quotes.我想用双引号将其保留为有效的 JSON。 Eslint expects single quotes in javascript files and I do not want to change that rule globally. Eslint 期望在 javascript 文件中使用单引号,我不想全局更改该规则。

How do I tell eslint inline to allow double quotes within a given code block without placing //eslint-disable-line at the end of each line?如何告诉 eslint inline 在给定的代码块中允许双引号,而不在每行的末尾放置//eslint-disable-line line?

I've tried placing the following at the top of the file with no success:我尝试将以下内容放在文件顶部但没有成功:

/*eslint quotes: [2, "double"]*/
var sampleData = [
  {"id": 1, "name": "foo"},
  {"id": 2, "name": "bar"},
    ...
];

Even it that did work, I only want to address double quotes in the code block containing double quotes, not the whole file.即使它确实有效,我也只想在包含双引号的代码块中处理双引号,而不是整个文件。

You can disable certain rules for a block by placing a disable rule before the block, and an enable block after it again:您可以通过在块之前放置禁用规则并再次在其后放置启用块来禁用块的某些规则:

/* eslint-disable no-alert, no-console */
var sampleData = [
  {"id": 1, "name": "foo"},
  {"id": 2, "name": "bar"},
    ...
];
/* eslint-enable no-alert, no-console */

Sometimes it is cumbersome to find a specific syntax to disallow a specific eslint rule.有时要找到一个特定的语法来禁止特定的 eslint 规则是很麻烦的。 In this situations you can use "global" disabling for a code block as the following:在这种情况下,您可以对代码块使用“全局”禁用,如下所示:

/*eslint-disable */

const sampleData = [
  {"id": 1, "name": "foo"},
  {"id": 2, "name": "bar"},
    ...
];

/*eslint-enable */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM