Skip to content

Commit

Permalink
Don't error on other modules. Oops
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobgardner committed Jul 8, 2019
1 parent 59882cf commit 5d9b764
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export function assertIsType<T = _RUNTIME_CHECK_ANY>(

export function isType<T = _RUNTIME_CHECK_ANY>(data: unknown): data is T;

// This is a useless function for the default export so it's not used
export default function(arg: never): never;

// export class _RUNTIME_CHECK_VALIDATION_ERROR {}

// export class ValidationError extends _RUNTIME_CHECK_VALIDATION_ERROR {}
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function transformer(program: ts.Program /*, config: Config*/) {
if (errors.length) {
const lines = errors.map(
({ message, file, line, character }) =>
`Error [runtime-check transform]: ${message}. This error occured in ${file}, Line: ${line}:${character}`,
`Error [runtime-check transform]: ${message}. This error occured in ${file}:${line}:${character}`,
);

throw new Error(lines.join('\n'));
Expand Down
43 changes: 33 additions & 10 deletions src/validationVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ import { writeRuntimeValidatorToFile } from './buildRuntimeValidator';
// TODO: This can all be broken up/abstracted/fped quite a bit.
// TODO: Organize this better in a way that makes more logical sense

function isImportThisModule(node: ts.ImportDeclaration): boolean {
const { moduleSpecifier } = node;

if (ts.isStringLiteral(moduleSpecifier)) {
if (moduleSpecifier.text === pjson.name) {
return true;
}
}

return false;
}

/*
This is where most of the work occurs for discovering the validation
functions and converting them to the usable types in the final build.
Expand Down Expand Up @@ -114,11 +126,16 @@ export class ValidationVisitor {
if (namedBindings) {
if (ts.isNamespaceImport(namedBindings)) {
// * as something
// TODO: Implement
return emitErrorFromNode(
node,
`Namespace import is not yet supported. Please use \`import { ${TYPE_ASSERTION_NAME} } from '${pjson.name}';\``,
);

if (isImportThisModule(node)) {
// TODO: Implement
return emitErrorFromNode(
node,
`Namespace import is not yet supported. Please use \`import { ${TYPE_ASSERTION_NAME} } from '${pjson.name}';\``,
);
}

return node;
} else {
// {namedImports}
return transformNamedImport(
Expand All @@ -130,11 +147,17 @@ export class ValidationVisitor {
}
} else if (name) {
// defaultImport
// TODO: Implement
return emitErrorFromNode(
node,
`Default import is not yet supported. Please use \`import { ${TYPE_ASSERTION_NAME} } from '${pjson.name}';\``,
);

// TODO: Add regression tests making sure to test files where other
// modules are being imported....
if (isImportThisModule(node)) {
return emitErrorFromNode(
node,
`Default import is not yet supported. Please use \`import { ${TYPE_ASSERTION_NAME} } from '${pjson.name}';\``,
);
}

return node;
}

return node;
Expand Down

0 comments on commit 5d9b764

Please sign in to comment.