@terrazzo/parser 0.0.10 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # @terrazzo/parser
2
+
3
+ ## 0.0.12
4
+
5
+ ### Patch Changes
6
+
7
+ - [#285](https://github.com/terrazzoapp/terrazzo/pull/285) [`e8a0df1`](https://github.com/terrazzoapp/terrazzo/commit/e8a0df1f3b50cf7cb292bcc475aae271feae4569) Thanks [@drwpow](https://github.com/drwpow)! - Add support for multiple token files
8
+
9
+ - Updated dependencies [[`e8a0df1`](https://github.com/terrazzoapp/terrazzo/commit/e8a0df1f3b50cf7cb292bcc475aae271feae4569)]:
10
+ - @terrazzo/token-tools@0.0.6
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # 💠 @terrazzo/parser
1
+ # @terrazzo/parser
2
2
 
3
3
  JS API for parsing / validating / transforming DTCG tokens.
4
4
 
package/build/index.d.ts CHANGED
@@ -4,7 +4,7 @@ import type { ConfigInit } from '../config.js';
4
4
  import type Logger from '../logger.js';
5
5
 
6
6
  export interface BuildRunnerOptions {
7
- ast: DocumentNode;
7
+ sources: { filename?: URL; src: string; document: DocumentNode }[];
8
8
  config: ConfigInit;
9
9
  logger?: Logger;
10
10
  }
@@ -73,8 +73,8 @@ export interface TransformHookOptions {
73
73
  mode?: string;
74
74
  },
75
75
  ): void;
76
- /** Momoa document */
77
- ast: DocumentNode;
76
+ /** Momoa documents */
77
+ sources: { filename?: URL; src: string; document: DocumentNode }[];
78
78
  }
79
79
 
80
80
  export interface BuildHookOptions {
@@ -82,8 +82,8 @@ export interface BuildHookOptions {
82
82
  tokens: Record<string, TokenNormalized>;
83
83
  /** Query transformed values */
84
84
  getTransforms(params: TransformParams): TokenTransformed[];
85
- /** Momoa document */
86
- ast: DocumentNode;
85
+ /** Momoa documents */
86
+ sources: { filename?: URL; src: string; document: DocumentNode }[];
87
87
  outputFile: (
88
88
  /** Filename to output (relative to outDir) */
89
89
  filename: string,
@@ -101,8 +101,8 @@ export interface BuildEndHookOptions {
101
101
  tokens: Record<string, TokenNormalized>;
102
102
  /** Query transformed values */
103
103
  getTransforms(params: TransformParams): TokenTransformed[];
104
- /** Momoa document */
105
- ast: DocumentNode;
104
+ /** Momoa documents */
105
+ sources: { filename?: URL; src: string; document: DocumentNode }[];
106
106
  /** Final files to be written */
107
107
  outputFiles: OutputFileExpanded[];
108
108
  }
package/build/index.js CHANGED
@@ -5,7 +5,7 @@ import Logger from '../logger.js';
5
5
  /**
6
6
  * @typedef {object} BuildRunnerOptions
7
7
  * @typedef {Record<string, TokenNormalized>} BuildRunnerOptions.tokens
8
- * @typedef {DocumentNode} BuildRunnerOptions.ast
8
+ * @typedef {Array} BuildRunnerOptions.sources
9
9
  * @typedef {ConfigInit} BuildRunnerOptions.config
10
10
  * @typedef {Logger} BuildRunnerOptions.logger
11
11
  * @typedef {import("@humanwhocodes/momoa").DocumentNode} DocumentNode
@@ -60,7 +60,7 @@ function validateTransformParams({ params, token, logger, pluginName }) {
60
60
  * @param {BuildOptions} options
61
61
  * @return {Promise<BuildResult>}
62
62
  */
63
- export default async function build(tokens, { ast, logger = new Logger(), config }) {
63
+ export default async function build(tokens, { sources, logger = new Logger(), config }) {
64
64
  const formats = {};
65
65
  const result = { outputFiles: [] };
66
66
 
@@ -95,7 +95,7 @@ export default async function build(tokens, { ast, logger = new Logger(), config
95
95
  if (typeof plugin.transform === 'function') {
96
96
  await plugin.transform({
97
97
  tokens,
98
- ast,
98
+ sources,
99
99
  getTransforms,
100
100
  setTransform(id, params) {
101
101
  if (transformsLocked) {
@@ -165,7 +165,7 @@ export default async function build(tokens, { ast, logger = new Logger(), config
165
165
  const pluginBuildStart = performance.now();
166
166
  await plugin.build({
167
167
  tokens,
168
- ast,
168
+ sources,
169
169
  getTransforms,
170
170
  outputFile(filename, contents) {
171
171
  const resolved = new URL(filename, config.outDir);
@@ -196,7 +196,7 @@ export default async function build(tokens, { ast, logger = new Logger(), config
196
196
  if (typeof plugin.buildEnd === 'function') {
197
197
  await plugin.buildEnd({
198
198
  tokens,
199
- ast,
199
+ sources,
200
200
  getTransforms,
201
201
  format: (formatID) => createFormatter(formatID),
202
202
  outputFiles: structruedClone(result.outputFiles),
package/lint/index.d.ts CHANGED
@@ -26,7 +26,8 @@ export interface LinterOptions<O = any> {
26
26
  id: string;
27
27
  severity: LintRuleSeverity;
28
28
  };
29
- ast: DocumentNode;
29
+ document: DocumentNode;
30
+ filename?: URL;
30
31
  source?: string;
31
32
  /** Any options the user has declared for this plugin */
32
33
  options?: O;
@@ -34,7 +35,8 @@ export interface LinterOptions<O = any> {
34
35
  export type Linter = (options: LinterOptions) => Promise<LintNotice[] | undefined>;
35
36
 
36
37
  export interface LintRunnerOptions {
37
- ast: DocumentNode;
38
+ document: DocumentNode;
39
+ filename?: URL;
38
40
  config: ConfigInit;
39
41
  logger: Logger;
40
42
  }
package/logger.d.ts CHANGED
@@ -13,12 +13,14 @@ export interface LogEntry {
13
13
  message: string;
14
14
  /** (optional) Prefix message with label */
15
15
  label?: string;
16
+ /** (optional) File in disk */
17
+ filename?: URL;
16
18
  /** Continue on error? (default: false) */
17
19
  continueOnError?: boolean;
18
20
  /** (optional) Show a code frame for the erring node */
19
21
  node?: AnyNode;
20
22
  /** (optional) To show a code frame, provide the original source code */
21
- source?: string;
23
+ src?: string;
22
24
  }
23
25
 
24
26
  export interface DebugEntry {
@@ -29,7 +31,7 @@ export interface DebugEntry {
29
31
  /** Error message to be logged */
30
32
  message: string;
31
33
  /** (optional) Show code below message */
32
- codeFrame?: { source: string; line: number; column: number };
34
+ codeFrame?: { src: string; line: number; column: number };
33
35
  /** (optional) Display performance timing */
34
36
  timing?: number;
35
37
  }
package/logger.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { codeFrameColumns } from '@babel/code-frame';
2
2
  import color from 'picocolors';
3
+ import { fileURLToPath } from 'node:url';
3
4
  import wcmatch from 'wildcard-match';
4
5
 
5
6
  export const LOG_ORDER = ['error', 'warn', 'info', 'debug'];
@@ -23,8 +24,9 @@ export function formatMessage(entry, severity) {
23
24
  if (severity in MESSAGE_COLOR) {
24
25
  message = MESSAGE_COLOR[severity](message);
25
26
  }
26
- if (entry.source) {
27
- message = `${message}\n\n${codeFrameColumns(entry.source, { start: entry.node?.loc?.start })}`;
27
+ if (entry.src) {
28
+ const start = entry.node?.loc?.start;
29
+ message = `${message}\n\n${entry.filename ? `${fileURLToPath(entry.filename)}:${start?.line ?? 0}:${start?.column ?? 0}\n\n` : ''}${codeFrameColumns(entry.src, { start })}`;
28
30
  }
29
31
  return message;
30
32
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@terrazzo/parser",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "description": "Parser/validator for the Design Tokens Community Group (DTCG) standard.",
5
5
  "type": "module",
6
6
  "author": {
@@ -42,11 +42,11 @@
42
42
  "picocolors": "^1.0.1",
43
43
  "wildcard-match": "^5.1.3",
44
44
  "yaml": "^2.4.5",
45
- "@terrazzo/token-tools": "^0.0.4"
45
+ "@terrazzo/token-tools": "^0.0.6"
46
46
  },
47
47
  "scripts": {
48
48
  "lint": "biome check .",
49
- "test": "pnpm run \"/^test:.*/\"",
49
+ "test": "pnpm --filter @terrazzo/parser run \"/^test:.*/\"",
50
50
  "test:js": "vitest run",
51
51
  "test:ts": "tsc --noEmit"
52
52
  }
package/parse/index.d.ts CHANGED
@@ -5,24 +5,32 @@ import type Logger from '../logger.js';
5
5
 
6
6
  export * from './validate.js';
7
7
 
8
+ export interface ParseInput {
9
+ /** Source filename (if read from disk) */
10
+ filename?: URL;
11
+ /** JSON/YAML string, or JSON-serializable object (if already in memory) */
12
+ src: string | object;
13
+ }
14
+
8
15
  export interface ParseOptions {
9
16
  logger?: Logger;
17
+ config: ConfigInit;
10
18
  /** Skip lint step (default: false) */
11
19
  skipLint?: boolean;
12
- config: ConfigInit;
13
20
  /** Continue on error? (Useful for `tz check`) (default: false) */
14
21
  continueOnError?: boolean;
15
22
  }
16
23
 
17
24
  export interface ParseResult {
18
25
  tokens: Record<string, TokenNormalized>;
19
- ast: DocumentNode;
26
+ /** ASTs are returned in order of input array */
27
+ sources: { filename?: URL; src: string; document: DocumentNode }[];
20
28
  }
21
29
 
22
30
  /**
23
31
  * Parse and validate Tokens JSON, and lint it
24
32
  */
25
- export default function parse(input: string | object, options?: ParseOptions): Promise<ParseResult>;
33
+ export default function parse(input: ParseInput[], options?: ParseOptions): Promise<ParseResult>;
26
34
 
27
35
  /** Determine if an input is likely a JSON string */
28
36
  export function maybeJSONString(input: unknown): boolean;