【问题标题】:Disable check for key sorting in JS objects禁用检查 JS 对象中的键排序
【发布时间】:2018-11-23 14:01:10
【问题描述】:

我有一个使用create-react-app 创建的应用程序。默认情况下,它似乎检查对象键是否按字母顺序排序。当我自己键入代码时,这还不错,但是当我从其他来源复制'n'paste 时,这很疯狂。这是一个例子:

const styles = theme => ({
  appBar: {
    zIndex: theme.zIndex.drawer + 1,
    transition: theme.transitions.create(['width', 'margin'], {
      easing: theme.transitions.easing.sharp,
      duration: theme.transitions.duration.leavingScreen
    })
  },
  appBarShift: {
    marginLeft: drawerWidth,
    width: `calc(100% - ${drawerWidth}px)`,
    transition: theme.transitions.create(['width', 'margin'], {
      easing: theme.transitions.easing.sharp,
      duration: theme.transitions.duration.enteringScreen
    })
  },
  content: {
    flexGrow: 1,
    backgroundColor: theme.palette.background.default,
    padding: theme.spacing.unit * 3
  },
  drawerPaper: {
    position: 'relative',
    whiteSpace: 'nowrap',
    width: drawerWidth,
    transition: theme.transitions.create('width', {
      easing: theme.transitions.easing.sharp,
      duration: theme.transitions.duration.enteringScreen
    })
  },
  drawerPaperClose: {
    overflowX: 'hidden',
    transition: theme.transitions.create('width', {
      easing: theme.transitions.easing.sharp,
      duration: theme.transitions.duration.leavingScreen
    }),
    width: theme.spacing.unit * 7,
    [theme.breakpoints.up('sm')]: {
      width: theme.spacing.unit * 9
    }
  },
  hide: {
    display: 'none'
  },
  menuButton: {
    marginLeft: 12,
    marginRight: 36
  },
  root: {
    flexGrow: 1,
    height: 430,
    zIndex: 1,
    overflow: 'hidden',
    position: 'relative',
    display: 'flex'
  },
  toolbar: {
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'flex-end',
    padding: '0 8px',
    ...theme.mixins.toolbar
  }
});

我对第一级键进行了排序,但它似乎也检查了嵌套的键!现在我得到了

C:/Source/portal/src/components/MenuAppBar.js
(19,5): The key 'transition' is not sorted alphabetically

我似乎找不到启用 JS linting 的方法。有关于禁用 tslint 的提示,但在这种情况下我没有使用 Typescript。

我正在使用 VS Code 并尝试过 Sort JS object keysSort JSON objects。不幸的是,它们都没有对嵌套键进行排序。

【问题讨论】:

  • 那么你的项目根目录中有 .eslintrc 文件吗? eslint 是 create-react-app 的依赖,所以它应该是。你需要在那里禁用sort-keys 规则
  • 是的,我在其中添加了"sort-keys": false,但没有任何效果。我猜我做错了。
  • 尝试将其设置为0 而不是false。根据我过去的经验,我的.eslintrc 文件完全被create-react-app 所忽略。除了弹出您的应用程序 (npm run eject) 之外,可能没有其他解决方案,我不建议这样做。
  • 刚试过0,但也没有用。我正在使用 VS Code 并尝试过 Sort JS object keysSort JSON objects。不幸的是,它们都没有对嵌套键进行排序。

标签: javascript eslint create-react-app lint


【解决方案1】:

/* eslint sort-keys: 0 */

在样式文件的顶部添加这个

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-02
    • 2021-06-28
    • 2015-06-19
    • 2012-09-05
    • 2022-12-04
    • 2019-04-27
    • 1970-01-01
    • 2012-12-26
    相关资源
    最近更新 更多