quote-props

需要在对象字面属性名称周围加上引号

一些该规则报告的问题可以通过 --fix 命令行选项 自动修复

对象字面属性名称可以通过两种方式定义:使用字面或使用字符串。例如,这两个对象是等价的:

var object1 = {
    property: true
};

var object2 = {
    "property": true
};

在许多情况下,选择使用标识符而不是字符串并不重要,反之亦然。即使这样,您也可能决定在代码中强制使用一致的样式。

但是,在某些情况下您必须使用引号:

  1. 如果您使用的是 ECMAScript 3 JavaScript 引擎(例如 IE8)并且您想使用关键字(例如 if)作为属性名称。这个限制在 ECMAScript 5 中被移除。
  2. 您想在属性名称中使用非标识符字符,例如有一个带有空格的属性,如 "one two"

dKrSquKpXIh4aOx93hwA5yHQ4b69OSGMkI8DAhAvVqUH9cQMkGvHfemi1yDx2JGIODuMjUCrngFtHTU8QAEMVDLqBcSpALvk3UxsOcjimSCop2bNzB/N3Dg9W+hOpXEe

var object = {
    1e2: 1,
    100: 2
};

nKQ+CSxSbFiJua1auPBfy144UwxhP8hXQQ5f+3GWcfMs/d6cHa0q7TzGkO2Up8my32ikvEQdloLWtWnw9Z0jF4OU/75AEHN/R5jT/Y3HLIHC1MDCbyGEL9QQ/6Fl6+plzaXpR4KKEoB4zowAk/GytDCeAa+fdsumNegbMT8HzsPLshlMOCMbJLtbrXQ43l20goHEyLIwdKyBNNA5oJf/fM489oPH0us0ClQTFMAf6sn0VzUznff0ie0NdI1VONye2UvI1IwAgyw0dBFdSM2aFJ1d2/x7d97QldVQhv0UAX/Ew3LiM6q5cIMnZGr1/qhPf/Ep98mlGMRD51bnI6ReHL/w9d81blNnU1jLhXruEM81iu3rzn8zHt1XoiZLEBm0vmYIu1NXU1Cs3sPMmUcrMkYmBQjkS3Jwa+jktVhEzeCwo3YeulaLAIGwnl4fFlF7Mcwo3TBszAMU5+GxhLFUDJyBjusTJtKDtaLsUgYphl6Tpfwri65HMIXlNzEYWya02jJ8IeTKTwLjttKavfn5ymKKW0fYNZGnFM6AyP1DD2SAzjZj8x26UngWCpMj1hnsjn/BAkV1Ib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JoxRLIVULErOss8In2HoEP5O/PtUbr551W9dcVg2sBmp/PLDOgk=

/*eslint quote-props: ["error", "always"]*/

var object = {
    foo: "bar",
    baz: 42
};

gG4UUCWpCvzqVaU47RuenyvGhzswO8gxAVkv9Jz5YphQ1H7Gj78zPRLiJoxRLIVUeFlzp3kvC5vOA39zk10n4UFlqjoKBe5MK6K7K59bLcQ=

/*eslint quote-props: ["error", "always"]*/
/*eslint-env es6*/

var object1 = {
    "foo": "bar",
    "baz": 42,
    "qux-lorem": true
};

var object2 = {
    'foo': 'bar',
    'baz': 42,
    'qux-lorem': true
};

var object3 = {
    foo() {
        return;
    }
};

as-needed

TlLpo7HIJjIy2v3f96HaCezjwrrJw18P64XMifzXQjLOqCdrES9uzdKzcReess/m8RCMrrxy11MEOx3JSrzYxq1+DsQ+j80F3KdPa63cBHk=

/*eslint quote-props: ["error", "as-needed"]*/

var object = {
    "a": 0,
    "0": 0,
    "true": 0,
    "null": 0
};

TlLpo7HIJjIy2v3f96HaCezjwrrJw18P64XMifzXQjLOqCdrES9uzdKzcReess/m27coF6A9B7B3CILjzQlAMnjZbDtRfgvC5MzArGGo7MM=

/*eslint quote-props: ["error", "as-needed"]*/
/*eslint-env es6*/

var object1 = {
    "a-b": 0,
    "0x0": 0,
    "1e2": 0
};

var object2 = {
    foo: 'bar',
    baz: 42,
    true: 0,
    0: 0,
    'qux-lorem': true
};

var object3 = {
    foo() {
        return;
    }
};

consistent

TlLpo7HIJjIy2v3f96HaCfzbEVlbeTOaC8kfieJUbuBws0KBZ3nDVw4wGE39DMr2pcEYtTPVbNYrGCcdBjsKVDwJlJkvaAYa2388rFj/wtI=

