Skip to content

Commit

Permalink
Fix bug in alias resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Nov 25, 2024
1 parent ba5e712 commit 9bb214d
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 47 deletions.
5 changes: 5 additions & 0 deletions .changeset/selfish-fans-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@terrazzo/parser": patch
---

Fix bug in alias resolution
5 changes: 5 additions & 0 deletions .changeset/serious-wombats-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@terrazzo/parser": patch
---

Fix type error with parser output
2 changes: 1 addition & 1 deletion packages/parser/src/parse/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export function applyAliases(
const { id: aliasID, mode: aliasMode } = parseAlias(subvalue);
const aliasToken = tokens[aliasOfID]!;
const possibleTypes: string[] = expectedAliasTypes?.[property as keyof typeof expectedAliasTypes] || [];
if (!possibleTypes.includes(aliasToken.$type)) {
if (possibleTypes.length && !possibleTypes.includes(aliasToken.$type)) {
const elementNode = ($valueNode as ArrayNode).elements[i]!.value;
logger.error({
message: `Invalid alias: expected $type: "${possibleTypes.join('" or "')}", received $type: "${aliasToken.$type}".`,
Expand Down
18 changes: 11 additions & 7 deletions packages/parser/src/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { type Token, type TokenNormalized, isTokenMatch, pluralize, splitID } fr
import type ytm from 'yaml-to-momoa';
import lintRunner from '../lint/index.js';
import Logger from '../logger.js';
import type { ConfigInit } from '../types.js';
import type { ConfigInit, InputSource } from '../types.js';
import { applyAliases } from './alias.js';
import { getObjMembers, injectObjMembers, maybeJSONString, traverse } from './json.js';
import normalize from './normalize.js';
import type { ParseInput } from './types.js';
import validate from './validate.js';

export * from './validate.js';
Expand All @@ -31,12 +30,12 @@ export interface ParseOptions {

export interface ParseResult {
tokens: Record<string, TokenNormalized>;
sources: Record<string, ParseInput>;
sources: InputSource[];
}

/** Parse */
export default async function parse(
input: ParseInput[],
input: Omit<InputSource, 'document'>[],
{
logger = new Logger(),
skipLint = false,
Expand All @@ -48,7 +47,7 @@ export default async function parse(
let tokens: Record<string, TokenNormalized> = {};
// note: only keeps track of sources with locations on disk; in-memory sources are discarded
// (it’s only for reporting line numbers, which doesn’t mean as much for dynamic sources)
const sources: Record<string, ParseInput> = {};
const sources: Record<string, InputSource> = {};

if (!Array.isArray(input)) {
logger.error({ group: 'parser', label: 'init', message: 'Input must be an array of input objects.' });
Expand Down Expand Up @@ -86,6 +85,7 @@ export default async function parse(
sources[input[i]!.filename!.protocol === 'file:' ? input[i]!.filename!.href : input[i]!.filename!.href] = {
filename: input[i]!.filename,
src: result.src,
document: result.document,
};
}
}
Expand Down Expand Up @@ -170,7 +170,7 @@ export default async function parse(

return {
tokens,
sources,
sources: Object.values(sources),
};
}

Expand Down Expand Up @@ -449,5 +449,9 @@ async function parseSingle(
timing: performance.now() - normalizeStart,
});

return { tokens, document, src };
return {
tokens,
document,
src,
};
}
6 changes: 0 additions & 6 deletions packages/parser/src/parse/types.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/plugin-css/test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('tz build', () => {
it('outDir', async () => {
const cwd = new URL('./fixtures/cli-config-outdir/', import.meta.url);
await execa('node', [cmd, 'build'], { cwd });
expect(fs.readFileSync(new URL('./styles/out/actual.css', cwd), 'utf8')).toMatchFileSnapshot(
await expect(fs.readFileSync(new URL('./styles/out/actual.css', cwd), 'utf8')).toMatchFileSnapshot(
fileURLToPath(new URL('./styles/out/want.css', cwd)),
);
});
Expand Down
20 changes: 12 additions & 8 deletions packages/plugin-css/test/js.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('Node.js API', () => {
config,
});
const result = await build(tokens, { sources, config });
expect(result.outputFiles.find((f) => f.filename === output)?.contents).toMatchFileSnapshot(
await expect(result.outputFiles.find((f) => f.filename === output)?.contents).toMatchFileSnapshot(
fileURLToPath(new URL('./want.css', cwd)),
);
},
Expand Down Expand Up @@ -93,12 +93,16 @@ describe('Node.js API', () => {
{ cwd },
);
const tokensJSON = new URL('./tokens.json', cwd);
const { tokens, sources } = await parse([{ filename: tokensJSON, src: fs.readFileSync(tokensJSON, 'utf8') }], {
config,
});
const result = await build(tokens, { sources, config });
expect(result.outputFiles.find((f) => f.filename === output)?.contents).toMatchFileSnapshot(
fileURLToPath(new URL('./want.css', cwd)),
);
try {
const { tokens, sources } = await parse([{ filename: tokensJSON, src: fs.readFileSync(tokensJSON, 'utf8') }], {
config,
});
const result = await build(tokens, { sources, config });
await expect(result.outputFiles.find((f) => f.filename === output)?.contents).toMatchFileSnapshot(
fileURLToPath(new URL('./want.css', cwd)),
);
} catch (err) {
console.error(err);
}
});
});
4 changes: 2 additions & 2 deletions packages/plugin-js/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ describe('@terrazzo/plugin-js', () => {
config,
});
const result = await build(tokens, { sources, config });
expect(result.outputFiles.find((f) => f.filename === filename)?.contents).toMatchFileSnapshot(
await expect(result.outputFiles.find((f) => f.filename === filename)?.contents).toMatchFileSnapshot(
fileURLToPath(new URL('./want.js', cwd)),
);
expect(
await expect(
result.outputFiles.find((f) => f.filename === filename.replace(/\.js$/, '.d.ts'))?.contents,
).toMatchFileSnapshot(fileURLToPath(new URL('./want.d.ts', cwd)));

Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-sass/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('@terrazzo/plugin-scss', () => {
config,
});
const result = await build(tokens, { sources, config });
expect(result.outputFiles.find((f) => f.filename === filename)?.contents).toMatchFileSnapshot(
await expect(result.outputFiles.find((f) => f.filename === filename)?.contents).toMatchFileSnapshot(
fileURLToPath(new URL('./want.scss', cwd)),
);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-swift/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('@terrazzo/plugin-swift', () => {
});
const result = await build(tokens, { config, sources });
for (const { filename, contents } of result.outputFiles) {
expect(contents).toMatchFileSnapshot(fileURLToPath(new URL(filename, cwd)));
await expect(contents).toMatchFileSnapshot(fileURLToPath(new URL(filename, cwd)));
}
});
});
32 changes: 12 additions & 20 deletions packages/plugin-tailwind/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,16 @@ describe('@cobalt-ui/plugin-tailwind', () => {
const config = defineConfig(
{
outDir: './cjs/',
plugins: [
pluginTailwind({
...baseConfig,
filename: './actual.js',
format: 'cjs',
}),
],
plugins: [pluginTailwind({ ...baseConfig, filename: './actual.js', format: 'cjs' })],
},
{ cwd },
);
const { tokens, ast } = await parse(fs.readFileSync(new URL('../tokens.yaml', cwd), 'utf8'));
await build(tokens, { ast, config });
const { tokens, sources } = await parse([{ src: fs.readFileSync(new URL('../tokens.yaml', cwd), 'utf8') }], {
config,
});
await build(tokens, { sources, config });

expect(fs.readFileSync(new URL('./actual.js', cwd), 'utf8')).toMatchFileSnapshot(
await expect(fs.readFileSync(new URL('./actual.js', cwd), 'utf8')).toMatchFileSnapshot(
fileURLToPath(new URL('./want.js', cwd)),
);
});
Expand All @@ -83,20 +79,16 @@ describe('@cobalt-ui/plugin-tailwind', () => {
const config = defineConfig(
{
outDir: './cjs/',
plugins: [
pluginTailwind({
...baseConfig,
filename: './actual.js',
format: 'esm',
}),
],
plugins: [pluginTailwind({ ...baseConfig, filename: './actual.js', format: 'esm' })],
},
{ cwd },
);
const { tokens, ast } = await parse(fs.readFileSync(new URL('../tokens.yaml', cwd), 'utf8'));
await build(tokens, { ast, config });
const { tokens, sources } = await parse([{ src: fs.readFileSync(new URL('../tokens.yaml', cwd), 'utf8') }], {
config,
});
await build(tokens, { sources, config });

expect(fs.readFileSync(new URL('./actual.js', cwd), 'utf8')).toMatchFileSnapshot(
await expect(fs.readFileSync(new URL('./actual.js', cwd), 'utf8')).toMatchFileSnapshot(
fileURLToPath(new URL('./want.js', cwd)),
);
});
Expand Down

0 comments on commit 9bb214d

Please sign in to comment.