ts-auto-guard
Advanced tools
Comparing version 4.0.0 to 4.1.0
@@ -16,3 +16,7 @@ #!/usr/bin/env node | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -135,3 +139,3 @@ if (k2 === undefined) k2 = k; | ||
]; | ||
var options = __assign({ paths: [], help: false }, command_line_args_1.default(optionList)); | ||
var options = __assign({ paths: [], help: false }, (0, command_line_args_1.default)(optionList)); | ||
function run() { | ||
@@ -155,3 +159,3 @@ return __awaiter(this, void 0, void 0, function () { | ||
try { | ||
eval("const " + options['import-guards'] + " = true"); | ||
eval("const ".concat(options['import-guards'], " = true")); | ||
} | ||
@@ -166,3 +170,3 @@ catch (error) { | ||
_a.trys.push([2, 4, , 5]); | ||
return [4 /*yield*/, index_1.generate({ | ||
return [4 /*yield*/, (0, index_1.generate)({ | ||
paths: options.paths, | ||
@@ -200,5 +204,5 @@ processOptions: { | ||
var _a = require('../package.json'), name = _a.name, version = _a.version, description = _a.description; | ||
console.log(command_line_usage_1.default([ | ||
console.log((0, command_line_usage_1.default)([ | ||
{ | ||
header: name + " " + version, | ||
header: "".concat(name, " ").concat(version), | ||
content: description, | ||
@@ -205,0 +209,0 @@ }, |
import { Project } from 'ts-morph'; | ||
export declare function assertNever<T>(_: never): T; | ||
export interface IProcessOptions { | ||
@@ -3,0 +4,0 @@ exportAll?: boolean; |
189
lib/index.js
@@ -55,5 +55,10 @@ "use strict"; | ||
}; | ||
var __spread = (this && this.__spread) || function () { | ||
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); | ||
return ar; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
@@ -72,3 +77,3 @@ var __values = (this && this.__values) || function(o) { | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.processProject = exports.generate = void 0; | ||
exports.processProject = exports.generate = exports.assertNever = void 0; | ||
var ts_morph_1 = require("ts-morph"); | ||
@@ -82,3 +87,3 @@ var GENERATED_WARNING = 'WARNING: Do not manually change this file.'; | ||
} | ||
console.error.apply(console, __spread(["ERROR: " + message], args)); | ||
console.error.apply(console, __spreadArray(["ERROR: ".concat(message)], __read(args), false)); | ||
} | ||
@@ -99,4 +104,4 @@ function lowerFirst(s) { | ||
.getDeclarations() | ||
.reduce(function (acc, node) { return __spread(acc, [node], node.getAncestors()); }, []) | ||
.filter(ts_morph_1.Node.isExportableNode) | ||
.reduce(function (acc, node) { return __spreadArray(__spreadArray(__spreadArray([], __read(acc), false), [node], false), __read(node.getAncestors()), false); }, []) | ||
.filter(ts_morph_1.Node.isExportable) | ||
.find(function (n) { return n.isExported(); }) || null); | ||
@@ -113,3 +118,3 @@ } | ||
if (!exportable.isExported()) { | ||
reportError(name + " is not exported from " + sourceFile.getFilePath()); | ||
reportError("".concat(name, " is not exported from ").concat(sourceFile.getFilePath())); | ||
} | ||
@@ -119,3 +124,3 @@ addDependency(sourceFile, name, isDefault); | ||
function outFilePath(sourcePath, guardFileName) { | ||
var outPath = sourcePath.replace(/\.(ts|tsx|d\.ts)$/, "." + guardFileName + ".ts"); | ||
var outPath = sourcePath.replace(/\.(ts|tsx|d\.ts)$/, ".".concat(guardFileName, ".ts")); | ||
if (outPath === sourcePath) | ||
@@ -130,3 +135,3 @@ throw new Error('Internal Error: sourcePath and outFilePath are identical: ' + outPath); | ||
else { | ||
console.warn(sourceFile.getFilePath() + " is named like a guard file, but does not contain the generated header. Consider removing or renaming the file, or change the guardFileName setting."); | ||
console.warn("".concat(sourceFile.getFilePath(), " is named like a guard file, but does not contain the generated header. Consider removing or renaming the file, or change the guardFileName setting.")); | ||
} | ||
@@ -191,3 +196,3 @@ } | ||
if (command !== 'type-guard') { | ||
reportError("command " + command + " is not supported!"); | ||
reportError("command ".concat(command, " is not supported!")); | ||
return null; | ||
@@ -243,40 +248,40 @@ } | ||
function eq(a, b) { | ||
return a + " === " + b; | ||
return "".concat(a, " === ").concat(b); | ||
} | ||
function ne(a, b) { | ||
return a + " !== " + b; | ||
return "".concat(a, " !== ").concat(b); | ||
} | ||
function typeOf(varName, type) { | ||
return eq("typeof " + varName, "\"" + type + "\""); | ||
return eq("typeof ".concat(varName), "\"".concat(type, "\"")); | ||
} | ||
function typeUnionConditions(varName, types, addDependency, project, path, arrayDepth, records, outFile, options) { | ||
var conditions = []; | ||
conditions.push.apply(conditions, __spread(types | ||
conditions.push.apply(conditions, __spreadArray([], __read(types | ||
.map(function (type) { | ||
return typeConditions(varName, type, addDependency, project, path, arrayDepth, true, records, outFile, options); | ||
}) | ||
.filter(function (v) { return v !== null; }))); | ||
return ors.apply(void 0, __spread(conditions)); | ||
.filter(function (v) { return v !== null; })), false)); | ||
return ors.apply(void 0, __spreadArray([], __read(conditions), false)); | ||
} | ||
function typeIntersectionConditions(varName, types, addDependency, project, path, arrayDepth, records, outFile, options) { | ||
var conditions = []; | ||
conditions.push.apply(conditions, __spread(types | ||
conditions.push.apply(conditions, __spreadArray([], __read(types | ||
.map(function (type) { | ||
return typeConditions(varName, type, addDependency, project, path, arrayDepth, true, records, outFile, options); | ||
}) | ||
.filter(function (v) { return v !== null; }))); | ||
return ands.apply(void 0, __spread(conditions)); | ||
.filter(function (v) { return v !== null; })), false)); | ||
return ands.apply(void 0, __spreadArray([], __read(conditions), false)); | ||
} | ||
function parens(code) { | ||
return "(" + code + ")"; | ||
return "(".concat(code, ")"); | ||
} | ||
function arrayCondition(varName, arrayType, addDependency, project, path, arrayDepth, records, outFile, options) { | ||
if (arrayType.getText() === 'never') { | ||
return ands("Array.isArray(" + varName + ")", eq(varName + ".length", '0')); | ||
return ands("Array.isArray(".concat(varName, ")"), eq("".concat(varName, ".length"), '0')); | ||
} | ||
var indexIdentifier = "i" + arrayDepth; | ||
var elementPath = path + "[${" + indexIdentifier + "}]"; | ||
var indexIdentifier = "i".concat(arrayDepth); | ||
var elementPath = "".concat(path, "[${").concat(indexIdentifier, "}]"); | ||
var conditions = typeConditions('e', arrayType, addDependency, project, elementPath, arrayDepth + 1, true, records, outFile, options); | ||
if (conditions === null) { | ||
return "Array.isArray(" + varName + ")"; | ||
return "Array.isArray(".concat(varName, ")"); | ||
} | ||
@@ -286,5 +291,5 @@ // Bit of a hack, just check if the second argument is used before actually | ||
var secondArg = conditions.includes(elementPath) | ||
? ", " + indexIdentifier + ": number" | ||
? ", ".concat(indexIdentifier, ": number") | ||
: ''; | ||
return ands("Array.isArray(" + varName + ")", varName + ".every((e: any" + secondArg + ") =>\n" + conditions + "\n)"); | ||
return ands("Array.isArray(".concat(varName, ")"), "".concat(varName, ".every((e: any").concat(secondArg, ") =>\n").concat(conditions, "\n)")); | ||
} | ||
@@ -296,4 +301,16 @@ function objectTypeCondition(varName, callable) { | ||
} | ||
function indexKeyTypeToString(type) { | ||
switch (true) { | ||
case type.isString(): | ||
return 'string'; | ||
case type.isNumber(): | ||
return 'number'; | ||
case type.isAny(): | ||
return 'any'; | ||
default: | ||
throw new Error("Invalid type for index key: ".concat(type.getText(), ". Only string or number are expected.")); | ||
} | ||
} | ||
function objectCondition(varName, type, addDependency, project, path, arrayDepth, records, outFile, options) { | ||
var _a, _b; | ||
var _a; | ||
var conditions = []; | ||
@@ -304,3 +321,3 @@ var symbol = type.getSymbol(); | ||
// tslint:disable-next-line:no-console | ||
console.error("Unable to get symbol for type " + type.getText()); | ||
console.error("Unable to get symbol for type ".concat(type.getText())); | ||
return typeOf(varName, 'object'); | ||
@@ -312,3 +329,3 @@ } | ||
if (declaration === undefined) { | ||
reportError("Couldn't find declaration for type " + type.getText()); | ||
reportError("Couldn't find declaration for type ".concat(type.getText())); | ||
return null; | ||
@@ -324,3 +341,3 @@ } | ||
!commentBefore.getText().includes(suppressComment)) { | ||
console.warn("\nIt seems that " + varName + " has a function type.\nNote that it is impossible to check if a function has the correct signature and return type at runtime.\nTo disable this warning, put comment \"" + suppressComment + "\" before the declaration.\n"); | ||
console.warn("\nIt seems that ".concat(varName, " has a function type.\nNote that it is impossible to check if a function has the correct signature and return type at runtime.\nTo disable this warning, put comment \"").concat(suppressComment, "\" before the declaration.\n")); | ||
} | ||
@@ -342,4 +359,4 @@ } | ||
// getProperties does not include methods like `foo(): void` | ||
var properties = __spread(declaration.getProperties(), declaration.getMethods()).map(function (p) { return ({ name: p.getName(), type: p.getType() }); }); | ||
conditions.push.apply(conditions, __spread(propertiesConditions(varName, properties, addDependency, project, path, arrayDepth, records, outFile, options))); | ||
var properties = __spreadArray(__spreadArray([], __read(declaration.getProperties()), false), __read(declaration.getMethods()), false).map(function (p) { return ({ name: p.getName(), type: p.getType() }); }); | ||
conditions.push.apply(conditions, __spreadArray([], __read(propertiesConditions(varName, properties, addDependency, project, path, arrayDepth, records, outFile, options)), false)); | ||
var indexSignatures = declaration | ||
@@ -349,3 +366,6 @@ .getIndexSignatures() | ||
if (indexSignatures.length) { | ||
conditions.push(indexSignaturesCondition(varName, indexSignatures, properties, addDependency, project, path, arrayDepth, records, outFile, options)); | ||
conditions.push(indexSignaturesCondition(varName, indexSignatures.map(function (x) { return ({ | ||
keyType: indexKeyTypeToString(x.keyType), | ||
type: x.type, | ||
}); }), properties, addDependency, project, path, arrayDepth, records, outFile, options)); | ||
} | ||
@@ -369,8 +389,11 @@ } | ||
}); | ||
conditions.push.apply(conditions, __spread(propertiesConditions(varName, propertySignatures, addDependency, project, path, arrayDepth, records, outFile, options))); | ||
var typeArguments = type.getAliasTypeArguments(); | ||
if (((_b = type.getAliasSymbol()) === null || _b === void 0 ? void 0 : _b.getName()) === 'Record' && | ||
typeArguments.length === 2) { | ||
conditions.push(indexSignaturesCondition(varName, [{ keyType: typeArguments[0], type: typeArguments[1] }], propertySignatures, addDependency, project, path, arrayDepth, records, outFile, options)); | ||
conditions.push.apply(conditions, __spreadArray([], __read(propertiesConditions(varName, propertySignatures, addDependency, project, path, arrayDepth, records, outFile, options)), false)); | ||
var stringIndexType = type.getStringIndexType(); | ||
if (stringIndexType) { | ||
conditions.push(indexSignaturesCondition(varName, [{ keyType: 'string', type: stringIndexType }], propertySignatures, addDependency, project, path, arrayDepth, records, outFile, options)); | ||
} | ||
var numberIndexType = type.getNumberIndexType(); | ||
if (numberIndexType) { | ||
conditions.push(indexSignaturesCondition(varName, [{ keyType: 'number', type: numberIndexType }], propertySignatures, addDependency, project, path, arrayDepth, records, outFile, options)); | ||
} | ||
} | ||
@@ -380,7 +403,7 @@ catch (error) { | ||
// see https://github.com/dsherret/ts-simple-ast/issues/397 | ||
reportError("Internal ts-simple-ast error for " + type.getText(), error); | ||
reportError("Internal ts-simple-ast error for ".concat(type.getText()), error); | ||
} | ||
} | ||
} | ||
return ands.apply(void 0, __spread(conditions)); | ||
return ands.apply(void 0, __spreadArray([], __read(conditions), false)); | ||
} | ||
@@ -390,3 +413,3 @@ function tupleCondition(varName, type, addDependency, project, path, arrayDepth, records, outFile, options) { | ||
var conditions = types.reduce(function (acc, elementType, i) { | ||
var condition = typeConditions(varName + "[" + i + "]", elementType, addDependency, project, path, arrayDepth, true, records, outFile, options); | ||
var condition = typeConditions("".concat(varName, "[").concat(i, "]"), elementType, addDependency, project, path, arrayDepth, true, records, outFile, options); | ||
if (condition !== null) { | ||
@@ -396,4 +419,4 @@ acc.push(condition); | ||
return acc; | ||
}, ["Array.isArray(" + varName + ")"]); | ||
return ands.apply(void 0, __spread(conditions)); | ||
}, ["Array.isArray(".concat(varName, ")")]); | ||
return ands.apply(void 0, __spreadArray([], __read(conditions), false)); | ||
} | ||
@@ -417,3 +440,3 @@ function literalCondition(varName, type, addDependency) { | ||
// type.getText() returns incorrect module name for some reason | ||
return eq(varName, node.getSymbol().getName() + "." + type.getSymbol().getName()); | ||
return eq(varName, "".concat(node.getSymbol().getName(), ".").concat(type.getSymbol().getName())); | ||
} | ||
@@ -428,3 +451,3 @@ return eq(varName, type.getText()); | ||
} | ||
return record.guardName + "(" + varName + ") as boolean"; | ||
return "".concat(record.guardName, "(").concat(varName, ") as boolean"); | ||
} | ||
@@ -469,3 +492,3 @@ return null; | ||
typeToDependency(type, addDependency); | ||
return varName + " instanceof " + type.getSymbol().getName(); | ||
return "".concat(varName, " instanceof ").concat(type.getSymbol().getName()); | ||
} | ||
@@ -487,4 +510,4 @@ if (type.isTuple()) { | ||
var strippedName = propertyName.replace(/"/g, ''); | ||
var varName = objName + "[\"" + strippedName + "\"]"; | ||
var propertyPath = path + "[\"" + strippedName + "\"]"; | ||
var varName = "".concat(objName, "[\"").concat(strippedName, "\"]"); | ||
var propertyPath = "".concat(path, "[\"").concat(strippedName, "\"]"); | ||
var expectedType = property.type.getText(); | ||
@@ -497,3 +520,3 @@ var conditions = typeConditions(varName, property.type, addDependency, project, propertyPath, arrayDepth, true, records, outFile, options); | ||
return (conditions && | ||
"evaluate(" + conditions + ", `" + propertyPath + "`, " + JSON.stringify(expectedType) + ", " + varName + ")"); | ||
"evaluate(".concat(conditions, ", `").concat(propertyPath, "`, ").concat(JSON.stringify(expectedType), ", ").concat(varName, ")")); | ||
} | ||
@@ -509,8 +532,26 @@ return conditions; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
function assertNever(_) { | ||
throw new Error('should be unreachable.'); | ||
} | ||
exports.assertNever = assertNever; | ||
function signatureKeyConditions(keyType, varName) { | ||
if (keyType === 'string') { | ||
return typeOf(varName, 'string'); | ||
} | ||
else if (keyType === 'number') { | ||
return typeOf(varName, 'number'); | ||
} | ||
else if (keyType === 'any') { | ||
return null; | ||
} | ||
else { | ||
return assertNever(keyType); | ||
} | ||
} | ||
function indexSignatureConditions(objName, keyName, valueUsed, keyUsed, index, addDependency, project, path, arrayDepth, records, outFile, options) { | ||
var debug = options.debug; | ||
var expectedType = index.type.getText(); | ||
var expectedKeyType = index.keyType.getText(); | ||
var conditions = typeConditions(objName, index.type, addDependency, project, path + " " + objName, arrayDepth, true, records, outFile, options); | ||
var keyConditions = typeConditions(keyName, index.keyType, addDependency, project, path + " " + keyName, arrayDepth, true, records, outFile, options); | ||
var conditions = typeConditions(objName, index.type, addDependency, project, "".concat(path, " ").concat(objName), arrayDepth, true, records, outFile, options); | ||
var keyConditions = signatureKeyConditions(index.keyType, keyName); | ||
if (conditions) { | ||
@@ -525,5 +566,5 @@ valueUsed(); | ||
var evaluation = conditions && | ||
"evaluate(" + conditions + ", `" + path + "[\"" + cleanKeyReplacer + "\"]`, " + JSON.stringify(expectedType) + ", " + objName + ")"; | ||
"evaluate(".concat(conditions, ", `").concat(path, "[\"").concat(cleanKeyReplacer, "\"]`, ").concat(JSON.stringify(expectedType), ", ").concat(objName, ")"); | ||
var keyEvaluation = keyConditions && | ||
"evaluate(" + keyConditions + ", `" + path + " (key: \"" + cleanKeyReplacer + "\")`, " + JSON.stringify(expectedKeyType) + ", " + keyName + ")"; | ||
"evaluate(".concat(keyConditions, ", `").concat(path, " (key: \"").concat(cleanKeyReplacer, "\")`, ").concat(index.keyType, ", ").concat(keyName, ")"); | ||
if (evaluation || keyEvaluation) { | ||
@@ -552,16 +593,16 @@ keyUsed(); | ||
}; | ||
var conditions = ors.apply(void 0, __spread(indexSignatures | ||
var conditions = ors.apply(void 0, __spreadArray([], __read(indexSignatures | ||
.map(function (indexSignature) { | ||
return indexSignatureConditions('value', 'key', valueUsed, keyUsed, indexSignature, addDependency, project, path, arrayDepth, records, outFile, options); | ||
}) | ||
.filter(function (v) { return v !== null; }))); | ||
.filter(function (v) { return v !== null; })), false)); | ||
var staticKeysFilter = properties.length | ||
? "\n .filter(([key]) => ![" + properties | ||
? "\n .filter(([key]) => ![".concat(properties | ||
.map(function (_a) { | ||
var name = _a.name; | ||
return "\"" + name + "\""; | ||
return "\"".concat(name, "\""); | ||
}) | ||
.join(',') + "].includes(key))" | ||
.join(','), "].includes(key))") | ||
: ''; | ||
return "Object.entries<any>(" + varName + ")" + staticKeysFilter + "\n .every(([" + keyPrefix + "key, " + valuePrefix + "value]) => " + conditions + ")"; | ||
return "Object.entries<any>(".concat(varName, ")").concat(staticKeysFilter, "\n .every(([").concat(keyPrefix, "key, ").concat(valuePrefix, "value]) => ").concat(conditions, ")"); | ||
} | ||
@@ -577,9 +618,9 @@ function generateTypeGuard(functionName, typeDeclaration, addDependency, project, records, outFile, options) { | ||
var secondArgument = debug | ||
? ", argumentName: string = \"" + defaultArgumentName + "\"" | ||
? ", argumentName: string = \"".concat(defaultArgumentName, "\"") | ||
: ''; | ||
var signature = "export function " + functionName + "(" + signatureObjName + ": unknown" + secondArgument + "): " + signatureObjName + " is " + typeName + " {\n"; | ||
var signature = "export function ".concat(functionName, "(").concat(signatureObjName, ": unknown").concat(secondArgument, "): ").concat(signatureObjName, " is ").concat(typeName, " {\n"); | ||
var shortCircuit = shortCircuitCondition | ||
? "if (" + shortCircuitCondition + ") return true\n" | ||
? "if (".concat(shortCircuitCondition, ") return true\n") | ||
: ''; | ||
var functionBody = "const " + innerObjName + " = " + signatureObjName + " as " + typeName + "\nreturn (\n" + (conditions || true) + "\n)\n}\n"; | ||
var functionBody = "const ".concat(innerObjName, " = ").concat(signatureObjName, " as ").concat(typeName, "\nreturn (\n").concat(conditions || true, "\n)\n}\n"); | ||
return [signature, shortCircuit, functionBody].join(''); | ||
@@ -600,3 +641,3 @@ } | ||
if (previousAlias !== undefined && previousAlias !== alias) { | ||
reportError("Conflicting export alias for \"" + sourceFile.getFilePath() + "\": \"" + alias + "\" vs \"" + previousAlias + "\""); | ||
reportError("Conflicting export alias for \"".concat(sourceFile.getFilePath(), "\": \"").concat(alias, "\" vs \"").concat(previousAlias, "\"")); | ||
} | ||
@@ -635,3 +676,3 @@ imports[name] = alias; | ||
project | ||
.getSourceFiles("./**/*." + guardFileName + ".ts") | ||
.getSourceFiles("./**/*.".concat(guardFileName, ".ts")) | ||
.forEach(function (sourceFile) { return deleteGuardFile(sourceFile); }); | ||
@@ -648,3 +689,3 @@ var sourceFiles = project.getSourceFiles(); | ||
} | ||
var childVisitedFiles = __spread(visitedFiles, [sourceFile]); | ||
var childVisitedFiles = __spreadArray(__spreadArray([], __read(visitedFiles), false), [sourceFile], false); | ||
// Add all dependencies to the ordered list first (if they have beeen specified and have not already been added) | ||
@@ -765,4 +806,4 @@ sourceFile.getImportDeclarations().forEach(function (importDeclaration) { | ||
"/*", | ||
" * Generated type guards for \"" + path + "\".", | ||
" * " + GENERATED_WARNING, | ||
" * Generated type guards for \"".concat(path, "\"."), | ||
" * ".concat(GENERATED_WARNING), | ||
" */", | ||
@@ -777,8 +818,8 @@ ].join('\n')); | ||
.replace(/\.(ts|tsx|d\.ts)$/, ''); | ||
var importStatement = "import * as " + options.importGuards + " from \"" + relativeOutPath + "\";"; | ||
var exportStatement = "export { " + options.importGuards + " };"; | ||
var importStatement = "import * as ".concat(options.importGuards, " from \"").concat(relativeOutPath, "\";"); | ||
var exportStatement = "export { ".concat(options.importGuards, " };"); | ||
var _f = 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.hasImport || (reduced.hasImport = nodeText.includes("import * as ".concat(options.importGuards))); | ||
reduced.hasExport || (reduced.hasExport = nodeText.includes("export { ".concat(options.importGuards, " }"))); | ||
reduced.statements += 1; | ||
@@ -785,0 +826,0 @@ return reduced; |
{ | ||
"name": "ts-auto-guard", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Generate type guard functions from TypeScript interfaces", | ||
@@ -26,3 +26,3 @@ "homepage": "https://github.com/rhys-vdw/ts-auto-guard", | ||
"command-line-usage": "^6.1.0", | ||
"ts-morph": "^9.1.0", | ||
"ts-morph": "^16.0.0", | ||
"tsconfig": "^7.0.0" | ||
@@ -35,12 +35,12 @@ }, | ||
"@types/uglify-js": "^3.11.1", | ||
"@typescript-eslint/eslint-plugin": "^4.6.0", | ||
"@typescript-eslint/parser": "^4.6.0", | ||
"@typescript-eslint/eslint-plugin": "^5.40.0", | ||
"@typescript-eslint/parser": "^5.40.0", | ||
"cross-env": "^7.0.2", | ||
"eslint": "^7.12.1", | ||
"eslint": "~8.22.0", | ||
"prettier": "^2.1.2", | ||
"tape": "^5.0.1", | ||
"ts-node": "^9.0.0", | ||
"typescript": "^4.0.5", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.8.4", | ||
"uglify-js": "^3.11.4" | ||
} | ||
} |
@@ -6,2 +6,3 @@ import test from 'tape' | ||
import { processProject } from '../src' | ||
const WorkingDir = path.dirname(__filename) | ||
@@ -15,2 +16,3 @@ const TestFile = 'ImportTest.ts' | ||
guardFile: string | ||
only?: true | ||
} | ||
@@ -20,16 +22,17 @@ | ||
class Blueprint { | ||
inputContents: string | ||
expectedContents: string | ||
message: string | ||
constructor(message: string, inputFile: string, guardFile: string) { | ||
this.inputContents = inputFile | ||
this.expectedContents = guardFile | ||
this.message = message | ||
} | ||
constructor( | ||
public message: string, | ||
public inputContents: string, | ||
public expectedContents: string, | ||
public only: true | undefined | ||
) {} | ||
createTestFile() { | ||
fs.writeFileSync(TestFilePath, this.inputContents) | ||
} | ||
deleteTestFile() { | ||
fs.unlinkSync(TestFilePath) | ||
} | ||
buildProject() { | ||
@@ -45,6 +48,18 @@ const project = new Project({ | ||
} | ||
run() { | ||
test(this.message, t => { | ||
const fn = this.only ? test.only : test | ||
fn(this.message, t => { | ||
this.createTestFile() | ||
const project = this.buildProject() | ||
const syntacticDiagnostics = project | ||
.getLanguageService() | ||
.compilerObject.getSyntacticDiagnostics(TestFilePath) | ||
t.deepEquals(syntacticDiagnostics, []) | ||
const semanticDiagnostics = project | ||
.getLanguageService() | ||
.compilerObject.getSemanticDiagnostics(TestFilePath) | ||
t.deepEquals(semanticDiagnostics, []) | ||
t.doesNotThrow(() => { | ||
@@ -63,3 +78,3 @@ processProject(project, { exportAll: true }) | ||
function genBlueprint(def: TestDefinition) { | ||
return new Blueprint(def.message, def.inputFile, def.guardFile) | ||
return new Blueprint(def.message, def.inputFile, def.guardFile, def.only) | ||
} | ||
@@ -72,5 +87,5 @@ | ||
'interfaces from scoped package in node modules requires no import', | ||
inputFile: `import { InMemoryFileSystemHostOptions } from "@ts-morph/common"; | ||
inputFile: `import { DirEntry } from "@ts-morph/common"; | ||
export interface Foo { | ||
target: InMemoryFileSystemHostOptions | ||
target: DirEntry | ||
}`, | ||
@@ -88,5 +103,10 @@ guardFile: `import { Foo } from "./ImportTest"; | ||
typeof typedObj["target"] === "function") && | ||
(typeof typedObj["target"]["skipLoadingLibFiles"] === "undefined" || | ||
typedObj["target"]["skipLoadingLibFiles"] === false || | ||
typedObj["target"]["skipLoadingLibFiles"] === true) | ||
typeof typedObj["target"]["path"] === "string" && | ||
(typedObj["target"]["path"] !== null && | ||
typeof typedObj["target"]["path"] === "object" || | ||
typeof typedObj["target"]["path"] === "function") && | ||
typeof typedObj["target"]["path"]["_standardizedFilePathBrand"] === "undefined" && | ||
typeof typedObj["target"]["isFile"] === "boolean" && | ||
typeof typedObj["target"]["isDirectory"] === "boolean" && | ||
typeof typedObj["target"]["isSymlink"] === "boolean" | ||
) | ||
@@ -93,0 +113,0 @@ } |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
146618
3023
1
+ Added@ts-morph/common@0.17.0(transitive)
+ Addedbrace-expansion@2.0.1(transitive)
+ Addedcode-block-writer@11.0.3(transitive)
+ Addedminimatch@5.1.6(transitive)
+ Addedpath-browserify@1.0.1(transitive)
+ Addedts-morph@16.0.0(transitive)
- Removed@dsherret/to-absolute-glob@2.0.2(transitive)
- Removed@ts-morph/common@0.7.5(transitive)
- Removed@types/minimatch@3.0.5(transitive)
- Removedarray-differ@3.0.0(transitive)
- Removedarray-union@2.1.0(transitive)
- Removedarrify@2.0.1(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedcode-block-writer@10.1.1(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedis-absolute@1.0.0(transitive)
- Removedis-negated-glob@1.0.0(transitive)
- Removedis-relative@1.0.0(transitive)
- Removedis-unc-path@1.0.0(transitive)
- Removedis-windows@1.0.2(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedmultimatch@5.0.0(transitive)
- Removedts-morph@9.1.0(transitive)
- Removedtypescript@4.1.6(transitive)
- Removedunc-path-regex@0.1.2(transitive)
Updatedts-morph@^16.0.0