├── .eslintignore ├── .eslintrc.js ├── .gitignore ├── .npmrc ├── .prettierignore ├── .prettierrc ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── Makefile ├── README.md ├── lib ├── analyze-scope.js ├── babylon-to-espree │ ├── convertAST.js │ ├── convertComments.js │ ├── convertProgramNode.js │ ├── convertTemplateType.js │ ├── convertToken.js │ ├── convertTokens.js │ └── index.js ├── index.js ├── parse-with-scope.js ├── parse.js └── visitor-keys.js ├── package.json ├── test ├── babel.config.js ├── fixtures │ ├── config │ │ └── babel.config.decorators-legacy.js │ ├── eslint-plugin-import │ │ ├── .eslintrc.yml │ │ ├── a.js │ │ ├── b.js │ │ └── c.js │ └── rules │ │ ├── strict │ │ ├── function-with.js │ │ ├── function-without.js │ │ ├── global-with-function-with.js │ │ ├── global-with-function-without.js │ │ ├── global-with.js │ │ └── none.js │ │ └── syntax-error.js ├── helpers │ └── assert-implements-ast.js └── specs │ ├── babel-eslint.js │ ├── integration.js │ └── non-regression.js └── yarn.lock /.eslintignore: -------------------------------------------------------------------------------- 1 | !.*.js 2 | test/fixtures 3 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | module.exports = { 4 | root: true, 5 | extends: "babel", 6 | plugins: ["prettier"], 7 | rules: { 8 | "max-len": "off", 9 | strict: "error", 10 | "prettier/prettier": "error", 11 | }, 12 | env: { 13 | node: true, 14 | }, 15 | parserOptions: { 16 | sourceType: "script", 17 | }, 18 | overrides: [ 19 | { 20 | files: ["test/**/*"], 21 | env: { 22 | mocha: true, 23 | }, 24 | }, 25 | ], 26 | }; 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock = false 2 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | *.json 2 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "trailingComma": "es5" 3 | } -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - "12" 5 | - "10" 6 | - "8" 7 | 8 | matrix: 9 | fast_finish: true 10 | include: 11 | - node_js: "node" 12 | env: LINT=true 13 | 14 | script: 15 | - 'if [ -n "${LINT-}" ]; then npm run lint ; fi' 16 | - 'if [ -z "${LINT-}" ]; then npm test ; fi' 17 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Code of Conduct 2 | 3 | This project shares the same code of conduct as [Babel](https://github.com/babel/babel), which can be found [here](https://github.com/babel/babel/blob/master/CODE_OF_CONDUCT.md). 4 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Thanks for contributing! 4 | 5 | If you're about to report an issue - please first check if it's already been reported in the [issues page](https://github.com/babel/babel-eslint/issues). 6 | 7 | Also check to see if the latest versions of eslint/babel-eslint still produce the issue. 8 | 9 | Also check out the [Known Issues](https://github.com/babel/babel-eslint#known-issues) section of the README. 10 | 11 | If you are having issues with JSX you might want to check out eslint-plugin-react. If there's an issue with new experimental syntax you might need to report that in eslint-plugin-babel instead. 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014-2016 Sebastian McKenzie 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: publish-patch 2 | 3 | publish-patch: 4 | ./node_modules/.bin/mocha 5 | npm version patch 6 | npm publish 7 | git push --follow-tags 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # babel-eslint [![npm](https://img.shields.io/npm/v/babel-eslint.svg)](https://www.npmjs.com/package/babel-eslint) [![travis](https://img.shields.io/travis/babel/babel-eslint/master.svg)](https://travis-ci.org/babel/babel-eslint) [![npm-downloads](https://img.shields.io/npm/dm/babel-eslint.svg)](https://www.npmjs.com/package/babel-eslint) 2 | 3 | ## NOTE: babel-eslint is now `@babel/eslint-parser` and has moved into the [Babel monorepo](https://github.com/babel/babel/tree/main/eslint/babel-eslint-parser). 4 | 5 | **babel-eslint** allows you to lint **ALL** valid Babel code with the fantastic 6 | [ESLint](https://github.com/eslint/eslint). 7 | 8 | ## Breaking changes in v11.x.x 9 | 10 | As of the v11.x.x release, babel-eslint now requires Babel as a peer dependency and expects a valid [Babel configuration file](https://babeljs.io/docs/en/configuration) to exist. This ensures that the same Babel configuration is used during both linting and compilation. 11 | 12 | v11 also changes some AST node types to match espree v6: 13 | 14 | - `ExperimentalSpreadProperty` became `SpreadElement`. 15 | - `ExperimentalRestProperty` became `RestElement`. 16 | - `Literal` became `JSXText` (for JSXText). 17 | 18 | ## When should I use babel-eslint? 19 | 20 | ESLint's default parser and core rules [only support the latest final ECMAScript standard](https://github.com/eslint/eslint/blob/a675c89573836adaf108a932696b061946abf1e6/README.md#what-about-experimental-features) and do not support experimental (such as new features) and non-standard (such as Flow or TypeScript types) syntax provided by Babel. babel-eslint is a parser that allows ESLint to run on source code that is transformed by Babel. 21 | 22 | **Note:** You only need to use babel-eslint if you are using Babel to transform your code. If this is not the case, please use the relevant parser for your chosen flavor of ECMAScript (note that the default parser supports all non-experimental syntax as well as JSX). 23 | 24 | ## How does it work? 25 | 26 | ESLint allows for the use of [custom parsers](https://eslint.org/docs/developer-guide/working-with-custom-parsers). When using this plugin, your code is parsed by Babel's parser (using the configuration specified in your [Babel configuration file](https://babeljs.io/docs/en/configuration)) and the resulting AST is 27 | transformed into an [ESTree](https://github.com/estree/estree)-compliant structure that ESLint can understand. All location info such as line numbers, 28 | columns is also retained so you can track down errors with ease. 29 | 30 | **Note:** ESLint's core rules do not support experimental syntax and may therefore not work as expected when using babel-eslint. Please use the companion [`eslint-plugin-babel`](https://github.com/babel/eslint-plugin-babel) plugin for core rules that you have issues with. 31 | 32 | ## Usage 33 | 34 | ### Installation 35 | 36 | ```sh 37 | $ npm install eslint babel-eslint --save-dev 38 | # or 39 | $ yarn add eslint babel-eslint -D 40 | ``` 41 | 42 | **Note:** babel-eslint requires `babel/core@>=7.2.0` and a valid Babel configuration file to run. If you do not have this already set up, please see the [Babel Usage Guide](https://babeljs.io/docs/en/usage). 43 | 44 | ### Setup 45 | 46 | To use babel-eslint, `"babel-eslint"` must be specified as the `parser` in your ESLint configuration file (see [here](https://eslint.org/docs/user-guide/configuring#specifying-parser) for more detailed information). 47 | 48 | **.eslintrc.js** 49 | 50 | ```js 51 | module.exports = { 52 | parser: "babel-eslint", 53 | }; 54 | ``` 55 | 56 | With the parser set, your configuration can be configured as described in the [Configuring ESLint](https://eslint.org/docs/user-guide/configuring) documentation. 57 | 58 | **Note:** The `parserOptions` described in the [official documentation](https://eslint.org/docs/user-guide/configuring#specifying-parser-options) are for the default parser and are not necessarily supported by babel-eslint. Please see the section directly below for supported `parserOptions`. 59 | 60 | ### Additional parser configuration 61 | 62 | Additional configuration options can be set in your ESLint configuration under the `parserOptions` key. Please note that the `ecmaFeatures` config property may still be required for ESLint to work properly with features not in ECMAScript 5 by default. 63 | 64 | - `requireConfigFile` (default `true`) can be set to `false` to allow babel-eslint to run on files that do not have a Babel configuration associated with them. This can be useful for linting files that are not transformed by Babel (such as tooling configuration files), though we recommend using the default parser via [glob-based configuration](https://eslint.org/docs/user-guide/configuring#configuration-based-on-glob-patterns). Note: babel-eslint will not parse any experimental syntax when no configuration file is found. 65 | - `sourceType` can be set to `"module"`(default) or `"script"` if your code isn't using ECMAScript modules. 66 | - `allowImportExportEverywhere` (default `false`) can be set to `true` to allow import and export declarations to appear anywhere a statement is allowed if your build environment supports that. Otherwise import and export declarations can only appear at a program's top level. 67 | - `ecmaFeatures.globalReturn` (default `false`) allow return statements in the global scope when used with `sourceType: "script"`. 68 | - `babelOptions` passes through Babel's configuration [loading](https://babeljs.io/docs/en/options#config-loading-options) and [merging](https://babeljs.io/docs/en/options#config-merging-options) options (for instance, in case of a monorepo). When not defined, babel-eslint will use Babel's default configuration file resolution logic. 69 | 70 | **.eslintrc.js** 71 | 72 | ```js 73 | module.exports = { 74 | parser: "babel-eslint", 75 | parserOptions: { 76 | sourceType: "module", 77 | allowImportExportEverywhere: false, 78 | ecmaFeatures: { 79 | globalReturn: false, 80 | }, 81 | babelOptions: { 82 | configFile: "path/to/config.js", 83 | }, 84 | }, 85 | }; 86 | ``` 87 | 88 | **.eslintrc.js using glob-based configuration** 89 | 90 | This configuration would use the default parser for all files except for those found by the `"files/transformed/by/babel/*.js"` glob. 91 | 92 | ```js 93 | module.exports = { 94 | rules: { 95 | indent: "error" 96 | }, 97 | overrides: [ 98 | { 99 | files: ["files/transformed/by/babel/*.js"], 100 | parser: "babel-eslint", 101 | } 102 | ] 103 | }; 104 | ``` 105 | 106 | ### Run 107 | 108 | ```sh 109 | $ ./node_modules/.bin/eslint yourfile.js 110 | ``` 111 | 112 | ## Known issues 113 | 114 | Flow: 115 | 116 | > Check out [eslint-plugin-flowtype](https://github.com/gajus/eslint-plugin-flowtype): An `eslint` plugin that makes flow type annotations global variables and marks declarations as used. Solves the problem of false positives with `no-undef` and `no-unused-vars`. 117 | 118 | - `no-undef` for global flow types: `ReactElement`, `ReactClass` [#130](https://github.com/babel/babel-eslint/issues/130#issuecomment-111215076) 119 | - Workaround: define types as globals in `.eslintrc` or define types and import them `import type ReactElement from './types'` 120 | - `no-unused-vars/no-undef` with Flow declarations (`declare module A {}`) [#132](https://github.com/babel/babel-eslint/issues/132#issuecomment-112815926) 121 | 122 | Modules/strict mode 123 | 124 | - `no-unused-vars: ["error", { vars: local }]` [#136](https://github.com/babel/babel-eslint/issues/136) 125 | 126 | Please check out [eslint-plugin-react](https://github.com/yannickcr/eslint-plugin-react) for React/JSX issues. 127 | 128 | - `no-unused-vars` with jsx 129 | 130 | Please check out [eslint-plugin-babel](https://github.com/babel/eslint-plugin-babel) for other issues. 131 | 132 | ## Questions and support 133 | 134 | If you have an issue, please first check if it can be reproduced with the default parser and with the latest versions of `eslint` and `babel-eslint`. If it is not reproducible with the default parser, it is most likely an issue with babel-eslint. 135 | 136 | For questions and support please visit the [`#discussion`](https://babeljs.slack.com/messages/discussion/) Babel Slack channel (sign up [here](https://github.com/babel/notes/issues/38)) or the ESLint [Gitter](https://gitter.im/eslint/eslint). 137 | -------------------------------------------------------------------------------- /lib/analyze-scope.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const t = require("@babel/core").types; 4 | const escope = require("eslint-scope"); 5 | const Definition = require("eslint-scope/lib/definition").Definition; 6 | const OriginalPatternVisitor = require("eslint-scope/lib/pattern-visitor"); 7 | const OriginalReferencer = require("eslint-scope/lib/referencer"); 8 | const fallback = require("eslint-visitor-keys").getKeys; 9 | const childVisitorKeys = require("./visitor-keys"); 10 | 11 | const flowFlippedAliasKeys = t.FLIPPED_ALIAS_KEYS.Flow.concat([ 12 | "ArrayPattern", 13 | "ClassDeclaration", 14 | "ClassExpression", 15 | "FunctionDeclaration", 16 | "FunctionExpression", 17 | "Identifier", 18 | "ObjectPattern", 19 | "RestElement", 20 | ]); 21 | const visitorKeysMap = Object.keys(t.VISITOR_KEYS).reduce(function(acc, key) { 22 | const value = t.VISITOR_KEYS[key]; 23 | if (flowFlippedAliasKeys.indexOf(value) === -1) { 24 | acc[key] = value; 25 | } 26 | return acc; 27 | }, {}); 28 | 29 | const propertyTypes = { 30 | // loops 31 | callProperties: { type: "loop", values: ["value"] }, 32 | indexers: { type: "loop", values: ["key", "value"] }, 33 | properties: { type: "loop", values: ["argument", "value"] }, 34 | types: { type: "loop" }, 35 | params: { type: "loop" }, 36 | // single property 37 | argument: { type: "single" }, 38 | elementType: { type: "single" }, 39 | qualification: { type: "single" }, 40 | rest: { type: "single" }, 41 | returnType: { type: "single" }, 42 | // others 43 | typeAnnotation: { type: "typeAnnotation" }, 44 | typeParameters: { type: "typeParameters" }, 45 | id: { type: "id" }, 46 | }; 47 | 48 | class PatternVisitor extends OriginalPatternVisitor { 49 | ArrayPattern(node) { 50 | node.elements.forEach(this.visit, this); 51 | } 52 | 53 | ObjectPattern(node) { 54 | node.properties.forEach(this.visit, this); 55 | } 56 | } 57 | 58 | class Referencer extends OriginalReferencer { 59 | // inherits. 60 | visitPattern(node, options, callback) { 61 | if (!node) { 62 | return; 63 | } 64 | 65 | // Visit type annotations. 66 | this._checkIdentifierOrVisit(node.typeAnnotation); 67 | if (t.isAssignmentPattern(node)) { 68 | this._checkIdentifierOrVisit(node.left.typeAnnotation); 69 | } 70 | 71 | // Overwrite `super.visitPattern(node, options, callback)` in order to not visit `ArrayPattern#typeAnnotation` and `ObjectPattern#typeAnnotation`. 72 | if (typeof options === "function") { 73 | callback = options; 74 | options = { processRightHandNodes: false }; 75 | } 76 | 77 | const visitor = new PatternVisitor(this.options, node, callback); 78 | visitor.visit(node); 79 | 80 | // Process the right hand nodes recursively. 81 | if (options.processRightHandNodes) { 82 | visitor.rightHandNodes.forEach(this.visit, this); 83 | } 84 | } 85 | 86 | // inherits. 87 | visitClass(node) { 88 | // Decorators. 89 | this._visitArray(node.decorators); 90 | 91 | // Flow type parameters. 92 | const typeParamScope = this._nestTypeParamScope(node); 93 | 94 | // Flow super types. 95 | this._visitTypeAnnotation(node.implements); 96 | this._visitTypeAnnotation( 97 | node.superTypeParameters && node.superTypeParameters.params 98 | ); 99 | 100 | // Basic. 101 | super.visitClass(node); 102 | 103 | // Close the type parameter scope. 104 | if (typeParamScope) { 105 | this.close(node); 106 | } 107 | } 108 | 109 | // inherits. 110 | visitFunction(node) { 111 | const typeParamScope = this._nestTypeParamScope(node); 112 | 113 | // Flow return types. 114 | this._checkIdentifierOrVisit(node.returnType); 115 | 116 | // Basic. 117 | super.visitFunction(node); 118 | 119 | // Close the type parameter scope. 120 | if (typeParamScope) { 121 | this.close(node); 122 | } 123 | } 124 | 125 | // inherits. 126 | visitProperty(node) { 127 | if (node.value && node.value.type === "TypeCastExpression") { 128 | this._visitTypeAnnotation(node.value); 129 | } 130 | this._visitArray(node.decorators); 131 | super.visitProperty(node); 132 | } 133 | 134 | InterfaceDeclaration(node) { 135 | this._createScopeVariable(node, node.id); 136 | 137 | const typeParamScope = this._nestTypeParamScope(node); 138 | 139 | // TODO: Handle mixins 140 | this._visitArray(node.extends); 141 | this.visit(node.body); 142 | 143 | if (typeParamScope) { 144 | this.close(node); 145 | } 146 | } 147 | 148 | TypeAlias(node) { 149 | this._createScopeVariable(node, node.id); 150 | 151 | const typeParamScope = this._nestTypeParamScope(node); 152 | 153 | this.visit(node.right); 154 | 155 | if (typeParamScope) { 156 | this.close(node); 157 | } 158 | } 159 | 160 | ClassProperty(node) { 161 | this._visitClassProperty(node); 162 | } 163 | 164 | ClassPrivateProperty(node) { 165 | this._visitClassProperty(node); 166 | } 167 | 168 | DeclareModule(node) { 169 | this._visitDeclareX(node); 170 | } 171 | 172 | DeclareFunction(node) { 173 | this._visitDeclareX(node); 174 | } 175 | 176 | DeclareVariable(node) { 177 | this._visitDeclareX(node); 178 | } 179 | 180 | DeclareClass(node) { 181 | this._visitDeclareX(node); 182 | } 183 | 184 | // visit OptionalMemberExpression as a MemberExpression. 185 | OptionalMemberExpression(node) { 186 | super.MemberExpression(node); 187 | } 188 | 189 | _visitClassProperty(node) { 190 | this._visitTypeAnnotation(node.typeAnnotation); 191 | this.visitProperty(node); 192 | } 193 | 194 | _visitDeclareX(node) { 195 | if (node.id) { 196 | this._createScopeVariable(node, node.id); 197 | } 198 | 199 | const typeParamScope = this._nestTypeParamScope(node); 200 | if (typeParamScope) { 201 | this.close(node); 202 | } 203 | } 204 | 205 | _createScopeVariable(node, name) { 206 | this.currentScope().variableScope.__define( 207 | name, 208 | new Definition("Variable", name, node, null, null, null) 209 | ); 210 | } 211 | 212 | _nestTypeParamScope(node) { 213 | if (!node.typeParameters) { 214 | return null; 215 | } 216 | 217 | const parentScope = this.scopeManager.__currentScope; 218 | const scope = new escope.Scope( 219 | this.scopeManager, 220 | "type-parameters", 221 | parentScope, 222 | node, 223 | false 224 | ); 225 | 226 | this.scopeManager.__nestScope(scope); 227 | for (let j = 0; j < node.typeParameters.params.length; j++) { 228 | const name = node.typeParameters.params[j]; 229 | scope.__define(name, new Definition("TypeParameter", name, name)); 230 | if (name.typeAnnotation) { 231 | this._checkIdentifierOrVisit(name); 232 | } 233 | } 234 | scope.__define = function() { 235 | return parentScope.__define.apply(parentScope, arguments); 236 | }; 237 | 238 | return scope; 239 | } 240 | 241 | _visitTypeAnnotation(node) { 242 | if (!node) { 243 | return; 244 | } 245 | if (Array.isArray(node)) { 246 | node.forEach(this._visitTypeAnnotation, this); 247 | return; 248 | } 249 | 250 | // get property to check (params, id, etc...) 251 | const visitorValues = visitorKeysMap[node.type]; 252 | if (!visitorValues) { 253 | return; 254 | } 255 | 256 | // can have multiple properties 257 | for (let i = 0; i < visitorValues.length; i++) { 258 | const visitorValue = visitorValues[i]; 259 | const propertyType = propertyTypes[visitorValue]; 260 | const nodeProperty = node[visitorValue]; 261 | // check if property or type is defined 262 | if (propertyType == null || nodeProperty == null) { 263 | continue; 264 | } 265 | if (propertyType.type === "loop") { 266 | for (let j = 0; j < nodeProperty.length; j++) { 267 | if (Array.isArray(propertyType.values)) { 268 | for (let k = 0; k < propertyType.values.length; k++) { 269 | const loopPropertyNode = nodeProperty[j][propertyType.values[k]]; 270 | if (loopPropertyNode) { 271 | this._checkIdentifierOrVisit(loopPropertyNode); 272 | } 273 | } 274 | } else { 275 | this._checkIdentifierOrVisit(nodeProperty[j]); 276 | } 277 | } 278 | } else if (propertyType.type === "single") { 279 | this._checkIdentifierOrVisit(nodeProperty); 280 | } else if (propertyType.type === "typeAnnotation") { 281 | this._visitTypeAnnotation(node.typeAnnotation); 282 | } else if (propertyType.type === "typeParameters") { 283 | for (let l = 0; l < node.typeParameters.params.length; l++) { 284 | this._checkIdentifierOrVisit(node.typeParameters.params[l]); 285 | } 286 | } else if (propertyType.type === "id") { 287 | if (node.id.type === "Identifier") { 288 | this._checkIdentifierOrVisit(node.id); 289 | } else { 290 | this._visitTypeAnnotation(node.id); 291 | } 292 | } 293 | } 294 | } 295 | 296 | _checkIdentifierOrVisit(node) { 297 | if (node && node.typeAnnotation) { 298 | this._visitTypeAnnotation(node.typeAnnotation); 299 | } else if (node && node.type === "Identifier") { 300 | this.visit(node); 301 | } else { 302 | this._visitTypeAnnotation(node); 303 | } 304 | } 305 | 306 | _visitArray(nodeList) { 307 | if (nodeList) { 308 | for (const node of nodeList) { 309 | this.visit(node); 310 | } 311 | } 312 | } 313 | } 314 | 315 | module.exports = function(ast, parserOptions) { 316 | const options = { 317 | ignoreEval: true, 318 | optimistic: false, 319 | directive: false, 320 | nodejsScope: 321 | ast.sourceType === "script" && 322 | (parserOptions.ecmaFeatures && 323 | parserOptions.ecmaFeatures.globalReturn) === true, 324 | impliedStrict: false, 325 | sourceType: ast.sourceType, 326 | ecmaVersion: parserOptions.ecmaVersion, 327 | fallback, 328 | }; 329 | 330 | options.childVisitorKeys = childVisitorKeys; 331 | 332 | const scopeManager = new escope.ScopeManager(options); 333 | const referencer = new Referencer(options, scopeManager); 334 | 335 | referencer.visit(ast); 336 | 337 | return scopeManager; 338 | }; 339 | -------------------------------------------------------------------------------- /lib/babylon-to-espree/convertAST.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const t = require("@babel/core").types; 4 | const convertProgramNode = require("./convertProgramNode"); 5 | 6 | module.exports = function(ast, traverse, code) { 7 | const state = { source: code }; 8 | 9 | // Monkey patch visitor keys in order to be able to traverse the estree nodes 10 | t.VISITOR_KEYS.Property = t.VISITOR_KEYS.ObjectProperty; 11 | t.VISITOR_KEYS.MethodDefinition = [ 12 | "key", 13 | "value", 14 | "decorators", 15 | "returnType", 16 | "typeParameters", 17 | ]; 18 | 19 | traverse(ast, astTransformVisitor, null, state); 20 | 21 | delete t.VISITOR_KEYS.Property; 22 | delete t.VISITOR_KEYS.MethodDefinition; 23 | 24 | convertProgramNode(ast); 25 | }; 26 | 27 | const astTransformVisitor = { 28 | noScope: true, 29 | enter(path) { 30 | const node = path.node; 31 | 32 | // private var to track original node type 33 | node._babelType = node.type; 34 | 35 | if (node.innerComments) { 36 | delete node.innerComments; 37 | } 38 | 39 | if (node.trailingComments) { 40 | delete node.trailingComments; 41 | } 42 | 43 | if (node.leadingComments) { 44 | delete node.leadingComments; 45 | } 46 | }, 47 | exit(path) { 48 | const node = path.node; 49 | 50 | if (path.isTypeParameter()) { 51 | node.type = "Identifier"; 52 | node.typeAnnotation = node.bound; 53 | delete node.bound; 54 | } 55 | 56 | // flow: prevent "no-undef" 57 | // for "Component" in: "let x: React.Component" 58 | if (path.isQualifiedTypeIdentifier()) { 59 | delete node.id; 60 | } 61 | // for "b" in: "var a: { b: Foo }" 62 | if (path.isObjectTypeProperty()) { 63 | delete node.key; 64 | } 65 | // for "indexer" in: "var a: {[indexer: string]: number}" 66 | if (path.isObjectTypeIndexer()) { 67 | delete node.id; 68 | } 69 | // for "param" in: "var a: { func(param: Foo): Bar };" 70 | if (path.isFunctionTypeParam()) { 71 | delete node.name; 72 | } 73 | 74 | // modules 75 | 76 | if (path.isImportDeclaration()) { 77 | delete node.isType; 78 | } 79 | 80 | // template string range fixes 81 | if (path.isTemplateLiteral()) { 82 | for (let j = 0; j < node.quasis.length; j++) { 83 | const q = node.quasis[j]; 84 | q.range[0] -= 1; 85 | if (q.tail) { 86 | q.range[1] += 1; 87 | } else { 88 | q.range[1] += 2; 89 | } 90 | q.loc.start.column -= 1; 91 | if (q.tail) { 92 | q.loc.end.column += 1; 93 | } else { 94 | q.loc.end.column += 2; 95 | } 96 | } 97 | } 98 | }, 99 | }; 100 | -------------------------------------------------------------------------------- /lib/babylon-to-espree/convertComments.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | module.exports = function(comments) { 4 | for (let i = 0; i < comments.length; i++) { 5 | const comment = comments[i]; 6 | if (comment.type === "CommentBlock") { 7 | comment.type = "Block"; 8 | } else if (comment.type === "CommentLine") { 9 | comment.type = "Line"; 10 | } 11 | // sometimes comments don't get ranges computed, 12 | // even with options.ranges === true 13 | if (!comment.range) { 14 | comment.range = [comment.start, comment.end]; 15 | } 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /lib/babylon-to-espree/convertProgramNode.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | module.exports = function(ast) { 4 | ast.type = "Program"; 5 | ast.sourceType = ast.program.sourceType; 6 | ast.directives = ast.program.directives; 7 | ast.body = ast.program.body; 8 | delete ast.program; 9 | 10 | if (ast.comments.length) { 11 | const lastComment = ast.comments[ast.comments.length - 1]; 12 | 13 | if (!ast.tokens.length) { 14 | // if no tokens, the program starts at the end of the last comment 15 | ast.start = lastComment.end; 16 | ast.loc.start.line = lastComment.loc.end.line; 17 | ast.loc.start.column = lastComment.loc.end.column; 18 | } else { 19 | const lastToken = ast.tokens[ast.tokens.length - 1]; 20 | 21 | if (lastComment.end > lastToken.end) { 22 | // If there is a comment after the last token, the program ends at the 23 | // last token and not the comment 24 | ast.range[1] = lastToken.end; 25 | ast.loc.end.line = lastToken.loc.end.line; 26 | ast.loc.end.column = lastToken.loc.end.column; 27 | } 28 | } 29 | } else { 30 | if (!ast.tokens.length) { 31 | ast.loc.start.line = 1; 32 | ast.loc.end.line = 1; 33 | } 34 | } 35 | 36 | if (ast.body && ast.body.length > 0) { 37 | ast.loc.start.line = ast.body[0].loc.start.line; 38 | ast.range[0] = ast.body[0].start; 39 | } 40 | }; 41 | -------------------------------------------------------------------------------- /lib/babylon-to-espree/convertTemplateType.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | module.exports = function(tokens, tt) { 4 | let curlyBrace = null; 5 | let templateTokens = []; 6 | const result = []; 7 | 8 | function addTemplateType() { 9 | const start = templateTokens[0]; 10 | const end = templateTokens[templateTokens.length - 1]; 11 | 12 | const value = templateTokens.reduce((result, token) => { 13 | if (token.value) { 14 | result += token.value; 15 | } else if (token.type !== tt.template) { 16 | result += token.type.label; 17 | } 18 | 19 | return result; 20 | }, ""); 21 | 22 | result.push({ 23 | type: "Template", 24 | value: value, 25 | start: start.start, 26 | end: end.end, 27 | loc: { 28 | start: start.loc.start, 29 | end: end.loc.end, 30 | }, 31 | }); 32 | 33 | templateTokens = []; 34 | } 35 | 36 | tokens.forEach(token => { 37 | switch (token.type) { 38 | case tt.backQuote: 39 | if (curlyBrace) { 40 | result.push(curlyBrace); 41 | curlyBrace = null; 42 | } 43 | 44 | templateTokens.push(token); 45 | 46 | if (templateTokens.length > 1) { 47 | addTemplateType(); 48 | } 49 | 50 | break; 51 | 52 | case tt.dollarBraceL: 53 | templateTokens.push(token); 54 | addTemplateType(); 55 | break; 56 | 57 | case tt.braceR: 58 | if (curlyBrace) { 59 | result.push(curlyBrace); 60 | } 61 | 62 | curlyBrace = token; 63 | break; 64 | 65 | case tt.template: 66 | if (curlyBrace) { 67 | templateTokens.push(curlyBrace); 68 | curlyBrace = null; 69 | } 70 | 71 | templateTokens.push(token); 72 | break; 73 | 74 | case tt.eof: 75 | if (curlyBrace) { 76 | result.push(curlyBrace); 77 | } 78 | 79 | break; 80 | 81 | default: 82 | if (curlyBrace) { 83 | result.push(curlyBrace); 84 | curlyBrace = null; 85 | } 86 | 87 | result.push(token); 88 | } 89 | }); 90 | 91 | return result; 92 | }; 93 | -------------------------------------------------------------------------------- /lib/babylon-to-espree/convertToken.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | module.exports = function(token, tt, source) { 4 | const type = token.type; 5 | token.range = [token.start, token.end]; 6 | 7 | if (type === tt.name) { 8 | token.type = "Identifier"; 9 | } else if ( 10 | type === tt.semi || 11 | type === tt.comma || 12 | type === tt.parenL || 13 | type === tt.parenR || 14 | type === tt.braceL || 15 | type === tt.braceR || 16 | type === tt.slash || 17 | type === tt.dot || 18 | type === tt.bracketL || 19 | type === tt.bracketR || 20 | type === tt.ellipsis || 21 | type === tt.arrow || 22 | type === tt.pipeline || 23 | type === tt.star || 24 | type === tt.incDec || 25 | type === tt.colon || 26 | type === tt.question || 27 | type === tt.questionDot || 28 | type === tt.template || 29 | type === tt.backQuote || 30 | type === tt.dollarBraceL || 31 | type === tt.at || 32 | type === tt.logicalOR || 33 | type === tt.logicalAND || 34 | type === tt.nullishCoalescing || 35 | type === tt.bitwiseOR || 36 | type === tt.bitwiseXOR || 37 | type === tt.bitwiseAND || 38 | type === tt.equality || 39 | type === tt.relational || 40 | type === tt.bitShift || 41 | type === tt.plusMin || 42 | type === tt.modulo || 43 | type === tt.exponent || 44 | type === tt.bang || 45 | type === tt.tilde || 46 | type === tt.doubleColon || 47 | type === tt.hash || 48 | type.isAssign 49 | ) { 50 | token.type = "Punctuator"; 51 | if (!token.value) token.value = type.label; 52 | } else if (type === tt.jsxTagStart) { 53 | token.type = "Punctuator"; 54 | token.value = "<"; 55 | } else if (type === tt.jsxTagEnd) { 56 | token.type = "Punctuator"; 57 | token.value = ">"; 58 | } else if (type === tt.jsxName) { 59 | token.type = "JSXIdentifier"; 60 | } else if (type === tt.jsxText) { 61 | token.type = "JSXText"; 62 | } else if (type.keyword === "null") { 63 | token.type = "Null"; 64 | } else if (type.keyword === "false" || type.keyword === "true") { 65 | token.type = "Boolean"; 66 | } else if (type.keyword) { 67 | token.type = "Keyword"; 68 | } else if (type === tt.num) { 69 | token.type = "Numeric"; 70 | token.value = source.slice(token.start, token.end); 71 | } else if (type === tt.string) { 72 | token.type = "String"; 73 | token.value = source.slice(token.start, token.end); 74 | } else if (type === tt.regexp) { 75 | token.type = "RegularExpression"; 76 | const value = token.value; 77 | token.regex = { 78 | pattern: value.pattern, 79 | flags: value.flags, 80 | }; 81 | token.value = `/${value.pattern}/${value.flags}`; 82 | } 83 | 84 | return token; 85 | }; 86 | -------------------------------------------------------------------------------- /lib/babylon-to-espree/convertTokens.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const convertTemplateType = require("./convertTemplateType"); 4 | const convertToken = require("./convertToken"); 5 | 6 | module.exports = function(tokens, tt, code) { 7 | return convertTemplateType(tokens, tt) 8 | .filter(t => t.type !== "CommentLine" && t.type !== "CommentBlock") 9 | .map(t => convertToken(t, tt, code)); 10 | }; 11 | -------------------------------------------------------------------------------- /lib/babylon-to-espree/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const convertTokens = require("./convertTokens"); 4 | const convertComments = require("./convertComments"); 5 | const convertAST = require("./convertAST"); 6 | 7 | module.exports = function(ast, traverse, tt, code) { 8 | ast.tokens = convertTokens(ast.tokens, tt, code); 9 | convertComments(ast.comments); 10 | convertAST(ast, traverse, code); 11 | }; 12 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const semver = require("semver"); 4 | const babelCore = require("@babel/core"); 5 | const packageJson = require("../package.json"); 6 | 7 | const CURRENT_BABEL_VERSION = babelCore.version; 8 | const SUPPORTED_BABEL_VERSION_RANGE = 9 | packageJson.peerDependencies["@babel/core"]; 10 | const IS_RUNNING_SUPPORTED_VERSION = semver.satisfies( 11 | CURRENT_BABEL_VERSION, 12 | SUPPORTED_BABEL_VERSION_RANGE 13 | ); 14 | 15 | exports.parse = function(code, options) { 16 | return exports.parseForESLint(code, options).ast; 17 | }; 18 | 19 | exports.parseForESLint = function(code, options = {}) { 20 | if (!IS_RUNNING_SUPPORTED_VERSION) { 21 | throw new Error( 22 | `babel-eslint@${packageJson.version} does not support @babel/core@${CURRENT_BABEL_VERSION}. Please downgrade to babel-eslint@^10 or upgrade to @babel/core@${SUPPORTED_BABEL_VERSION_RANGE}` 23 | ); 24 | } 25 | 26 | options.babelOptions = options.babelOptions || {}; 27 | options.ecmaVersion = options.ecmaVersion || 2018; 28 | options.sourceType = options.sourceType || "module"; 29 | options.allowImportExportEverywhere = 30 | options.allowImportExportEverywhere || false; 31 | 32 | return require("./parse-with-scope")(code, options); 33 | }; 34 | -------------------------------------------------------------------------------- /lib/parse-with-scope.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const visitorKeys = require("./visitor-keys"); 4 | const analyzeScope = require("./analyze-scope"); 5 | const parse = require("./parse"); 6 | 7 | module.exports = function(code, options) { 8 | const ast = parse(code, options); 9 | const scopeManager = analyzeScope(ast, options); 10 | 11 | return { ast, scopeManager, visitorKeys }; 12 | }; 13 | -------------------------------------------------------------------------------- /lib/parse.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const babylonToEspree = require("./babylon-to-espree"); 4 | const { 5 | parseSync: parse, 6 | tokTypes: tt, 7 | traverse, 8 | loadPartialConfig, 9 | } = require("@babel/core"); 10 | 11 | module.exports = function(code, options) { 12 | let opts = { 13 | sourceType: options.sourceType, 14 | filename: options.filePath, 15 | cwd: options.babelOptions.cwd, 16 | root: options.babelOptions.root, 17 | rootMode: options.babelOptions.rootMode, 18 | envName: options.babelOptions.envName, 19 | configFile: options.babelOptions.configFile, 20 | babelrc: options.babelOptions.babelrc, 21 | babelrcRoots: options.babelOptions.babelrcRoots, 22 | extends: options.babelOptions.extends, 23 | env: options.babelOptions.env, 24 | overrides: options.babelOptions.overrides, 25 | test: options.babelOptions.test, 26 | include: options.babelOptions.include, 27 | exclude: options.babelOptions.exclude, 28 | ignore: options.babelOptions.ignore, 29 | only: options.babelOptions.only, 30 | parserOpts: { 31 | allowImportExportEverywhere: options.allowImportExportEverywhere, // consistent with espree 32 | allowReturnOutsideFunction: true, 33 | allowSuperOutsideMethod: true, 34 | ranges: true, 35 | tokens: true, 36 | plugins: ["estree"], 37 | }, 38 | caller: { 39 | name: "babel-eslint", 40 | }, 41 | }; 42 | 43 | if (options.requireConfigFile !== false) { 44 | const config = loadPartialConfig(opts); 45 | 46 | if (config !== null) { 47 | if (!config.hasFilesystemConfig()) { 48 | throw new Error( 49 | `No Babel config file detected for ${config.options.filename}. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files.` 50 | ); 51 | } 52 | 53 | opts = config.options; 54 | } 55 | } 56 | 57 | let ast; 58 | 59 | try { 60 | ast = parse(code, opts); 61 | } catch (err) { 62 | if (err instanceof SyntaxError) { 63 | err.lineNumber = err.loc.line; 64 | err.column = err.loc.column; 65 | } 66 | 67 | throw err; 68 | } 69 | 70 | babylonToEspree(ast, traverse, tt, code); 71 | 72 | return ast; 73 | }; 74 | -------------------------------------------------------------------------------- /lib/visitor-keys.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const BABEL_VISITOR_KEYS = require("@babel/core").types.VISITOR_KEYS; 4 | const ESLINT_VISITOR_KEYS = require("eslint-visitor-keys").KEYS; 5 | 6 | module.exports = Object.assign( 7 | { 8 | Literal: ESLINT_VISITOR_KEYS.Literal, 9 | MethodDefinition: ["decorators"].concat( 10 | ESLINT_VISITOR_KEYS.MethodDefinition 11 | ), 12 | Property: ["decorators"].concat(ESLINT_VISITOR_KEYS.Property), 13 | }, 14 | BABEL_VISITOR_KEYS 15 | ); 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "babel-eslint", 3 | "version": "11.0.0-beta.2", 4 | "description": "Custom parser for ESLint", 5 | "author": "Sebastian McKenzie ", 6 | "license": "MIT", 7 | "repository": { 8 | "type": "git", 9 | "url": "https://github.com/babel/babel-eslint.git" 10 | }, 11 | "bugs": { 12 | "url": "https://github.com/babel/babel-eslint/issues" 13 | }, 14 | "homepage": "https://github.com/babel/babel-eslint", 15 | "scripts": { 16 | "test": "npm run lint && npm run test-only", 17 | "test-only": "cd test && mocha specs && cd -", 18 | "lint": "eslint .", 19 | "lint-fix": "npm run lint -- --fix", 20 | "precommit": "lint-staged", 21 | "preversion": "npm test", 22 | "changelog": "git log `git describe --tags --abbrev=0`..HEAD --pretty=format:' * %s (%an)' | grep -v 'Merge pull request'" 23 | }, 24 | "engines": { 25 | "node": ">=8" 26 | }, 27 | "main": "lib/index.js", 28 | "files": [ 29 | "lib" 30 | ], 31 | "peerDependencies": { 32 | "@babel/core": ">=7.2.0", 33 | "eslint": ">= 6.0.0" 34 | }, 35 | "dependencies": { 36 | "eslint-scope": "5.0.0", 37 | "eslint-visitor-keys": "^1.1.0", 38 | "semver": "^6.3.0" 39 | }, 40 | "devDependencies": { 41 | "@babel/core": "^7.2.0", 42 | "@babel/plugin-proposal-class-properties": "^7.1.0", 43 | "@babel/plugin-proposal-decorators": "^7.1.2", 44 | "@babel/plugin-proposal-nullish-coalescing-operator": "^7.0.0", 45 | "@babel/plugin-proposal-optional-chaining": "^7.0.0", 46 | "@babel/plugin-proposal-pipeline-operator": "^7.0.0", 47 | "@babel/plugin-syntax-dynamic-import": "^7.0.0", 48 | "@babel/plugin-syntax-export-default-from": "^7.0.0", 49 | "@babel/plugin-syntax-export-namespace-from": "^7.0.0", 50 | "@babel/plugin-syntax-import-meta": "^7.0.0", 51 | "@babel/plugin-syntax-numeric-separator": "^7.0.0", 52 | "@babel/preset-env": "^7.1.5", 53 | "@babel/preset-flow": "^7.0.0", 54 | "@babel/preset-react": "^7.0.0", 55 | "babel-eslint": "^10.0.2", 56 | "dedent": "^0.7.0", 57 | "eslint": "^6.0.1", 58 | "eslint-config-babel": "^9.0.0", 59 | "eslint-plugin-flowtype": "^3.11.1", 60 | "eslint-plugin-import": "^2.14.0", 61 | "eslint-plugin-prettier": "^3.1.0", 62 | "espree": "^6.0.0", 63 | "husky": "^1.0.0-rc.13", 64 | "lint-staged": "^7.2.2", 65 | "mocha": "^6.1.4", 66 | "prettier": "^1.4.4" 67 | }, 68 | "lint-staged": { 69 | "*.js": [ 70 | "eslint --format=codeframe --fix", 71 | "git add" 72 | ] 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /test/babel.config.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | module.exports = { 4 | presets: [ 5 | ["@babel/preset-env", { forceAllTransforms: true }], 6 | ["@babel/preset-flow", { all: true }], 7 | "@babel/preset-react", 8 | ], 9 | plugins: [ 10 | "@babel/plugin-syntax-dynamic-import", 11 | "@babel/plugin-syntax-import-meta", 12 | "@babel/plugin-syntax-export-default-from", 13 | "@babel/plugin-proposal-class-properties", 14 | "@babel/plugin-proposal-nullish-coalescing-operator", 15 | "@babel/plugin-proposal-optional-chaining", 16 | "@babel/plugin-syntax-numeric-separator", 17 | "@babel/plugin-syntax-export-namespace-from", 18 | ["@babel/plugin-proposal-decorators", { decoratorsBeforeExport: false }], 19 | ["@babel/plugin-proposal-pipeline-operator", { proposal: "minimal" }], 20 | ], 21 | }; 22 | -------------------------------------------------------------------------------- /test/fixtures/config/babel.config.decorators-legacy.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | module.exports = { 4 | presets: [["@babel/preset-env", { forceAllTransforms: true }]], 5 | plugins: [["@babel/plugin-proposal-decorators", { legacy: true }]], 6 | }; 7 | -------------------------------------------------------------------------------- /test/fixtures/eslint-plugin-import/.eslintrc.yml: -------------------------------------------------------------------------------- 1 | root: true 2 | 3 | # babel-eslint 4 | parser: ../../../lib/index.js 5 | 6 | # use eslint-plugin-import 7 | plugins: 8 | - import 9 | rules: 10 | import/no-named-as-default: error 11 | no-unused-vars: error 12 | -------------------------------------------------------------------------------- /test/fixtures/eslint-plugin-import/a.js: -------------------------------------------------------------------------------- 1 | export default function foo() { } 2 | -------------------------------------------------------------------------------- /test/fixtures/eslint-plugin-import/b.js: -------------------------------------------------------------------------------- 1 | import foo from './a.js'; 2 | -------------------------------------------------------------------------------- /test/fixtures/eslint-plugin-import/c.js: -------------------------------------------------------------------------------- 1 | // @flow 2 | type Foo = {}; 3 | 4 | const FlowTypeButton = ({ }: Foo) => { }; 5 | -------------------------------------------------------------------------------- /test/fixtures/rules/strict/function-with.js: -------------------------------------------------------------------------------- 1 | function x () { 2 | "use strict"; 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/rules/strict/function-without.js: -------------------------------------------------------------------------------- 1 | function x () {} 2 | -------------------------------------------------------------------------------- /test/fixtures/rules/strict/global-with-function-with.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | function x () { 4 | "use strict"; 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/rules/strict/global-with-function-without.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | function x () {} 4 | -------------------------------------------------------------------------------- /test/fixtures/rules/strict/global-with.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | /* 3 | The empty statement is intentional. As of now, ESLint won't enforce 4 | string: [2, "global"] on a program with an empty body. A test for that without 5 | massaging the AST to ESlint's input format should fail. 6 | */ 7 | -------------------------------------------------------------------------------- /test/fixtures/rules/strict/none.js: -------------------------------------------------------------------------------- 1 | "no use strict anywhere"; 2 | -------------------------------------------------------------------------------- /test/fixtures/rules/syntax-error.js: -------------------------------------------------------------------------------- 1 | class ClassName { 2 | constructor() { 3 | 4 | }, 5 | aMethod() {} 6 | } 7 | -------------------------------------------------------------------------------- /test/helpers/assert-implements-ast.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | // Checks if the source ast implements the target ast. Ignores extra keys on source ast 4 | module.exports = function assertImplementsAST(target, source, path) { 5 | if (!path) { 6 | path = []; 7 | } 8 | 9 | function error(text) { 10 | const err = new Error(`At ${path.join(".")}: ${text}:`); 11 | err.depth = path.length + 1; 12 | throw err; 13 | } 14 | 15 | const typeA = target === null ? "null" : typeof target; 16 | const typeB = source === null ? "null" : typeof source; 17 | if (typeA !== typeB) { 18 | error( 19 | `have different types (${typeA} !== ${typeB}) (${target} !== ${source})` 20 | ); 21 | } else if ( 22 | typeA === "object" && 23 | ["RegExp"].indexOf(target.constructor.name) !== -1 && 24 | target.constructor.name !== source.constructor.name 25 | ) { 26 | error( 27 | `object have different constructors (${target.constructor.name} !== ${source.constructor.name}` 28 | ); 29 | } else if (typeA === "object") { 30 | const keysTarget = Object.keys(target); 31 | for (const i in keysTarget) { 32 | const key = keysTarget[i]; 33 | path.push(key); 34 | assertImplementsAST(target[key], source[key], path); 35 | path.pop(); 36 | } 37 | } else if (target !== source) { 38 | error( 39 | `are different (${JSON.stringify(target)} !== ${JSON.stringify(source)})` 40 | ); 41 | } 42 | }; 43 | -------------------------------------------------------------------------------- /test/specs/babel-eslint.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const assert = require("assert"); 4 | const babelEslint = require("../.."); 5 | const espree = require("espree"); 6 | const escope = require("eslint-scope"); 7 | const unpad = require("dedent"); 8 | const assertImplementsAST = require("../helpers/assert-implements-ast"); 9 | 10 | function parseAndAssertSame(code) { 11 | code = unpad(code); 12 | const esAST = espree.parse(code, { 13 | ecmaFeatures: { 14 | // enable JSX parsing 15 | jsx: true, 16 | // enable return in global scope 17 | globalReturn: true, 18 | // enable implied strict mode (if ecmaVersion >= 5) 19 | impliedStrict: true, 20 | // allow experimental object rest/spread 21 | experimentalObjectRestSpread: true, 22 | }, 23 | tokens: true, 24 | loc: true, 25 | range: true, 26 | comment: true, 27 | ecmaVersion: 2018, 28 | sourceType: "module", 29 | }); 30 | const babylonAST = babelEslint.parseForESLint(code, { 31 | eslintVisitorKeys: true, 32 | eslintScopeManager: true, 33 | }).ast; 34 | assertImplementsAST(esAST, babylonAST); 35 | } 36 | 37 | describe("babylon-to-espree", () => { 38 | describe("compatibility", () => { 39 | it("should allow ast.analyze to be called without options", function() { 40 | const esAST = babelEslint.parseForESLint("`test`", { 41 | eslintScopeManager: true, 42 | eslintVisitorKeys: true, 43 | }).ast; 44 | 45 | assert.doesNotThrow( 46 | () => { 47 | escope.analyze(esAST); 48 | }, 49 | TypeError, 50 | "Should allow no options argument." 51 | ); 52 | }); 53 | }); 54 | 55 | describe("templates", () => { 56 | it("empty template string", () => { 57 | parseAndAssertSame("``"); 58 | }); 59 | 60 | it("template string", () => { 61 | parseAndAssertSame("`test`"); 62 | }); 63 | 64 | it("template string using $", () => { 65 | parseAndAssertSame("`$`"); 66 | }); 67 | 68 | it("template string with expression", () => { 69 | parseAndAssertSame("`${a}`"); 70 | }); 71 | 72 | it("template string with multiple expressions", () => { 73 | parseAndAssertSame("`${a}${b}${c}`"); 74 | }); 75 | 76 | it("template string with expression and strings", () => { 77 | parseAndAssertSame("`a${a}a`"); 78 | }); 79 | 80 | it("template string with binary expression", () => { 81 | parseAndAssertSame("`a${a + b}a`"); 82 | }); 83 | 84 | it("tagged template", () => { 85 | parseAndAssertSame("jsx``"); 86 | }); 87 | 88 | it("tagged template with expression", () => { 89 | parseAndAssertSame("jsx``"); 90 | }); 91 | 92 | it("tagged template with new operator", () => { 93 | parseAndAssertSame("new raw`42`"); 94 | }); 95 | 96 | it("template with nested function/object", () => { 97 | parseAndAssertSame( 98 | "`outer${{x: {y: 10}}}bar${`nested${function(){return 1;}}endnest`}end`" 99 | ); 100 | }); 101 | 102 | it("template with braces inside and outside of template string #96", () => { 103 | parseAndAssertSame( 104 | "if (a) { var target = `{}a:${webpackPort}{}}}}`; } else { app.use(); }" 105 | ); 106 | }); 107 | 108 | it("template also with braces #96", () => { 109 | parseAndAssertSame(` 110 | export default function f1() { 111 | function f2(foo) { 112 | const bar = 3; 113 | return \`\${foo} \${bar}\`; 114 | } 115 | return f2; 116 | } 117 | `); 118 | }); 119 | 120 | it("template with destructuring #31", () => { 121 | parseAndAssertSame(` 122 | module.exports = { 123 | render() { 124 | var {name} = this.props; 125 | return Math.max(null, \`Name: \${name}, Name: \${name}\`); 126 | } 127 | }; 128 | `); 129 | }); 130 | 131 | it("template with arrow returning template #603", () => { 132 | parseAndAssertSame(` 133 | var a = \`\${() => { 134 | \`\${''}\` 135 | }}\`; 136 | `); 137 | }); 138 | 139 | it("template string with object with template string inside", () => { 140 | parseAndAssertSame("`${ { a:`${2}` } }`"); 141 | }); 142 | }); 143 | 144 | it("simple expression", () => { 145 | parseAndAssertSame("a = 1"); 146 | }); 147 | 148 | it("logical NOT", () => { 149 | parseAndAssertSame("!0"); 150 | }); 151 | 152 | it("bitwise NOT", () => { 153 | parseAndAssertSame("~0"); 154 | }); 155 | 156 | it("class declaration", () => { 157 | parseAndAssertSame("class Foo {}"); 158 | }); 159 | 160 | it("class expression", () => { 161 | parseAndAssertSame("var a = class Foo {}"); 162 | }); 163 | 164 | it("jsx expression", () => { 165 | parseAndAssertSame(""); 166 | }); 167 | 168 | it("jsx expression with 'this' as identifier", () => { 169 | parseAndAssertSame(""); 170 | }); 171 | 172 | it("jsx expression with a dynamic attribute", () => { 173 | parseAndAssertSame(""); 174 | }); 175 | 176 | it("jsx expression with a member expression as identifier", () => { 177 | parseAndAssertSame(""); 178 | }); 179 | 180 | it("jsx expression with spread", () => { 181 | parseAndAssertSame("var myDivElement =
;"); 182 | }); 183 | 184 | it("empty jsx text", () => { 185 | parseAndAssertSame(""); 186 | }); 187 | 188 | it("jsx text with content", () => { 189 | parseAndAssertSame("Hello, world!"); 190 | }); 191 | 192 | it("nested jsx", () => { 193 | parseAndAssertSame("
\n

Wat

\n
"); 194 | }); 195 | 196 | it("default import", () => { 197 | parseAndAssertSame('import foo from "foo";'); 198 | }); 199 | 200 | it("import specifier", () => { 201 | parseAndAssertSame('import { foo } from "foo";'); 202 | }); 203 | 204 | it("import specifier with name", () => { 205 | parseAndAssertSame('import { foo as bar } from "foo";'); 206 | }); 207 | 208 | it("import bare", () => { 209 | parseAndAssertSame('import "foo";'); 210 | }); 211 | 212 | it("export default class declaration", () => { 213 | parseAndAssertSame("export default class Foo {}"); 214 | }); 215 | 216 | it("export default class expression", () => { 217 | parseAndAssertSame("export default class {}"); 218 | }); 219 | 220 | it("export default function declaration", () => { 221 | parseAndAssertSame("export default function Foo() {}"); 222 | }); 223 | 224 | it("export default function expression", () => { 225 | parseAndAssertSame("export default function () {}"); 226 | }); 227 | 228 | it("export all", () => { 229 | parseAndAssertSame('export * from "foo";'); 230 | }); 231 | 232 | it("export named", () => { 233 | parseAndAssertSame("var foo = 1;export { foo };"); 234 | }); 235 | 236 | it("export named alias", () => { 237 | parseAndAssertSame("var foo = 1;export { foo as bar };"); 238 | }); 239 | 240 | // Espree doesn't support the optional chaining operator yet 241 | it("optional chaining operator (token)", () => { 242 | const code = "foo?.bar"; 243 | const babylonAST = babelEslint.parseForESLint(code, { 244 | eslintVisitorKeys: true, 245 | eslintScopeManager: true, 246 | }).ast; 247 | assert.strictEqual(babylonAST.tokens[1].type, "Punctuator"); 248 | }); 249 | 250 | // Espree doesn't support the nullish coalescing operator yet 251 | it("nullish coalescing operator (token)", () => { 252 | const code = "foo ?? bar"; 253 | const babylonAST = babelEslint.parseForESLint(code, { 254 | eslintVisitorKeys: true, 255 | eslintScopeManager: true, 256 | }).ast; 257 | assert.strictEqual(babylonAST.tokens[1].type, "Punctuator"); 258 | }); 259 | 260 | // Espree doesn't support the pipeline operator yet 261 | it("pipeline operator (token)", () => { 262 | const code = "foo |> bar"; 263 | const babylonAST = babelEslint.parseForESLint(code, { 264 | eslintVisitorKeys: true, 265 | eslintScopeManager: true, 266 | }).ast; 267 | assert.strictEqual(babylonAST.tokens[1].type, "Punctuator"); 268 | }); 269 | 270 | // Espree doesn't support the private fields yet 271 | it("hash (token)", () => { 272 | const code = "class A { #x }"; 273 | const babylonAST = babelEslint.parseForESLint(code, { 274 | eslintVisitorKeys: true, 275 | eslintScopeManager: true, 276 | }).ast; 277 | assert.strictEqual(babylonAST.tokens[3].type, "Punctuator"); 278 | assert.strictEqual(babylonAST.tokens[3].value, "#"); 279 | }); 280 | 281 | it.skip("empty program with line comment", () => { 282 | parseAndAssertSame("// single comment"); 283 | }); 284 | 285 | it.skip("empty program with block comment", () => { 286 | parseAndAssertSame(" /* multiline\n * comment\n*/"); 287 | }); 288 | 289 | it("line comments", () => { 290 | parseAndAssertSame(` 291 | // single comment 292 | var foo = 15; // comment next to statement 293 | // second comment after statement 294 | `); 295 | }); 296 | 297 | it("block comments", () => { 298 | parseAndAssertSame(` 299 | /* single comment */ 300 | var foo = 15; /* comment next to statement */ 301 | /* 302 | * multiline 303 | * comment 304 | */ 305 | `); 306 | }); 307 | 308 | it("block comments #124", () => { 309 | parseAndAssertSame(` 310 | React.createClass({ 311 | render() { 312 | // return ( 313 | //
314 | // ); // <-- this is the line that is reported 315 | } 316 | }); 317 | `); 318 | }); 319 | 320 | it("null", () => { 321 | parseAndAssertSame("null"); 322 | }); 323 | 324 | it("boolean", () => { 325 | parseAndAssertSame("if (true) {} else if (false) {}"); 326 | }); 327 | 328 | it("regexp", () => { 329 | parseAndAssertSame("/affix-top|affix-bottom|affix|[a-z]/"); 330 | }); 331 | 332 | it("regexp", () => { 333 | parseAndAssertSame("const foo = /foo/;"); 334 | }); 335 | 336 | it("regexp y flag", () => { 337 | parseAndAssertSame("const foo = /foo/y;"); 338 | }); 339 | 340 | it("regexp u flag", () => { 341 | parseAndAssertSame("const foo = /foo/u;"); 342 | }); 343 | 344 | it("regexp in a template string", () => { 345 | parseAndAssertSame('`${/\\d/.exec("1")[0]}`'); 346 | }); 347 | 348 | it("first line is empty", () => { 349 | parseAndAssertSame('\nimport Immutable from "immutable";'); 350 | }); 351 | 352 | it("empty", () => { 353 | parseAndAssertSame(""); 354 | }); 355 | 356 | it("jsdoc", () => { 357 | parseAndAssertSame(` 358 | /** 359 | * @param {object} options 360 | * @return {number} 361 | */ 362 | const test = function({ a, b, c }) { 363 | return a + b + c; 364 | }; 365 | module.exports = test; 366 | `); 367 | }); 368 | 369 | it("empty block with comment", () => { 370 | parseAndAssertSame(` 371 | function a () { 372 | try { 373 | b(); 374 | } catch (e) { 375 | // asdf 376 | } 377 | } 378 | `); 379 | }); 380 | 381 | describe("babel tests", () => { 382 | it("MethodDefinition", () => { 383 | parseAndAssertSame(` 384 | export default class A { 385 | a() {} 386 | } 387 | `); 388 | }); 389 | 390 | it("MethodDefinition 2", () => { 391 | parseAndAssertSame( 392 | "export default class Bar { get bar() { return 42; }}" 393 | ); 394 | }); 395 | 396 | it("ClassMethod", () => { 397 | parseAndAssertSame(` 398 | class A { 399 | constructor() { 400 | } 401 | } 402 | `); 403 | }); 404 | 405 | it("ClassMethod multiple params", () => { 406 | parseAndAssertSame(` 407 | class A { 408 | constructor(a, b, c) { 409 | } 410 | } 411 | `); 412 | }); 413 | 414 | it("ClassMethod multiline", () => { 415 | parseAndAssertSame(` 416 | class A { 417 | constructor ( 418 | a, 419 | b, 420 | c 421 | ) 422 | 423 | { 424 | 425 | } 426 | } 427 | `); 428 | }); 429 | 430 | it("ClassMethod oneline", () => { 431 | parseAndAssertSame("class A { constructor(a, b, c) {} }"); 432 | }); 433 | 434 | it("ObjectMethod", () => { 435 | parseAndAssertSame(` 436 | var a = { 437 | b(c) { 438 | } 439 | } 440 | `); 441 | }); 442 | 443 | it("do not allow import export everywhere", () => { 444 | assert.throws(() => { 445 | parseAndAssertSame('function F() { import a from "a"; }'); 446 | }, /SyntaxError: 'import' and 'export' may only appear at the top level/); 447 | }); 448 | 449 | it("return outside function", () => { 450 | parseAndAssertSame("return;"); 451 | }); 452 | 453 | it("super outside method", () => { 454 | assert.throws(() => { 455 | parseAndAssertSame("function F() { super(); }"); 456 | }, /SyntaxError: 'super' keyword outside a method/); 457 | }); 458 | 459 | it("StringLiteral", () => { 460 | parseAndAssertSame(""); 461 | parseAndAssertSame(""); 462 | parseAndAssertSame("a"); 463 | }); 464 | 465 | it("getters and setters", () => { 466 | parseAndAssertSame("class A { get x ( ) { ; } }"); 467 | parseAndAssertSame(` 468 | class A { 469 | get x( 470 | ) 471 | { 472 | ; 473 | } 474 | } 475 | `); 476 | parseAndAssertSame("class A { set x (a) { ; } }"); 477 | parseAndAssertSame(` 478 | class A { 479 | set x(a 480 | ) 481 | { 482 | ; 483 | } 484 | } 485 | `); 486 | parseAndAssertSame(` 487 | var B = { 488 | get x () { 489 | return this.ecks; 490 | }, 491 | set x (ecks) { 492 | this.ecks = ecks; 493 | } 494 | }; 495 | `); 496 | }); 497 | 498 | it("RestOperator", () => { 499 | parseAndAssertSame("var { a, ...b } = c"); 500 | parseAndAssertSame("var [ a, ...b ] = c"); 501 | parseAndAssertSame("var a = function (...b) {}"); 502 | }); 503 | 504 | it("SpreadOperator", () => { 505 | parseAndAssertSame("var a = { b, ...c }"); 506 | parseAndAssertSame("var a = [ a, ...b ]"); 507 | parseAndAssertSame("var a = sum(...b)"); 508 | }); 509 | 510 | it("Async/Await", () => { 511 | parseAndAssertSame(` 512 | async function a() { 513 | await 1; 514 | } 515 | `); 516 | }); 517 | }); 518 | }); 519 | -------------------------------------------------------------------------------- /test/specs/integration.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const assert = require("assert"); 4 | const eslint = require("eslint"); 5 | const fs = require("fs"); 6 | const path = require("path"); 7 | 8 | const parser = require("../.."); 9 | 10 | eslint.linter.defineParser("current-babel-eslint", parser); 11 | 12 | const paths = { 13 | fixtures: path.join(__dirname, "..", "fixtures", "rules"), 14 | }; 15 | 16 | const encoding = "utf8"; 17 | const errorLevel = 2; 18 | 19 | const baseEslintOpts = { 20 | parser: "current-babel-eslint", 21 | parserOptions: { 22 | sourceType: "script", 23 | }, 24 | }; 25 | 26 | /** 27 | * Load a fixture and run eslint.linter.verify() on it. 28 | * Pass the return value to done(). 29 | * @param object opts 30 | * @param function done 31 | */ 32 | function lint(opts, done) { 33 | readFixture(opts.fixture, (err, src) => { 34 | if (err) return done(err); 35 | done(null, eslint.linter.verify(src, opts.eslint)); 36 | }); 37 | } 38 | 39 | /** 40 | * Read a fixture file, passing the content to done(). 41 | * @param string|array id 42 | * @param function done 43 | */ 44 | function readFixture(id, done) { 45 | if (Array.isArray(id)) id = path.join.apply(path, id); 46 | if (!path.extname(id)) id += ".js"; 47 | fs.readFile(path.join(paths.fixtures, id), encoding, done); 48 | } 49 | // readFixture 50 | 51 | describe("Rules:", () => { 52 | describe("`strict`", strictSuite); 53 | }); 54 | // describe 55 | 56 | function strictSuite() { 57 | const ruleId = "strict"; 58 | 59 | describe("when set to 'never'", () => { 60 | const eslintOpts = Object.assign({}, baseEslintOpts, { 61 | rules: {}, 62 | }); 63 | eslintOpts.rules[ruleId] = [errorLevel, "never"]; 64 | 65 | ["global-with", "function-with"].forEach(fixture => { 66 | it(`should error on ${fixture.match(/^[^-]+/)[0]} directive`, done => { 67 | lint( 68 | { 69 | fixture: ["strict", fixture], 70 | eslint: eslintOpts, 71 | }, 72 | (err, report) => { 73 | if (err) return done(err); 74 | assert(report[0].ruleId === ruleId); 75 | done(); 76 | } 77 | ); 78 | }); 79 | // it 80 | }); 81 | }); 82 | // describe 83 | 84 | describe("when set to 'global'", () => { 85 | const eslintOpts = Object.assign({}, baseEslintOpts, { 86 | rules: {}, 87 | }); 88 | eslintOpts.rules[ruleId] = [errorLevel, "global"]; 89 | 90 | it("shouldn't error on single global directive", done => { 91 | lint( 92 | { 93 | fixture: ["strict", "global-with"], 94 | eslint: eslintOpts, 95 | }, 96 | (err, report) => { 97 | if (err) return done(err); 98 | assert(!report.length); 99 | done(); 100 | } 101 | ); 102 | }); 103 | // it 104 | 105 | it("should error twice on global directive: no and function directive: yes", done => { 106 | lint( 107 | { 108 | fixture: ["strict", "function-with"], 109 | eslint: eslintOpts, 110 | }, 111 | (err, report) => { 112 | if (err) return done(err); 113 | [0, 1].forEach(i => { 114 | assert(report[i].ruleId === ruleId); 115 | }); 116 | done(); 117 | } 118 | ); 119 | }); 120 | // it 121 | 122 | it("should error on function directive", done => { 123 | lint( 124 | { 125 | fixture: ["strict", "global-with-function-with"], 126 | eslint: eslintOpts, 127 | }, 128 | (err, report) => { 129 | if (err) return done(err); 130 | assert(report[0].ruleId === ruleId); 131 | 132 | // This is to make sure the test fails prior to adapting Babel AST 133 | // directive representation to ESLint format. Otherwise it reports an 134 | // error for missing global directive that masquerades as the expected 135 | // result of the previous assertion. 136 | assert(report[0].nodeType !== "Program"); 137 | done(); 138 | } 139 | ); 140 | }); 141 | // it 142 | 143 | it("should error on no directive", done => { 144 | lint( 145 | { 146 | fixture: ["strict", "none"], 147 | eslint: eslintOpts, 148 | }, 149 | (err, report) => { 150 | if (err) return done(err); 151 | assert(report[0].ruleId === ruleId); 152 | done(); 153 | } 154 | ); 155 | }); 156 | // it 157 | }); 158 | // describe 159 | 160 | describe("when set to 'function'", () => { 161 | const eslintOpts = Object.assign({}, baseEslintOpts, { 162 | rules: {}, 163 | }); 164 | eslintOpts.rules[ruleId] = [errorLevel, "function"]; 165 | 166 | it("shouldn't error on single function directive", done => { 167 | lint( 168 | { 169 | fixture: ["strict", "function-with"], 170 | eslint: eslintOpts, 171 | }, 172 | (err, report) => { 173 | if (err) return done(err); 174 | assert(!report.length); 175 | done(); 176 | } 177 | ); 178 | }); 179 | // it 180 | 181 | it("should error twice on function directive: no and global directive: yes", done => { 182 | lint( 183 | { 184 | fixture: ["strict", "global-with-function-without"], 185 | eslint: eslintOpts, 186 | }, 187 | (err, report) => { 188 | if (err) return done(err); 189 | [0, 1].forEach(i => { 190 | assert(report[i].ruleId === ruleId); 191 | }); 192 | done(); 193 | } 194 | ); 195 | }); 196 | // it 197 | 198 | it("should error on only global directive", done => { 199 | lint( 200 | { 201 | fixture: ["strict", "global-with"], 202 | eslint: eslintOpts, 203 | }, 204 | (err, report) => { 205 | if (err) return done(err); 206 | assert(report[0].ruleId === ruleId); 207 | done(); 208 | } 209 | ); 210 | }); 211 | // it 212 | 213 | it("should error on extraneous global directive", done => { 214 | lint( 215 | { 216 | fixture: ["strict", "global-with-function-with"], 217 | eslint: eslintOpts, 218 | }, 219 | (err, report) => { 220 | if (err) return done(err); 221 | assert(report[0].ruleId === ruleId); 222 | assert(report[0].nodeType.indexOf("Function") === -1); 223 | done(); 224 | } 225 | ); 226 | }); 227 | // it 228 | }); 229 | } 230 | 231 | describe("https://github.com/babel/babel-eslint/issues/558", () => { 232 | it("doesn't crash with eslint-plugin-import", () => { 233 | const engine = new eslint.CLIEngine({ ignore: false }); 234 | engine.executeOnFiles([ 235 | "fixtures/eslint-plugin-import/a.js", 236 | "fixtures/eslint-plugin-import/b.js", 237 | "fixtures/eslint-plugin-import/c.js", 238 | ]); 239 | }); 240 | }); 241 | -------------------------------------------------------------------------------- /test/specs/non-regression.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const eslint = require("eslint"); 4 | const path = require("path"); 5 | const unpad = require("dedent"); 6 | 7 | const parser = require("../.."); 8 | 9 | function verifyAndAssertMessagesWithSpecificESLint( 10 | code, 11 | rules, 12 | expectedMessages, 13 | sourceType, 14 | overrideConfig, 15 | linter 16 | ) { 17 | const config = { 18 | parser: "current-babel-eslint", 19 | rules, 20 | env: { 21 | node: true, 22 | es6: true, 23 | }, 24 | parserOptions: { 25 | sourceType, 26 | ecmaFeatures: { 27 | globalReturn: true, 28 | }, 29 | }, 30 | }; 31 | 32 | if (overrideConfig) { 33 | for (const key in overrideConfig) { 34 | config[key] = overrideConfig[key]; 35 | } 36 | } 37 | 38 | const messages = linter.verify(code, config); 39 | 40 | if (messages.length !== expectedMessages.length) { 41 | throw new Error( 42 | `Expected ${expectedMessages.length} message(s), got ${ 43 | messages.length 44 | }\n${JSON.stringify(messages, null, 2)}` 45 | ); 46 | } 47 | 48 | messages.forEach((message, i) => { 49 | const formatedMessage = `${message.line}:${message.column} ${ 50 | message.message 51 | }${message.ruleId ? ` ${message.ruleId}` : ""}`; 52 | if (formatedMessage !== expectedMessages[i]) { 53 | throw new Error( 54 | ` 55 | Message ${i} does not match: 56 | Expected: ${expectedMessages[i]} 57 | Actual: ${formatedMessage} 58 | ` 59 | ); 60 | } 61 | }); 62 | } 63 | 64 | function verifyAndAssertMessages( 65 | code, 66 | rules, 67 | expectedMessages, 68 | sourceType, 69 | overrideConfig 70 | ) { 71 | const linter = new eslint.Linter(); 72 | linter.defineParser("current-babel-eslint", parser); 73 | 74 | verifyAndAssertMessagesWithSpecificESLint( 75 | unpad(`${code}`), 76 | rules || {}, 77 | expectedMessages || [], 78 | sourceType, 79 | overrideConfig, 80 | linter 81 | ); 82 | } 83 | 84 | describe("verify", () => { 85 | it("arrow function support (issue #1)", () => { 86 | verifyAndAssertMessages("describe('stuff', () => {});"); 87 | }); 88 | 89 | it("EOL validation (issue #2)", () => { 90 | verifyAndAssertMessages( 91 | 'module.exports = "something";', 92 | { "eol-last": 1, semi: 1 }, 93 | ["1:30 Newline required at end of file but not found. eol-last"] 94 | ); 95 | }); 96 | 97 | xit("Readable error messages (issue #3)", () => { 98 | verifyAndAssertMessages("{ , res }", {}, [ 99 | "1:3 Parsing error: Unexpected token", 100 | ]); 101 | }); 102 | 103 | it("Modules support (issue #5)", () => { 104 | verifyAndAssertMessages( 105 | ` 106 | import Foo from 'foo'; 107 | export default Foo; 108 | export const c = 'c'; 109 | export class Store {} 110 | ` 111 | ); 112 | }); 113 | 114 | it("Rest parameters (issue #7)", () => { 115 | verifyAndAssertMessages("function foo(...args) { return args; }", { 116 | "no-undef": 1, 117 | }); 118 | }); 119 | 120 | it("Exported classes should be used (issue #8)", () => { 121 | verifyAndAssertMessages("class Foo {} module.exports = Foo;", { 122 | "no-unused-vars": 1, 123 | }); 124 | }); 125 | 126 | it("super keyword in class (issue #10)", () => { 127 | verifyAndAssertMessages("class Foo { constructor() { super() } }", { 128 | "no-undef": 1, 129 | }); 130 | }); 131 | 132 | it("Rest parameter in destructuring assignment (issue #11)", () => { 133 | verifyAndAssertMessages( 134 | "const [a, ...rest] = ['1', '2', '3']; module.exports = rest;", 135 | { "no-undef": 1 } 136 | ); 137 | }); 138 | 139 | it("JSX attribute names marked as variables (issue #12)", () => { 140 | verifyAndAssertMessages('module.exports =
', { 141 | "no-undef": 1, 142 | }); 143 | }); 144 | 145 | it("Multiple destructured assignment with compound properties (issue #16)", () => { 146 | verifyAndAssertMessages("module.exports = { ...a.a, ...a.b };", { 147 | "no-dupe-keys": 1, 148 | }); 149 | }); 150 | 151 | it("Arrow function with non-block bodies (issue #20)", () => { 152 | verifyAndAssertMessages( 153 | '"use strict"; () => 1', 154 | { strict: [1, "global"] }, 155 | [], 156 | "script" 157 | ); 158 | }); 159 | 160 | it("#242", () => { 161 | verifyAndAssertMessages('"use strict"; asdf;', { 162 | "no-irregular-whitespace": 1, 163 | }); 164 | }); 165 | 166 | it("await keyword (issue #22)", () => { 167 | verifyAndAssertMessages("async function foo() { await bar(); }", { 168 | "no-unused-expressions": 1, 169 | }); 170 | }); 171 | 172 | it("arrow functions (issue #27)", () => { 173 | verifyAndAssertMessages("[1, 2, 3].map(i => i * 2);", { 174 | "func-names": 1, 175 | "space-before-blocks": 1, 176 | }); 177 | }); 178 | 179 | it("comment with padded-blocks (issue #33)", () => { 180 | verifyAndAssertMessages( 181 | ` 182 | if (a) { 183 | // i'm a comment! 184 | let b = c 185 | } 186 | `, 187 | { "padded-blocks": [1, "never"] } 188 | ); 189 | }); 190 | 191 | describe("flow", () => { 192 | it("check regular function", () => { 193 | verifyAndAssertMessages( 194 | "function a(b, c) { b += 1; c += 1; return b + c; } a;", 195 | { "no-unused-vars": 1, "no-undef": 1 } 196 | ); 197 | }); 198 | 199 | it("type alias", () => { 200 | verifyAndAssertMessages("type SomeNewType = any;", { "no-undef": 1 }); 201 | }); 202 | 203 | it("type cast expression #102", () => { 204 | verifyAndAssertMessages("for (let a of (a: Array)) {}"); 205 | }); 206 | 207 | it("multiple nullable type annotations and return #108", () => { 208 | verifyAndAssertMessages( 209 | ` 210 | import type Foo from 'foo'; 211 | import type Foo2 from 'foo'; 212 | import type Foo3 from 'foo'; 213 | function log(foo: ?Foo, foo2: ?Foo2): ?Foo3 { 214 | console.log(foo, foo2); 215 | } 216 | log(1, 2); 217 | `, 218 | { "no-unused-vars": 1, "no-undef": 1 } 219 | ); 220 | }); 221 | 222 | it("interface declaration", () => { 223 | verifyAndAssertMessages( 224 | ` 225 | interface Foo {}; 226 | interface Bar { 227 | foo: Foo, 228 | }; 229 | `, 230 | { "no-unused-vars": 1, "no-undef": 1 }, 231 | ["2:11 'Bar' is defined but never used. no-unused-vars"] 232 | ); 233 | }); 234 | 235 | it("type parameter bounds (classes)", () => { 236 | verifyAndAssertMessages( 237 | ` 238 | import type {Foo, Foo2} from 'foo'; 239 | import Base from 'base'; 240 | class Log extends Base { 241 | messages: {[T1]: T2}; 242 | } 243 | new Log(); 244 | `, 245 | { "no-unused-vars": 1, "no-undef": 1 }, 246 | ["3:34 'T4' is defined but never used. no-unused-vars"] 247 | ); 248 | }); 249 | 250 | it("type parameter scope (classes)", () => { 251 | verifyAndAssertMessages( 252 | ` 253 | T; 254 | class Foo {} 255 | T; 256 | new Foo(); 257 | `, 258 | { "no-unused-vars": 1, "no-undef": 1 }, 259 | [ 260 | "1:1 'T' is not defined. no-undef", 261 | "2:11 'T' is defined but never used. no-unused-vars", 262 | "3:1 'T' is not defined. no-undef", 263 | ] 264 | ); 265 | }); 266 | 267 | it("type parameter bounds (interfaces)", () => { 268 | verifyAndAssertMessages( 269 | ` 270 | import type {Foo, Foo2, Bar} from ''; 271 | interface Log extends Bar { 272 | messages: {[T1]: T2}; 273 | } 274 | `, 275 | { "no-unused-vars": 1, "no-undef": 1 }, 276 | [ 277 | "2:11 'Log' is defined but never used. no-unused-vars", 278 | "2:38 'T4' is defined but never used. no-unused-vars", 279 | ] 280 | ); 281 | }); 282 | 283 | it("type parameter scope (interfaces)", () => { 284 | verifyAndAssertMessages( 285 | ` 286 | T; 287 | interface Foo {}; 288 | T; 289 | Foo; 290 | `, 291 | { "no-unused-vars": 1, "no-undef": 1 }, 292 | [ 293 | "1:1 'T' is not defined. no-undef", 294 | "2:15 'T' is defined but never used. no-unused-vars", 295 | "3:1 'T' is not defined. no-undef", 296 | ] 297 | ); 298 | }); 299 | 300 | it("type parameter bounds (type aliases)", () => { 301 | verifyAndAssertMessages( 302 | ` 303 | import type {Foo, Foo2, Foo3} from 'foo'; 304 | type Log = { 305 | messages: {[T1]: T2}; 306 | delay: Foo3; 307 | }; 308 | `, 309 | { "no-unused-vars": 1, "no-undef": 1 }, 310 | [ 311 | "2:6 'Log' is defined but never used. no-unused-vars", 312 | "2:29 'T3' is defined but never used. no-unused-vars", 313 | ] 314 | ); 315 | }); 316 | 317 | it("type parameter scope (type aliases)", () => { 318 | verifyAndAssertMessages( 319 | ` 320 | T; 321 | type Foo = {}; 322 | T; 323 | Foo; 324 | `, 325 | { "no-unused-vars": 1, "no-undef": 1 }, 326 | [ 327 | "1:1 'T' is not defined. no-undef", 328 | "2:10 'T' is defined but never used. no-unused-vars", 329 | "3:1 'T' is not defined. no-undef", 330 | ] 331 | ); 332 | }); 333 | 334 | it("type parameter bounds (functions)", () => { 335 | verifyAndAssertMessages( 336 | ` 337 | import type Foo from 'foo'; 338 | import type Foo2 from 'foo'; 339 | function log(a: T1, b: T2): T3 { return a + b; } 340 | log(1, 2); 341 | `, 342 | { "no-unused-vars": 1, "no-undef": 1 }, 343 | ["3:37 'T4' is defined but never used. no-unused-vars"] 344 | ); 345 | }); 346 | 347 | it("type parameter scope (functions)", () => { 348 | verifyAndAssertMessages( 349 | ` 350 | T; 351 | function log() {} 352 | T; 353 | log; 354 | `, 355 | { "no-unused-vars": 1, "no-undef": 1 }, 356 | [ 357 | "1:1 'T' is not defined. no-undef", 358 | "2:14 'T' is defined but never used. no-unused-vars", 359 | "3:1 'T' is not defined. no-undef", 360 | ] 361 | ); 362 | }); 363 | 364 | it("nested type annotations", () => { 365 | verifyAndAssertMessages( 366 | ` 367 | import type Foo from 'foo'; 368 | function foo(callback: () => Foo) { 369 | return callback(); 370 | } 371 | foo(); 372 | `, 373 | { "no-unused-vars": 1, "no-undef": 1 } 374 | ); 375 | }); 376 | 377 | it("type in var declaration", () => { 378 | verifyAndAssertMessages( 379 | ` 380 | import type Foo from 'foo'; 381 | var x: Foo = 1; 382 | x; 383 | `, 384 | { "no-unused-vars": 1, "no-undef": 1 } 385 | ); 386 | }); 387 | 388 | it("object type annotation", () => { 389 | verifyAndAssertMessages( 390 | ` 391 | import type Foo from 'foo'; 392 | var a: {numVal: Foo}; 393 | a; 394 | `, 395 | { "no-unused-vars": 1, "no-undef": 1 } 396 | ); 397 | }); 398 | 399 | it("object property types", () => { 400 | verifyAndAssertMessages( 401 | ` 402 | import type Foo from 'foo'; 403 | import type Foo2 from 'foo'; 404 | var a = { 405 | circle: (null : ?{ setNativeProps(props: Foo): Foo2 }) 406 | }; 407 | a; 408 | `, 409 | { "no-unused-vars": 1, "no-undef": 1 } 410 | ); 411 | }); 412 | 413 | it("namespaced types", () => { 414 | verifyAndAssertMessages( 415 | ` 416 | var React = require('react-native'); 417 | var b = { 418 | openExternalExample: (null: ?React.Component) 419 | }; 420 | var c = { 421 | render(): React.Component {} 422 | }; 423 | b; 424 | c; 425 | `, 426 | { "no-unused-vars": 1, "no-undef": 1 } 427 | ); 428 | }); 429 | 430 | it("ArrayTypeAnnotation", () => { 431 | verifyAndAssertMessages( 432 | ` 433 | import type Foo from 'foo'; 434 | var x: Foo[]; x; 435 | `, 436 | { "no-unused-vars": 1, "no-undef": 1 } 437 | ); 438 | }); 439 | 440 | it("ClassImplements", () => { 441 | verifyAndAssertMessages( 442 | ` 443 | import type Bar from 'foo'; 444 | export default class Foo implements Bar {} 445 | `, 446 | { "no-unused-vars": 1, "no-undef": 1 } 447 | ); 448 | }); 449 | 450 | it("type alias creates declaration + usage", () => { 451 | verifyAndAssertMessages( 452 | ` 453 | type Foo = any; 454 | var x : Foo = 1; x; 455 | `, 456 | { "no-unused-vars": 1, "no-undef": 1 } 457 | ); 458 | }); 459 | 460 | it("type alias with type parameters", () => { 461 | verifyAndAssertMessages( 462 | ` 463 | import type Bar from 'foo'; 464 | import type Foo3 from 'foo'; 465 | type Foo = Bar 466 | var x : Foo = 1; x; 467 | `, 468 | { "no-unused-vars": 1, "no-undef": 1 } 469 | ); 470 | }); 471 | 472 | it("export type alias", () => { 473 | verifyAndAssertMessages( 474 | ` 475 | import type Foo2 from 'foo'; 476 | export type Foo = Foo2; 477 | `, 478 | { "no-unused-vars": 1, "no-undef": 1 } 479 | ); 480 | }); 481 | 482 | it("polymorphic types #109", () => { 483 | verifyAndAssertMessages( 484 | "export default function groupByEveryN(array: Array, n: number): Array> { n; }", 485 | { "no-unused-vars": 1, "no-undef": 1 } 486 | ); 487 | }); 488 | 489 | it("types definition from import", () => { 490 | verifyAndAssertMessages( 491 | ` 492 | import type Promise from 'bluebird'; 493 | type Operation = () => Promise; 494 | x: Operation; 495 | `, 496 | { "no-unused-vars": 1, "no-undef": 1 } 497 | ); 498 | }); 499 | 500 | it("polymorphic/generic types for class #123", () => { 501 | verifyAndAssertMessages( 502 | ` 503 | class Box { 504 | value: T; 505 | } 506 | var box = new Box(); 507 | console.log(box.value); 508 | `, 509 | { "no-unused-vars": 1, "no-undef": 1 } 510 | ); 511 | }); 512 | 513 | it("polymorphic/generic types for function #123", () => { 514 | verifyAndAssertMessages( 515 | ` 516 | export function identity(value) { 517 | var a: T = value; a; 518 | } 519 | `, 520 | { "no-unused-vars": 1, "no-undef": 1 } 521 | ); 522 | }); 523 | 524 | it("polymorphic/generic types for type alias #123", () => { 525 | verifyAndAssertMessages( 526 | ` 527 | import Bar from './Bar'; 528 | type Foo = Bar; var x: Foo = 1; console.log(x); 529 | `, 530 | { "no-unused-vars": 1, "no-undef": 1 } 531 | ); 532 | }); 533 | 534 | it("polymorphic/generic types - outside of fn scope #123", () => { 535 | verifyAndAssertMessages( 536 | ` 537 | export function foo(value) { value; }; 538 | var b: T = 1; b; 539 | `, 540 | { "no-unused-vars": 1, "no-undef": 1 }, 541 | [ 542 | "1:21 'T' is defined but never used. no-unused-vars", 543 | "2:8 'T' is not defined. no-undef", 544 | ] 545 | ); 546 | }); 547 | 548 | it("polymorphic/generic types - extending unknown #123", () => { 549 | verifyAndAssertMessages( 550 | ` 551 | import Bar from 'bar'; 552 | export class Foo extends Bar {} 553 | `, 554 | { "no-unused-vars": 1, "no-undef": 1 }, 555 | ["2:30 'T' is not defined. no-undef"] 556 | ); 557 | }); 558 | 559 | it("polymorphic/generic types - function calls", () => { 560 | verifyAndAssertMessages( 561 | ` 562 | function f(): T {} 563 | f(); 564 | `, 565 | { "no-unused-vars": 1, "no-undef": 1 }, 566 | ["2:3 'T' is not defined. no-undef"] 567 | ); 568 | }); 569 | 570 | it("polymorphic/generic types - function calls #644", () => { 571 | verifyAndAssertMessages( 572 | ` 573 | import type {Type} from 'Type'; 574 | function f(): T {} 575 | f(); 576 | `, 577 | { "no-unused-vars": 1, "no-undef": 1 } 578 | ); 579 | }); 580 | 581 | it("support declarations #132", () => { 582 | verifyAndAssertMessages( 583 | ` 584 | declare class A { static () : number } 585 | declare module B { declare var x: number; } 586 | declare function foo(): void; 587 | declare var bar 588 | A; B; foo(); bar; 589 | `, 590 | { "no-undef": 1, "no-unused-vars": 1 } 591 | ); 592 | }); 593 | 594 | it("supports type spreading", () => { 595 | verifyAndAssertMessages( 596 | ` 597 | type U = {}; 598 | type T = {a: number, ...U, ...V}; 599 | `, 600 | { "no-undef": 1, "no-unused-vars": 1 }, 601 | [ 602 | "2:6 'T' is defined but never used. no-unused-vars", 603 | "2:31 'V' is not defined. no-undef", 604 | ] 605 | ); 606 | }); 607 | 608 | it("1", () => { 609 | verifyAndAssertMessages( 610 | ` 611 | import type Foo from 'foo'; 612 | import type Foo2 from 'foo'; 613 | export default function(a: Foo, b: ?Foo2, c){ a; b; c; } 614 | `, 615 | { "no-unused-vars": 1, "no-undef": 1 } 616 | ); 617 | }); 618 | 619 | it("2", () => { 620 | verifyAndAssertMessages( 621 | ` 622 | import type Foo from 'foo'; 623 | export default function(a: () => Foo){ a; } 624 | `, 625 | { "no-unused-vars": 1, "no-undef": 1 } 626 | ); 627 | }); 628 | 629 | it("3", () => { 630 | verifyAndAssertMessages( 631 | ` 632 | import type Foo from 'foo'; 633 | import type Foo2 from 'foo'; 634 | export default function(a: (_:Foo) => Foo2){ a; } 635 | `, 636 | { "no-unused-vars": 1, "no-undef": 1 } 637 | ); 638 | }); 639 | 640 | it("4", () => { 641 | verifyAndAssertMessages( 642 | ` 643 | import type Foo from 'foo'; 644 | import type Foo2 from 'foo'; 645 | import type Foo3 from 'foo'; 646 | export default function(a: (_1:Foo, _2:Foo2) => Foo3){ a; } 647 | `, 648 | { "no-unused-vars": 1, "no-undef": 1 } 649 | ); 650 | }); 651 | 652 | it("5", () => { 653 | verifyAndAssertMessages( 654 | ` 655 | import type Foo from 'foo'; 656 | import type Foo2 from 'foo'; 657 | export default function(a: (_1:Foo, ...foo:Array) => number){ a; } 658 | `, 659 | { "no-unused-vars": 1, "no-undef": 1 } 660 | ); 661 | }); 662 | 663 | it("6", () => { 664 | verifyAndAssertMessages( 665 | ` 666 | import type Foo from 'foo'; 667 | export default function(): Foo {} 668 | `, 669 | { "no-unused-vars": 1, "no-undef": 1 } 670 | ); 671 | }); 672 | 673 | it("7", () => { 674 | verifyAndAssertMessages( 675 | ` 676 | import type Foo from 'foo'; 677 | export default function():() => Foo {} 678 | `, 679 | { "no-unused-vars": 1, "no-undef": 1 } 680 | ); 681 | }); 682 | 683 | it("8", () => { 684 | verifyAndAssertMessages( 685 | ` 686 | import type Foo from 'foo'; 687 | import type Foo2 from 'foo'; 688 | export default function():(_?:Foo) => Foo2{} 689 | `, 690 | { "no-unused-vars": 1, "no-undef": 1 } 691 | ); 692 | }); 693 | 694 | it("9", () => { 695 | verifyAndAssertMessages( 696 | "export default function (a: T1, b: T2) { b; }", 697 | { "no-unused-vars": 1, "no-undef": 1 } 698 | ); 699 | }); 700 | 701 | it("10", () => { 702 | verifyAndAssertMessages( 703 | "var a=function(a: T1, b: T2) {return a + b;}; a;", 704 | { "no-unused-vars": 1, "no-undef": 1 } 705 | ); 706 | }); 707 | 708 | it("11", () => { 709 | verifyAndAssertMessages("var a={*id(x: T): T { x; }}; a;", { 710 | "no-unused-vars": 1, 711 | "no-undef": 1, 712 | }); 713 | }); 714 | 715 | it("12", () => { 716 | verifyAndAssertMessages("var a={async id(x: T): T { x; }}; a;", { 717 | "no-unused-vars": 1, 718 | "no-undef": 1, 719 | }); 720 | }); 721 | 722 | it("13", () => { 723 | verifyAndAssertMessages("var a={123(x: T): T { x; }}; a;", { 724 | "no-unused-vars": 1, 725 | "no-undef": 1, 726 | }); 727 | }); 728 | 729 | it("14", () => { 730 | verifyAndAssertMessages( 731 | ` 732 | import type Foo from 'foo'; 733 | import type Foo2 from 'foo'; 734 | export default class Bar {set fooProp(value:Foo):Foo2{ value; }} 735 | `, 736 | { "no-unused-vars": 1, "no-undef": 1 } 737 | ); 738 | }); 739 | 740 | it("15", () => { 741 | verifyAndAssertMessages( 742 | ` 743 | import type Foo2 from 'foo'; 744 | export default class Foo {get fooProp(): Foo2{}} 745 | `, 746 | { "no-unused-vars": 1, "no-undef": 1 } 747 | ); 748 | }); 749 | 750 | it("16", () => { 751 | verifyAndAssertMessages( 752 | ` 753 | import type Foo from 'foo'; 754 | var numVal:Foo; numVal; 755 | `, 756 | { "no-unused-vars": 1, "no-undef": 1 } 757 | ); 758 | }); 759 | 760 | it("17", () => { 761 | verifyAndAssertMessages( 762 | ` 763 | import type Foo from 'foo'; 764 | var a: {numVal: Foo;}; a; 765 | `, 766 | { "no-unused-vars": 1, "no-undef": 1 } 767 | ); 768 | }); 769 | 770 | it("18", () => { 771 | verifyAndAssertMessages( 772 | ` 773 | import type Foo from 'foo'; 774 | import type Foo2 from 'foo'; 775 | import type Foo3 from 'foo'; 776 | var a: ?{numVal: Foo; [indexer: Foo2]: Foo3}; a; 777 | `, 778 | { "no-unused-vars": 1, "no-undef": 1 } 779 | ); 780 | }); 781 | 782 | it("19", () => { 783 | verifyAndAssertMessages( 784 | ` 785 | import type Foo from 'foo'; 786 | import type Foo2 from 'foo'; 787 | var a: {numVal: Foo; subObj?: ?{strVal: Foo2}}; a; 788 | `, 789 | { "no-unused-vars": 1, "no-undef": 1 } 790 | ); 791 | }); 792 | 793 | it("20", () => { 794 | verifyAndAssertMessages( 795 | ` 796 | import type Foo from 'foo'; 797 | import type Foo2 from 'foo'; 798 | import type Foo3 from 'foo'; 799 | import type Foo4 from 'foo'; 800 | var a: { [a: Foo]: Foo2; [b: Foo3]: Foo4; }; a; 801 | `, 802 | { "no-unused-vars": 1, "no-undef": 1 } 803 | ); 804 | }); 805 | 806 | it("21", () => { 807 | verifyAndAssertMessages( 808 | ` 809 | import type Foo from 'foo'; 810 | import type Foo2 from 'foo'; 811 | import type Foo3 from 'foo'; 812 | var a: {add(x:Foo, ...y:Array): Foo3}; a; 813 | `, 814 | { "no-unused-vars": 1, "no-undef": 1 } 815 | ); 816 | }); 817 | 818 | it("22", () => { 819 | verifyAndAssertMessages( 820 | ` 821 | import type Foo from 'foo'; 822 | import type Foo2 from 'foo'; 823 | import type Foo3 from 'foo'; 824 | var a: { id(x: Foo2): Foo3; }; a; 825 | `, 826 | { "no-unused-vars": 1, "no-undef": 1 } 827 | ); 828 | }); 829 | 830 | it("23", () => { 831 | verifyAndAssertMessages( 832 | ` 833 | import type Foo from 'foo'; 834 | var a:Array = [1, 2, 3]; a; 835 | `, 836 | { "no-unused-vars": 1, "no-undef": 1 } 837 | ); 838 | }); 839 | 840 | it("24", () => { 841 | verifyAndAssertMessages( 842 | ` 843 | import type Baz from 'baz'; 844 | export default class Bar extends Baz { }; 845 | `, 846 | { "no-unused-vars": 1, "no-undef": 1 } 847 | ); 848 | }); 849 | 850 | it("25", () => { 851 | verifyAndAssertMessages( 852 | "export default class Bar { bar(): T { return 42; }}", 853 | { "no-unused-vars": 1, "no-undef": 1 } 854 | ); 855 | }); 856 | 857 | it("26", () => { 858 | verifyAndAssertMessages( 859 | ` 860 | import type Foo from 'foo'; 861 | import type Foo2 from 'foo'; 862 | export default class Bar { static prop1:Foo; prop2:Foo2; } 863 | `, 864 | { "no-unused-vars": 1, "no-undef": 1 } 865 | ); 866 | }); 867 | 868 | it("27", () => { 869 | verifyAndAssertMessages( 870 | ` 871 | import type Foo from 'foo'; 872 | import type Foo2 from 'foo'; 873 | var x : Foo | Foo2 = 4; x; 874 | `, 875 | { "no-unused-vars": 1, "no-undef": 1 } 876 | ); 877 | }); 878 | 879 | it("28", () => { 880 | verifyAndAssertMessages( 881 | ` 882 | import type Foo from 'foo'; 883 | import type Foo2 from 'foo'; 884 | var x : () => Foo | () => Foo2; x; 885 | `, 886 | { "no-unused-vars": 1, "no-undef": 1 } 887 | ); 888 | }); 889 | 890 | it("29", () => { 891 | verifyAndAssertMessages( 892 | ` 893 | import type Foo from 'foo'; 894 | import type Foo2 from 'foo'; 895 | var x: typeof Foo | number = Foo2; x; 896 | `, 897 | { "no-unused-vars": 1, "no-undef": 1 } 898 | ); 899 | }); 900 | 901 | it("30", () => { 902 | verifyAndAssertMessages( 903 | ` 904 | import type Foo from 'foo'; 905 | var {x}: {x: Foo; } = { x: 'hello' }; x; 906 | `, 907 | { "no-unused-vars": 1, "no-undef": 1 } 908 | ); 909 | }); 910 | 911 | it("31", () => { 912 | verifyAndAssertMessages( 913 | ` 914 | import type Foo from 'foo'; 915 | var [x]: Array = [ 'hello' ]; x; 916 | `, 917 | { "no-unused-vars": 1, "no-undef": 1 } 918 | ); 919 | }); 920 | 921 | it("32", () => { 922 | verifyAndAssertMessages( 923 | ` 924 | import type Foo from 'foo'; 925 | export default function({x}: { x: Foo; }) { x; } 926 | `, 927 | { "no-unused-vars": 1, "no-undef": 1 } 928 | ); 929 | }); 930 | 931 | it("33", () => { 932 | verifyAndAssertMessages( 933 | ` 934 | import type Foo from 'foo'; 935 | function foo([x]: Array) { x; } foo(); 936 | `, 937 | { "no-unused-vars": 1, "no-undef": 1 } 938 | ); 939 | }); 940 | 941 | it("34", () => { 942 | verifyAndAssertMessages( 943 | ` 944 | import type Foo from 'foo'; 945 | import type Foo2 from 'foo'; 946 | var a: Map >; a; 947 | `, 948 | { "no-unused-vars": 1, "no-undef": 1 } 949 | ); 950 | }); 951 | 952 | it("35", () => { 953 | verifyAndAssertMessages( 954 | ` 955 | import type Foo from 'foo'; 956 | var a: ?Promise[]; a; 957 | `, 958 | { "no-unused-vars": 1, "no-undef": 1 } 959 | ); 960 | }); 961 | 962 | it("36", () => { 963 | verifyAndAssertMessages( 964 | ` 965 | import type Foo from 'foo'; 966 | import type Foo2 from 'foo'; 967 | var a:(...rest:Array) => Foo2; a; 968 | `, 969 | { "no-unused-vars": 1, "no-undef": 1 } 970 | ); 971 | }); 972 | 973 | it("37", () => { 974 | verifyAndAssertMessages( 975 | ` 976 | import type Foo from 'foo'; 977 | import type Foo2 from 'foo'; 978 | import type Foo3 from 'foo'; 979 | import type Foo4 from 'foo'; 980 | var a: (x: Foo2, ...y:Foo3[]) => Foo4; a; 981 | `, 982 | { "no-unused-vars": 1, "no-undef": 1 } 983 | ); 984 | }); 985 | 986 | it("38", () => { 987 | verifyAndAssertMessages( 988 | ` 989 | import type {foo, bar} from 'baz'; 990 | foo; bar; 991 | `, 992 | { "no-unused-vars": 1, "no-undef": 1 } 993 | ); 994 | }); 995 | 996 | it("39", () => { 997 | verifyAndAssertMessages( 998 | ` 999 | import type {foo as bar} from 'baz'; 1000 | bar; 1001 | `, 1002 | { "no-unused-vars": 1, "no-undef": 1 } 1003 | ); 1004 | }); 1005 | 1006 | it("40", () => { 1007 | verifyAndAssertMessages( 1008 | ` 1009 | import type from 'foo'; 1010 | type; 1011 | `, 1012 | { "no-unused-vars": 1, "no-undef": 1 } 1013 | ); 1014 | }); 1015 | 1016 | it("41", () => { 1017 | verifyAndAssertMessages( 1018 | ` 1019 | import type, {foo} from 'bar'; 1020 | type; foo; 1021 | `, 1022 | { "no-unused-vars": 1, "no-undef": 1 } 1023 | ); 1024 | }); 1025 | 1026 | it("43", () => { 1027 | verifyAndAssertMessages( 1028 | ` 1029 | import type Foo from 'foo'; 1030 | var a: Foo[]; a; 1031 | `, 1032 | { "no-unused-vars": 1, "no-undef": 1 } 1033 | ); 1034 | }); 1035 | 1036 | it("44", () => { 1037 | verifyAndAssertMessages( 1038 | ` 1039 | import type Foo from 'foo'; 1040 | var a: ?Foo[]; a; 1041 | `, 1042 | { "no-unused-vars": 1, "no-undef": 1 } 1043 | ); 1044 | }); 1045 | 1046 | it("45", () => { 1047 | verifyAndAssertMessages( 1048 | ` 1049 | import type Foo from 'foo'; 1050 | var a: (?Foo)[]; a; 1051 | `, 1052 | { "no-unused-vars": 1, "no-undef": 1 } 1053 | ); 1054 | }); 1055 | 1056 | it("46", () => { 1057 | verifyAndAssertMessages( 1058 | ` 1059 | import type Foo from 'foo'; 1060 | var a: () => Foo[]; a; 1061 | `, 1062 | { "no-unused-vars": 1, "no-undef": 1 } 1063 | ); 1064 | }); 1065 | 1066 | it("47", () => { 1067 | verifyAndAssertMessages( 1068 | ` 1069 | import type Foo from 'foo'; 1070 | var a: (() => Foo)[]; a; 1071 | `, 1072 | { "no-unused-vars": 1, "no-undef": 1 } 1073 | ); 1074 | }); 1075 | 1076 | it("48", () => { 1077 | verifyAndAssertMessages( 1078 | ` 1079 | import type Foo from 'foo'; 1080 | var a: typeof Foo[]; a; 1081 | `, 1082 | { "no-unused-vars": 1, "no-undef": 1 } 1083 | ); 1084 | }); 1085 | 1086 | it("49", () => { 1087 | verifyAndAssertMessages( 1088 | ` 1089 | import type Foo from 'foo'; 1090 | import type Foo2 from 'foo'; 1091 | import type Foo3 from 'foo'; 1092 | var a : [Foo, Foo2,] = [123, 'duck',]; a; 1093 | `, 1094 | { "no-unused-vars": 1, "no-undef": 1 } 1095 | ); 1096 | }); 1097 | }); 1098 | 1099 | it("class usage", () => { 1100 | verifyAndAssertMessages("class Lol {} module.exports = Lol;", { 1101 | "no-unused-vars": 1, 1102 | }); 1103 | }); 1104 | 1105 | it("class definition: gaearon/redux#24", () => { 1106 | verifyAndAssertMessages( 1107 | ` 1108 | export default function root(stores) { 1109 | return DecoratedComponent => class ReduxRootDecorator { 1110 | a() { DecoratedComponent; stores; } 1111 | }; 1112 | } 1113 | `, 1114 | { "no-undef": 1, "no-unused-vars": 1 } 1115 | ); 1116 | }); 1117 | 1118 | it("class properties #71", () => { 1119 | verifyAndAssertMessages("class Lol { foo = 'bar'; }", { "no-undef": 1 }); 1120 | }); 1121 | 1122 | it("template strings #31", () => { 1123 | verifyAndAssertMessages("console.log(`${a}, b`);", { "comma-spacing": 1 }); 1124 | }); 1125 | 1126 | it("template with destructuring #31", () => { 1127 | verifyAndAssertMessages( 1128 | ` 1129 | module.exports = { 1130 | render() { 1131 | var {name} = this.props; 1132 | return Math.max(null, \`Name: \${name}, Name: \${name}\`); 1133 | } 1134 | }; 1135 | `, 1136 | { "comma-spacing": 1 } 1137 | ); 1138 | }); 1139 | 1140 | it("template with arrow returning template #603", () => { 1141 | verifyAndAssertMessages( 1142 | ` 1143 | var a = \`\${() => { 1144 | \`\${''}\` 1145 | }}\`; 1146 | `, 1147 | { indent: 1 }, 1148 | [] 1149 | ); 1150 | }); 1151 | 1152 | describe("decorators #72 (legacy)", () => { 1153 | function verifyDecoratorsLegacyAndAssertMessages( 1154 | code, 1155 | rules, 1156 | expectedMessages, 1157 | sourceType 1158 | ) { 1159 | const overrideConfig = { 1160 | parserOptions: { 1161 | sourceType, 1162 | babelOptions: { 1163 | configFile: path.resolve( 1164 | __dirname, 1165 | "../fixtures/config/babel.config.decorators-legacy.js" 1166 | ), 1167 | }, 1168 | }, 1169 | }; 1170 | return verifyAndAssertMessages( 1171 | code, 1172 | rules, 1173 | expectedMessages, 1174 | sourceType, 1175 | overrideConfig 1176 | ); 1177 | } 1178 | 1179 | it("class declaration", () => { 1180 | verifyDecoratorsLegacyAndAssertMessages( 1181 | ` 1182 | import classDeclaration from 'decorator'; 1183 | import decoratorParameter from 'decorator'; 1184 | @classDeclaration((parameter) => parameter) 1185 | @classDeclaration(decoratorParameter) 1186 | @classDeclaration 1187 | export class TextareaAutosize {} 1188 | `, 1189 | { "no-unused-vars": 1 } 1190 | ); 1191 | }); 1192 | 1193 | it("method definition", () => { 1194 | verifyDecoratorsLegacyAndAssertMessages( 1195 | ` 1196 | import classMethodDeclarationA from 'decorator'; 1197 | import decoratorParameter from 'decorator'; 1198 | export class TextareaAutosize { 1199 | @classMethodDeclarationA((parameter) => parameter) 1200 | @classMethodDeclarationA(decoratorParameter) 1201 | @classMethodDeclarationA 1202 | methodDeclaration(e) { 1203 | e(); 1204 | } 1205 | } 1206 | `, 1207 | { "no-unused-vars": 1 } 1208 | ); 1209 | }); 1210 | 1211 | it("method definition get/set", () => { 1212 | verifyDecoratorsLegacyAndAssertMessages( 1213 | ` 1214 | import classMethodDeclarationA from 'decorator'; 1215 | import decoratorParameter from 'decorator'; 1216 | export class TextareaAutosize { 1217 | @classMethodDeclarationA((parameter) => parameter) 1218 | @classMethodDeclarationA(decoratorParameter) 1219 | @classMethodDeclarationA 1220 | get bar() { } 1221 | @classMethodDeclarationA((parameter) => parameter) 1222 | @classMethodDeclarationA(decoratorParameter) 1223 | @classMethodDeclarationA 1224 | set bar(val) { val; } 1225 | } 1226 | `, 1227 | { "no-unused-vars": 1 } 1228 | ); 1229 | }); 1230 | 1231 | it("object property", () => { 1232 | verifyDecoratorsLegacyAndAssertMessages( 1233 | ` 1234 | import classMethodDeclarationA from 'decorator'; 1235 | import decoratorParameter from 'decorator'; 1236 | var obj = { 1237 | @classMethodDeclarationA((parameter) => parameter) 1238 | @classMethodDeclarationA(decoratorParameter) 1239 | @classMethodDeclarationA 1240 | methodDeclaration(e) { 1241 | e(); 1242 | } 1243 | }; 1244 | obj; 1245 | `, 1246 | { "no-unused-vars": 1 } 1247 | ); 1248 | }); 1249 | 1250 | it("object property get/set", () => { 1251 | verifyDecoratorsLegacyAndAssertMessages( 1252 | ` 1253 | import classMethodDeclarationA from 'decorator'; 1254 | import decoratorParameter from 'decorator'; 1255 | var obj = { 1256 | @classMethodDeclarationA((parameter) => parameter) 1257 | @classMethodDeclarationA(decoratorParameter) 1258 | @classMethodDeclarationA 1259 | get bar() { }, 1260 | @classMethodDeclarationA((parameter) => parameter) 1261 | @classMethodDeclarationA(decoratorParameter) 1262 | @classMethodDeclarationA 1263 | set bar(val) { val; } 1264 | }; 1265 | obj; 1266 | `, 1267 | { "no-unused-vars": 1 } 1268 | ); 1269 | }); 1270 | }); 1271 | 1272 | describe("decorators #72", () => { 1273 | it("class declaration", () => { 1274 | verifyAndAssertMessages( 1275 | ` 1276 | import classDeclaration from 'decorator'; 1277 | import decoratorParameter from 'decorator'; 1278 | export 1279 | @classDeclaration((parameter) => parameter) 1280 | @classDeclaration(decoratorParameter) 1281 | @classDeclaration 1282 | class TextareaAutosize {} 1283 | `, 1284 | { "no-unused-vars": 1 } 1285 | ); 1286 | }); 1287 | 1288 | it("method definition", () => { 1289 | verifyAndAssertMessages( 1290 | ` 1291 | import classMethodDeclarationA from 'decorator'; 1292 | import decoratorParameter from 'decorator'; 1293 | export class TextareaAutosize { 1294 | @classMethodDeclarationA((parameter) => parameter) 1295 | @classMethodDeclarationA(decoratorParameter) 1296 | @classMethodDeclarationA 1297 | methodDeclaration(e) { 1298 | e(); 1299 | } 1300 | } 1301 | `, 1302 | { "no-unused-vars": 1 } 1303 | ); 1304 | }); 1305 | 1306 | it("method definition get/set", () => { 1307 | verifyAndAssertMessages( 1308 | ` 1309 | import classMethodDeclarationA from 'decorator'; 1310 | import decoratorParameter from 'decorator'; 1311 | export class TextareaAutosize { 1312 | @classMethodDeclarationA((parameter) => parameter) 1313 | @classMethodDeclarationA(decoratorParameter) 1314 | @classMethodDeclarationA 1315 | get bar() { } 1316 | @classMethodDeclarationA((parameter) => parameter) 1317 | @classMethodDeclarationA(decoratorParameter) 1318 | @classMethodDeclarationA 1319 | set bar(val) { val; } 1320 | } 1321 | `, 1322 | { "no-unused-vars": 1 } 1323 | ); 1324 | }); 1325 | }); 1326 | 1327 | it("detects minimal no-unused-vars case #120", () => { 1328 | verifyAndAssertMessages("var unused;", { "no-unused-vars": 1 }, [ 1329 | "1:5 'unused' is defined but never used. no-unused-vars", 1330 | ]); 1331 | }); 1332 | 1333 | // This two tests are disabled, as the feature to visit properties when 1334 | // there is a spread/rest operator has been removed as it caused problems 1335 | // with other rules #249 1336 | it.skip("visits excluded properties left of spread #95", () => { 1337 | verifyAndAssertMessages( 1338 | "var originalObject = {}; var {field1, field2, ...clone} = originalObject;", 1339 | { "no-unused-vars": 1 } 1340 | ); 1341 | }); 1342 | 1343 | it.skip("visits excluded properties left of spread #210", () => { 1344 | verifyAndAssertMessages( 1345 | "const props = { yo: 'yo' }; const { ...otherProps } = props;", 1346 | { "no-unused-vars": 1 } 1347 | ); 1348 | }); 1349 | 1350 | it("does not mark spread variables false-positive", () => { 1351 | verifyAndAssertMessages( 1352 | "var originalObject = {}; var {field1, field2, ...clone} = originalObject;", 1353 | { "no-undef": 1, "no-redeclare": 1 } 1354 | ); 1355 | }); 1356 | 1357 | it("does not mark spread variables false-positive", () => { 1358 | verifyAndAssertMessages( 1359 | "const props = { yo: 'yo' }; const { ...otherProps } = props;", 1360 | { "no-undef": 1, "no-redeclare": 1 } 1361 | ); 1362 | }); 1363 | 1364 | it("does not mark spread variables as use-before-define #249", () => { 1365 | verifyAndAssertMessages( 1366 | "var originalObject = {}; var {field1, field2, ...clone} = originalObject;", 1367 | { "no-use-before-define": 1 } 1368 | ); 1369 | }); 1370 | 1371 | it("detects no-unused-vars with object destructuring #142", () => { 1372 | verifyAndAssertMessages( 1373 | "const {Bacona} = require('baconjs')", 1374 | { "no-undef": 1, "no-unused-vars": 1 }, 1375 | ["1:8 'Bacona' is assigned a value but never used. no-unused-vars"] 1376 | ); 1377 | }); 1378 | 1379 | it("don't warn no-unused-vars with spread #142", () => { 1380 | verifyAndAssertMessages( 1381 | ` 1382 | export default function test(data) { 1383 | return { 1384 | foo: 'bar', 1385 | ...data 1386 | }; 1387 | } 1388 | `, 1389 | { "no-undef": 1, "no-unused-vars": 1 } 1390 | ); 1391 | }); 1392 | 1393 | it("excludes comment tokens #153", () => { 1394 | verifyAndAssertMessages( 1395 | ` 1396 | var a = [ 1397 | 1, 1398 | 2, // a trailing comment makes this line fail comma-dangle (always-multiline) 1399 | ]; 1400 | `, 1401 | { "comma-dangle": [2, "always-multiline"] } 1402 | ); 1403 | 1404 | verifyAndAssertMessages( 1405 | ` 1406 | switch (a) { 1407 | // A comment here makes the above line fail brace-style 1408 | case 1: 1409 | console.log(a); 1410 | } 1411 | `, 1412 | { "brace-style": 2 } 1413 | ); 1414 | }); 1415 | 1416 | it("ternary and parens #149", () => { 1417 | verifyAndAssertMessages("true ? (true) : false;", { "space-infix-ops": 1 }); 1418 | }); 1419 | 1420 | it("line comment space-in-parens #124", () => { 1421 | verifyAndAssertMessages( 1422 | ` 1423 | React.createClass({ 1424 | render() { 1425 | // return ( 1426 | //
1427 | // ); // <-- this is the line that is reported 1428 | } 1429 | }); 1430 | `, 1431 | { "space-in-parens": 1 } 1432 | ); 1433 | }); 1434 | 1435 | it("block comment space-in-parens #124", () => { 1436 | verifyAndAssertMessages( 1437 | ` 1438 | React.createClass({ 1439 | render() { 1440 | /* 1441 | return ( 1442 |
1443 | ); // <-- this is the line that is reported 1444 | */ 1445 | } 1446 | }); 1447 | `, 1448 | { "space-in-parens": 1 } 1449 | ); 1450 | }); 1451 | 1452 | it("no no-undef error with rest #11", () => { 1453 | verifyAndAssertMessages("const [a, ...rest] = ['1', '2', '3']; a; rest;", { 1454 | "no-undef": 1, 1455 | "no-unused-vars": 1, 1456 | }); 1457 | }); 1458 | 1459 | it("async function with space-before-function-paren #168", () => { 1460 | verifyAndAssertMessages("it('handles updates', async function() {});", { 1461 | "space-before-function-paren": [1, "never"], 1462 | }); 1463 | }); 1464 | 1465 | it("default param flow type no-unused-vars #184", () => { 1466 | verifyAndAssertMessages( 1467 | ` 1468 | type ResolveOptionType = { 1469 | depth?: number, 1470 | identifier?: string 1471 | }; 1472 | 1473 | export default function resolve( 1474 | options: ResolveOptionType = {} 1475 | ): Object { 1476 | options; 1477 | } 1478 | `, 1479 | { "no-unused-vars": 1, "no-undef": 1 } 1480 | ); 1481 | }); 1482 | 1483 | it("no-use-before-define #192", () => { 1484 | verifyAndAssertMessages( 1485 | ` 1486 | console.log(x); 1487 | var x = 1; 1488 | `, 1489 | { "no-use-before-define": 1 }, 1490 | ["1:13 'x' was used before it was defined. no-use-before-define"] 1491 | ); 1492 | }); 1493 | 1494 | it("jsx and stringliteral #216", () => { 1495 | verifyAndAssertMessages("
"); 1496 | }); 1497 | 1498 | it("getter/setter #218", () => { 1499 | verifyAndAssertMessages( 1500 | ` 1501 | class Person { 1502 | set a (v) { } 1503 | } 1504 | `, 1505 | { 1506 | "space-before-function-paren": 1, 1507 | "keyword-spacing": [1, { before: true }], 1508 | indent: 1, 1509 | } 1510 | ); 1511 | }); 1512 | 1513 | it("getter/setter #220", () => { 1514 | verifyAndAssertMessages( 1515 | ` 1516 | var B = { 1517 | get x () { 1518 | return this.ecks; 1519 | }, 1520 | set x (ecks) { 1521 | this.ecks = ecks; 1522 | } 1523 | }; 1524 | `, 1525 | { "no-dupe-keys": 1 } 1526 | ); 1527 | }); 1528 | 1529 | it("fixes issues with flow types and ObjectPattern", () => { 1530 | verifyAndAssertMessages( 1531 | ` 1532 | import type Foo from 'bar'; 1533 | export default class Foobar { 1534 | foo({ bar }: Foo) { bar; } 1535 | bar({ foo }: Foo) { foo; } 1536 | } 1537 | `, 1538 | { "no-unused-vars": 1, "no-shadow": 1 } 1539 | ); 1540 | }); 1541 | 1542 | it("correctly detects redeclares if in script mode #217", () => { 1543 | verifyAndAssertMessages( 1544 | ` 1545 | var a = 321; 1546 | var a = 123; 1547 | `, 1548 | { "no-redeclare": 1 }, 1549 | ["2:5 'a' is already defined. no-redeclare"], 1550 | "script" 1551 | ); 1552 | }); 1553 | 1554 | it("correctly detects redeclares if in module mode #217", () => { 1555 | verifyAndAssertMessages( 1556 | ` 1557 | var a = 321; 1558 | var a = 123; 1559 | `, 1560 | { "no-redeclare": 1 }, 1561 | ["2:5 'a' is already defined. no-redeclare"], 1562 | "module" 1563 | ); 1564 | }); 1565 | 1566 | it("no-implicit-globals in script", () => { 1567 | verifyAndAssertMessages( 1568 | "var leakedGlobal = 1;", 1569 | { "no-implicit-globals": 1 }, 1570 | [ 1571 | "1:5 Implicit global variable, assign as global property instead. no-implicit-globals", 1572 | ], 1573 | "script", 1574 | { 1575 | env: {}, 1576 | parserOptions: { ecmaVersion: 6, sourceType: "script" }, 1577 | } 1578 | ); 1579 | }); 1580 | 1581 | it("no-implicit-globals in module", () => { 1582 | verifyAndAssertMessages( 1583 | "var leakedGlobal = 1;", 1584 | { "no-implicit-globals": 1 }, 1585 | [], 1586 | "module", 1587 | { 1588 | env: {}, 1589 | parserOptions: { ecmaVersion: 6, sourceType: "module" }, 1590 | } 1591 | ); 1592 | }); 1593 | 1594 | it("no-implicit-globals in default", () => { 1595 | verifyAndAssertMessages( 1596 | "var leakedGlobal = 1;", 1597 | { "no-implicit-globals": 1 }, 1598 | [], 1599 | null, 1600 | { 1601 | env: {}, 1602 | parserOptions: { ecmaVersion: 6 }, 1603 | } 1604 | ); 1605 | }); 1606 | 1607 | it("allowImportExportEverywhere option (#327)", () => { 1608 | verifyAndAssertMessages( 1609 | ` 1610 | if (true) { import Foo from 'foo'; } 1611 | function foo() { import Bar from 'bar'; } 1612 | switch (a) { case 1: import FooBar from 'foobar'; } 1613 | `, 1614 | {}, 1615 | [], 1616 | "module", 1617 | { 1618 | env: {}, 1619 | parserOptions: { 1620 | ecmaVersion: 6, 1621 | sourceType: "module", 1622 | allowImportExportEverywhere: true, 1623 | }, 1624 | } 1625 | ); 1626 | }); 1627 | 1628 | it("with does not crash parsing in script mode (strict off) #171", () => { 1629 | verifyAndAssertMessages("with (arguments) { length; }", {}, [], "script"); 1630 | }); 1631 | 1632 | xit("with does crash parsing in module mode (strict on) #171", () => { 1633 | verifyAndAssertMessages("with (arguments) { length; }", {}, [ 1634 | "1:1 Parsing error: 'with' in strict mode", 1635 | ]); 1636 | }); 1637 | 1638 | it("new.target is not reported as undef #235", () => { 1639 | verifyAndAssertMessages("function foo () { return new.target }", { 1640 | "no-undef": 1, 1641 | }); 1642 | }); 1643 | 1644 | it("decorator does not create TypeError #229", () => { 1645 | verifyAndAssertMessages( 1646 | ` 1647 | class A { 1648 | @test 1649 | f() {} 1650 | } 1651 | `, 1652 | { "no-undef": 1 }, 1653 | ["2:4 'test' is not defined. no-undef"] 1654 | ); 1655 | }); 1656 | 1657 | it("Flow definition does not trigger warnings #223", () => { 1658 | verifyAndAssertMessages( 1659 | ` 1660 | import { Map as $Map } from 'immutable'; 1661 | function myFunction($state: $Map, { a, b, c } : { a: ?Object, b: ?Object, c: $Map }) {} 1662 | `, 1663 | { "no-dupe-args": 1, "no-redeclare": 1, "no-shadow": 1 } 1664 | ); 1665 | }); 1666 | 1667 | it("newline-before-return with comments #289", () => { 1668 | verifyAndAssertMessages( 1669 | ` 1670 | function a() { 1671 | if (b) { 1672 | /* eslint-disable no-console */ 1673 | console.log('test'); 1674 | /* eslint-enable no-console */ 1675 | } 1676 | 1677 | return hasGlobal; 1678 | } 1679 | `, 1680 | { "newline-before-return": 1 } 1681 | ); 1682 | }); 1683 | 1684 | it("spaced-comment with shebang #163", () => { 1685 | verifyAndAssertMessages( 1686 | ` 1687 | #!/usr/bin/env babel-node 1688 | import {spawn} from 'foobar'; 1689 | `, 1690 | { "spaced-comment": 1 } 1691 | ); 1692 | }); 1693 | 1694 | describe("Class Property Declarations", () => { 1695 | it("no-redeclare false positive 1", () => { 1696 | verifyAndAssertMessages( 1697 | ` 1698 | class Group { 1699 | static propTypes = {}; 1700 | } 1701 | class TypicalForm { 1702 | static propTypes = {}; 1703 | } 1704 | `, 1705 | { "no-redeclare": 1 } 1706 | ); 1707 | }); 1708 | 1709 | it("no-redeclare false positive 2", () => { 1710 | verifyAndAssertMessages( 1711 | ` 1712 | function validate() {} 1713 | class MyComponent { 1714 | static validate = validate; 1715 | } 1716 | `, 1717 | { "no-redeclare": 1 } 1718 | ); 1719 | }); 1720 | 1721 | it("check references", () => { 1722 | verifyAndAssertMessages( 1723 | ` 1724 | var a; 1725 | class A { 1726 | prop1; 1727 | prop2 = a; 1728 | prop3 = b; 1729 | } 1730 | new A 1731 | `, 1732 | { "no-undef": 1, "no-unused-vars": 1, "no-redeclare": 1 }, 1733 | ["5:11 'b' is not defined. no-undef"] 1734 | ); 1735 | }); 1736 | }); 1737 | 1738 | it("dynamic import support", () => { 1739 | verifyAndAssertMessages("import('test-module').then(() => {})"); 1740 | }); 1741 | 1742 | it("regex with es6 unicodeCodePointEscapes", () => { 1743 | verifyAndAssertMessages( 1744 | "string.replace(/[\u{0000A0}-\u{10FFFF}<>&]/gmiu, (char) => `&#x${char.codePointAt(0).toString(16)};`);" 1745 | ); 1746 | }); 1747 | 1748 | describe("private class properties", () => { 1749 | it("should not be undefined", () => { 1750 | verifyAndAssertMessages( 1751 | ` 1752 | class C { 1753 | #d = 1; 1754 | } 1755 | `, 1756 | { "no-undef": 1 } 1757 | ); 1758 | }); 1759 | 1760 | it("should not be unused", () => { 1761 | verifyAndAssertMessages( 1762 | ` 1763 | export class C { 1764 | #d = 1; 1765 | } 1766 | `, 1767 | { "no-unused-vars": 1 } 1768 | ); 1769 | }); 1770 | }); 1771 | 1772 | describe("optional chaining operator", () => { 1773 | it("should not be undefined #595", () => { 1774 | verifyAndAssertMessages( 1775 | ` 1776 | const foo = {}; 1777 | foo?.bar; 1778 | `, 1779 | { "no-undef": 1 } 1780 | ); 1781 | }); 1782 | }); 1783 | 1784 | it("flow types on class method should be visited correctly", () => { 1785 | verifyAndAssertMessages( 1786 | ` 1787 | import type NodeType from 'foo'; 1788 | class NodeUtils { 1789 | finishNodeAt(node: T): T { return node; } 1790 | } 1791 | 1792 | new NodeUtils(); 1793 | `, 1794 | { "no-unused-vars": 1 } 1795 | ); 1796 | }); 1797 | 1798 | it("works with dynamicImport", () => { 1799 | verifyAndAssertMessages( 1800 | ` 1801 | import('a'); 1802 | ` 1803 | ); 1804 | }); 1805 | 1806 | it("works with numericSeparator", () => { 1807 | verifyAndAssertMessages( 1808 | ` 1809 | 1_000 1810 | ` 1811 | ); 1812 | }); 1813 | 1814 | it("works with optionalChaining", () => { 1815 | verifyAndAssertMessages( 1816 | ` 1817 | a?.b 1818 | ` 1819 | ); 1820 | }); 1821 | 1822 | it("works with import.meta", () => { 1823 | verifyAndAssertMessages( 1824 | ` 1825 | import.meta 1826 | ` 1827 | ); 1828 | }); 1829 | 1830 | it("works with classPrivateProperties", () => { 1831 | verifyAndAssertMessages( 1832 | ` 1833 | class A { #a = 1; } 1834 | ` 1835 | ); 1836 | }); 1837 | 1838 | it("works with optionalCatchBinding", () => { 1839 | verifyAndAssertMessages( 1840 | ` 1841 | try {} catch {} 1842 | try {} catch {} finally {} 1843 | ` 1844 | ); 1845 | }); 1846 | 1847 | it("exportDefaultFrom", () => { 1848 | verifyAndAssertMessages( 1849 | ` 1850 | export v from "mod" 1851 | ` 1852 | ); 1853 | }); 1854 | 1855 | it("exportNamespaceFrom", () => { 1856 | verifyAndAssertMessages( 1857 | ` 1858 | export * as ns from "mod" 1859 | ` 1860 | ); 1861 | }); 1862 | 1863 | it("ignore eval in scope analysis", () => { 1864 | verifyAndAssertMessages( 1865 | ` 1866 | const a = 1; 1867 | console.log(a); 1868 | eval(''); 1869 | `, 1870 | { "no-unused-vars": 1, "no-undef": 1 } 1871 | ); 1872 | }); 1873 | }); 1874 | --------------------------------------------------------------------------------