This repository was archived by the owner on Jan 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] [no-object-literal-type-assertion] Add rule (#225)
- Loading branch information
1 parent
3aec5a1
commit 1a97263
Showing
4 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Forbids an object literal to appear in a type assertion expression (no-object-literal-type-assertion) | ||
|
||
Always prefer `const x: T = { ... };` to `const x = { ... } as T;`. Casting to `any` and `unknown` is still allowed. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule. | ||
|
||
```ts | ||
const x = { ... } as T; | ||
``` | ||
|
||
Examples of **correct** code for this rule. | ||
|
||
```ts | ||
const x: T = { ... }; | ||
const y = { ... } as any; | ||
const z = { ... } as unknown; | ||
``` | ||
|
||
## Options | ||
```json | ||
{ | ||
"typescript/no-object-literal-type-assertion": "error" | ||
} | ||
``` | ||
|
||
## Compatibility | ||
|
||
* TSLint: [no-object-literal-type-assertion](https://palantir.github.io/tslint/rules/no-object-literal-type-assertion/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* @fileoverview Forbids an object literal to appear in a type assertion expression | ||
* @author Armano <https://github.com/armano2> | ||
*/ | ||
"use strict"; | ||
|
||
const util = require("../util"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: | ||
"Forbids an object literal to appear in a type assertion expression", | ||
extraDescription: [ | ||
util.tslintRule("no-object-literal-type-assertion"), | ||
], | ||
category: "TypeScript", | ||
url: util.metaDocsUrl("no-object-literal-type-assertions"), | ||
}, | ||
messages: { | ||
unexpectedTypeAssertion: | ||
"Type assertion on object literals is forbidden, use a type annotation instead.", | ||
}, | ||
schema: [], | ||
}, | ||
create(context) { | ||
//---------------------------------------------------------------------- | ||
// Public | ||
//---------------------------------------------------------------------- | ||
|
||
/** | ||
* Check whatever node should be reported | ||
* @param {ASTNode} node the node to be evaluated. | ||
* @returns {*} true or false | ||
*/ | ||
function checkType(node) { | ||
if ( | ||
node && | ||
node.type === "TSTypeAnnotation" && | ||
node.typeAnnotation | ||
) { | ||
switch (node.typeAnnotation.type) { | ||
case "TSAnyKeyword": | ||
case "TSUnknownKeyword": | ||
return false; | ||
default: | ||
break; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
return { | ||
"TSTypeAssertionExpression, TSAsExpression"(node) { | ||
if ( | ||
checkType(node.typeAnnotation) && | ||
node.expression.type === "ObjectExpression" | ||
) { | ||
context.report({ | ||
node, | ||
messageId: "unexpectedTypeAssertion", | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/** | ||
* @fileoverview Forbids an object literal to appear in a type assertion expression | ||
* @author Armano <https://github.com/armano2> | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require("../../../lib/rules/no-object-literal-type-assertion"), | ||
RuleTester = require("eslint").RuleTester; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: "typescript-eslint-parser", | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
sourceType: "module", | ||
ecmaFeatures: { | ||
jsx: false, | ||
}, | ||
}, | ||
}); | ||
|
||
ruleTester.run("no-object-literal-type-assertion", rule, { | ||
valid: [ | ||
{ | ||
code: `<T> x;`, | ||
}, | ||
{ | ||
code: `x as T;`, | ||
}, | ||
{ | ||
code: `const foo = bar;`, | ||
}, | ||
{ | ||
code: `const foo: baz = bar;`, | ||
}, | ||
{ | ||
code: `const x: T = {};`, | ||
}, | ||
{ | ||
code: `const foo = { bar: { } };`, | ||
}, | ||
// Allow cast to 'any' | ||
{ | ||
code: `const foo = {} as any;`, | ||
}, | ||
{ | ||
code: `const foo = <any> {};`, | ||
}, | ||
// Allow cast to 'unknown' | ||
{ | ||
code: `const foo = {} as unknown;`, | ||
}, | ||
{ | ||
code: `const foo = <unknown> {};`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: `<T> ({});`, | ||
errors: [ | ||
{ | ||
messageId: "unexpectedTypeAssertion", | ||
line: 1, | ||
column: 1, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `({}) as T;`, | ||
errors: [ | ||
{ | ||
messageId: "unexpectedTypeAssertion", | ||
line: 1, | ||
column: 1, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `const x = {} as T;`, | ||
errors: [ | ||
{ | ||
messageId: "unexpectedTypeAssertion", | ||
line: 1, | ||
column: 11, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |