Skip to content

Commit

Permalink
Fix types inside of tests (#1021)
Browse files Browse the repository at this point in the history
* chore: format everything

* nit: ignore dist properly

* fix: type errors in tests

* chore: organize imorts

* fix: remaining type issues

* revert: engines change

* fix: type issue
  • Loading branch information
Princesseuh authored Jul 15, 2024
1 parent dd46c2c commit ed25fe0
Show file tree
Hide file tree
Showing 90 changed files with 445 additions and 436 deletions.
15 changes: 12 additions & 3 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.1/schema.json",
"files": {
"ignore": ["**/dist", "**/pnpm-lock.yaml", "wasm_exec.ts"],
"ignore": ["**/dist/**", "**/pnpm-lock.yaml", "wasm_exec.ts"],
"include": ["packages/**"]
},
"formatter": {
Expand Down Expand Up @@ -31,8 +31,7 @@
"fix": "safe"
}
}
},
"ignore": ["wasm_exec.ts"]
}
},
"javascript": {
"formatter": {
Expand All @@ -52,6 +51,16 @@
}
},
"overrides": [
{
"include": ["**/stress/**"],
"linter": {
"rules": {
"suspicious": {
"noConsoleLog": "off"
}
}
}
},
{
"include": ["package.json"],
"json": {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"devDependencies": {
"@changesets/cli": "^2.25.0",
"sass": "^1.55.0",
"tsx": "^3.10.1",
"typescript": "~4.9.0",
"tsx": "^4.16.2",
"typescript": "~5.5.3",
"uvu": "^0.5.6",
"@biomejs/biome": "1.8.1"
},
Expand Down
12 changes: 6 additions & 6 deletions packages/compiler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ The Astro compiler can convert `.astro` syntax to a TypeScript Module whose defa
- `.astro` files rely on a server implementation exposed as `astro/runtime/server/index.js` in the Node ecosystem. Other runtimes currently need to bring their own rendering implementation and reference it via `internalURL`. This is a pain point we're looking into fixing.

```js
import { transform } from '@astrojs/compiler';
import { transform, type TransformResult } from "@astrojs/compiler";

const result = await transform(source, {
filename: '/Users/astro/Code/project/src/pages/index.astro',
sourcemap: 'both',
internalURL: 'astro/runtime/server/index.js',
filename: "/Users/astro/Code/project/src/pages/index.astro",
sourcemap: "both",
internalURL: "astro/runtime/server/index.js",
});
```

Expand All @@ -40,8 +40,8 @@ The Astro compiler can emit an AST using the `parse` method.
- The `@astrojs/compiler/utils` entrypoint exposes a `walk` function that can be used to traverse the AST. It also exposes the `is` helper which can be used as guards to derive the proper types for each `node`.

```js
import { parse } from '@astrojs/compiler';
import { walk, is } from '@astrojs/compiler/utils';
import { parse } from "@astrojs/compiler";
import { walk, is } from "@astrojs/compiler/utils";

const result = await parse(source, {
position: false, // defaults to `true`
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/browser/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type * as types from '../shared/types';
import type * as types from '../shared/types.js';
import Go from './wasm_exec.js';

export const transform: typeof types.transform = (input, options) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/browser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
RootNode,
TagLikeNode,
TextNode,
} from '../shared/ast';
} from '../shared/ast.js';

export type Visitor = (node: Node, parent?: ParentNode, index?: number) => void | Promise<void>;

Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ export type {
PreprocessorResult,
TransformOptions,
TransformResult,
} from '../shared/types';
} from '../shared/types.js';
import { promises as fs } from 'node:fs';
import { fileURLToPath } from 'node:url';
import type * as types from '../shared/types';
import type * as types from '../shared/types.js';
import Go from './wasm_exec.js';

export const transform: typeof types.transform = async (input, options) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/node/sync.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import type * as types from '../shared/types';
import type * as types from '../shared/types.js';
import Go from './wasm_exec.js';

type UnwrappedPromise<T> = T extends (...params: any) => Promise<infer Return>
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
RootNode,
TagLikeNode,
TextNode,
} from '../shared/ast';
} from '../shared/ast.js';

export type Visitor = (node: Node, parent?: ParentNode, index?: number) => void | Promise<void>;

Expand Down
13 changes: 8 additions & 5 deletions packages/compiler/src/shared/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { RootNode } from './ast';
import type { DiagnosticCode } from './diagnostics';
export type * from './ast';
import type { RootNode } from './ast.js';
import type { DiagnosticCode } from './diagnostics.js';
export type * from './ast.js';

export interface PreprocessorResult {
code: string;
Expand Down Expand Up @@ -53,7 +53,7 @@ export interface TransformOptions {
*/
as?: 'document' | 'fragment';
transitionsAnimationURL?: string;
resolvePath?: (specifier: string) => Promise<string>;
resolvePath?: (specifier: string) => Promise<string> | string;
preprocessStyle?: (
content: string,
attrs: Record<string, string>
Expand All @@ -67,7 +67,10 @@ export interface TransformOptions {
renderScript?: boolean;
}

export type ConvertToTSXOptions = Pick<TransformOptions, 'filename' | 'normalizedFilename'>;
export type ConvertToTSXOptions = Pick<
TransformOptions,
'filename' | 'normalizedFilename' | 'sourcemap'
>;

export type HoistedScript = { type: string } & (
| {
Expand Down
8 changes: 5 additions & 3 deletions packages/compiler/test/bad-styles/unclosed-style.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { parse } from '@astrojs/compiler';
import { type ParseResult, parse } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import type { ElementNode } from '../../types.js';

test('can compile unfinished style', async () => {
let error = 0;
let result: unknown;
let result: ParseResult = {} as ParseResult;

try {
result = await parse('<style>');
} catch (e) {
error = 1;
}

const style = result.ast.children[0];
const style = result.ast.children[0] as ElementNode;
assert.equal(error, 0, 'Expected to compile with unfinished style.');
assert.ok(result.ast, 'Expected to compile with unfinished style.');
assert.equal(style.name, 'style', 'Expected to compile with unfinished style.');
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/test/basic/body-after-head-component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transform } from '@astrojs/compiler';
import { type TransformResult, transform } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';

Expand All @@ -20,7 +20,7 @@ const isProd = true;
</html>
`;

let result: unknown;
let result: TransformResult;
test.before(async () => {
result = await transform(FIXTURE);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/test/basic/body-expression.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transform } from '@astrojs/compiler';
import { type TransformResult, transform } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';

Expand All @@ -17,7 +17,7 @@ const slugs = ['one', 'two', 'three'];
</html>
`;

let result: unknown;
let result: TransformResult;
test.before(async () => {
result = await transform(FIXTURE);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/test/basic/comment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transform } from '@astrojs/compiler';
import { type TransformResult, transform } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';

Expand All @@ -9,7 +9,7 @@ const FIXTURE = `---
<div />
`;

let result: unknown;
let result: TransformResult;
test.before(async () => {
result = await transform(FIXTURE);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/test/basic/component-name.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { transform } from '@astrojs/compiler';
import { type TransformResult, transform } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';

const FIXTURE = '<div>Hello world!</div>';

let result: unknown;
let result: TransformResult;
test.before(async () => {
result = await transform(FIXTURE, {
filename: '/src/components/Cool.astro',
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/test/basic/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test('exported type', async () => {
const FIXTURE = `---
// this is fine
export type NumberType = number;
// astro hangs because of this typedef.
// astro hangs because of this typedef.
// comment it out and astro will work fine.
export type FuncType = (x: number) => number;
---
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/test/basic/expression-then-node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transform } from '@astrojs/compiler';
import { type TransformResult, transform } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';

Expand Down Expand Up @@ -28,7 +28,7 @@ const { each } = Astro.props;
</Show>
`;

let result: unknown;
let result: TransformResult;
test.before(async () => {
result = await transform(FIXTURE);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/test/basic/fragment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transform } from '@astrojs/compiler';
import { type TransformResult, transform } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';

Expand All @@ -12,7 +12,7 @@ import ThemeToggleButton from './ThemeToggleButton.tsx';
<body><div>Hello!</div></body>
`;

let result: unknown;
let result: TransformResult;
test.before(async () => {
result = await transform(FIXTURE);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/test/basic/head-injection.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { transform } from '@astrojs/compiler';
import { type TransformResult, transform } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';

const FIXTURE = '<html><head><title>Ah</title></head></html>';

let result: unknown;
let result: TransformResult;
test.before(async () => {
result = await transform(FIXTURE);
});
Expand Down
6 changes: 3 additions & 3 deletions packages/compiler/test/basic/lt-gt-text.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transform } from '@astrojs/compiler';
import { type TransformResult, transform } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';

Expand All @@ -23,7 +23,7 @@ const featuredProject = projects[0];
title="Jeanine White: Personal Site"
description="Jeanine White: Developer, Speaker, and Writer..."
/>
</head>
<body>
<Nav />
Expand All @@ -33,7 +33,7 @@ const featuredProject = projects[0];
</html>
`;

let result: unknown;
let result: TransformResult;
test.before(async () => {
result = await transform(FIXTURE);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/test/basic/null-chars.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transform } from '@astrojs/compiler';
import { type TransformResult, transform } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';

Expand All @@ -8,7 +8,7 @@ const FIXTURE = `
</div>
`;

let result: unknown;
let result: TransformResult;
test.before(async () => {
result = await transform(FIXTURE, {
filename:
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/test/basic/props-interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transform } from '@astrojs/compiler';
import { type TransformResult, transform } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';

Expand All @@ -13,7 +13,7 @@ const props = { ...Astro.props } as Props;
<slot></slot>
</body>`;

let result: unknown;
let result: TransformResult;
test.before(async () => {
result = await transform(FIXTURE);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/test/basic/script-before-html.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transform } from '@astrojs/compiler';
import { type TransformResult, transform } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';

Expand Down Expand Up @@ -30,7 +30,7 @@ html {
</style>
`;

let result: unknown;
let result: TransformResult;
test.before(async () => {
result = await transform(FIXTURE);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/test/basic/script-fragment.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { transform } from '@astrojs/compiler';
import { type TransformResult, transform } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';

const FIXTURE = `<script src={Astro.resolve("../scripts/no_hoist_nonmodule.js")}></script>`;

let result: unknown;
let result: TransformResult;
test.before(async () => {
result = await transform(FIXTURE);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/test/basic/top-level-expressions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transform } from '@astrojs/compiler';
import { type TransformResult, transform } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';

Expand Down Expand Up @@ -28,7 +28,7 @@ const internal = [];
{false && (<span id="frag-undefined" />)}
`;

let result: unknown;
let result: TransformResult;
test.before(async () => {
result = await transform(FIXTURE);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/test/basic/trailing-newline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transform } from '@astrojs/compiler';
import { type TransformResult, transform } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';

Expand All @@ -21,7 +21,7 @@ const FIXTURE = `{
}
`;

let result: unknown;
let result: TransformResult;
test.before(async () => {
result = await transform(FIXTURE);
});
Expand Down
Loading

0 comments on commit ed25fe0

Please sign in to comment.