2010-06-14 2 views
46

자바 스크립트 코드 100 % JSLint를 깨끗하게하려고합니다.JSLint가 예기치 않은 '&'및 '|' - 이것을 깨끗이하고 싶습니다.

UUID를 만들기 위해 다른 JS 코드를 사용하고 있습니다.

s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1); 

이 줄은 두 오류 메시지를 생성하는 JSLint를 선동 : 그 코드는 다음과 같은 라인을 가지고

1) Unexpected use of '&' 
2) Unexpected use of '|' 

이해하지 않는 이유 - 나는 제거하기 위해 코딩하는 방법에 대한 조언을 감사하겠습니다 오류 메시지

+1

나는 중지 한 후 특정 메시지를 다시 활성화하기 위해 나는 JSLint의 의견이 하나 개의 사용을 포장했습니다/* jslint의 비트 : */ – Zhami

답변

76

"이유"가 실제 이유입니다 bitwise 연산은 JS에서는 매우 드물며 JS 코드에 나타나는 연산자는 항상 부울 버전 (&&, ||)의 오타입니다. 그래서 JSLint는 관심을 가지고 있습니다. 이것은 bitwise 작전의 합법적 인 사용이다. 난 당신이 bitwise 플래그와 함께 경고를 침묵 수 있다고 생각 :

/*jslint bitwise: true */ 
+19

사실 내가 함께 "잘못된"코드를 포장 : /* jslint 비트 : 거짓 */ 및 /* jslint 비트 : 사실은 */ 가 잘 작동합니다. – Zhami

+3

그것은 드문 일이 아닙니다. Crockford는 그것이 나쁘다고 생각합니다. JS의 모든 것이 문자열 이었기 때문에 비트 연산자는 결국 비쌌습니다. 그러나 현대의 JS 엔진은이를 최적화하므로 더 이상 그렇지 않습니다. – gotofritz

+1

@fritzfromlondon : 전 노드 일에서 JS에 bitwise 작업을 실제로 수행 한 사람이 아무도 없었습니다. 그 이유는 거의 없었기 때문입니다. 만약 당신이 그런 것처럼 보였다면, 아마도 다른 것을 의미했을 것입니다. . 이는 더 많은 시스템 -Y 코드가 브라우저에서 작성되고 특히 JS의 브라우저 외부에서 작성됨에 따라 변경되었습니다. –

3

bitwise 옵션을 제공 했습니까? 이 옵션은 자바에서 비효율적 인 경향이있는 비트 연산의 모든 사용에 대해 경고합니다 (비트 연산의 경우 int로 변환 된 다음 다시 변환해야 함)

0

당신이 활성화/비활성화를 확인을 지정하는 명령 행에서 설정 파일을 지정할 수 있습니다 this javascript lint 도구를 사용하는 경우. 이

#---------------------------------------------------------------------------- 
# 
# This is a slightly edited version of the jsl.default.conf file that comes 
# with the install package for JavaScript Lint. 
# 
#---------------------------------------------------------------------------- 

# 
# Configuration File for JavaScript Lint 0.3.0 
# Developed by Matthias Miller (http://www.JavaScriptLint.com) 
# 
# This configuration file can be used to lint a collection of scripts, or to 
# enable or disable warnings for scripts that are linted via the command line. 
# 

