JSLint mixed spaces and tabs error

半城伤御伤魂 提交于 2019-12-03 22:16:17

This error occurs when your indentation uses a combination of both spaces and tabs, for example, {SPACE}{SPACE}{TAB}{SPACE} or {TAB}{SPACE}{TAB}. I'm not really sure why it's an error and not a warning, but the solution is to revisit the line and make sure you only use spaces OR tabs.

The problem with mixing tabs and spaces is you could run into indentation issues when the file is viewed in a different application. For instance, one user may have tabs configured to equal two spaces, another could open the first user's file and see uneven indentation because two spaces plus one tab equals 6 spaces as opposed to 4 in the first's app. Using one or the other ensures better readability of your code.

Interestingly enough, Stack Overflow normalizes tabs into 4 spaces, so copying and pasting your code from here back into JSLint fixes the problem.

Dave Dopson

You might also consider using the "smarttabs" option available in JSHint (JSHint is a drop-in replacement for JSLint, just better).

This article is really insightful, objectively explains the tradeoffs involved in tabs v spaces (I didn't realize there was even that much one could say on the subject), and demonstrates how smart tab logic should behave:

http://www.emacswiki.org/emacs/SmartTabs

Basically, if you use tabs for 'indentation' you are allowed to use spaces for 'alignment' as long as any spaces are "used for alignment only", ie that they are preceeded by the correct number of indentation tabs:

Which makes this code snippet legal ("---->" represents a TAB):

function foo() {
---->var a = 4,
---->    b = 5,
---->    c = 6;
}

You can do this with a file called '.jshintrc':

{
    "smarttabs": true
}

Or you can set it in the source code with a comment:

/*jslint smarttabs:true */

Or you could just ditch tabs entirely ... (religious war ensues).

Personally, I use JSHint which is a derivative project of JSLint with arguably more configurability and such. For most purposes, they are the same tool. http://jshint.com/docs/#options. I'd reccomend it. Most options are common between the two tools.

I also don't use tabs. Ever. Given the choice, I'm a two-spaces guy.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!