Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Relax "no-null-undefined-union" rule. #4625

Merged
merged 7 commits into from Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/configuration.ts
Expand Up @@ -429,7 +429,6 @@ export function getRulesDirectories(
* @param ruleConfigValue The raw option setting of a rule
*/
function parseRuleOptions(
// tslint:disable-next-line no-null-undefined-union
ruleConfigValue: RawRuleConfig,
rawDefaultRuleSeverity: string | undefined,
): Partial<IOptions> {
Expand Down Expand Up @@ -508,12 +507,11 @@ export interface RawConfigFile {
jsRules?: RawRulesConfig | boolean;
}
export interface RawRulesConfig {
// tslint:disable-next-line no-null-undefined-union
[key: string]: RawRuleConfig;
}

// tslint:disable-next-line no-null-undefined-union
export type RawRuleConfig =
// tslint:disable-next-line no-null-undefined-union
| null
| undefined
| boolean
Expand Down
25 changes: 4 additions & 21 deletions src/rules/noNullUndefinedUnionRule.ts
Expand Up @@ -15,16 +15,7 @@
* limitations under the License.
*/

import {
isParameterDeclaration,
isPropertyDeclaration,
isPropertySignature,
isSignatureDeclaration,
isTypeAliasDeclaration,
isTypeReference,
isUnionType,
isVariableDeclaration,
} from "tsutils";
import { isSignatureDeclaration, isTypeReference, isUnionType, isUnionTypeNode } from "tsutils";
import * as ts from "typescript";

import * as Lint from "../index";
Expand Down Expand Up @@ -66,18 +57,10 @@ function walk(ctx: Lint.WalkContext, tc: ts.TypeChecker): void {
}

function getType(node: ts.Node, tc: ts.TypeChecker): ts.Type | undefined {
// This is a comprehensive intersection between `HasType` and has property `name`.
// The node name kind must be identifier, or else this rule will throw errors while descending.
if (
(isVariableDeclaration(node) ||
isParameterDeclaration(node) ||
isPropertySignature(node) ||
isPropertyDeclaration(node) ||
isTypeAliasDeclaration(node)) &&
node.name.kind === ts.SyntaxKind.Identifier
) {
if (isUnionTypeNode(node)) {
return tc.getTypeAtLocation(node);
} else if (isSignatureDeclaration(node)) {
} else if (isSignatureDeclaration(node) && node.type === undefined) {
// Explicit types should be handled by the first case.
const signature = tc.getSignatureFromDeclaration(node);
return signature === undefined ? undefined : signature.getReturnType();
} else {
Expand Down
56 changes: 38 additions & 18 deletions test/rules/no-null-undefined-union/test.ts.lint
@@ -1,39 +1,45 @@
[typescript]: >= 2.4.0

interface someInterface {
a: number | undefined | null;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]
b: boolean;
}

const c: string | null | undefined;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]
type SomeType =

export type SomeType =
~~~~~~~~~~~~~~~~~~~~~~
| null
~~~~~~~~~~
~~~~
| undefined
~~~~~~~~~~~~~~~
| boolean;
~~~~~~~~~~~~~~ [0]
~~~~~~~~~~~~~ [0]

const x: SomeType;

const someFunc = (): SomeType => {}

interface SomeInterface {
foo: number | undefined | null;
~~~~~~~~~~~~~~~~~~~~~~~~~ [0]
bar: boolean;
}

function(foo: SomeInterface) {}

const y: string | null | undefined;
~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

const someFunc = (): string | undefined | null => {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]
~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

const someFunc = (foo: null | string | undefined, bar: boolean) => {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]
~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

function someFunc(): number | undefined | null {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]
~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

function someFunc(): Promise<number | null | undefined> {} // error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]
~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

function someFunc(bar: boolean, foo: null | number | undefined) {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]
~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

function someFunc() {
function testFunc() {
~~~~~~~~~~~~~~~~~~~~~
const somePredicate = (): boolean => true;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -52,4 +58,18 @@ function someFunc() {
}
~ [0]

const z = testFunc();

type Text = string | null

interface TextInterface {
foo?: Text
nrathi marked this conversation as resolved.
Show resolved Hide resolved
}

interface SuperTextInterface {
bar?: Text | number
}

function someFunc(foo?: Text, bar?: Text | number) {}

[0]: Union type cannot include both 'null' and 'undefined'.