2016-07-01 5 views
0

저는 JSHint를 초보자로 삼아 자바 스크립트의 유효성을 검사하려고합니다. node.js가 설치되어 있고 JSHint를 실행하려고하면 아래 오류가 발생합니다. 나는 오류가 발생하는 이유HTML로 JSHint를 실행하고 오류가 발생했습니다.

{ 
"bitwise": true, 
"camelcase": true, 
"curly": true, 
"eqeqeq": true, 
"es3": false, 
"forin": true, 
"freeze": true, 
"immed": true, 
"indent": 4, 
"latedef": "nofunc", 
    "newcap": true, 
    "noarg": true, 
    "noempty": true, 
    "nonbsp": true, 
    "nonew": true, 
"plusplus": false, 
"quotmark": "single", 
"undef": true, 
"unused": false, 
"strict": false, 
"maxparams": 10, 
"maxdepth": 5, 
"maxstatements": 40, 
"maxcomplexity": 8, 
"maxlen": 120, 
"asi": false, 
"boss": false, 
"debug": false, 
"eqnull": true, 
"esnext": false, 
"evil": false, 
"expr": false, 
"funcscope": false, 
"globalstrict": false, 
"iterator": false, 
"lastsemic": false, 
"laxbreak": false, 
"laxcomma": false, 
"loopfunc": true, 
"maxerr": false, 
"moz": false, 
"multistr": false, 
"notypeof": false, 
"proto": false, 
"scripturl": false, 
"shadow": false, 
"sub": true, 
"supernew": false, 
"validthis": false, 
"noyield": false, 
"browser": true, 
"node": true, 
"globals": { 
    "angular": false, 
    "$": false 
    } 
} 

누군가가 알려 주시기 바랍니다 수 :

C:\Users\574839\Desktop\testscr.js:1 
(function (exports, require, module, __filename, __dirname) { <!DOCTYPE html> 
                 ^
SyntaxError: Unexpected token < 
at Object.exports.runInThisContext (vm.js:53:16) 
at Module._compile (module.js:513:28) 
at Object.Module._extensions..js (module.js:550:10) 
at Module.load (module.js:458:32) 
at tryModuleLoad (module.js:417:12) 
at Function.Module._load (module.js:409:3) 
at Function.Module.runMain (module.js:575:10) 
at startup (node.js:160:18) 
at node.js:456:3 

나는 다음과 같은 옵션이있는 .jshintrc 파일이?

내 JS 코드는 다음과 같습니다.

<!DOCTYPE html> 
<html> 
<body> 

<h1>What Can JavaScript Do?</h1> 

<p id="demo">JavaScript can change HTML content.</p> 

<button type="button" onclick="document.getElementById('demo').innerHTML='Hello JavaScript!'">Click Me!</button> 

</body> 
</html> 

답변

1

이것은 JavaScript 파일이 아니며 HTML 파일입니다. 하지만 다행히 JSHint는 --extract 옵션을 전달하면 HTML 파일의 자바 스크립트에서 작동 할 수 있습니다. documentation에서

:

--extract=[auto|always|never]

는 linting 전에 HTML 파일에서 자바 스크립트를 추출 JSHint을 알려줍니다 :

tmp ☭ cat test.html 
<html> 
    <head> 
    <title>Hello, World!</title> 
    <script> 
     function hello() { 
     return "Hello, World!"; 
     } 
    </script> 
    </head> 
    <body> 
    <h1>Hello, World!</h1> 
    <script> 
     console.log(hello()) 
    </script> 
    </body> 
</html> 

tmp ☭ jshint --extract=auto test.html 
test.html: line 13, col 27, Missing semicolon. 

1 error 

이는 항상 항상 JSHint으로 설정하면 JavaScript를 추출하려고 시도하십시오. 으로 설정하면 파일이 HTML 파일 인 것처럼 보일 경우에만 으로 변경됩니다.

한편이 경우와 같이 onclick과 같은 HTML 속성 이벤트 핸들러에서 자바 스크립트를 인식하지 못합니다. 이는 나쁜 습관입니다. 당신이 정말로 당신의 자바 스크립트를 원한다면 HTML에 당신과 같은 스크립트 태그에 넣어 한 다음

testscr.html : 또한 당신이 노드로 전달하는 것 같습니다

<!DOCTYPE html> 
<html> 
    <body> 

    <h1>What Can JavaScript Do?</h1> 

    <p id="demo">JavaScript can change HTML content.</p> 

    <button type="button" onclick="sayHello()">Click Me!</button> 
    <script> 
     function sayHello() { 
     document.getElementById('demo').innerHTML='Hello JavaScript!'; 
     } 
    </script> 
    </body> 
</html> 

, node testscr.js과 같지만 수행해야하는 작업은 jshint에 전달하는 것입니다. jshint --extract=always testscr.js

+0

JSHint에 JS에서 HTML을 추출하고 JS 만 테스트 할 수있는 방법이 있습니까? – InfiniteLoop

+0

감사합니다. 마지막 질문 하나. 콘솔에 오류/경고를 표시하는 것 외에 로그 파일처럼 생성됩니까? 테스트 결과를 어디서 찾을 지 모르겠습니다. – InfiniteLoop

+0

내가 말한 것을 긁어 내고 대답을 다시 보아라. 나는 그것을 새롭게했다. 출력을 파일에 넣고 싶다면'jshint --extract = always testscr.js> output.txt' –