Skip to content

Commit

Permalink
Various lint/typescript fixes (#32)
Browse files Browse the repository at this point in the history
- Run `lint` as a GitHub PR check.

- Run `tsc` as part of lint task.

- Ignore the build output (`dist`) in Prettier and eslint.

- Coerce types of `querySelector` return values.

- Clean up rendering the project tabs.

## Test Plan

Run Cypress. `yarn lint`. We'll see if the GH Action works on this PR!
  • Loading branch information
oyamauchi authored Oct 2, 2023
1 parent 2ed3b79 commit 4a35e7a
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
root: true,
ignorePatterns: ['build'],
ignorePatterns: ['build', 'dist'],
rules: {
eqeqeq: ['error', 'always'],
},
Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Node.js CI

on:
push:
branches: ['main']
pull_request:
branches: ['main']

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Use Node.js 18.x
uses: actions/setup-node@v3
with:
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
node-version: 18.x
cache: 'yarn'
- run: yarn --frozen-lockfile
- run: yarn lint
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Build products
build
dist
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"serve:widget": "parcel serve ./src/*.html --dist-dir build",
"cypress:open": "cypress open",
"cypress:run": "cypress run",
"lint": "prettier --check . && eslint .",
"lint": "tsc --noEmit && prettier --check . && eslint .",
"prepare": "husky install"
},
"browserslist": {
Expand Down
2 changes: 1 addition & 1 deletion src/calculator-form.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { html, css, nothing, TemplateResult } from 'lit';
import { downIcon, questionIcon } from './icons';
import { questionIcon } from './icons';
import { select, multiselect, selectStyles, OptionParam } from './select';
import { inputStyles } from './styles/input';
import './currency-input';
Expand Down
3 changes: 1 addition & 2 deletions src/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
} from './calculator-types';
import { CALCULATOR_FOOTER } from './calculator-footer';
import { fetchApi } from './api/fetch';
import { NO_PROJECT } from './projects';
import { downIcon } from './icons';

const loadedTemplate = (
Expand Down Expand Up @@ -126,7 +125,7 @@ export class RewiringAmericaCalculator extends LitElement {
tax_filing,
household_size,
});
return await fetchApi(
return await fetchApi<ICalculatedIncentiveResults>(
this.apiKey,
this.apiHost,
'/api/v0/calculator',
Expand Down
5 changes: 3 additions & 2 deletions src/state-calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import '@shoelace-style/shoelace/dist/components/spinner/spinner';
import { STATES } from './states';
import { authorityLogosStyles } from './authority-logos';
import { APIResponse, APIUtilitiesResponse } from './api/calculator-types-v1';
import { SlSelect } from '@shoelace-style/shoelace';

const loadingTemplate = () => html`
<div class="card card-content">
Expand Down Expand Up @@ -146,10 +147,10 @@ export class RewiringAmericaStateCalculator extends LitElement {
override async firstUpdated() {
// Give the browser a chance to paint
await new Promise(r => setTimeout(r, 0));
const select = this.renderRoot.querySelector('sl-select');
const select = this.renderRoot.querySelector('sl-select') as SlSelect;
const combobox = this.renderRoot
.querySelector('sl-select')
?.renderRoot.querySelector('div.select__combobox');
?.renderRoot.querySelector('div.select__combobox') as HTMLElement;

select?.addEventListener('keydown', event => {
if (event.key === 'Tab' && select.open) {
Expand Down
20 changes: 8 additions & 12 deletions src/state-incentive-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,9 @@ export const stateIncentivesTemplate = (
]),
) as Record<Project, Incentive[]>;

const nonSelectedProjects = Object.entries(PROJECTS)
.filter(([project, _]) => !selectedProjects.includes(project as Project))
.sort(([a], [b]) => shortLabel(a).localeCompare(shortLabel(b)))
.map(([project, _]) => project);
const nonSelectedProjects = (Object.keys(PROJECTS) as Project[])
.filter(project => !selectedProjects.includes(project))
.sort((a, b) => shortLabel(a).localeCompare(shortLabel(b)));

// Only offer "other" tabs if there are incentives for that project.
const otherTabs = (
Expand All @@ -462,22 +461,19 @@ export const stateIncentivesTemplate = (
.map(([project]) => project);

const projectTab =
selectedProjectTab &&
selectedProjects.includes(selectedProjectTab as Project)
selectedProjectTab && selectedProjects.includes(selectedProjectTab)
? selectedProjectTab
: selectedProjects[0];
const otherTab =
selectedOtherTab &&
nonSelectedProjects.includes(selectedOtherTab as Project)
selectedOtherTab && nonSelectedProjects.includes(selectedOtherTab)
? selectedOtherTab
: nonSelectedProjects[0];

const selectedIncentives = incentivesByProject[projectTab] ?? [];
const selectedOtherIncentives =
incentivesByProject[otherTab as Project] ?? [];
const selectedOtherIncentives = incentivesByProject[otherTab] ?? [];

const otherIncentivesLabel =
selectedIncentives.length == 0
selectedIncentives.length === 0
? 'Incentives available to you'
: 'Other incentives available to you';

Expand All @@ -494,7 +490,7 @@ export const stateIncentivesTemplate = (
selectedOtherIncentives,
otherTabs,
// If a nonexistent tab is selected, pretend the first one is selected.
otherTab as Project,
otherTab,
onOtherTabSelected,
)}
${authorityLogosTemplate(response)}`;
Expand Down

1 comment on commit 4a35e7a

@vercel
Copy link

@vercel vercel bot commented on 4a35e7a Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.