Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin): [restrict-template-expressions] rename allowNullable to allowNullish #2006

Merged
merged 1 commit into from May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -34,14 +34,14 @@ type Options = {
// if true, also allow any in template expressions
allowAny?: boolean;
// if true, also allow null and undefined in template expressions
allowNullable?: boolean;
allowNullish?: boolean;
};

const defaults = {
allowNumber: true,
allowBoolean: false,
allowAny: false,
allowNullable: false,
allowNullish: false,
};
```

Expand Down Expand Up @@ -75,9 +75,9 @@ const msg1 = `arg = ${user.name}`;
const msg2 = `arg = ${user.name || 'the user with no name'}`;
```

### `allowNullable`
### `allowNullish`

Examples of additional **correct** code for this rule with `{ allowNullable: true }`:
Examples of additional **correct** code for this rule with `{ allowNullish: true }`:

```ts
const arg = condition ? 'ok' : null;
Expand Down
Expand Up @@ -10,7 +10,7 @@ type Options = [
allowNumber?: boolean;
allowBoolean?: boolean;
allowAny?: boolean;
allowNullable?: boolean;
allowNullish?: boolean;
},
];

Expand All @@ -36,7 +36,7 @@ export default util.createRule<Options, MessageId>({
allowNumber: { type: 'boolean' },
allowBoolean: { type: 'boolean' },
allowAny: { type: 'boolean' },
allowNullable: { type: 'boolean' },
allowNullish: { type: 'boolean' },
},
},
],
Expand Down Expand Up @@ -77,7 +77,7 @@ export default util.createRule<Options, MessageId>({
}

if (
options.allowNullable &&
options.allowNullish &&
util.isTypeFlagSet(type, ts.TypeFlags.Null | ts.TypeFlags.Undefined)
) {
return true;
Expand Down
Expand Up @@ -166,31 +166,31 @@ ruleTester.run('restrict-template-expressions', rule, {
}
`,
},
// allowNullable
// allowNullish
{
options: [{ allowNullable: true }],
options: [{ allowNullish: true }],
code: `
const arg = null;
const msg = \`arg = \${arg}\`;
`,
},
{
options: [{ allowNullable: true }],
options: [{ allowNullish: true }],
code: `
declare const arg: string | null | undefined;
const msg = \`arg = \${arg}\`;
`,
},
{
options: [{ allowNullable: true }],
options: [{ allowNullish: true }],
code: `
function test<T extends null | undefined>(arg: T) {
return \`arg = \${arg}\`;
}
`,
},
{
options: [{ allowNullable: true }],
options: [{ allowNullish: true }],
code: `
function test<T extends string | null>(arg: T) {
return \`arg = \${arg}\`;
Expand All @@ -199,7 +199,7 @@ ruleTester.run('restrict-template-expressions', rule, {
},
// allow ALL
{
options: [{ allowNumber: true, allowBoolean: true, allowNullable: true }],
options: [{ allowNumber: true, allowBoolean: true, allowNullish: true }],
code: `
type All = string | number | boolean | null | undefined;
function test<T extends All>(arg: T) {
Expand Down Expand Up @@ -245,7 +245,7 @@ ruleTester.run('restrict-template-expressions', rule, {
errors: [{ messageId: 'invalidType', line: 3, column: 30 }],
},
{
options: [{ allowNumber: true, allowBoolean: true, allowNullable: true }],
options: [{ allowNumber: true, allowBoolean: true, allowNullish: true }],
code: `
const arg = {};
const msg = \`arg = \${arg}\`;
Expand All @@ -260,7 +260,7 @@ ruleTester.run('restrict-template-expressions', rule, {
errors: [{ messageId: 'invalidType', line: 3, column: 30 }],
},
{
options: [{ allowNumber: true, allowBoolean: true, allowNullable: true }],
options: [{ allowNumber: true, allowBoolean: true, allowNullish: true }],
code: `
function test<T extends {}>(arg: T) {
return \`arg = \${arg}\`;
Expand All @@ -269,7 +269,7 @@ ruleTester.run('restrict-template-expressions', rule, {
errors: [{ messageId: 'invalidType', line: 3, column: 27 }],
},
{
options: [{ allowNumber: true, allowBoolean: true, allowNullable: true }],
options: [{ allowNumber: true, allowBoolean: true, allowNullish: true }],
code: `
function test(arg: any) {
return \`arg = \${arg}\`;
Expand Down