### Warnings 
# Enable or disable warnings based on requirements. 
# Use "+WarningName" to display or "-WarningName" to suppress. 
# 
-no_return_value    # function {0} does not always return a value 
+duplicate_formal    # duplicate formal argument {0} 
-equal_as_assign    # test for equality (==) mistyped as assignment (=)?{0} 
+var_hides_arg    # variable {0} hides argument 
-redeclared_var    # redeclaration of {0} {1} 
-anon_no_return_value   # anonymous function does not always return a value 
+missing_semicolon   # missing semicolon 
+meaningless_block   # meaningless block; curly braces have no impact 
+comma_separated_stmts  # multiple statements separated by commas (use semicolons?) 
+unreachable_code    # unreachable code 
-missing_break    # missing break statement 
-missing_break_for_last_case # missing break statement for last case in switch 
-comparison_type_conv   # comparisons against null, 0, true, false, or an empty string allowing implicit type conversion (use === or !==) 
-inc_dec_within_stmt   # increment (++) and decrement (--) operators used as part of greater statement 
+useless_void     # use of the void type may be unnecessary (void is always undefined) 
+multiple_plus_minus   # unknown order of operations for successive plus (e.g. x+++y) or minus (e.g. x---y) signs 
+use_of_label     # use of label 
-block_without_braces   # block statement without curly braces 
-leading_decimal_point  # leading decimal point may indicate a number or an object member 
+trailing_decimal_point  # trailing decimal point may indicate a number or an object member 
-octal_number     # leading zeros make an octal number 
+nested_comment    # nested comment 
-misplaced_regex    # regular expressions should be preceded by a left parenthesis, assignment, colon, or comma 
-ambiguous_newline   # unexpected end of line; it is ambiguous whether these lines are part of the same statement 
-empty_statement    # empty statement or extra semicolon 
-missing_option_explicit  # the "option explicit" control comment is missing 
+partial_option_explicit  # the "option explicit" control comment, if used, must be in the first script tag 
+dup_option_explicit   # duplicate "option explicit" control comment 
+useless_assign    # useless assignment 
-ambiguous_nested_stmt  # block statements containing block statements should use curly braces to resolve ambiguity 
+ambiguous_else_stmt   # the else statement could be matched with one of multiple if statements (use curly braces to indicate intent) 
-missing_default_case   # missing default case in switch statement 
+duplicate_case_in_switch  # duplicate case in switch statements 
+default_not_at_end   # the default case is not at the end of the switch statement 
+legacy_cc_not_understood  # couldn't understand control comment using /*@[email protected]*/ syntax 
+jsl_cc_not_understood  # couldn't understand control comment using /*jsl:keyword*/ syntax 
+useless_comparison   # useless comparison; comparing identical expressions 
+with_statement    # with statement hides undeclared variables; use temporary variable instead 
+trailing_comma_in_array  # extra comma is not recommended in array initializers 
+assign_to_function_call  # assignment to a function call 
-parseint_missing_radix  # parseInt missing radix parameter 


### Output format 
# Customize the format of the error message. 
# __FILE__ indicates current file path 
# __FILENAME__ indicates current file name 
# __LINE__ indicates current line 
# __ERROR__ indicates error message 
# 
# Visual Studio syntax (default): 
+output-format __FILE__(__LINE__): __ERROR__ 
# Alternative syntax: 
#+output-format __FILE__:__LINE__: __ERROR__ 


### Context 
# Show the in-line position of the error. 
# Use "+context" to display or "-context" to suppress. 
# 
+context 


### Semicolons 
# By default, assignments of an anonymous function to a variable or 
# property (such as a function prototype) must be followed by a semicolon. 
# 
+lambda_assign_requires_semicolon 


### Control Comments 
# Both JavaScript Lint and the JScript interpreter confuse each other with the syntax for 
# the /*@[email protected]*/ control comments and JScript conditional comments. (The latter is 
# enabled in JScript with @[email protected]). The /*jsl:keyword*/ syntax is preferred for this reason, 
# although legacy control comments are enabled by default for backward compatibility. 
# 
#+legacy_control_comments 


### JScript Function Extensions 
# JScript allows member functions to be defined like this: 
#  function MyObj() { /*constructor*/ } 
#  function MyObj.prototype.go() { /*member function*/ } 
# 
# It also allows events to be attached like this: 
#  function window::onload() { /*init page*/ } 
# 
# This is a Microsoft-only JavaScript extension. Enable this setting to allow them. 
# 
-jscript_function_extensions 


### Defining identifiers 
# By default, "option explicit" is enabled on a per-file basis. 
# To enable this for all files, use "+always_use_option_explicit" 
-always_use_option_explicit 

# Define certain identifiers of which the lint is not aware. 
# (Use this in conjunction with the "undeclared identifier" warning.) 
# 
# Common uses for webpages might be: 
#+define window 
#+define document 


### Interactive 
# Prompt for a keystroke before exiting. 
#+pauseatend 


### Files 
# Specify which files to lint 
# Use "+recurse" to enable recursion (disabled by default). 
# To add a set of files, use "+process FileName", "+process Folder\Path\*.js", 
# or "+process Folder\Path\*.htm". 
# 

P.S.처럼 보이는 jsl.conf 파일로

jsl -conf jsl.conf 

을 다음과 같이

예를 들어, 나는 일반적으로 내 호출 위의 JSLint 검사를 모두 통과하는 my javascript UUID page을 확인하십시오. /* jslint 비트 : 여기에 거짓 */ .... 문 .... :-)