Skip to content

Commit c8863c7

Browse files
[npm install] node weirdness, fix standalone expressions and more tests
1 parent 76dfd84 commit c8863c7

6 files changed

Lines changed: 344 additions & 14 deletions

File tree

apps/test-tokenizer/src/test-maker/cache/cache.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

apps/test-tokenizer/src/test-maker/tests/auto-syntax-validator-tests.interface.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5307,6 +5307,18 @@ export const AUTO_SYNTAX_TESTS: IAutoSyntaxValidatorTest[] = [
53075307
`end`,
53085308
],
53095309
},
5310+
{
5311+
name: `problems`,
5312+
code: [`compile_opt idl2`, `!quiet`, `end`],
5313+
},
5314+
{
5315+
name: `no problems (procedure)`,
5316+
code: [`compile_opt idl2`, `a = hash()`, `a`, `end`],
5317+
},
5318+
{
5319+
name: `problems`,
5320+
code: [`compile_opt idl2`, `a = hash()`, `a['key']`, `end`],
5321+
},
53105322
],
53115323
},
53125324
{

libs/parsing/syntax-validators/src/lib/validators/standalone-expression.ts

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
TOKEN_NAMES,
1414
} from '@idl/tokenizer';
1515
import { IDL_PROBLEM_CODES } from '@idl/types/problem-codes';
16+
import { PositionArray } from '@idl/types/tokenizer';
1617