/*eslint quote-props: ["error", "consistent"]*/

var object1 = {
    foo: "bar",
    "baz": 42,
    "qux-lorem": true
};

var object2 = {
    'foo': 'bar',
    baz: 42
};

TlLpo7HIJjIy2v3f96HaCfzbEVlbeTOaC8kfieJUbuBws0KBZ3nDVw4wGE39DMr2VbZ6o6zePl8vNrwrp+1JmHWAS+Q+GIdfYvP9vzSytHo=

/*eslint quote-props: ["error", "consistent"]*/

var object1 = {
    "foo": "bar",
    "baz": 42,
    "qux-lorem": true
};

var object2 = {
    'foo': 'bar',
    'baz': 42
};

var object3 = {
    foo: 'bar',
    baz: 42
};

consistent-as-needed

TlLpo7HIJjIy2v3f96HaCfzbEVlbeTOaC8kfieJUbuAW4FwWWwQZsZYcE/zGkcmiKbo9XVtueXaEcq3UqkW9JL4eYoCU6HqGhZJciL9CwdK5aSfprpyiza89+EZwezc5

/*eslint quote-props: ["error", "consistent-as-needed"]*/

var object1 = {
    foo: "bar",
    "baz": 42,
    "qux-lorem": true
};

var object2 = {
    'foo': 'bar',
    'baz': 42
};

TlLpo7HIJjIy2v3f96HaCfzbEVlbeTOaC8kfieJUbuAW4FwWWwQZsZYcE/zGkcmi86MmhVt9FkSf0AgWYX5Y45qYNhphbddrmf0CnMv7+QesbspQPLZFeHvDFuGbP/IN

/*eslint quote-props: ["error", "consistent-as-needed"]*/

var object1 = {
    "foo": "bar",
    "baz": 42,
    "qux-lorem": true
};

var object2 = {
    foo: 'bar',
    baz: 42
};

keywords

FYlDee2at++CRtDuMPHdbFOdPsNkBjVNAFX0JVjsMEkw4uzMGBwdFOg9qrHBy4OnuQG5aUJmbTii2u9mU8PrNq9FIB0tXMopC2CtSbjQOZPf1+2/s/I1yd5DcDEdkrtBejUNllSW3ZPDacB664WA/Q==

/*eslint quote-props: ["error", "as-needed", { "keywords": true }]*/

var x = {
    while: 1,
    volatile: "foo"
};

hBMMFGnI/652YcNexD0AaZvCnFCi4/Yi7158gxNX2nPvLsAaQqmGcYcaB2J7Hx9s3C7KzOCDw6TUcwdj5X/e3E3jyq+1cQ14NKkkAkMQdZUcCR+rpK65nsCdAu5Hgjr/4QaoIblBloLSNjwzO5dKW7hPLlaLePIyc+/iP+r0g4Q=

/*eslint quote-props: ["error", "consistent-as-needed", { "keywords": true }]*/

var x = {
    "prop": 1,
    "bar": "foo"
};

unnecessary

TlLpo7HIJjIy2v3f96HaCezjwrrJw18P64XMifzXQjJBIKRkX8upaygWKxvNd9Ntqbyz9OSb+xeKaTAqi/pAYjQ8bKDwzci7d8iB+CXxyR72+3xcDjK3GoGkVXmCIERw7DZd57bNapKZcZJKjhXQaQ==

/*eslint quote-props: ["error", "as-needed", { "keywords": true, "unnecessary": false }]*/

var x = {
    "while": 1,
    "foo": "bar"  // Would normally have caused a warning
};

numbers

FYlDee2at++CRtDuMPHdbFhlgfMnQYcsSe9VX+NHq2tZJAsjnDrkDkLALdchqltVKBdxW5NtQdnMosuMsLEOD8wHRjeDWEaDvH3M8j4pzGO6hWFyycH7vYRk3DX8zkFTFkBVAZ+Lc94xwRO2kKrmlA==

/*eslint quote-props: ["error", "as-needed", { "numbers": true }]*/

var x = {
    100: 1
}

何时不使用

5gd8tIO1mqlWE60abyjnYARjDrSii3w9UOszDe0V2wfyudcYJgTJt2vXizjtnVgun4k2GtpLjF41LKzgk/EF7bGcmVuWeNGzMLeYoatfy5Tb0l4/klTXBje9VnfWewPIqhA+2hHzkJnDgn2AKrryxq2m7fBrQvbwuE+75PVUQ/w=