Skip to content

chore: turn on prefer-nullish-coalescing locally #1259

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

Merged
merged 2 commits into from
Nov 25, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = {
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/prefer-nullish-coalescing': 'error',
'@typescript-eslint/prefer-optional-chain': 'error',
'@typescript-eslint/unbound-method': 'off',

Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin-tslint/src/rules/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ const tslintConfig = memoize(
return Configuration.loadConfigurationFromPath(lintFile);
}
return Configuration.parseConfigFile({
rules: tslintRules || {},
rulesDirectory: tslintRulesDirectory || [],
rules: tslintRules ?? {},
rulesDirectory: tslintRulesDirectory ?? [],
});
},
(lintFile: string | undefined, tslintRules = {}, tslintRulesDirectory = []) =>
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/array-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export default util.createRule<Options, MessageIds>({
const sourceCode = context.getSourceCode();

const defaultOption = options.default;
const readonlyOption = options.readonly || defaultOption;
const readonlyOption = options.readonly ?? defaultOption;

const isArraySimpleOption =
defaultOption === 'array-simple' && readonlyOption === 'array-simple';
Expand Down
9 changes: 5 additions & 4 deletions packages/eslint-plugin/src/rules/camelcase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ export default util.createRule<Options, MessageIds>({

const genericType = options.genericType;
const properties = options.properties;
const allow = (options.allow || []).map(entry => ({
name: entry,
regex: new RegExp(entry),
}));
const allow =
options.allow?.map(entry => ({
name: entry,
regex: new RegExp(entry),
})) ?? [];

/**
* Checks if a string contains an underscore and isn't all upper-case
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default util.createRule({
node: node.id,
messageId: 'interfaceOverType',
fix(fixer) {
const typeNode = node.typeParameters || node.id;
const typeNode = node.typeParameters ?? node.id;
const fixes: TSESLint.RuleFix[] = [];

const firstToken = sourceCode.getFirstToken(node);
Expand Down Expand Up @@ -70,7 +70,7 @@ export default util.createRule({
node: node.id,
messageId: 'typeOverInterface',
fix(fixer) {
const typeNode = node.typeParameters || node.id;
const typeNode = node.typeParameters ?? node.id;
const fixes: TSESLint.RuleFix[] = [];

const firstToken = sourceCode.getFirstToken(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ export default util.createRule<Options, MessageIds>({
defaultOptions: [{ accessibility: 'explicit' }],
create(context, [option]) {
const sourceCode = context.getSourceCode();
const baseCheck: AccessibilityLevel = option.accessibility || 'explicit';
const overrides = option.overrides || {};
const ctorCheck = overrides.constructors || baseCheck;
const accessorCheck = overrides.accessors || baseCheck;
const methodCheck = overrides.methods || baseCheck;
const propCheck = overrides.properties || baseCheck;
const paramPropCheck = overrides.parameterProperties || baseCheck;
const ignoredMethodNames = new Set(option.ignoredMethodNames || []);
const baseCheck: AccessibilityLevel = option.accessibility ?? 'explicit';
const overrides = option.overrides ?? {};
const ctorCheck = overrides.constructors ?? baseCheck;
const accessorCheck = overrides.accessors ?? baseCheck;
const methodCheck = overrides.methods ?? baseCheck;
const propCheck = overrides.properties ?? baseCheck;
const paramPropCheck = overrides.parameterProperties ?? baseCheck;
const ignoredMethodNames = new Set(option.ignoredMethodNames ?? []);
/**
* Generates the report for rule violations
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/func-call-spacing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default util.createRule<Options, MessageIds>({

const closingParenToken = sourceCode.getLastToken(node)!;
const lastCalleeTokenWithoutPossibleParens = sourceCode.getLastToken(
node.typeParameters || node.callee,
node.typeParameters ?? node.callee,
)!;
const openingParenToken = sourceCode.getFirstTokenBetween(
lastCalleeTokenWithoutPossibleParens,
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/member-delimiter-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default util.createRule<Options, MessageIds>({

// use the base options as the defaults for the cases
const baseOptions = options;
const overrides = baseOptions.overrides || {};
const overrides = baseOptions.overrides ?? {};
const interfaceOptions: BaseOptions = util.deepMerge(
baseOptions,
overrides.interface,
Expand Down Expand Up @@ -227,7 +227,7 @@ export default util.createRule<Options, MessageIds>({
const opts = isSingleLine ? typeOpts.singleline : typeOpts.multiline;

members.forEach((member, index) => {
checkLastToken(member, opts || {}, index === members.length - 1);
checkLastToken(member, opts ?? {}, index === members.length - 1);
});
}

Expand Down
8 changes: 4 additions & 4 deletions packages/eslint-plugin/src/rules/member-ordering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,28 +388,28 @@ export default util.createRule<Options, MessageIds>({
ClassDeclaration(node): void {
validateMembersOrder(
node.body.body,
options.classes || options.default!,
options.classes ?? options.default!,
true,
);
},
ClassExpression(node): void {
validateMembersOrder(
node.body.body,
options.classExpressions || options.default!,
options.classExpressions ?? options.default!,
true,
);
},
TSInterfaceDeclaration(node): void {
validateMembersOrder(
node.body.body,
options.interfaces || options.default!,
options.interfaces ?? options.default!,
false,
);
},
TSTypeLiteral(node): void {
validateMembersOrder(
node.members,
options.typeLiterals || options.default!,
options.typeLiterals ?? options.default!,
false,
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default util.createRule({
}

function symbolIsNamespaceInScope(symbol: ts.Symbol): boolean {
const symbolDeclarations = symbol.getDeclarations() || [];
const symbolDeclarations = symbol.getDeclarations() ?? [];

if (
symbolDeclarations.some(decl =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default util.createRule<Options, MessageIds>({
? new RegExp(userOptions.ignoredNamesRegex)
: null,
ignoreArgsIfArgsAfterAreUsed:
userOptions.ignoreArgsIfArgsAfterAreUsed || false,
userOptions.ignoreArgsIfArgsAfterAreUsed ?? false,
};

function handleIdentifier(identifier: ts.Identifier): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ export default util.createRule<Options, MessageIds>({
node.async &&
isOpeningParenToken(sourceCode.getFirstToken(node, { skip: 1 })!)
) {
return overrideConfig.asyncArrow || baseConfig;
return overrideConfig.asyncArrow ?? baseConfig;
}
} else if (isNamedFunction(node)) {
return overrideConfig.named || baseConfig;
return overrideConfig.named ?? baseConfig;

// `generator-star-spacing` should warn anonymous generators. E.g. `function* () {}`
} else if (!node.generator) {
return overrideConfig.anonymous || baseConfig;
return overrideConfig.anonymous ?? baseConfig;
}

return 'ignore';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default util.createRule<Options, MessageIds>({
const punctuators = [':', '=>'];
const sourceCode = context.getSourceCode();

const overrides = options!.overrides || { colon: {}, arrow: {} };
const overrides = options?.overrides ?? { colon: {}, arrow: {} };

const colonOptions = Object.assign(
{},
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/unified-signatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ export default util.createRule({
key?: string,
containingNode?: ContainingNode,
): void {
key = key || getOverloadKey(signature);
key = key ?? getOverloadKey(signature);
if (
currentScope &&
(containingNode || signature).parent === currentScope.parent
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export function getConstrainedTypeAtLocation(
const nodeType = checker.getTypeAtLocation(node);
const constrained = checker.getBaseConstraintOfType(nodeType);

return constrained || nodeType;
return constrained ?? nodeType;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/tests/rules/no-explicit-any.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ const test = <T extends Partial<never>>() => {};
suggestions: e.suggestions ?? suggestions(testCase.code),
})),
});
const options = testCase.options || [];
const options = testCase.options ?? [];
const code = `// fixToUnknown: true\n${testCase.code}`;
acc.push({
code,
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/src/analyze-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ export function analyzeScope(
parserOptions.ecmaFeatures.globalReturn) === true,
impliedStrict: false,
sourceType: parserOptions.sourceType,
ecmaVersion: parserOptions.ecmaVersion || 2018,
ecmaVersion: parserOptions.ecmaVersion ?? 2018,
childVisitorKeys,
fallback,
};
Expand Down
18 changes: 9 additions & 9 deletions packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class Converter {
this.allowPattern = allowPattern;
}

const result = this.convertNode(node as TSNode, parent || node.parent);
const result = this.convertNode(node as TSNode, parent ?? node.parent);

this.registerTSNodeInNodeMap(node, result);

Expand Down Expand Up @@ -1193,12 +1193,12 @@ export class Converter {
if (node.dotDotDotToken) {
result = this.createNode<TSESTree.RestElement>(node, {
type: AST_NODE_TYPES.RestElement,
argument: this.convertChild(node.propertyName || node.name),
argument: this.convertChild(node.propertyName ?? node.name),
});
} else {
result = this.createNode<TSESTree.Property>(node, {
type: AST_NODE_TYPES.Property,
key: this.convertChild(node.propertyName || node.name),
key: this.convertChild(node.propertyName ?? node.name),
value: this.convertChild(node.name),
computed: Boolean(
node.propertyName &&
Expand Down Expand Up @@ -1387,7 +1387,7 @@ export class Converter {
if (node.modifiers) {
return this.createNode<TSESTree.TSParameterProperty>(node, {
type: AST_NODE_TYPES.TSParameterProperty,
accessibility: getTSNodeAccessibility(node) || undefined,
accessibility: getTSNodeAccessibility(node) ?? undefined,
readonly:
hasModifier(SyntaxKind.ReadonlyKeyword, node) || undefined,
static: hasModifier(SyntaxKind.StaticKeyword, node) || undefined,
Expand All @@ -1402,7 +1402,7 @@ export class Converter {

case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression: {
const heritageClauses = node.heritageClauses || [];
const heritageClauses = node.heritageClauses ?? [];
const classNodeType =
node.kind === SyntaxKind.ClassDeclaration
? AST_NODE_TYPES.ClassDeclaration
Expand Down Expand Up @@ -1533,7 +1533,7 @@ export class Converter {
return this.createNode<TSESTree.ImportSpecifier>(node, {
type: AST_NODE_TYPES.ImportSpecifier,
local: this.convertChild(node.name),
imported: this.convertChild(node.propertyName || node.name),
imported: this.convertChild(node.propertyName ?? node.name),
});

case SyntaxKind.ImportClause:
Expand Down Expand Up @@ -1563,7 +1563,7 @@ export class Converter {
case SyntaxKind.ExportSpecifier:
return this.createNode<TSESTree.ExportSpecifier>(node, {
type: AST_NODE_TYPES.ExportSpecifier,
local: this.convertChild(node.propertyName || node.name),
local: this.convertChild(node.propertyName ?? node.name),
exported: this.convertChild(node.name),
});

Expand All @@ -1584,7 +1584,7 @@ export class Converter {

case SyntaxKind.PrefixUnaryExpression:
case SyntaxKind.PostfixUnaryExpression: {
const operator = (getTextForTokenKind(node.operator) || '') as any;
const operator = (getTextForTokenKind(node.operator) ?? '') as any;
/**
* ESTree uses UpdateExpression for ++/--
*/
Expand Down Expand Up @@ -2375,7 +2375,7 @@ export class Converter {
}

case SyntaxKind.InterfaceDeclaration: {
const interfaceHeritageClauses = node.heritageClauses || [];
const interfaceHeritageClauses = node.heritageClauses ?? [];
const result = this.createNode<TSESTree.TSInterfaceDeclaration>(node, {
type: AST_NODE_TYPES.TSInterfaceDeclaration,
body: this.createNode<TSESTree.TSInterfaceBody>(node, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ function getProgramsForProjects(
log('Found existing program for file. %s', filePath);

updatedProgram =
updatedProgram || existingWatch.getProgram().getProgram();
updatedProgram ?? existingWatch.getProgram().getProgram();
// sets parent pointers in source files
updatedProgram.getTypeChecker();

Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-estree/src/simple-traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function getVisitorKeysForNode(
node: TSESTree.Node,
): readonly string[] {
const keys = allVisitorKeys[node.type];
return keys || [];
return keys ?? [];
}

interface SimpleTraverseOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ class FixturesTester {
}
}

const ignore = config.ignore || [];
const fileType = config.fileType || 'js';
const ignoreSourceType = config.ignoreSourceType || [];
const ignore = config.ignore ?? [];
const fileType = config.fileType ?? 'js';
const ignoreSourceType = config.ignoreSourceType ?? [];
const jsx = isJSXFileType(fileType);

/**
Expand Down