code

JSLint가 갑자기보고합니다. "use strict"의 함수 형식을 사용합니다.

codestyles 2020. 9. 28. 09:08
반응형

JSLint가 갑자기보고합니다. "use strict"의 함수 형식을 사용합니다.


나는 진술을 포함한다 :

"use strict";

대부분의 Javascript 파일 시작 부분에 있습니다.

JSLint는 이전에 이것에 대해 경고 한 적이 없습니다. 그러나 이제는 다음과 같이 말합니다.

"엄격한 사용"의 함수 형식을 사용하십시오.

"함수 형식"이 무엇인지 아는 사람이 있습니까?


'use strict';래핑 함수의 첫 번째 문으로 포함 하므로 해당 함수에만 영향을줍니다. 이것은 엄격하지 않은 스크립트를 연결할 때 문제를 방지합니다.

Douglas Crockford의 최신 블로그 게시물 Strict Mode Is Coming To Town을 참조하십시오 .

해당 게시물의 예 :

(function () {
   'use strict';
   // this function is strict...
}());

(function () {
   // but this function is sloppy...
}());

업데이트 : 즉각적인 기능 (예 : 노드 모듈)으로 감싸고 싶지 않은 경우 경고를 비활성화 할 수 있습니다.

대한 JSLint (당 Zhami ) :

/*jslint node: true */

대한 JSHint :

/*jshint strict:false */

또는 ( Laith Shadeed)

/* jshint -W097 */

JSHint에서 임의의 경고를 비활성화하려면 JSHint 소스 코드 에서 맵을 확인하십시오 (자세한 내용은 docs 참조 ).

업데이트 2 : JSHintnode:boolean옵션을 지원합니다 . .jshintrcgithub를 참조하십시오 .

/* jshint node: true */

NodeJS 용 모듈을 작성하는 경우 이미 캡슐화되어 있습니다. 파일 상단에 다음을 포함하여 JSLint에 노드가 있음을 알립니다.

/*jslint node: true */

대신 jshint 를 사용하는 것이 좋습니다 .

를 통해이 경고를 억제 할 수 있습니다 /*jshint globalstrict: true*/.

라이브러리를 작성하는 경우 nodejs의 경우와 같이 코드가 모듈로 캡슐화되는 경우에만 전역 엄격을 사용하는 것이 좋습니다.

그렇지 않으면 라이브러리를 사용하는 모든 사람을 엄격 모드로 강제 설정합니다.


Cross Platform JavaScript 블로그 게시물에 따라 Node.js / browserify 애플리케이션을 만들기 시작했습니다 . 그리고 내 새로운 Gruntfile이 jshint를 통과하지 못했기 때문에이 문제가 발생했습니다.

다행히도 Grunt 에 대한 Leanpub 책에서 답을 찾았습니다 .

지금 시도하면 Gruntfile을 스캔하고 몇 가지 오류가 발생합니다.

$ grunt jshint

Running "jshint:all" (jshint) task
Linting Gruntfile.js...ERROR
[L1:C1] W097: Use the function form of "use strict".
'use strict';
Linting Gruntfile.js...ERROR
[L3:C1] W117: 'module' is not defined.
module.exports = function (grunt) {

Warning: Task "jshint:all" failed. Use --force to continue.

두 오류는 Gruntfile이 Node 프로그램이고 기본적으로 JSHint가 .NET module의 문자열 버전을 인식하거나 허용하지 않기 때문 입니다 use strict. Node 프로그램을 허용하는 JSHint 규칙을 설정할 수 있습니다. jshint 작업 구성을 편집하고 옵션 키를 추가해 보겠습니다.

jshint: {
  options: {
    node: true
  },
}

Adding node: true to the jshint options, to put jshint into "Node mode", removed both errors for me.


Add a file .jslintrc (or .jshintrc in the case of jshint) at the root of your project with the following content:

{
    "node": true
}

There's nothing innately wrong with the string form.

Rather than avoid the "global" strict form for worry of concatenating non-strict javascript, it's probably better to just fix the damn non-strict javascript to be strict.


I think everyone missed the "suddenly" part of this question. Most likely, your .jshintrc has a syntax error, so it's not including the 'browser' line. Run it through a json validator to see where the error is.


This is how simple it is: If you want to be strict with all your code, add "use strict"; at the start of your JavaScript.

But if you only want to be strict with some of your code, use the function form. Anyhow, I would recomend you to use it at the beginning of your JavaScript because this will help you be a better coder.

참고URL : https://stackoverflow.com/questions/4462478/jslint-is-suddenly-reporting-use-the-function-form-of-use-strict

반응형