How does jslint work




















It started as a fork of the JsLint project. They removed some rules and added new ones that would better suit the way a JavaScript developer codes today. EsLint : Is open source and started in The founder says he love JsHint but found himself in need of a more pluggable linter. The primary reason ESLint was created was to allow developers to create their own linting rules.

ESLint is designed to have all rules completely pluggable. This is the linter for the person who needs to add and change linting rules. Jscs : Started and ships with 90 validation rules or according to their github , including presets from popular style guides. This is the download history for the last month from npmjs. Since there are heaps of code editors and a bunch of linters, I will focus on just one editor. With that said this guide may still be useful for you.

Here are some good things to know. The Metrics field to the right of the text field will update in real time as you type, tallying a running list of statistics about your code, such as a count of how many functions it contains.

Of course, it also displays any linting errors it finds. Here are two example commands that check a single file and a directory, respectively:. In the first example, JSHint will lint the index. JSHint will print any errors it finds in your terminal. But, from here the complexity can ramp up significantly because JSHint is completely configurable and it also exposes an API, meaning it can be used as a JavaScript module in your own JavaScript files.

A custom configuration, which should be stored in a file named. You can include your custom configurations by specifying the path to your.

You can use these basics to get started down the road of customizing your own linting rules with JSHint. GitHub stars aside, when it comes to JavaScript linting ESLint is probably the linter seen the most in the wild and is going to be the go-to for a lot of folks. And, similar to JSHint, you ease in by using defaults and add customizations as your preferences or needs change. If you install ESLint globally, its configurations will apply to any and all project files you run it against.

But if you want different configurations for different projects, you can install it as a development dependency and create a different configuration file for each project. Note: Regardless of how much you plan on customizing your linting rules, you must start with this step because ESLint needs the. The first question asked of you is how to configure ESLint.

You have three options: use a popular style guide, answer questions about your style, or let ESLint configure itself for you by inspecting your files to decide how to set up the rules. If the prospect of configuring it yourself right off the bat seems intimidating, you can fall back on using a popular style guide developed by one of a few known organizations.

Regardless of which path you go down, ESLint will use your answers to generate a file named. At least for those just getting started with linting. If value is false default , JSHint will consider that variable as read-only. Affects the undef option. You can also blacklist certain global variables to make sure they are not used anywhere in the current file. A directive for telling JSHint about global variables that are defined in the current file but used elsewhere.

Affects the unused option. A directive for telling JSHint about all properties you intend to use. This directive is deprecated. All code in between ignore:start and ignore:end won't be passed to JSHint so you can use any language extension such as Facebook React. Additionally, you can ignore a single line with a trailing comment:. Most often, when you need to tune JSHint to your own taste, all you need to do is to find an appropriate option.

Trying to figure out how JSHint options work can be confusing and frustrating and we're working on fixing that! JSHint has two types of options: enforcing and relaxing. This was commonly used in browsers to link source files together before ES6 modules appeared.

Use of global variables is strongly discouraged, but unfortunately web browsers require their use. Each of the names listed will indicate a read-only global variable. The names are separated by , comma. This directive should not be used if import or export is used. This directive inhibits warnings, but it does not declare the names in the execution environment.

However, if any of those names are expected to be supplied by other files and those other files fail to do so, then execution errors will result.

It is usually better to use top-level variable declarations instead: var ADSAFE; var report; var jslint; Using var in this way allows comparing a global variable to the undefined value to determine whether it is has been used in the global context.

These options can also be set from the jslint. Each property name in the program is looked up in this list. If a name is not found, that indicates an error, most likely a typing error. It contains all of the names that were used with dot notation, subscript notation, and object literals to name the properties of objects. You can look through the list for misspellings. JSLint will check the spelling of all property names against the list.

That way, you can have JSLint look for misspellings for you. JSLint introduces a new reserved word: ignore. It is used in parameter lists and in catch clauses to indicate a parameter that will be ignored. Unused warnings will not be produced for ignore.

The var statement suffers from a bad practice called hoisting. The let statement does not do hoisting and respects block scope. The const statement is like let except that it marks the variable but not its contents as read only, making it an error to attempt to assign to the variable.

When given a choice, const is the best, var is the worst. JSLint uses the intersection of the var rules and the let rules, and by doing so avoids the errors related to either. A name should be declared only once in a function. It should be declared before it is used.

It should not be used outside of the block in which it is declared. A variable should not have the same name as a variable or parameter in an outer function. Do not mix var and let. Declare one name per statement. Fortran made a terrible mistake in using the equality operator as its assignment operator. That mistake has been replicated in most languages since then. The visual similarity is a source of errors. Assignments are not allowed in expression position, and comparisons are not allowed in statement position.

This also reduces confusion. JavaScript uses a C-like syntax that requires the use of semicolons to delimit certain statements. JavaScript attempts to make those semicolons optional with an automatic semicolon insertion mechanism, but it does not work very well. Automatic semicolon insertion was added to make things easier for beginners.

Unfortunately, it sometimes fails. Do not rely on it unless you are a beginner. JSLint expects that every statement will be followed by ; except for for , function , if , switch , try , and while. JSLint does not expect to see unnecessary semicolons, the empty statement, or empty blocks.

The function statement creates a variable and assigns the function object to it. It should be used in a file or function body, but not inside of a block.

The function expression unfortunately looks like the function statement. It may appear anywhere that an expression may appear, but not in statement position and not in a loop. It produces a function object but does not create a variable in which to store it.

The enhanced object literal provides an ES6 shorthand for creating a property whose value is a function, saving you from having to type : colon and function. Finally, ES6 provides an even shorter form of function expression that leaves out the words function and return :. The , comma operator is unnecessary and can mask programming errors. JSLint expects to see the comma used as a separator, but not as an operator.

It does not expect to see elided elements in array literals. A comma should not appear after the last element of an array literal or object literal. JSLint expects blocks with function , if , switch , while , for , do , and try statements and nowhere else.

That form is known to contribute to mistakes in projects where many programmers are working on the same code. That is why JSLint expects the use of a block:.



0コメント

  • 1000 / 1000