Skip to content

Commit

Permalink
Check types with tsc in CI (withastro#2535)
Browse files Browse the repository at this point in the history
  • Loading branch information
delucis authored Feb 2, 2023
1 parent ce645ee commit 70f6648
Show file tree
Hide file tree
Showing 25 changed files with 85 additions and 55 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ jobs:

- name: Run Check
run: pnpm run check
tsc:
name: Check for type issues with tsc
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install Tools & Dependencies
uses: ./.github/actions/install

- name: Run Check
run: pnpm run tsc
eslint:
name: Check for code issues with ESLint
runs-on: ubuntu-latest
Expand Down
3 changes: 2 additions & 1 deletion integrations/astro-asides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ function remarkAsides(): unified.Plugin<[], mdast.Root> {
const variants = new Set(['note', 'tip', 'caution', 'danger']);

const transformer: unified.Transformer<mdast.Root> = (tree, file) => {
// @ts-expect-error Possibly infinite type instantiation we can’t do anything about.
visit(tree, (node, index, parent) => {
if (node.type !== 'containerDirective') return;
if (!parent || index === null || node.type !== 'containerDirective') return;
const type = node.name;
if (!variants.has(type)) return;

Expand Down
2 changes: 1 addition & 1 deletion integrations/utils/isMDX.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { VFile } from 'vfile';
import type { VFile } from 'vfile';

export function isMDXFile(file: VFile) {
return file.history[0].endsWith('.mdx');
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"test": "vitest",
"preview": "astro preview",
"check": "astro check",
"tsc": "tsc",
"format": "pnpm run format:code",
"format:ci": "pnpm run format:imports && pnpm run format:code",
"format:code": "prettier -w . --cache --plugin-search-dir=.",
Expand Down Expand Up @@ -37,6 +38,7 @@
"@docsearch/css": "^3.2.2",
"@docsearch/react": "^3.2.0",
"@types/chroma-js": "^2.1.4",
"@types/hast": "^2.3.4",
"@types/html-escaper": "^3.0.0",
"@types/mdast": "^3.0.10",
"@types/node": "^18.6.4",
Expand Down Expand Up @@ -64,6 +66,7 @@
"kleur": "^4.1.5",
"mdast-util-from-markdown": "^1.2.0",
"mdast-util-to-hast": "^12.2.4",
"mdast-util-to-string": "^3.1.1",
"micromark-util-character": "^1.1.0",
"micromark-util-symbol": "^1.0.1",
"node-fetch": "^3.2.10",
Expand Down
14 changes: 10 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 34 additions & 20 deletions scripts/generate-integration-pages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import kleur from 'kleur';
import type { Definition, HTML, Link, Root, Text } from 'mdast';
import type {
Blockquote,
Definition,
HTML,
Link,
ListContent,
Paragraph,
PhrasingContent,
Root,
Text,
} from 'mdast';
import { toString } from 'mdast-util-to-string';
import fetch from 'node-fetch';
import fs from 'node:fs';
import { remark } from 'remark';
Expand Down Expand Up @@ -193,35 +204,38 @@ function closeUnclosedLinebreaks() {
/** Remark plugin to replace GitHub note/warning syntax with docs-style asides. */
function replaceAsides() {
return function transform(tree: Root) {
visit(tree, 'blockquote', (node) => {
visit(tree, 'blockquote', (node: Blockquote | Paragraph) => {
const openingParagraph = node.children[0];
const [firstChild, trailingText] = openingParagraph.children;

if (!('children' in openingParagraph)) return;
const [firstChild, trailingText, ...children] = openingParagraph.children;

// check for **Note:** or **Warning:** at the beginning of the first paragraph
if (firstChild.type !== 'strong' || !/Note|Warning/.test(firstChild.children[0].value)) {
return;
}
if (firstChild.type !== 'strong') return;
const firstChildText = toString(firstChild);
if (!/Note|Warning/.test(firstChildText)) return;

// assign aside type
const AsideType =
firstChild.children[0].value.toLowerCase() === 'warning' ? 'caution' : 'note';
const AsideType = firstChildText.toLowerCase() === 'warning' ? 'caution' : 'note';

// remove blockquotes `>`
node.type = 'paragraph';

// replace **strong** for :::aside
firstChild.type = 'text';
firstChild.value = `:::${AsideType}`;

// if trailingText starts with `: ` replace it with a newline
trailingText.value = trailingText.value.replace(/^: /, '\n');
if ('value' in trailingText) {
trailingText.value = trailingText.value.replace(/^: /, '\n');
}

// Opening and closing ::: text to wrap blockquote.
const openAside: Text = { type: 'text', value: `:::${AsideType}` };
const closeAside: Text = { type: 'text', value: '\n:::' };

// append ::: at end of the paragraph
const lastChild = {
type: 'text',
value: '\n:::',
};
openingParagraph.children.push(lastChild);
openingParagraph.children = [
openAside,
trailingText,
...children,
closeAside,
] as ListContent[];
});
};
}
Expand Down Expand Up @@ -263,7 +277,7 @@ function removeTOC() {
const firstItemContent = node.children[0].children[0];
if (firstItemContent.type !== 'paragraph') return;
return firstItemContent.children.some(
(child) =>
(child: PhrasingContent) =>
child.type === 'link' &&
(child.url.startsWith('#why') || child.url.startsWith('#installation'))
);
Expand Down
2 changes: 1 addition & 1 deletion scripts/lib/linkcheck/base/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CheckBase } from './check';
import type { CheckBase } from './check';

export interface LinkCheckerOptions {
baseUrl: string;
Expand Down
4 changes: 2 additions & 2 deletions scripts/lib/linkcheck/base/check.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LinkIssue } from './issue';
import { AllPagesByPathname, HtmlPage } from './page';
import type { LinkIssue } from './issue';
import type { AllPagesByPathname, HtmlPage } from './page';

export interface CheckHtmlPageContext {
allPages: AllPagesByPathname;
Expand Down
6 changes: 3 additions & 3 deletions scripts/lib/linkcheck/base/issue.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AnnotationProperties } from '@actions/core';
import type { AnnotationProperties } from '@actions/core';
import { formatCount } from '../../output.mjs';
import { CheckBase } from './check';
import { HtmlPage } from './page';
import type { CheckBase } from './check';
import type { HtmlPage } from './page';

export interface LinkIssue {
type: IssueType;
Expand Down
2 changes: 1 addition & 1 deletion scripts/lib/linkcheck/base/page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Document, Element } from 'domhandler';
import type { Document, Element } from 'domhandler';
import htmlparser2 from 'htmlparser2';

export interface AllPagesByPathname {
Expand Down
4 changes: 2 additions & 2 deletions scripts/lib/linkcheck/steps/build-index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import path from 'path';
import { dedentMd } from '../../output.mjs';
import { LinkCheckerOptions } from '../base/base';
import type { LinkCheckerOptions } from '../base/base';
import { AllPagesByPathname, HtmlPage } from '../base/page';

/**
Expand All @@ -13,7 +13,7 @@ export function getPagePathnamesFromSitemap(options: LinkCheckerOptions) {
const sitemaps = distContents.filter((path) => /^sitemap-\d+\.xml$/.test(path));

const sitemapRegex = new RegExp(`<loc>${options.baseUrl}(/.*?)</loc>`, 'ig');
const uniquePagePaths = new Set();
const uniquePagePaths = new Set<string>();

for (const filename of sitemaps) {
const sitemapFilePath = path.join(options.buildOutputDir, filename);
Expand Down
4 changes: 2 additions & 2 deletions scripts/lib/linkcheck/steps/find-issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import fs from 'fs';
import path from 'path';
import { dedentMd } from '../../output.mjs';
import { indexOfHref, LinkCheckerOptions, LinkCheckerState } from '../base/base';
import { LinkIssue } from '../base/issue';
import { AllPagesByPathname, HtmlPage } from '../base/page';
import type { LinkIssue } from '../base/issue';
import type { AllPagesByPathname, HtmlPage } from '../base/page';

/**
* Goes through all pre-parsed and indexed pages, runs all configured checks,
Expand Down
2 changes: 1 addition & 1 deletion scripts/lib/linkcheck/steps/optional-autofix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs';
import kleur from 'kleur';
import { dedentMd, formatCount } from '../../output.mjs';
import { LinkCheckerOptions, LinkCheckerState, replaceHrefs } from '../base/base';
import { LinkIssue } from '../base/issue';
import type { LinkIssue } from '../base/issue';

/**
* Handle all autofix-related tasks:
Expand Down
6 changes: 3 additions & 3 deletions scripts/lib/linkcheck/steps/output-issues.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import core from '@actions/core';
import kleur from 'kleur';
import { dedentMd, formatCount } from '../../output.mjs';
import { LinkCheckerState } from '../base/base';
import { IssueType, LinkIssue } from '../base/issue';
import { HtmlPage } from '../base/page';
import type { LinkCheckerState } from '../base/base';
import type { IssueType, LinkIssue } from '../base/issue';
import type { HtmlPage } from '../base/page';

/**
* Outputs the result of the link check to the console.
Expand Down
2 changes: 1 addition & 1 deletion src/components/tabs/useTabState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function useTabState(
if (!storeKey) return localState;

const curr = $tabStore[storeKey]?.curr ?? initialCurr;
function setCurr(newCurr) {
function setCurr(newCurr: string) {
if (storeKey) {
tabStore.setKey(storeKey, { curr: newCurr });
} else {
Expand Down
1 change: 0 additions & 1 deletion src/i18n/ar/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export default UIDictionary({
'astro logo on a starry expanse of space, with a purple saturn-like planet floating in the right foreground',
// Left Sidebar
'leftSidebar.a11yTitle': 'الأساسي',
'leftSidebar.noTranslations': 'لم يتم العثور على أي ترجمة',
'leftSidebar.viewInEnglish': 'أعرض الإنجليزية',
// Right Sidebar
'rightSidebar.a11yTitle': 'ثانوي',
Expand Down
3 changes: 0 additions & 3 deletions src/i18n/de/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ export default NavDictionary({
'getting-started': 'Erste Schritte',
install: 'Installation',
'editor-setup': 'Editor-Einrichtung',
migrate: 'Migrations-Anleitung',
'integrations/integrations': 'Mit Astro gebaut',
'comparing-astro-vs-other-tools': 'Astro vs. X',

// Core Concepts
coreConcepts: 'Kernkonzepte',
Expand Down
1 change: 0 additions & 1 deletion src/i18n/es/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export default NavDictionary({
'getting-started': 'Empezando',
install: 'Instalación',
'editor-setup': 'Configuración del editor de código',
migrate: 'Guía de migración',
coreConcepts: 'Conceptos básicos',
'concepts/why-astro': '¿Por qué usar Astro?',
'concepts/mpa-vs-spa': 'MPA vs. SPA',
Expand Down
3 changes: 0 additions & 3 deletions src/i18n/fr/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ export default NavDictionary({
'getting-started': 'Bien démarrer',
install: 'Installation',
'editor-setup': "Configuration de l'éditeur de code",
migrate: 'Guide de Migration',
'integrations/integrations': 'Construit avec Astro',
'comparing-astro-vs-other-tools': 'Astro vs. X',

// Core Concepts
coreConcepts: 'Concepts Fondamentaux',
Expand Down
1 change: 0 additions & 1 deletion src/i18n/ja/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export default NavDictionary({
'getting-started': 'はじめに',
install: 'インストール',
'editor-setup': 'エディタのセットアップ',
migrate: 'アップグレードガイド',

// Tutorials
tutorials: 'チュートリアル',
Expand Down
1 change: 0 additions & 1 deletion src/i18n/pt-br/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export default NavDictionary({
install: 'Instalação',
'editor-setup': 'Configuração do Editor',
'guides/migrate-to-astro': 'Migre para o Astro',
migrate: 'Guia de Migração',
coreConcepts: 'Principais Conceitos',
'concepts/why-astro': 'Por que Astro?',
'concepts/islands': 'Ilhas Astro',
Expand Down
1 change: 0 additions & 1 deletion src/i18n/zh-tw/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export default NavDictionary({
'getting-started': '新手上路',
install: '安裝',
'editor-setup': '編輯器設定',
migrate: '轉移指南',

// Core Concepts
coreConcepts: '主要概念',
Expand Down
4 changes: 3 additions & 1 deletion src/util-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Do not import this file from a hydrated client-side component.
*/

// @ts-expect-error Package without types we can’t do anything about.
import EleventyFetch from '@11ty/eleventy-fetch';

export type CachedFetchOptions = {
Expand All @@ -26,7 +27,8 @@ export async function cachedFetch(
type: 'buffer',
fetchOptions,
});
} catch (error) {
} catch (e: unknown) {
const error = e as Error;
const msg: string = error?.message || error.toString();
const matches = msg.match(/^Bad response for (.*) \(.*?\): (.*)$/);
status = parseInt(matches?.[2] || '') || 404;
Expand Down
2 changes: 1 addition & 1 deletion src/util/getGithubEditUrl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AstroGlobal } from 'astro';
import type { AstroGlobal } from 'astro';
import { getLanguageFromURL } from '../util';

/** Gets the URL to edit the page on GitHub */
Expand Down
3 changes: 3 additions & 0 deletions src/util/groupPagesByLang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import type { MarkdownInstance } from 'astro';

export const groupPagesByLang = <T extends MarkdownInstance<Record<string, unknown>>>(pages: T[]) =>
pages.reduce((pages, page) => {
if (!page.url) {
throw new Error('No `url` for page: ' + page.file);
}
const lang = page.url.split('/')[1];
if (!pages[lang]) pages[lang] = [];
pages[lang].push(page);
Expand Down

0 comments on commit 70f6648

Please sign in to comment.