Skip to content

Commit

Permalink
Merge branch 'master' into typescript-eslint-no-unbound-method
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher committed Feb 7, 2019
2 parents c624d05 + fc50167 commit 30035e8
Show file tree
Hide file tree
Showing 49 changed files with 49,089 additions and 40,355 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -44,7 +44,7 @@
},
"devDependencies": {
"@babel/code-frame": "7.0.0",
"@babel/parser": "7.3.0",
"@babel/parser": "7.3.2",
"@commitlint/cli": "^7.1.2",
"@commitlint/config-conventional": "^7.1.2",
"@commitlint/travis-cli": "^7.1.2",
Expand Down
10 changes: 1 addition & 9 deletions packages/eslint-plugin-tslint/src/index.ts
Expand Up @@ -3,20 +3,12 @@ import memoize from 'lodash.memoize';
import { Configuration, RuleSeverity } from 'tslint';
import { Program } from 'typescript';
import { CustomLinter } from './custom-linter';
import { ParserServices } from '@typescript-eslint/typescript-estree';

//------------------------------------------------------------------------------
// Plugin Definition
//------------------------------------------------------------------------------

/**
* @todo share types between packages
*/
interface ParserServices {
program: Program | undefined;
esTreeNodeToTSNodeMap: WeakMap<object, any> | undefined;
tsNodeToESTreeNodeMap: WeakMap<object, any> | undefined;
}

type RawRuleConfig =
| null
| undefined
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/README.md
Expand Up @@ -128,6 +128,7 @@ Then you should add `airbnb` (or `airbnb-base`) to your `extends` section of `.e
| [`@typescript-eslint/no-empty-interface`](./docs/rules/no-empty-interface.md) | Disallow the declaration of empty interfaces (`no-empty-interface` from TSLint) | :heavy_check_mark: | |
| [`@typescript-eslint/no-explicit-any`](./docs/rules/no-explicit-any.md) | Disallow usage of the `any` type (`no-any` from TSLint) | :heavy_check_mark: | |
| [`@typescript-eslint/no-extraneous-class`](./docs/rules/no-extraneous-class.md) | Forbids the use of classes as namespaces (`no-unnecessary-class` from TSLint) | | |
| [`@typescript-eslint/no-for-in-array`](./docs/rules/no-for-in-array.md) | Disallow iterating over an array with a for-in loop (`no-for-in-array` from TSLint) | | |
| [`@typescript-eslint/no-inferrable-types`](./docs/rules/no-inferrable-types.md) | Disallows explicit type declarations for variables or parameters initialized to a number, string, or boolean. (`no-inferrable-types` from TSLint) | :heavy_check_mark: | :wrench: |
| [`@typescript-eslint/no-misused-new`](./docs/rules/no-misused-new.md) | Enforce valid definition of `new` and `constructor`. (`no-misused-new` from TSLint) | :heavy_check_mark: | |
| [`@typescript-eslint/no-namespace`](./docs/rules/no-namespace.md) | Disallow the use of custom TypeScript modules and namespaces (`no-namespace` from TSLint) | :heavy_check_mark: | |
Expand Down
13 changes: 7 additions & 6 deletions packages/eslint-plugin/ROADMAP.md
@@ -1,10 +1,10 @@
# Roadmap

(30) = done<br>
🌟 (79) = in ESLint core<br>
🔌 (33) = in another plugin<br>
🌓 (16) = implementations differ or ESLint version is missing functionality<br>
🛑 (67) = unimplemented
✅ = done<br>
🌟 = in ESLint core<br>
🔌 = in another plugin<br>
🌓 = implementations differ or ESLint version is missing functionality<br>
🛑 = unimplemented<br>

## TSLint rules

Expand Down Expand Up @@ -59,7 +59,7 @@
| [`no-empty`] | 🌟 | [`no-empty`][no-empty] |
| [`no-eval`] | 🌟 | [`no-eval`][no-eval] |
| [`no-floating-promises`] | 🛑 | N/A ([relevant plugin][plugin:promise]) |
| [`no-for-in-array`] | 🛑 | N/A |
| [`no-for-in-array`] | | [`@typescript-eslint/no-for-in-array`] |
| [`no-implicit-dependencies`] | 🔌 | [`import/no-extraneous-dependencies`] |
| [`no-inferred-empty-object-type`] | 🛑 | N/A |
| [`no-invalid-template-strings`] | 🌟 | [`no-template-curly-in-string`][no-template-curly-in-string] |
Expand Down Expand Up @@ -587,6 +587,7 @@ Relevant plugins: [`chai-expect-keywords`](https://github.com/gavinaiken/eslint-
[`@typescript-eslint/member-delimiter-style`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/member-delimiter-style.md
[`@typescript-eslint/prefer-interface`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-interface.md
[`@typescript-eslint/no-array-constructor`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-array-constructor.md
[`@typescript-eslint/no-for-in-array`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-for-in-array.md

<!-- eslint-plugin-import -->

Expand Down
44 changes: 44 additions & 0 deletions packages/eslint-plugin/docs/rules/no-for-in-array.md
@@ -0,0 +1,44 @@
# Disallow iterating over an array with a for-in loop (no-for-in-array)

This rule prohibits iterating over an array with a for-in loop.

## Rule Details

Rationale from TSLint:

A for-in loop (`for (var k in o)`) iterates over the properties of an Object.
While it is legal to use for-in loops with array types, it is not common.
for-in will iterate over the indices of the array as strings, omitting any "holes" in
the array.
More common is to use for-of, which iterates over the values of an array.
If you want to iterate over the indices, alternatives include:

```js
array.forEach((value, index) => { ... });
for (const [index, value] of array.entries()) { ... }
for (let i = 0; i < array.length; i++) { ... }
```

Examples of **incorrect** code for this rule:

```js
for (const x in [3, 4, 5]) {
console.log(x);
}
```

Examples of **correct** code for this rule:

```js
for (const x in { a: 3, b: 4, c: 5 }) {
console.log(x);
}
```

## When Not To Use It

If you want to iterate through a loop using the indices in an array as strings, you can turn off this rule.

## Related to

- TSLint: ['no-for-in-array'](https://palantir.github.io/tslint/rules/no-for-in-array/)
35 changes: 25 additions & 10 deletions packages/eslint-plugin/lib/rules/adjacent-overload-signatures.js
Expand Up @@ -73,6 +73,17 @@ module.exports = {
}
}

/**
* Determine whether two methods are the same or not
* @param {{ name: string; static: boolean }} method1 a method to compare
* @param {{ name: string; static: boolean }} method2 another method to compare with
* @returns {boolean} true if two methods are the same
* @private
*/
function isSameMethod(method1, method2) {
return method1.name === method2.name && method1.static === method2.static;
}

/**
* Check the body for overload methods.
* @param {ASTNode} node the body to be inspected.
Expand All @@ -83,28 +94,32 @@ module.exports = {
const members = node.body || node.members;

if (members) {
let name;
let index;
let lastName;
const seen = [];
let lastMethod;
const seenMethods = [];

members.forEach(member => {
name = getMemberName(member);
const name = getMemberName(member);
const method = {
name,
static: member.static
};

index = seen.indexOf(name);
if (index > -1 && lastName !== name) {
const index = seenMethods.findIndex(seenMethod =>
isSameMethod(method, seenMethod)
);
if (index > -1 && !isSameMethod(method, lastMethod)) {
context.report({
node: member,
messageId: 'adjacentSignature',
data: {
name
name: (method.static ? 'static ' : '') + method.name
}
});
} else if (name && index === -1) {
seen.push(name);
seenMethods.push(method);
}

lastName = name;
lastMethod = method;
});
}
}
Expand Down
56 changes: 56 additions & 0 deletions packages/eslint-plugin/lib/rules/no-for-in-array.js
@@ -0,0 +1,56 @@
/**
* @fileoverview Disallow iterating over an array with a for-in loop
* @author Benjamin Lichtman
*/
'use strict';
const ts = require('typescript');
const util = require('../util');

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

/**
* @type {import("eslint").Rule.RuleModule}
*/
module.exports = {
meta: {
docs: {
description: 'Disallow iterating over an array with a for-in loop',
category: 'Functionality',
recommended: false,
extraDescription: [util.tslintRule('no-for-in-array')],
url: util.metaDocsUrl('no-for-in-array')
},
fixable: null,
messages: {
forInViolation:
'For-in loops over arrays are forbidden. Use for-of or array.forEach instead.'
},
schema: [],
type: 'problem'
},

create(context) {
return {
ForInStatement(node) {
const parserServices = util.getParserServices(context);
const checker = parserServices.program.getTypeChecker();
const originalNode = parserServices.esTreeNodeToTSNodeMap.get(node);

const type = checker.getTypeAtLocation(originalNode.expression);

if (
(typeof type.symbol !== 'undefined' &&
type.symbol.name === 'Array') ||
(type.flags & ts.TypeFlags.StringLike) !== 0
) {
context.report({
node,
messageId: 'forInViolation'
});
}
}
};
}
};
2 changes: 1 addition & 1 deletion packages/eslint-plugin/lib/util.js
Expand Up @@ -109,7 +109,7 @@ exports.upperCaseFirst = str => str[0].toUpperCase() + str.slice(1);
/**
* Try to retrieve typescript parser service from context
* @param {RuleContext} context Rule context
* @returns {{esTreeNodeToTSNodeMap}|{program}|Object|*} parserServices
* @returns {{program: Program, esTreeNodeToTSNodeMap: NodeMap}} parserServices
*/
exports.getParserServices = context => {
if (
Expand Down
Expand Up @@ -211,6 +211,23 @@ class Foo {
foo(sn: string | number): void {}
bar(): void {}
baz(): void {}
}
`,
`
class Foo {
name: string;
static foo(s: string): void;
static foo(n: number): void;
static foo(sn: string | number): void {}
bar(): void {}
baz(): void {}
}
`,
`
class Test {
static test() {}
untest() {}
test() {}
}
`,
// examples from https://github.com/nzakas/eslint-plugin-typescript/issues/138
Expand Down Expand Up @@ -789,6 +806,26 @@ class Foo {
column: 5
}
]
},
{
code: `
class Foo {
static foo(s: string): void;
name: string;
static foo(n: number): void;
static foo(sn: string | number): void {}
bar(): void {}
baz(): void {}
}
`,
errors: [
{
messageId: 'adjacentSignature',
data: { name: 'static foo' },
line: 5,
column: 5
}
]
}
]
});
69 changes: 69 additions & 0 deletions packages/eslint-plugin/tests/lib/rules/no-for-in-array.js
@@ -0,0 +1,69 @@
/**
* @fileoverview Disallow iterating over an array with a for-in loop
* @author Benjamin Lichtman
*/
'use strict';

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const rule = require('../../../lib/rules/no-for-in-array'),
RuleTester = require('eslint').RuleTester,
path = require('path');

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

