Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(eslint-plugin): [restrict-template-expressions] rename `allowNul…
…lable` to `allowNullish` (#2006)
  • Loading branch information
bradzacher committed May 21, 2020
1 parent bfd9b60 commit 264b017
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
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 @@ -158,31 +158,31 @@ ruleTester.run('restrict-template-expressions', rule, {
const msg = \`arg = \${user.name || 'the user with no name'}\`;
`,
},
// 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 @@ -191,7 +191,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 @@ -272,7 +272,7 @@ ruleTester.run('restrict-template-expressions', rule, {
],
},
{
options: [{ allowNumber: true, allowBoolean: true, allowNullable: true }],
options: [{ allowNumber: true, allowBoolean: true, allowNullish: true }],
code: `
const arg = {};
const msg = \`arg = \${arg}\`;
Expand All @@ -296,7 +296,7 @@ ruleTester.run('restrict-template-expressions', rule, {
],
},
{
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 @@ -307,7 +307,7 @@ ruleTester.run('restrict-template-expressions', rule, {
],
},
{
options: [{ allowNumber: true, allowBoolean: true, allowNullable: true }],
options: [{ allowNumber: true, allowBoolean: true, allowNullish: true }],
code: `
function test<TWithNoConstraint>(arg: T) {
return \`arg = \${arg}\`;
Expand All @@ -323,7 +323,7 @@ ruleTester.run('restrict-template-expressions', rule, {
],
},
{
options: [{ allowNumber: true, allowBoolean: true, allowNullable: true }],
options: [{ allowNumber: true, allowBoolean: true, allowNullish: true }],
code: `
function test(arg: any) {
return \`arg = \${arg}\`;
Expand Down

0 comments on commit 264b017

Please sign in to comment.