Skip to content

Commit

Permalink
refactor: no unnecessary nullish coalescing
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode committed Oct 18, 2023
1 parent d27927b commit ad8c60c
Show file tree
Hide file tree
Showing 71 changed files with 111 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class TuiInputCardGroupedComponent
}

get hasCleaner(): boolean {
return !!this.value?.card?.trim() && !this.readOnly && !this.computedDisabled;
return !!this.value?.card.trim() && !this.readOnly && !this.computedDisabled;
}

get hasDropdown(): boolean {
Expand All @@ -201,7 +201,7 @@ export class TuiInputCardGroupedComponent
}

get hasCardNumber(): boolean {
return !!this.value?.card?.trim();
return !!this.value?.card.trim();
}

get idCard(): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ describe(`InputCardGrouped`, () => {

function expectCardOutlet(): boolean {
return (
fixture.componentRef.location?.nativeElement?.querySelectorAll(
fixture.componentRef.location.nativeElement?.querySelectorAll(
`.t-icon-outlet`,
)?.length === 1 || false
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe(`TuiDocCodeComponent`, () => {
});

async function waitHighlightJsParseContent(): Promise<void> {
await fixture?.whenStable();
await fixture.whenStable();
fixture.detectChanges();
await new Promise(resolve => {
setTimeout(resolve, 100);
Expand Down
4 changes: 2 additions & 2 deletions projects/addon-mobile/components/app-bar/app-bar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export class TuiAppBarComponent {
() =>
2 *
Math.max(
this.side.first?.nativeElement.clientWidth,
this.side.last?.nativeElement.clientWidth,
this.side.first.nativeElement.clientWidth,
this.side.last.nativeElement.clientWidth,
),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class TuiSheetCloseDirective {
() => this.scroll$.pipe(startWith(this.el.nativeElement.scrollTop)),
tuiIsFalsy,
),
filter(y => this.sheet.item?.closeable && this.shouldClose(y)),
filter(y => this.sheet.item.closeable && this.shouldClose(y)),
distinctUntilChanged(),
tuiZonefull(this.zone),
),
Expand Down
2 changes: 1 addition & 1 deletion projects/addon-table/components/table/th/th.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ export class TuiThComponent<T extends Partial<Record<keyof T, any>>> {
}

private get isCurrentAndAscDirection(): boolean {
return this.sorter === this.table?.sorter && this.table?.direction === -1;
return this.sorter === this.table?.sorter && this.table.direction === -1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe(`TableBarsHost`, () => {
component = testComponent.component;
service = TestBed.inject(TuiTableBarsService);
(service.bar$ as any)._events = [];
subscription?.unsubscribe();
subscription.unsubscribe();
});

function getBar(): DebugElement {
Expand Down
2 changes: 1 addition & 1 deletion projects/cdk/classes/test/validation-error.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ describe(`TuiValidationError`, () => {
const error = new TuiValidationError(`content`, {test: `context`});

expect(error.message).toBe(`content`);
expect(error.context?.test).toBe(`context`);
expect(error.context.test).toBe(`context`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const FAKE_HISTORY_STATE = {label: 'ignoreMe'} as const;
const isFakeHistoryState = (
historyState: Record<string, unknown>,
): historyState is typeof FAKE_HISTORY_STATE =>
historyState?.label === FAKE_HISTORY_STATE.label;
historyState.label === FAKE_HISTORY_STATE.label;

@Component({
selector: 'tui-dialog-host',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe(`TuiActiveZoneDirective`, () => {
}

onClick(element: HTMLInputElement): void {
element?.focus();
element.focus();
}
}

Expand Down
2 changes: 1 addition & 1 deletion projects/cdk/directives/autofilled/autofilled.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class TuiAutofilledDirective {
@HostListener('transitionstart', ['$event'])
transitionStartHandler({propertyName, target}: TransitionEvent): void {
const matchedAutofill =
propertyName.includes('box-shadow') && (target as Element)?.matches('input');
propertyName.includes('box-shadow') && (target as Element).matches('input');

if (matchedAutofill) {
this.autofilled = !this.autofilled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function getInput<T>(fixture: ComponentFixture<T>): HTMLInputElement {
}

function getInputClassList<T>(fixture: ComponentFixture<T>): string[] {
return Array.from(getInput<T>(fixture)?.classList || []).filter(
return Array.from(getInput<T>(fixture).classList || []).filter(
className => !className.includes(`ng-`),
);
}
2 changes: 1 addition & 1 deletion projects/cdk/directives/overscroll/overscroll.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class TuiOverscrollDirective {
if (
!cancelable ||
!tuiIsElement(target) ||
(target as HTMLInputElement)?.type === 'range'
(target as HTMLInputElement).type === 'range'
) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion projects/cdk/observables/is-observed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ type SubjectRxJS6<T> = Subject<T> & {observers: Array<Observer<T>>};
export function tuiIsObserved<T>(observable: Subject<T>): boolean {
return `observed` in observable
? (observable as SubjectRxJS7plus<T>).observed
: !!(observable as SubjectRxJS6<T>)?.observers?.length;
: !!(observable as SubjectRxJS6<T>).observers.length;
}
2 changes: 1 addition & 1 deletion projects/cdk/schematics/ng-update/steps/replace-const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function replaceConst({from, to}: ReplacementConst): void {
to.moduleSpecifier,
);
} else {
ref?.replaceWithText(to.name);
ref.replaceWithText(to.name);
}
});
}
2 changes: 1 addition & 1 deletion projects/cdk/schematics/ng-update/steps/replace-enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function replaceEnumWithString(
if (Node.isTypeReferenceNode(parent) && !keepAsType) {
const declaration = parent.getParent() as VariableDeclaration;

declaration.removeType?.();
declaration.removeType();
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function replaceProperties(
const statement = parent.getParent();
const identifier = statement.getChildrenOfKind(SyntaxKind.Identifier)[0];

identifier?.findReferencesAsNodes().forEach(ref => {
identifier.findReferencesAsNodes().forEach(ref => {
let parent = ref.getParent();

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function migrateExpand({
attr => attr.name === `tuiexpandcontent`,
);

const insertTo = templateElement?.sourceCodeLocation?.startTag.endOffset;
const insertTo = templateElement.sourceCodeLocation?.startTag.endOffset;

if (!insertTo || tuiExpandAttr) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ function replaceIdentifierReferences(
}

if (parent?.getText().startsWith(`this`)) {
parent = parent?.getParent();
parent = parent.getParent();
}

if (Node.isPropertyAccessExpression(parent)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function insertPolymorpheus({
{{ text }}
</ng-container>`;

const insertTo = element.sourceCodeLocation?.startTag?.endOffset;
const insertTo = element.sourceCodeLocation?.startTag.endOffset;

if (insertTo) {
recorder.insertRight(insertTo + templateOffset, content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ export function migrateTaigaProprietaryIcons(options: TuiSchema): Rule {
return;
}

if (Array.isArray(targetOptions?.assets)) {
if (Array.isArray(targetOptions.assets)) {
const tdsSrc = `@taiga-ui/proprietary-tds-icons/src`;
const hasIcons = (targetOptions.assets as Asset[]).find(
asset =>
tuiIsString(asset)
? asset.includes(tdsSrc)
: asset?.input?.includes(tdsSrc),
: asset.input.includes(tdsSrc),
);

if (hasIcons) {
Expand Down
20 changes: 8 additions & 12 deletions projects/cdk/schematics/ng-update/v3/steps/migrate-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function replaceBreadcrumbs({
elements.forEach(element => {
const itemsAttr = element.attrs.find(attr => attr.name === `[items]`);
const itemsValue = itemsAttr?.value;
const insertTo = element?.sourceCodeLocation?.startTag.endOffset;
const insertTo = element.sourceCodeLocation?.startTag.endOffset;

if (!itemsValue || !insertTo) {
return;
Expand Down Expand Up @@ -346,9 +346,8 @@ function migrateTuiHideSelectedPipe({
const template = getTemplateFromTemplateResource(resource, fileSystem);
const templateOffset = getTemplateOffset(resource);

const elementsWithPipe = findElementsInTemplateByFn(
template,
el => el.attrs?.some(attr => attr.value.match(HIDE_SELECTED_PIPE_WITH_ARGS_REG)),
const elementsWithPipe = findElementsInTemplateByFn(template, el =>
el.attrs.some(attr => attr.value.match(HIDE_SELECTED_PIPE_WITH_ARGS_REG)),
);

elementsWithPipe.forEach(el => {
Expand Down Expand Up @@ -387,14 +386,11 @@ function migrateBinaryAttributes({
const templateOffset = getTemplateOffset(resource);

TRUTHY_BOOLEAN_INPUT_TO_HTML_BINARY_ATTRIBUTE.forEach(attrName => {
const elements = findElementsInTemplateByFn(
template,
el =>
el.attrs?.some(
attr =>
attr.value === `true` &&
attr.name.includes(attrName.toLowerCase()),
),
const elements = findElementsInTemplateByFn(template, el =>
el.attrs.some(
attr =>
attr.value === `true` && attr.name.includes(attrName.toLowerCase()),
),
);

elements.forEach(el => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function insertTuiTextfieldInput({
? `<textarea tuiTextfield ${newAttrs}></textarea> `
: `<input tuiTextfield ${newAttrs}/> `;

const insertTo = element.sourceCodeLocation?.endTag?.startOffset;
const insertTo = element.sourceCodeLocation?.endTag.startOffset;

if (insertTo) {
recorder.insertRight(insertTo + templateOffset, content);
Expand Down Expand Up @@ -166,5 +166,5 @@ const excludedAttrs: Record<string, string[]> = {
};

function exclusion({tagName}: Element, attrName: string): boolean {
return excludedAttrs[tagName]?.includes(attrName);
return excludedAttrs[tagName].includes(attrName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ function replaceDeprecatedFunction(): void {
const parent = ref.getParent();

if (Node.isImportSpecifier(parent) || Node.isCallExpression(parent)) {
parent?.replaceWithText(
parent.replaceWithText(
parent
?.getText({includeJsDocComments: false})
.getText({includeJsDocComments: false})
.trim()
.replace(from, to ?? from),
);
Expand All @@ -64,7 +64,7 @@ function replacePadStart(references: Node[]): void {

parent.replaceWithText(
`${targetString.getText()}.padStart(${length.getText()}, ${
pad?.getText() ?? `" "`
pad.getText() ?? `" "`
})`,
);
}
Expand All @@ -83,7 +83,7 @@ function replaceNativeFocused(references: Node[]): void {
const setFocused = !focusedArg || focusedArg.getText() === `true`;

const focus = `${targetString.getText()}.focus(${
preventScroll?.getText() ? `{preventScroll: true}` : ``
preventScroll.getText() ? `{preventScroll: true}` : ``
})`;
const blur = `${targetString.getText()}.blur()`;

Expand Down
4 changes: 2 additions & 2 deletions projects/cdk/schematics/utils/angular-json-manipulations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ export async function isInvalidAngularJson(tree: Tree): Promise<boolean> {
}

function hasTaigaIcons(assets: Asset[]): boolean {
return !!assets?.find(asset =>
return !!assets.find(asset =>
tuiIsString(asset)
? asset.includes(`taiga-ui`)
: asset?.input?.includes(`taiga-ui`),
: asset.input.includes(`taiga-ui`),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function getProjectTargetOptions(
project: workspaces.ProjectDefinition,
buildTarget: string,
): Record<string, JsonArray | JsonObject | boolean | number | string | null | undefined> {
const buildTargetObject = project.targets?.get(buildTarget);
const buildTargetObject = project.targets.get(buildTarget);

if (buildTargetObject?.options) {
return buildTargetObject.options;
Expand Down
2 changes: 1 addition & 1 deletion projects/cdk/schematics/utils/push-to-array-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function pushToObjectArrayProperty(
}

if (forceToArray && !Node.isArrayLiteralExpression(property.getInitializer())) {
property.setInitializer(`[${property?.getInitializer()?.getText()}]`);
property.setInitializer(`[${property.getInitializer()?.getText()}]`);
}

const importsInitializer = property.getInitializer();
Expand Down
2 changes: 1 addition & 1 deletion projects/cdk/schematics/utils/replace-tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function replaceTag(
addAttributes: string[] = [],
): void {
const startTagOffset = sourceCodeLocation?.startTag.startOffset;
const endTagOffset = sourceCodeLocation?.endTag?.startOffset;
const endTagOffset = sourceCodeLocation?.endTag.startOffset;

if (endTagOffset) {
recorder.remove(endTagOffset + templateOffset + END_TAG_OFFSET, from.length);
Expand Down
9 changes: 4 additions & 5 deletions projects/cdk/schematics/utils/templates/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ export function findElementsWithAttribute(
html: string,
attributeName: string,
): Element[] {
return findElementsInTemplateByFn(
html,
el => el.attrs?.some(attr => attr.name === attributeName.toLowerCase()),
return findElementsInTemplateByFn(html, el =>
el.attrs.some(attr => attr.name === attributeName.toLowerCase()),
);
}

Expand All @@ -74,7 +73,7 @@ export function findElementsWithAttributeOnTag(
return findElementsInTemplateByFn(
html,
el =>
el.attrs?.some(attr =>
el.attrs.some(attr =>
attributeNames.map(name => name.toLowerCase()).includes(attr.name),
) &&
(tagNames.includes(el.tagName) || !tagNames.length),
Expand Down Expand Up @@ -123,7 +122,7 @@ export function findAttributeOnElementWithAttrs(

/** Shorthand function that checks if the specified element contains the given attribute. */
export function hasElementAttribute(element: Element, attributeName: string): boolean {
return element.attrs?.some(attr => attr.name === attributeName.toLowerCase());
return element.attrs.some(attr => attr.name === attributeName.toLowerCase());
}

/** Gets the start offset of the given attribute from a Parse5 element. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function decoratorToTemplateResource(decorator: Decorator): TemplateResource | n

if (templateUrl) {
const templatePath = path.parse(
templateUrl?.getInitializer()?.getText().replace(/['"`]/g, ``) || ``,
templateUrl.getInitializer()?.getText().replace(/['"`]/g, ``) || ``,
);

return {
Expand Down
2 changes: 1 addition & 1 deletion projects/cdk/services/scroll.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class TuiScrollService {

return observable.pipe(
tap(([scrollTop, scrollLeft]) => {
elementOrWindow.scrollTo?.(scrollLeft, scrollTop);
elementOrWindow.scrollTo(scrollLeft, scrollTop);
}),
);
}
Expand Down
2 changes: 1 addition & 1 deletion projects/cdk/tokens/is-firefox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import {tuiCreateTokenFromFactory} from '@taiga-ui/cdk/utils';
export const TUI_IS_FIREFOX = tuiCreateTokenFromFactory(
() =>
typeof (inject(WINDOW) as Window & {mozCancelFullScreen: unknown})
?.mozCancelFullScreen !== `undefined`,
.mozCancelFullScreen !== `undefined`,
);
2 changes: 1 addition & 1 deletion projects/cdk/tokens/is-webkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import {tuiCreateTokenFromFactory} from '@taiga-ui/cdk/utils';
export const TUI_IS_WEBKIT = tuiCreateTokenFromFactory(
() =>
!!(inject(WINDOW) as unknown as {webkitConvertPointFromNodeToPage?: unknown})
?.webkitConvertPointFromNodeToPage,
.webkitConvertPointFromNodeToPage,
);
Loading

0 comments on commit ad8c60c

Please sign in to comment.