Skip to content

Commit

Permalink
fix: remaining type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Princesseuh committed Jul 11, 2024
1 parent 4e7ad82 commit c2d857e
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 38 deletions.
10 changes: 10 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@
}
},
"overrides": [
{
"include": ["**/stress/**"],
"linter": {
"rules": {
"suspicious": {
"noConsoleLog": "off"
}
}
}
},
{
"include": ["package.json"],
"json": {
Expand Down
5 changes: 3 additions & 2 deletions packages/compiler/test/bad-styles/unclosed-style.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
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: ParseResult;
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
9 changes: 5 additions & 4 deletions packages/compiler/test/parse/ast.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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';

const FIXTURE = `
---
Expand All @@ -11,7 +12,7 @@ let value = 'world';
<div></div>
`;

let result: TransformResult;
let result: ParseResult;
test.before(async () => {
result = await parse(FIXTURE);
});
Expand All @@ -36,12 +37,12 @@ test('element', () => {
});

test('element with no attributes', () => {
const [, , , element] = result.ast.children;
const [, , , element] = result.ast.children as ElementNode[];
assert.equal(element.attributes, [], `Expected the "attributes" property to be an empty array`);
});

test('element with no children', () => {
const [, , , element] = result.ast.children;
const [, , , element] = result.ast.children as ElementNode[];
assert.equal(element.children, [], `Expected the "children" property to be an empty array`);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/test/parse/escaping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test('ampersand', async () => {
{
children: [{ value: output }],
},
] = result.ast.children;
] = result.ast.children as any;
assert.equal(output, STYLE, 'Expected AST style to equal input');
});

Expand Down
9 changes: 5 additions & 4 deletions packages/compiler/test/parse/fragment.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { parse } from '@astrojs/compiler';
import { type ParseResult, parse } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import type { FragmentNode } from '../../types.js';

const FIXTURE = '<>Hello</><Fragment>World</Fragment>';

let result: TransformResult;
let result: ParseResult;
test.before(async () => {
result = await parse(FIXTURE);
});

test('fragment shorthand', () => {
const [first] = result.ast.children;
const [first] = result.ast.children as FragmentNode[];
assert.equal(first.type, 'fragment', 'Expected first child to be of type "fragment"');
assert.equal(first.name, '', 'Expected first child to have name of ""');
});

test('fragment literal', () => {
const [, second] = result.ast.children;
const [, second] = result.ast.children as FragmentNode[];
assert.equal(second.type, 'fragment', 'Expected second child to be of type "fragment"');
assert.equal(second.name, 'Fragment', 'Expected second child to have name of ""');
});
Expand Down
5 changes: 3 additions & 2 deletions packages/compiler/test/parse/multibyte-characters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ test('properly maps the position', async () => {
const {
ast: { children },
} = await parse(FIXTURE);

const text = children[1];
assert.equal(text.position.start.offset, 5, 'properly maps the text start position');
assert.equal(text.position.end.offset, 8, 'properly maps the text end position');
assert.equal(text.position?.start.offset, 5, 'properly maps the text start position');
assert.equal(text.position?.end?.offset, 8, 'properly maps the text end position');
});

test.run();
4 changes: 2 additions & 2 deletions packages/compiler/test/parse/orphan-head.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse } from '@astrojs/compiler';
import { type ParseResult, parse } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';

Expand All @@ -18,7 +18,7 @@ const FIXTURE = `
</html>
`;

let result: TransformResult;
let result: ParseResult;
test.before(async () => {
result = await parse(FIXTURE);
});
Expand Down
31 changes: 16 additions & 15 deletions packages/compiler/test/parse/position.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { parse } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import type { ElementNode, FrontmatterNode } from '../../types.js';

test('include start and end positions', async () => {
const input = `---
Expand All @@ -10,9 +11,9 @@ test('include start and end positions', async () => {
<iframe>Hello</iframe><div></div>`;
const { ast } = await parse(input);

const iframe = ast.children[1];
const iframe = ast.children[1] as ElementNode;
assert.is(iframe.name, 'iframe');
assert.ok(iframe.position.start, 'Expected serialized output to contain a start position');
assert.ok(iframe.position?.start, 'Expected serialized output to contain a start position');
assert.ok(iframe.position.end, 'Expected serialized output to contain an end position');
});

Expand All @@ -25,9 +26,9 @@ test('include start and end positions for comments', async () => {
<iframe>Hello</iframe><div></div>`;
const { ast } = await parse(input);

const comment = ast.children[1];
const comment = ast.children[1] as ElementNode;
assert.is(comment.type, 'comment');
assert.ok(comment.position.start, 'Expected serialized output to contain a start position');
assert.ok(comment.position?.start, 'Expected serialized output to contain a start position');
assert.ok(comment.position.end, 'Expected serialized output to contain an end position');
});

Expand All @@ -39,20 +40,20 @@ test('include start and end positions for text', async () => {
Hello world!`;
const { ast } = await parse(input);

const text = ast.children[1];
const text = ast.children[1] as ElementNode;
assert.is(text.type, 'text');
assert.ok(text.position.start, 'Expected serialized output to contain a start position');
assert.ok(text.position.end, 'Expected serialized output to contain an end position');
assert.ok(text.position?.start, 'Expected serialized output to contain a start position');
assert.ok(text.position?.end, 'Expected serialized output to contain an end position');
});

test('include start and end positions for self-closing tags', async () => {
const input = '<input/>';
const { ast } = await parse(input);

const element = ast.children[0];
const element = ast.children[0] as ElementNode;
assert.is(element.type, 'element');
assert.is(element.name, 'input');
assert.ok(element.position.start, 'Expected serialized output to contain a start position');
assert.ok(element.position?.start, 'Expected serialized output to contain a start position');
assert.ok(element.position.end, 'Expected serialized output to contain an end position');
});

Expand All @@ -62,9 +63,9 @@ test('include correct start and end position for self-closing tag', async () =>
<li />`;
const { ast } = await parse(input);

const li = ast.children[1];
const li = ast.children[1] as ElementNode;
assert.is(li.name, 'li');
assert.ok(li.position.start, 'Expected serialized output to contain a start position');
assert.ok(li.position?.start, 'Expected serialized output to contain a start position');
assert.ok(li.position.end, 'Expected serialized output to contain an end position');

assert.equal(
Expand All @@ -85,9 +86,9 @@ test('include correct start and end position for normal closing tag', async () =
<li></li>`;
const { ast } = await parse(input);

const li = ast.children[1];
const li = ast.children[1] as ElementNode;
assert.is(li.name, 'li');
assert.ok(li.position.start, 'Expected serialized output to contain a start position');
assert.ok(li.position?.start, 'Expected serialized output to contain a start position');
assert.ok(li.position.end, 'Expected serialized output to contain an end position');

assert.equal(
Expand All @@ -107,9 +108,9 @@ test('include start and end position if frontmatter is only thing in file (#802)
---`;
const { ast } = await parse(input);

const frontmatter = ast.children[0];
const frontmatter = ast.children[0] as FrontmatterNode;
assert.is(frontmatter.type, 'frontmatter');
assert.ok(frontmatter.position.start, 'Expected serialized output to contain a start position');
assert.ok(frontmatter.position?.start, 'Expected serialized output to contain a start position');
assert.ok(frontmatter.position.end, 'Expected serialized output to contain an end position');

assert.equal(
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/test/parse/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let content = "Testing 123";
</Markdown>
`;

let result: TransformResult;
let result: string;
test.before(async () => {
const { ast } = await parse(FIXTURE);
try {
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/test/stress/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ async function test() {
}

// Throttle the rendering a paths to prevents creating too many Promises on the microtask queue.
function* throttle(max, tests) {
function* throttle(max: number, tests: any) {
const tmp = [];
let i = 0;
for (const t of tests) {
Expand Down
3 changes: 2 additions & 1 deletion packages/compiler/test/tsx-errors/eof.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { convertToTSX } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import type { TSXResult } from '../../types.js';

const FIXTURE = `<html>
<head>
Expand All @@ -13,7 +14,7 @@ const FIXTURE = `<html>
</body>
</html>`;

let result: TransformResult;
let result: TSXResult;
test.before(async () => {
result = await convertToTSX(FIXTURE, {
filename: '/src/components/EOF.astro',
Expand Down
3 changes: 2 additions & 1 deletion packages/compiler/test/tsx-errors/fragment-shorthand.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { convertToTSX } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import type { TSXResult } from '../../types.js';

const FIXTURE = `<html>
<head>
Expand All @@ -11,7 +12,7 @@ const FIXTURE = `<html>
</body>
</html>`;

let result: TransformResult;
let result: TSXResult;
test.before(async () => {
result = await convertToTSX(FIXTURE, {
filename: '/src/components/fragment.astro',
Expand Down
3 changes: 2 additions & 1 deletion packages/compiler/test/tsx-errors/unfinished-component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { convertToTSX } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import type { TSXResult } from '../../types.js';

const FIXTURE = '<div class={';

let result: TransformResult;
let result: TSXResult;
test.before(async () => {
result = await convertToTSX(FIXTURE, {
filename: '/src/components/unfinished.astro',
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/test/tsx-sourcemaps/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('tag close', async () => {
test('tag with spaces', async () => {
const input = '<Button ></Button>';
const { map } = await convertToTSX(input, { sourcemap: 'both', filename: 'index.astro' });
const tracer = new TraceMap(map);
const tracer = new TraceMap(map as any);

const generated = generatedPositionFor(tracer, { source: 'index.astro', line: 1, column: 14 });

Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { convertToTSX, transform } from '@astrojs/compiler';
import { TraceMap, generatedPositionFor, originalPositionFor } from '@jridgewell/trace-mapping';
import sass from 'sass';

export async function preprocessStyle(value, attrs): Promise<any> {
export async function preprocessStyle(value: any, attrs: any): Promise<any> {
if (!attrs.lang) {
return null;
}
Expand Down Expand Up @@ -48,7 +48,7 @@ export async function testTSXSourcemap(input: string, snippet: string) {
if (!snippetLoc) throw new Error(`Unable to find "${snippet}"`);

const { code, map } = await convertToTSX(input, { sourcemap: 'both', filename: 'index.astro' });
const tracer = new TraceMap(map);
const tracer = new TraceMap(map as any);

const generated = generatedPositionFor(tracer, {
source: 'index.astro',
Expand Down

0 comments on commit c2d857e

Please sign in to comment.