const rootDir = path.join(process.cwd(), 'tests/fixtures/');
const parserOptions = {
ecmaVersion: 2015,
tsconfigRootDir: rootDir,
project: './tsconfig.json'
};
const ruleTester = new RuleTester({
parserOptions,
parser: '@typescript-eslint/parser'
});

ruleTester.run('no-for-in-array', rule, {
valid: [
`
for (const x of [3, 4, 5]) {
console.log(x);
}`,
`
for (const x in { a: 1, b: 2, c: 3 }) {
console.log(x);
}`
],

invalid: [
{
code: `
for (const x in [3, 4, 5]) {
console.log(x);
}`,
errors: [
{
messageId: 'forInViolation',
type: 'ForInStatement'
}
]
},
{
code: `
const z = [3, 4, 5];
for (const x in z) {
console.log(x);
}`,
errors: [
{
messageId: 'forInViolation',
type: 'ForInStatement'
}
]
}
]
});
1 change: 0 additions & 1 deletion packages/parser/package.json
Expand Up @@ -43,7 +43,6 @@
"devDependencies": {
"@types/eslint": "^4.16.5",
"@types/eslint-visitor-keys": "^1.0.0",
"@types/estree": "^0.0.39",
"@typescript-eslint/shared-fixtures": "1.2.0"
}
}

0 comments on commit 30035e8

Please sign in to comment.