New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ts-auto-guard

Package Overview
Dependencies
Maintainers
2
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-auto-guard - npm Package Compare versions

Comparing version 1.0.0-alpha.13 to 1.0.0-alpha.14

26

lib/cli.js

@@ -89,2 +89,13 @@ #!/usr/bin/env node

{
description: 'Adds TypeGuard import to source file, to also export TypeGuard from source use with --import-guards. Optionally accepts a string to choose custom import alias.',
name: 'import-guards',
typeLabel: '{underline TypeGuard}',
type: String,
},
{
description: 'Overrides the default behavior for --import-guards by skipping export from source.',
name: 'prevent-export-imported',
type: Boolean,
},
{
description: 'Path to `tsconfig.json`.',

@@ -121,2 +132,15 @@ name: 'project',

}
if ('import-guards' in options) {
/** Checks if valid name passed as argument or replace with default if empty */
if (!options['import-guards']) {
options['import-guards'] = 'TypeGuards';
}
try {
eval("const " + options['import-guards'] + " = true");
}
catch (error) {
console.log('Please pass a valid import alias');
throw error;
}
}
_a.label = 2;

@@ -130,2 +154,4 @@ case 2:

exportAll: options['export-all'],
importGuards: options['import-guards'],
preventExportImported: options['prevent-export-imported'],
shortCircuitCondition: options.shortcircuit,

@@ -132,0 +158,0 @@ },

2

lib/index.d.ts
import { Project } from 'ts-morph';
export interface IProcessOptions {
exportAll?: boolean;
importGuards?: string;
preventExportImported?: boolean;
shortCircuitCondition?: string;

@@ -5,0 +7,0 @@ debug?: boolean;

@@ -591,2 +591,25 @@ "use strict";

].join('\n'));
if (options.importGuards) {
var relativeOutPath = './' +
outFile_1
.getFilePath()
.split('/')
.reverse()[0]
.replace(/\.(ts|tsx|d\.ts)$/, '');
var importStatement = "import * as " + options.importGuards + " from \"" + relativeOutPath + "\";";
var exportStatement = "export { " + options.importGuards + " };";
var _e = sourceFile.getStatements().reduce(function (reduced, node) {
var nodeText = node.getText().replace(/\s{2,}/g, ' ');
reduced.hasImport || (reduced.hasImport = nodeText.includes("import * as " + options.importGuards));
reduced.hasExport || (reduced.hasExport = nodeText.includes("export { " + options.importGuards + " }"));
reduced.statements += 1;
return reduced;
}, { hasImport: false, hasExport: false, statements: 0 }), hasImport = _e.hasImport, hasExport = _e.hasExport, statements = _e.statements;
if (!hasImport) {
sourceFile.insertStatements(0, importStatement);
}
if (!hasExport && !options.preventExportImported) {
sourceFile.insertStatements(!hasImport ? statements + 1 : statements, exportStatement);
}
}
outFile_1.formatText();

@@ -593,0 +616,0 @@ }

2

package.json
{
"name": "ts-auto-guard",
"version": "1.0.0-alpha.13",
"version": "1.0.0-alpha.14",
"description": "Generate type guard functions from TypeScript interfaces",

@@ -5,0 +5,0 @@ "homepage": "https://github.com/rhys-vdw/ts-auto-guard",

@@ -133,1 +133,22 @@ # ts-auto-guard

Using the `shortcircuit` option in combination with [uglify-js's `dead_code` and `global_defs` options](https://github.com/mishoo/UglifyJS2#compress-options) will let you omit the long and complicated checks from your production code.
## Add Import to Source File
ts-auto-guard supports an `ìmport-guards` flag. This flag will add an import statement at the top and a named export at the bottom of the source files for the generated type guards. The `ìmport-guards` flag also optionally accepts a custom name for the import alias, if none is passed then `TypeGuards` is used as a default.
If you would like to override the default behavior and not have the type guards exported from source use the `prevent-export-imported` flag with the `import-guards` flag.
```
$ ts-auto-guard --import-guards="Guards"
```
Will result in the following being added to your source code.
```ts
// my-project/Person.ts
import * as Guards from './Person.guard'
/** The rest of your source code */
export { Guards }
```

@@ -810,1 +810,33 @@ import { each, pull } from 'lodash'

)
testProcessProject(
'adds type guard import to source file and also exports',
{
// NOTE: This file is not automatically cleaned up with `formatText` after
// being modified so it requires this funky indentation to ensure that it is
// conforms to ts-morph's formatting.
'test.ts': `
/** @see {isEmpty} ts-auto-guard:type-guard */
export interface Empty { }
`,
},
{
'test.ts': `
import * as CustomGuardAlias from "./test.guard";
/** @see {isEmpty} ts-auto-guard:type-guard */
export interface Empty {}
export { CustomGuardAlias };`,
'test.guard.ts': `
import { Empty } from "./test";
export function isEmpty(obj: any, _argumentName?: string): obj is Empty {
return (
(obj !== null &&
typeof obj === "object" ||
typeof obj === "function")
)
}`,
},
{ options: { importGuards: 'CustomGuardAlias' } }
)

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc