【问题标题】:Expected indentation of 4 characters预期缩进 4 个字符
【发布时间】:2015-10-18 02:56:43
【问题描述】:

更新

作为@dev-null(顺便说一句,很多:))建议(见评论) 正确的方法是

jscs --fix file.js

我的缩进有什么问题?

.editorconfig

root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

.jscsrc

{
    "disallowKeywords": ["with"],
    "disallowKeywordsOnNewLine": ["else"],
    "disallowMixedSpacesAndTabs": true,
    "disallowMultipleVarDecl": "exceptUndefined",
    "disallowNewlineBeforeBlockStatements": true,
    "disallowQuotedKeysInObjects": true,
    "disallowSpaceAfterObjectKeys": true,
    "disallowSpaceAfterPrefixUnaryOperators": true,
    "disallowSpacesInFunction": {
        "beforeOpeningRoundBrace": true
    },
    "disallowSpacesInsideParentheses": true,
    "disallowTrailingWhitespace": true,
    "maximumLineLength": 100,
    "requireCamelCaseOrUpperCaseIdentifiers": true,
    "requireCapitalizedComments": true,
    "requireCapitalizedConstructors": true,
    "requireCurlyBraces": true,
    "requireSpaceAfterKeywords": [
        "if",
        "else",
        "for",
        "while",
        "do",
        "switch",
        "case",
        "return",
        "try",
        "catch",
        "typeof"
    ],
    "requireSpaceAfterLineComment": true,
    "requireSpaceAfterBinaryOperators": true,
    "requireSpaceBeforeBinaryOperators": true,
    "requireSpaceBeforeBlockStatements": true,
    "requireSpaceBeforeObjectValues": true,
    "requireSpacesInFunction": {
        "beforeOpeningCurlyBrace": true
    },
    "validateIndentation": 4,
    "validateLineBreaks": "LF",
    "validateQuoteMarks": "'"
}

index.js

'use strict';

/**
 * Module dependencies.
 */
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename  = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/database.json')[env];
const db = {};
const sequelizeFactory = (config) => {
  if (config.use_env_variable) {
    return new Sequelize(process.env[config.use_env_variable]);
  } else {
    return new Sequelize(config.database, config.username,config.password,config);
  }
};

const sequelize = sequelizeFactory(config);

fs
.readdirSync(__dirname)
.filter((file) => {
  return (file.indexOf('.') !== 0) && (file !== basename);
})
.forEach((file) => {
  let model = sequelize['import'](path.join(__dirname, file));
  db[model.name] = model;
});

Object.keys(db).forEach((modelName) => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

错误

Expected indentation of 4 characters at src/models/index.js :
    12 |const db = {};
    13 |const sequelizeFactory = (config) => {
    14 |  if (config.use_env_variable) {
------------^
    15 |    return new Sequelize(process.env[config.use_env_variable]);
    16 |  } else {

All identifiers must be camelCase or UPPER_CASE at src/models/index.js :
    12 |const db = {};
    13 |const sequelizeFactory = (config) => {
    14 |  if (config.use_env_variable) {
---------------------^
    15 |    return new Sequelize(process.env[config.use_env_variable]);
    16 |  } else {

Expected indentation of 8 characters at src/models/index.js :
    13 |const sequelizeFactory = (config) => {
    14 |  if (config.use_env_variable) {
    15 |    return new Sequelize(process.env[config.use_env_variable]);
----------------^
    16 |  } else {
    17 |    return new Sequelize(config.database, config.username,config.password,config);

All identifiers must be camelCase or UPPER_CASE at src/models/index.js :
    13 |const sequelizeFactory = (config) => {
    14 |  if (config.use_env_variable) {
    15 |    return new Sequelize(process.env[config.use_env_variable]);
----------------------------------------------------^
    16 |  } else {
    17 |    return new Sequelize(config.database, config.username,config.password,config);

Expected indentation of 4 characters at src/models/index.js :
    14 |  if (config.use_env_variable) {
    15 |    return new Sequelize(process.env[config.use_env_variable]);
    16 |  } else {
------------^
    17 |    return new Sequelize(config.database, config.username,config.password,config);
    18 |  }

Expected indentation of 8 characters at src/models/index.js :
    15 |    return new Sequelize(process.env[config.use_env_variable]);
    16 |  } else {
    17 |    return new Sequelize(config.database, config.username,config.password,config);
----------------^
    18 |  }
    19 |};

Expected indentation of 4 characters at src/models/index.js :
    16 |  } else {
    17 |    return new Sequelize(config.database, config.username,config.password,config);
    18 |  }
------------^
    19 |};
    20 |

Expected indentation of 4 characters at src/models/index.js :
    24 |.readdirSync(__dirname)
    25 |.filter((file) => {
    26 |  return (file.indexOf('.') !== 0) && (file !== basename);
------------^
    27 |})
    28 |.forEach((file) => {

Expected indentation of 4 characters at src/models/index.js :
    27 |})
    28 |.forEach((file) => {
    29 |  let model = sequelize['import'](path.join(__dirname, file));
------------^
    30 |  db[model.name] = model;
    31 |});

Expected indentation of 4 characters at src/models/index.js :
    28 |.forEach((file) => {
    29 |  let model = sequelize['import'](path.join(__dirname, file));
    30 |  db[model.name] = model;
------------^
    31 |});
    32 |

Expected indentation of 4 characters at src/models/index.js :
    32 |
    33 |Object.keys(db).forEach((modelName) => {
    34 |  if (db[modelName].associate) {
------------^
    35 |    db[modelName].associate(db);
    36 |  }

Expected indentation of 8 characters at src/models/index.js :
    33 |Object.keys(db).forEach((modelName) => {
    34 |  if (db[modelName].associate) {
    35 |    db[modelName].associate(db);
----------------^
    36 |  }
    37 |});

Expected indentation of 4 characters at src/models/index.js :
    34 |  if (db[modelName].associate) {
    35 |    db[modelName].associate(db);
    36 |  }
------------^
    37 |});
    38 |


13 code style errors found.

所以抛开关于 config.use_env_variable(在这种情况下我同意 jscsrc ^^) 有什么问题和正确的缩进?

【问题讨论】:

  • 仅供参考,您可以运行 jscs --fix file.js-x 来修复您的文件(仅添加/删除空格/换行符)

标签: node.js jscs


【解决方案1】:

index.js 文件中有 2 个空格的缩进。

如果您想要缩进 4 个空格,请重新缩进您的文件 :)

例如:

33 |Object.keys(db).forEach((modelName) => {
34 |  if (db[modelName].associate) {

if之前只有2个空格

【讨论】:

    【解决方案2】:

    如果你想缩进2个空格,你应该把"validateIndentation": 4,改成"validateIndentation": 2,

    我建议你使用预设 (http://jscs.info/overview)

    【讨论】:

    • "validateIndentation": 4,在所有其他文件中都是正确的 O 没有问题
    • 也许你的其他文件有 4 个空格缩进?
    猜你喜欢
    • 2020-08-17
    • 2022-01-21
    • 2018-12-13
    • 2012-04-06
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    • 2023-03-22
    • 2017-11-04
    相关资源
    最近更新 更多