Skip to content

Commit

Permalink
Merge pull request #240 from qonto/eslint-ts-config
Browse files Browse the repository at this point in the history
chore(lint): add specific set of rules for TypeScript linting and run lint with --fix option
  • Loading branch information
vscav authored Oct 16, 2023
2 parents 9249662 + 9e7f3ce commit d8ac234
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 29 deletions.
56 changes: 55 additions & 1 deletion ember-lottie/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,61 @@ module.exports = {
'plugin:@typescript-eslint/recommended',
],
rules: {
// Add any custom rules here
// https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/recommended.ts
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-non-null-assertion': 'error',
'@typescript-eslint/no-unused-vars': 'error',
// https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/strict.ts
'@typescript-eslint/array-type': [
'error',
{
default: 'array',
readonly: 'array',
},
],
'@typescript-eslint/ban-tslint-comment': 'error',
'@typescript-eslint/class-literal-property-style': 'error',
'@typescript-eslint/consistent-generic-constructors': 'error',
'@typescript-eslint/consistent-indexed-object-style': 'error',
'@typescript-eslint/consistent-type-assertions': 'error',
'@typescript-eslint/consistent-type-definitions': 'error',
'@typescript-eslint/consistent-type-imports': 'error',
'@typescript-eslint/explicit-function-return-type': 'error',
'@typescript-eslint/explicit-member-accessibility': [
'error',
{
accessibility: 'no-public',
},
],
'@typescript-eslint/explicit-module-boundary-types': 'error',
'@typescript-eslint/member-delimiter-style': 'error',
'@typescript-eslint/member-ordering': 'error',
'@typescript-eslint/method-signature-style': 'error',
'@typescript-eslint/no-confusing-non-null-assertion': 'error',
'@typescript-eslint/no-duplicate-enum-values': 'error',
'@typescript-eslint/no-dynamic-delete': 'error',
'@typescript-eslint/no-extraneous-class': 'error',
'@typescript-eslint/no-import-type-side-effects': 'error',
'@typescript-eslint/no-invalid-void-type': 'error',
'@typescript-eslint/no-non-null-asserted-nullish-coalescing': 'error',
'@typescript-eslint/no-require-imports': 'error',
'@typescript-eslint/no-type-alias': [
'error',
{
allowGenerics: 'always',
},
],
'@typescript-eslint/no-unsafe-declaration-merging': 'error',
'@typescript-eslint/parameter-properties': 'error',
'@typescript-eslint/prefer-enum-initializers': 'error',
'@typescript-eslint/prefer-for-of': 'error',
'@typescript-eslint/prefer-function-type': 'error',
'@typescript-eslint/prefer-literal-enum-member': 'error',
'@typescript-eslint/prefer-ts-expect-error': 'error',
'@typescript-eslint/sort-type-constituents': 'error',
'@typescript-eslint/type-annotation-spacing': 'error',
'@typescript-eslint/typedef': 'error',
'@typescript-eslint/unified-signatures': 'error',
},
},
// node files
Expand Down
24 changes: 12 additions & 12 deletions ember-lottie/src/components/lottie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { action } from '@ember/object';
import { buildWaiter } from '@ember/test-waiters';
import Ember from 'ember';

import { AnimationItem, LottiePlayer } from 'lottie-web';
import type { AnimationItem, LottiePlayer } from 'lottie-web';
import window from 'ember-window-mock';

