Skip to content

chore(typescript-estree): fixed no-unnecessary-condition complaints #7835

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function createDefaultProgram(
parseSettings.filePath || 'unnamed file',
);

if (parseSettings.projects?.length !== 1) {
if (parseSettings.projects.length !== 1) {
return undefined;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function createProjectProgram(
];
let hasMatchedAnError = false;

const extraFileExtensions = parseSettings.extraFileExtensions || [];
const { extraFileExtensions } = parseSettings;

extraFileExtensions.forEach(extraExtension => {
if (!extraExtension.startsWith('.')) {
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript-estree/src/getModifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function getModifiers(

return (
// @ts-expect-error intentional fallback for older TS versions
(node.modifiers as ts.Modifier[])?.filter(
(node.modifiers as ts.Modifier[] | undefined)?.filter(
(m): m is ts.Modifier => !ts.isDecorator(m),
)
);
Expand All @@ -52,6 +52,6 @@ export function getDecorators(

return (
// @ts-expect-error intentional fallback for older TS versions
(node.decorators as ts.Node[])?.filter(ts.isDecorator)
(node.decorators as ts.Node[] | undefined)?.filter(ts.isDecorator)
);
}
24 changes: 9 additions & 15 deletions packages/typescript-estree/src/node-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,12 @@ export function findFirstMatchingAncestor(
node: ts.Node,
predicate: (node: ts.Node) => boolean,
): ts.Node | undefined {
while (node) {
if (predicate(node)) {
return node;
let current: ts.Node | undefined = node;
while (current) {
if (predicate(current)) {
return current;
}
node = node.parent;
current = current.parent;
}
return undefined;
}
Expand Down Expand Up @@ -482,9 +483,7 @@ export function isComputedProperty(
export function isOptional(node: {
questionToken?: ts.QuestionToken;
}): boolean {
return node.questionToken
? node.questionToken.kind === SyntaxKind.QuestionToken
: false;
return !!node.questionToken;
}

/**
Expand Down Expand Up @@ -677,11 +676,7 @@ export function convertTokens(ast: ts.SourceFile): TSESTree.Token[] {
}

if (isToken(node) && node.kind !== SyntaxKind.EndOfFileToken) {
const converted = convertToken(node, ast);

if (converted) {
result.push(converted);
}
result.push(convertToken(node, ast));
} else {
node.getChildren(ast).forEach(walk);
}
Expand Down Expand Up @@ -943,11 +938,10 @@ export function getNamespaceModifiers(
let moduleDeclaration = node;
while (
(!modifiers || modifiers.length === 0) &&
ts.isModuleDeclaration(moduleDeclaration.parent) &&
moduleDeclaration.parent.name
ts.isModuleDeclaration(moduleDeclaration.parent)
) {
const parentModifiers = getModifiers(moduleDeclaration.parent);
if (parentModifiers && parentModifiers?.length > 0) {
if (parentModifiers?.length) {
modifiers = parentModifiers;
}
moduleDeclaration = moduleDeclaration.parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function inferSingleRun(options: TSESTreeOptions | undefined): boolean {
options?.project == null ||
// programs passed via options means the user should be managing the programs, so we shouldn't
// be creating our own single-run programs accidentally
options?.programs != null
options.programs != null
) {
return false;
}
Expand All @@ -34,7 +34,7 @@ export function inferSingleRun(options: TSESTreeOptions | undefined): boolean {
}

// Currently behind a flag while we gather real-world feedback
if (options?.allowAutomaticSingleRunInference) {
if (options.allowAutomaticSingleRunInference) {
if (
// Default to single runs for CI processes. CI=true is set by most CI providers by default.
process.env.CI === 'true' ||
Expand Down
33 changes: 15 additions & 18 deletions packages/typescript-estree/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ function parseAndGenerateServices<T extends TSESTreeOptions = TSESTreeOptions>(
if (
parseSettings.singleRun &&
!parseSettings.programs &&
parseSettings.projects?.length > 0
parseSettings.projects.length > 0
) {
parseSettings.programs = {
*[Symbol.iterator](): Iterator<ts.Program> {
Expand All @@ -207,25 +207,22 @@ function parseAndGenerateServices<T extends TSESTreeOptions = TSESTreeOptions>(
* Generate a full ts.Program or offer provided instances in order to be able to provide parser services, such as type-checking
*/
const hasFullTypeInformation =
parseSettings.programs != null || parseSettings.projects?.length > 0;
parseSettings.programs != null || parseSettings.projects.length > 0;

if (options !== undefined) {
if (
typeof options.errorOnTypeScriptSyntacticAndSemanticIssues ===
'boolean' &&
options.errorOnTypeScriptSyntacticAndSemanticIssues
) {
parseSettings.errorOnTypeScriptSyntacticAndSemanticIssues = true;
}
if (
typeof options.errorOnTypeScriptSyntacticAndSemanticIssues === 'boolean' &&
options.errorOnTypeScriptSyntacticAndSemanticIssues
) {
parseSettings.errorOnTypeScriptSyntacticAndSemanticIssues = true;
}

if (
parseSettings.errorOnTypeScriptSyntacticAndSemanticIssues &&
!hasFullTypeInformation
) {
throw new Error(
'Cannot calculate TypeScript semantic issues without a valid project.',
);
}
if (
parseSettings.errorOnTypeScriptSyntacticAndSemanticIssues &&
!hasFullTypeInformation
) {
throw new Error(
'Cannot calculate TypeScript semantic issues without a valid project.',
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-estree/tests/lib/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('parseAndGenerateServices', () => {
});

expect(
parseResult.services.esTreeNodeToTSNodeMap?.has(
parseResult.services.esTreeNodeToTSNodeMap.has(
parseResult.ast.body[0],
),
).toBe(setting);
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript-estree/tests/lib/semanticInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,11 @@ describe('semanticInfo', () => {
expect(boundName.name).toBe('x');

const tsBoundName =
parseResult.services.esTreeNodeToTSNodeMap?.get(boundName);
parseResult.services.esTreeNodeToTSNodeMap.get(boundName);
expectToBeDefined(tsBoundName);
expect(tsBoundName).toBeDefined();

expect(parseResult.services.tsNodeToESTreeNodeMap?.get(tsBoundName)).toBe(
expect(parseResult.services.tsNodeToESTreeNodeMap.get(tsBoundName)).toBe(
boundName,
);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-estree/tools/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function omitDeep<T = UnknownObject>(
> = {},
): UnknownObject {
function shouldOmit(keyName: string, val: unknown): boolean {
if (keysToOmit?.length) {
if (keysToOmit.length) {
return keysToOmit.some(
keyConfig => keyConfig.key === keyName && keyConfig.predicate(val),
);
Expand Down