Skip to content

Commit 510702c

Browse files
committed
feature: @putout/printer: type-checker: improve handling of absent arrow
1 parent 10a589d commit 510702c

4 files changed

Lines changed: 26 additions & 3 deletions

File tree

lib/tokenize/expressions/array-expression/comma.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export const isNewlineAfterComma = createTypeChecker([
3030

3131
const isSimpleBetweenObjects = createTypeChecker([
3232
['+', callWithNext(isObjectExpression)],
33-
['-: SpreadElement'],
34-
['-: Identifier'],
33+
['-: -> SpreadElement'],
34+
['-: -> Identifier'],
3535
['+: -> !CallExpression'],
3636
]);
3737

lib/tokenize/expressions/binary-expression/is-concatenation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const isConcatenation = createTypeChecker([
66
['-: parentPath -> ReturnStatement'],
77
['-: node.operator -> !', isPlus],
88
['-: node.loc', isSameLine],
9-
['+: BinaryExpression'],
9+
['+: -> BinaryExpression'],
1010
]);
1111

1212
function isSameLine(loc) {

lib/tokenize/type-checker/type-checker.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {instrument as _instrument} from '#type-checker/instrument';
33
import {parseOperation, parseTypeNames} from './parsers.js';
44
import {equal, maybeCall} from './comparators.js';
55

6+
const isString = (a) => typeof a === 'string';
7+
68
const SKIP = [
79
Infinity,
810
false,
@@ -29,6 +31,7 @@ export const createTypeChecker = (typeNames, overrides = {}) => {
2931
}
3032

3133
validateResults(results);
34+
validateTypeNames(checkers);
3235

3336
const typeChecker = (path, options) => {
3437
for (const [index, {result, selector, typeName, not}] of checkers.entries()) {
@@ -54,3 +57,12 @@ function validateResults(results) {
5457
if (!results.has(true))
5558
throw Error(`☝️Looks like type checker missing successful route ('+'), it will always fail`);
5659
}
60+
61+
const hasTypeName = ({typeName}) => isString(typeName);
62+
63+
function validateTypeNames(checkers) {
64+
for (const {typeName} of checkers.filter(hasTypeName)) {
65+
if (typeName.includes(':'))
66+
throw Error(`☝️Looks like typeName includes ':', most likely you forget the arrow: ' -> '`);
67+
}
68+
}

lib/tokenize/type-checker/type-checker.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,3 +501,14 @@ test('printer: type-checker: isFunction: cut options', (t) => {
501501
t.ok(result);
502502
t.end();
503503
});
504+
505+
test('printer: type-checker: no arrow', (t) => {
506+
const [error] = tryCatch(createTypeChecker, [
507+
'+: ClassDeclaration',
508+
]);
509+
510+
const expected = `☝️Looks like typeName includes ':', most likely you forget the arrow: ' -> '`;
511+
512+
t.equal(error.message, expected);
513+
t.end();
514+
});

0 commit comments

Comments
 (0)