eslint-plugin-no-type-assertion
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -8,3 +8,4 @@ module.exports = { | ||
messages: { | ||
typeAssertion: "Do not use as operator to assert expression type", | ||
angleBracketAssertion: "Do not use type assertion", | ||
asAssertion: "Do not use as operator for type assertion", | ||
}, | ||
@@ -16,2 +17,13 @@ schema: [], | ||
return { | ||
TSTypeAssertion(node) { | ||
const { typeAnnotation } = node; | ||
if (typeAnnotation.type === "TSTypeReference") { | ||
const { typeName } = typeAnnotation; | ||
if (typeName.type === "Identifier" && typeName.name === "const") { | ||
return; | ||
} | ||
} | ||
context.report({ node, messageId: "angleBracketAssertion" }); | ||
}, | ||
TSAsExpression(node) { | ||
@@ -26,3 +38,3 @@ const { typeAnnotation } = node; | ||
context.report({ node, messageId: "typeAssertion" }); | ||
context.report({ node, messageId: "asAssertion" }); | ||
}, | ||
@@ -29,0 +41,0 @@ }; |
{ | ||
"name": "eslint-plugin-no-type-assertion", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Disallow type assertions in TypeScript code", | ||
@@ -22,3 +22,3 @@ "main": "lib/index.js", | ||
"scripts": { | ||
"test": "mocha tests --recursive", | ||
"test": "jest tests", | ||
"prettier": "prettier --check \"{lib,test}/**/*.js\"" | ||
@@ -32,3 +32,3 @@ }, | ||
"eslint": "^7.0.0", | ||
"mocha": "^7.1.2", | ||
"jest": "^26.0.1", | ||
"prettier": "^2.0.5", | ||
@@ -35,0 +35,0 @@ "typescript": "^3.9.3" |
# eslint-plugin-no-type-assertion | ||
Disallow type assertions in TypeScript code. The rule will forbid `as` operator when used for changing the expression type, unless it's used for [const assertions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions). | ||
Disallow type assertions in TypeScript code. The rule will forbid both `as` operator, as well as angle-bracketed syntax, unlessused for [const assertions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions). | ||
The following code becomes invalid: | ||
```typescript | ||
const foo: unknown = 42; | ||
const bar = <number>foo; | ||
const baz = foo as number; | ||
``` | ||
## Installation | ||
@@ -52,2 +61,6 @@ | ||
### 1.1.0 | ||
- Add support for angle-bracketed assertion syntax | ||
### 1.0.1 | ||
@@ -54,0 +67,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4333
40
72