1718
/**
1819
* Tokens that can appear first
@@ -76,6 +77,18 @@ AFTER_VAR[TOKEN_NAMES.ASSIGNMENT] = true;
7677
AFTER_VAR[TOKEN_NAMES.OPERATOR_INCREMENT_DECREMENT] = true;
7778
AFTER_VAR[TOKEN_NAMES.CALL_PROCEDURE_METHOD] = true;
7879

80+
/**
81+
* Checks if two tokens are on the same line or not
82+
*
83+
* If nothing after, then returns true because we are on the "same" line
84+
*/
85+
function ValidateSameLine(ref: PositionArray, next?: PositionArray) {
86+
if (!next) {
87+
return false;
88+
}
89+
return ref[0] === next[0];
90+
}
91+
7992
/**
8093
* Call back to make sure that, if we have children, there is a comma first
8194
*/
@@ -129,20 +142,40 @@ function Callback(token: TreeBranchToken, parsed: IParsed) {
129142
/** Get the end of the chain */
130143
i = GetChainEnd(kids, i);
131144

132-
/** If the next token is not allowed, then we have problem */
133-
if (!(kids[i + 1]?.name in AFTER_VAR) && kids[i + 1]) {
134-
parsed.parseProblems.push(
135-
SyntaxProblemWithTranslation(
136-
inMain && parsed.type === 'notebook'
137-
? IDL_PROBLEM_CODES.IMPLIED_PRINT_NOTEBOOK
138-
: IDL_PROBLEM_CODES.STANDALONE_EXPRESSION,
139-
kids[orig].pos,
140-
(kids[i] as TreeBranchToken)?.end?.pos || kids[i].pos
141-
)
142-
);
143-
} else {
145+
switch (true) {
146+
/**
147+
* Check last item in chain for valid skip condition
148+
*/
149+
case kids[i].name === TOKEN_NAMES.OPERATOR_INCREMENT_DECREMENT:
150+
break;
151+
/**
152+
* If there's a next token, but it is not on the same line, then
153+
* we have a problem
154+
*/
155+
case !(kids[i + 1] !== undefined) &&
156+
!ValidateSameLine(
157+
(kids[i] as TreeBranchToken)?.end?.pos || kids[i].pos,
158+
kids[i + 1]?.pos
159+
):
160+
/**
161+
* If the next token can't be next, then we have a problem
162+
*/
163+
// eslint-disable-next-line no-fallthrough
164+
case !(kids[i + 1]?.name in AFTER_VAR):
165+
parsed.parseProblems.push(
166+
SyntaxProblemWithTranslation(
167+
inMain && parsed.type === 'notebook'
168+
? IDL_PROBLEM_CODES.IMPLIED_PRINT_NOTEBOOK
169+
: IDL_PROBLEM_CODES.STANDALONE_EXPRESSION,
170+
kids[orig].pos,
171+
(kids[i] as TreeBranchToken)?.end?.pos || kids[i].pos
172+
)
173+
);
174+
break;
144175
// shift
145-
i++;
176+
default:
177+
i++;
178+
break;
146179
}
147180

148181
continue;

libs/tests/syntax-validators/src/lib/code.0.not-closed.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ describe(`[auto generated] Detects problems with statements not being closed`, (
4444
end: [0, 0, 1],
4545
canReport: true,
4646
},
47+
{
48+
code: 108,
49+
info: 'Standalone expression detected. One or more statements need to be assigned to a variable or have a value assigned to them.',
50+
start: [0, 0, 1],
51+
end: [0, 0, 1],
52+
canReport: true,
53+
},
4754
];
4855

4956
// verify results
@@ -451,6 +458,13 @@ describe(`[auto generated] Detects problems with statements not being closed`, (
451458
end: [0, 0, 1],
452459
canReport: true,
453460
},
461+
{
462+
code: 108,
463+
info: 'Standalone expression detected. One or more statements need to be assigned to a variable or have a value assigned to them.',
464+
start: [0, 0, 1],
465+
end: [0, 0, 1],
466+
canReport: true,
467+
},
454468
];
455469

456470
// verify results

libs/tests/syntax-validators/src/lib/code.108.standalone-expression.spec.ts

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,121 @@ describe(`[auto generated] Detect standalone expressions`, () => {
303303
tokenized.parseProblems.concat(tokenized.postProcessProblems)
304304
).toEqual(expected);
305305
});
306+
307+
it(`[auto generated] problems`, async () => {
308+
// create index
309+
const index = new IDLIndex(
310+
new LogManager({
311+
alert: () => {
312+
// do nothing
313+
},
314+
}),
315+
0
316+
);
317+
318+
// test code to extract tokens from
319+
const code = [`compile_opt idl2`, `!quiet`, `end`];
320+
321+
// extract tokens
322+
const tokenized = await index.getParsedProCode(
323+
'not-real',
324+
code,
325+
new CancellationToken(),
326+
{ postProcess: true }
327+
);
328+
329+
// define expected tokens
330+
const expected: SyntaxProblems = [
331+
{
332+
code: 108,
333+
info: 'Standalone expression detected. One or more statements need to be assigned to a variable or have a value assigned to them.',
334+
start: [1, 0, 6],
335+
end: [1, 0, 6],
336+
canReport: true,
337+
},
338+
];
339+
340+
// verify results
341+
expect(
342+
tokenized.parseProblems.concat(tokenized.postProcessProblems)
343+
).toEqual(expected);
344+
});
345+
346+
it(`[auto generated] no problems (procedure)`, async () => {
347+
// create index
348+
const index = new IDLIndex(
349+
new LogManager({
350+
alert: () => {
351+
// do nothing
352+
},
353+
}),
354+
0
355+
);
356+
357+
// test code to extract tokens from
358+
const code = [`compile_opt idl2`, `a = hash()`, `a`, `end`];
359+
360+
// extract tokens
361+
const tokenized = await index.getParsedProCode(
362+
'not-real',
363+
code,
364+
new CancellationToken(),
365+
{ postProcess: true }
366+
);
367+
368+
// define expected tokens
369+
const expected: SyntaxProblems = [
370+
{
371+
code: 104,
372+
info: 'Unused variable "a"',
373+
start: [1, 0, 1],
374+
end: [1, 0, 1],
375+
canReport: true,
376+
},
377+
];
378+
379+
// verify results
380+
expect(
381+
tokenized.parseProblems.concat(tokenized.postProcessProblems)
382+
).toEqual(expected);
383+
});
384+
385+
it(`[auto generated] problems`, async () => {
386+
// create index
387+
const index = new IDLIndex(
388+
new LogManager({
389+
alert: () => {
390+
// do nothing
391+
},
392+
}),
393+
0
394+
);
395+
396+
// test code to extract tokens from
397+
const code = [`compile_opt idl2`, `a = hash()`, `a['key']`, `end`];
398+
399+
// extract tokens
400+
const tokenized = await index.getParsedProCode(
401+
'not-real',
402+
code,
403+
new CancellationToken(),
404+
{ postProcess: true }
405+
);
406+
407+
// define expected tokens
408+
const expected: SyntaxProblems = [
409+
{
410+
code: 108,
411+
info: 'Standalone expression detected. One or more statements need to be assigned to a variable or have a value assigned to them.',
412+
start: [2, 0, 1],
413+
end: [2, 7, 1],
414+
canReport: true,
415+
},
416+
];
417+
418+
// verify results
419+
expect(
420+
tokenized.parseProblems.concat(tokenized.postProcessProblems)
421+
).toEqual(expected);
422+
});
306423
});

0 commit comments

Comments
 (0)