const waiter = buildWaiter('ember-lottie:lottie-waiter');
Expand Down Expand Up @@ -53,11 +53,11 @@ export default class LottieComponent extends Component<LottieSignature> {
private animation?: AnimationItem;
private mediaQuery = window.matchMedia?.('(prefers-reduced-motion: reduce)');

get autoplay() {
get autoplay(): boolean {
return this.canAutoplay ?? true;
}

get canAutoplay() {
get canAutoplay(): boolean | undefined {
const prefersReducedMotion = this.mediaQuery?.matches;
return !prefersReducedMotion && this.args.autoplay;
}
Expand Down Expand Up @@ -119,7 +119,15 @@ export default class LottieComponent extends Component<LottieSignature> {
);
}

willDestroy() {
@action
private handleReducedMotionPreferenceChange(): void {
const prefersReducedMotion = this.mediaQuery?.matches;
if (prefersReducedMotion) {
this.animation?.stop();
}
}

willDestroy(): void {
super.willDestroy();

this.mediaQuery?.removeEventListener(
Expand All @@ -132,14 +140,6 @@ export default class LottieComponent extends Component<LottieSignature> {
}
}

@action
private handleReducedMotionPreferenceChange() {
const prefersReducedMotion = this.mediaQuery?.matches;
if (prefersReducedMotion) {
this.animation?.stop();
}
}

private async loadLottie(): Promise<LottiePlayer> {
const lottieModule = await import('lottie-web');
return lottieModule.default;
Expand Down
2 changes: 1 addition & 1 deletion ember-lottie/src/template-registry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import LottieComponent from './components/lottie';
import type LottieComponent from './components/lottie';

export default interface Registry {
Lottie: typeof LottieComponent;
Expand Down
2 changes: 1 addition & 1 deletion ember-lottie/src/unpublished-development-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'ember-source/types';
import 'ember-source/types/preview';

declare class RenderModifier<
Args extends Array<unknown> = Array<unknown>,
Args extends unknown[] = unknown[],
> extends Modifier<{
Element: HTMLElement;
Args: { Positional: [(element: HTMLElement, args: Args) => void, ...Args] };
Expand Down
58 changes: 57 additions & 1 deletion test-app/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,63 @@ module.exports = {
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
],
rules: {},
rules: {
// https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/recommended.ts
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-non-null-assertion': 'error',
'@typescript-eslint/no-unused-vars': 'error',
// https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/strict.ts
'@typescript-eslint/array-type': [
'error',
{
default: 'array',
readonly: 'array',
},
],
'@typescript-eslint/ban-tslint-comment': 'error',
'@typescript-eslint/class-literal-property-style': 'error',
'@typescript-eslint/consistent-generic-constructors': 'error',
'@typescript-eslint/consistent-indexed-object-style': 'error',
'@typescript-eslint/consistent-type-assertions': 'error',
'@typescript-eslint/consistent-type-definitions': 'error',
'@typescript-eslint/consistent-type-imports': 'error',
'@typescript-eslint/explicit-function-return-type': 'error',
'@typescript-eslint/explicit-member-accessibility': [
'error',
{
accessibility: 'no-public',
},
],
'@typescript-eslint/explicit-module-boundary-types': 'error',
'@typescript-eslint/member-delimiter-style': 'error',
'@typescript-eslint/member-ordering': 'error',
'@typescript-eslint/method-signature-style': 'error',
'@typescript-eslint/no-confusing-non-null-assertion': 'error',
'@typescript-eslint/no-duplicate-enum-values': 'error',
'@typescript-eslint/no-dynamic-delete': 'error',
'@typescript-eslint/no-extraneous-class': 'error',
'@typescript-eslint/no-import-type-side-effects': 'error',
'@typescript-eslint/no-invalid-void-type': 'error',
'@typescript-eslint/no-non-null-asserted-nullish-coalescing': 'error',
'@typescript-eslint/no-require-imports': 'error',
'@typescript-eslint/no-type-alias': [
'error',
{
allowGenerics: 'always',
},
],
'@typescript-eslint/no-unsafe-declaration-merging': 'error',
'@typescript-eslint/parameter-properties': 'error',
'@typescript-eslint/prefer-enum-initializers': 'error',
'@typescript-eslint/prefer-for-of': 'error',
'@typescript-eslint/prefer-function-type': 'error',
'@typescript-eslint/prefer-literal-enum-member': 'error',
'@typescript-eslint/prefer-ts-expect-error': 'error',
'@typescript-eslint/sort-type-constituents': 'error',
'@typescript-eslint/type-annotation-spacing': 'error',
'@typescript-eslint/typedef': 'error',
'@typescript-eslint/unified-signatures': 'error',
},
},
// node files
{
Expand Down
2 changes: 1 addition & 1 deletion test-app/app/config/environment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare const config: {
environment: string;
modulePrefix: string;
podModulePrefix: string;
locationType: 'history' | 'hash' | 'none' | 'auto';
locationType: 'auto' | 'hash' | 'history' | 'none';
rootURL: string;
APP: Record<string, unknown>;
};
Expand Down
14 changes: 10 additions & 4 deletions test-app/tests/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import type { SetupTestOptions } from 'ember-qunit';
import {
setupApplicationTest as upstreamSetupApplicationTest,
setupRenderingTest as upstreamSetupRenderingTest,
setupTest as upstreamSetupTest,
SetupTestOptions,
} from 'ember-qunit';

// This file exists to provide wrappers around ember-qunit's / ember-mocha's
// test setup functions. This way, you can easily extend the setup that is
// needed per test type.

function setupApplicationTest(hooks: NestedHooks, options?: SetupTestOptions) {
function setupApplicationTest(
hooks: NestedHooks,
options: SetupTestOptions,
): void {
upstreamSetupApplicationTest(hooks, options);

// Additional setup for application tests can be done here.
Expand All @@ -28,13 +31,16 @@ function setupApplicationTest(hooks: NestedHooks, options?: SetupTestOptions) {
// setupMirage(hooks); // ember-cli-mirage
}

function setupRenderingTest(hooks: NestedHooks, options?: SetupTestOptions) {
function setupRenderingTest(
hooks: NestedHooks,
options: SetupTestOptions,
): void {
upstreamSetupRenderingTest(hooks, options);

// Additional setup for rendering tests can be done here.
}

function setupTest(hooks: NestedHooks, options?: SetupTestOptions) {
function setupTest(hooks: NestedHooks, options: SetupTestOptions): void {
upstreamSetupTest(hooks, options);

// Additional setup for unit tests can be done here.
Expand Down
14 changes: 7 additions & 7 deletions test-app/tests/integration/components/lottie-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module('Integration | Component | lottie', function (hooks) {
});

test('it renders', async function (this: TestContext, assert) {
this.args.onDataReady = () => assert.step('data ready called');
this.args.onDataReady = (): void => assert.step('data ready called');

await render(hbs`
<Lottie
Expand All @@ -48,7 +48,7 @@ module('Integration | Component | lottie', function (hooks) {
});

test('it calls window.matchMedia to check for prefers-reduced-motion', async function (this: TestContext, assert) {
window.matchMedia = (mediaQuery) => {
window.matchMedia = (mediaQuery): MediaQueryList => {
assert.step(`matchMedia(${mediaQuery})`);
return {
addEventListener: sinon.spy(),
Expand All @@ -70,7 +70,7 @@ module('Integration | Component | lottie', function (hooks) {
test('it should listen for changes to prefers-reduced-motion and cleanup the listener when destroyed', async function (this: TestContext, assert) {
const addEventListener = sinon.spy();
const removeEventListener = sinon.spy();
window.matchMedia = () => {
window.matchMedia = (): MediaQueryList => {
return {
addEventListener,
removeEventListener,
Expand All @@ -92,7 +92,7 @@ module('Integration | Component | lottie', function (hooks) {
});

test('it should not autoplay the animation when prefers-reduced-motion is enabled', async function (this: TestContext, assert) {
window.matchMedia = () => {
window.matchMedia = (): MediaQueryList => {
return {
addEventListener: () => {
/** noop */
Expand All @@ -116,7 +116,7 @@ module('Integration | Component | lottie', function (hooks) {
});

test('it should not autoplay the animation when autoplay is false', async function (this: TestContext, assert) {
window.matchMedia = () => {
window.matchMedia = (): MediaQueryList => {
return {
addEventListener: () => {
/** noop */
Expand All @@ -140,7 +140,7 @@ module('Integration | Component | lottie', function (hooks) {
});

test('it should autoplay the animation when prefers-reduced-motion is disabled', async function (this: TestContext, assert) {
window.matchMedia = () => {
window.matchMedia = (): MediaQueryList => {
return {
addEventListener: () => {
/** noop */
Expand All @@ -164,7 +164,7 @@ module('Integration | Component | lottie', function (hooks) {
});

test('it should autoplay the animation by default when prefers-reduced-motion is disabled', async function (this: TestContext, assert) {
window.matchMedia = () => {
window.matchMedia = (): MediaQueryList => {
return {
addEventListener: () => {
/** noop */
Expand Down
2 changes: 1 addition & 1 deletion test-app/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Types for compiled templates
declare module 'test-app/templates/*' {
import { TemplateFactory } from 'ember-cli-htmlbars';
import type { TemplateFactory } from 'ember-cli-htmlbars';

const tmpl: TemplateFactory;
export default tmpl;
Expand Down

0 comments on commit d8ac234

Please sign in to comment.