@rohal12/spindle 0.62.0 → 0.62.2

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.
@@ -7,7 +7,6 @@
7
7
  */
8
8
  import {
9
9
  analyzeMarkup,
10
- lineColumn,
11
10
  MarkupError,
12
11
  parseMarkup,
13
12
  tokenizeMarkup,
@@ -24,6 +23,8 @@ import {
24
23
  type ParametersOf,
25
24
  } from '../code-check';
26
25
  import { CodeSyntaxError } from '../js-lexer';
26
+ import { sourcePositions } from './line-column';
27
+ import { isRawMacro } from './ast';
27
28
 
28
29
  /** A passage to validate. */
29
30
  export interface MarkupPassage {
@@ -158,14 +159,24 @@ function closestName(
158
159
  name: string,
159
160
  names: Iterable<string>,
160
161
  ): string | undefined {
162
+ // Suggestions are optional: oversized authored names must not hold up validation.
163
+ if (name.length > 256) return undefined;
161
164
  const lower = name.toLowerCase();
162
165
  let best: string | undefined;
163
166
  let bestDistance = Math.max(2, Math.floor(lower.length / 3)) + 1;
167
+ let budget = 100_000;
168
+ let candidates = 1000;
164
169
  for (const candidate of names) {
170
+ if (--candidates < 0) break;
171
+ if (candidate.length > 256) continue;
172
+ if (Math.abs(candidate.length - lower.length) >= bestDistance) continue;
173
+ budget -= lower.length * candidate.length;
174
+ if (budget < 0) break;
165
175
  const d = distance(lower, candidate.toLowerCase());
166
176
  if (d < bestDistance) {
167
177
  best = candidate;
168
178
  bestDistance = d;
179
+ if (d === 0) break;
169
180
  }
170
181
  }
171
182
  return best;
@@ -202,9 +213,20 @@ export function validateMarkup(
202
213
  options: MarkupValidationOptions,
203
214
  ): MarkupDiagnostic[] {
204
215
  const diagnostics: MarkupDiagnostic[] = [];
216
+ const positions = new Map<
217
+ MarkupPassage,
218
+ ReturnType<typeof sourcePositions>
219
+ >();
205
220
  const hooks = {
206
221
  ...(options.isBlockMacro && { isBlock: options.isBlockMacro }),
207
222
  ...(options.branchParents && { branchParents: options.branchParents }),
223
+ isRaw: (name: string) =>
224
+ isRawMacro(
225
+ name,
226
+ options.parametersOf
227
+ ? options.parametersOf.widget?.(name) !== undefined
228
+ : undefined,
229
+ ),
208
230
  };
209
231
  const report = (
210
232
  passage: MarkupPassage,
@@ -214,7 +236,12 @@ export function validateMarkup(
214
236
  message: string,
215
237
  data?: MarkupDiagnosticData,
216
238
  ) => {
217
- const { line, column } = lineColumn(passage.content, start);
239
+ let position = positions.get(passage);
240
+ if (!position) {
241
+ position = sourcePositions(passage.content);
242
+ positions.set(passage, position);
243
+ }
244
+ const { line, column } = position(start);
218
245
  const d: MarkupDiagnostic = {
219
246
  passage: passage.name,
220
247
  line,
@@ -276,7 +303,7 @@ export function validateMarkup(
276
303
  }
277
304
  try {
278
305
  // The tokens first: they don't depend on which macros take a body
279
- const tokens = tokenizeMarkup(passage.content);
306
+ const tokens = tokenizeMarkup(passage.content, { hooks });
280
307
  addWidgets(tokens);
281
308
  if (!reported) continue;
282
309
  parseMarkup(passage.content, { hooks });
@@ -6,6 +6,7 @@ import {
6
6
  tokenizeMarkupTolerant,
7
7
  } from './markup/parse';
8
8
  import { isCodeAttribute } from './markup/code-attributes';
9
+ import { isRawMacro } from './markup/ast';
9
10
  import { errorMessage } from './utils/error-message';
10
11
  import {
11
12
  CodeSyntaxError,
@@ -46,11 +47,21 @@ import { parse } from 'acorn';
46
47
  function tokensOf(
47
48
  text: string,
48
49
  textMode: boolean,
49
- { tolerant }: { tolerant?: boolean } = {},
50
+ {
51
+ tolerant,
52
+ parametersOf,
53
+ }: { tolerant?: boolean; parametersOf?: ParametersOf } = {},
50
54
  ): Token[] {
51
- if (tolerant) return tokenizeMarkupTolerant(text, { text: textMode }).tokens;
55
+ const options = {
56
+ text: textMode,
57
+ hooks: parametersOf && {
58
+ isRaw: (name: string) =>
59
+ isRawMacro(name, parametersOf.widget?.(name) !== undefined),
60
+ },
61
+ };
62
+ if (tolerant) return tokenizeMarkupTolerant(text, options).tokens;
52
63
  try {
53
- return tokenizeMarkup(text, { text: textMode });
64
+ return tokenizeMarkup(text, options);
54
65
  } catch (err) {
55
66
  if (err instanceof MarkupError) return [];
56
67
  throw err;
@@ -177,7 +188,7 @@ function initializerSyntaxError(
177
188
  ): { message: string; pos: number } | undefined {
178
189
  const prefix = 'return (';
179
190
  try {
180
- parse(prefix + expr + ')', {
191
+ parse(prefix + expr + '\n)', {
181
192
  ecmaVersion: 'latest',
182
193
  allowReturnOutsideFunction: true,
183
194
  });
@@ -335,7 +346,7 @@ export function parseStoryVariables(
335
346
  const expr = content.slice(valueStart, valueEnd);
336
347
  let value: unknown;
337
348
  try {
338
- value = new Function('return (' + expr + ')')();
349
+ value = new Function('return (' + expr + '\n)')();
339
350
  } catch (err) {
340
351
  throw new Error(
341
352
  `${passageName}: Failed to evaluate "${sigil}${name} = ${expr}": ${errorMessage(err)}`,
@@ -797,7 +808,10 @@ function collectTokenRefs(
797
808
  // Selectors (`{.{$cls} button}`) are interpolated when rendered
798
809
  scanSelectors(content, token, at, emit, options);
799
810
 
800
- if (storeVarMacros.has(token.name.toLowerCase())) {
811
+ if (
812
+ storeVarMacros.has(token.name.toLowerCase()) &&
813
+ !parametersOf.widget?.(token.name)
814
+ ) {
801
815
  const lead = token.rawArgs.length - token.rawArgs.trimStart().length;
802
816
  const first = token.rawArgs.trim().split(/\s+/)[0] ?? '';
803
817
  const quoted = QUOTED_VAR_ARG_RE.exec(first);
@@ -818,7 +832,10 @@ function collectTokenRefs(
818
832
  }
819
833
  }
820
834
 
821
- if (token.name.toLowerCase() === 'do') {
835
+ if (
836
+ token.name.toLowerCase() === 'do' &&
837
+ !parametersOf.widget?.(token.name)
838
+ ) {
822
839
  // A {do} body is JavaScript: scan its source text as code, however
823
840
  // the markup tokens split it up.
824
841
  let close = t + 1;
package/src/tooling.ts CHANGED
@@ -10,7 +10,7 @@ import {
10
10
  type MarkupDiagnostic,
11
11
  type MarkupPassage,
12
12
  } from './markup/validate';
13
- import { isBlockMacro } from './markup/ast';
13
+ import { isBlockMacro, isRawMacro } from './markup/ast';
14
14
  import { tokenizeMarkupTolerant } from './markup/parse';
15
15
  import {
16
16
  parameterLookup,
@@ -263,10 +263,17 @@ export function passagePieces(
263
263
  source: string,
264
264
  macros: Iterable<ToolingMacro> = [],
265
265
  ): Piece[] {
266
+ const all = [...macros];
267
+ const parametersOf = parameterLookup(all);
266
268
  return passagePiecesOfTokens(
267
269
  source,
268
- macros,
269
- tokenizeMarkupTolerant(source).tokens,
270
+ all,
271
+ tokenizeMarkupTolerant(source, {
272
+ hooks: {
273
+ isRaw: (name) =>
274
+ isRawMacro(name, parametersOf.widget?.(name) !== undefined),
275
+ },
276
+ }).tokens,
270
277
  );
271
278
  }
272
279