typescript-is
Advanced tools
Comparing version 0.14.3 to 0.15.0
@@ -8,3 +8,3 @@ "use strict"; | ||
const utils_1 = require("./utils"); | ||
function createArrowFunction(type, optional, partialVisitorContext) { | ||
function createArrowFunction(type, rootName, optional, partialVisitorContext) { | ||
const functionMap = new Map(); | ||
@@ -23,3 +23,3 @@ const functionNames = new Set(); | ||
], undefined, undefined, ts.createBlock([ | ||
ts.createVariableStatement([ts.createModifier(ts.SyntaxKind.ConstKeyword)], [ts.createVariableDeclaration(VisitorUtils.pathIdentifier, undefined, ts.createArrayLiteral([ts.createStringLiteral('$')]))]), | ||
ts.createVariableStatement([ts.createModifier(ts.SyntaxKind.ConstKeyword)], [ts.createVariableDeclaration(VisitorUtils.pathIdentifier, undefined, ts.createArrayLiteral([ts.createStringLiteral(rootName)]))]), | ||
...declarations, | ||
@@ -30,3 +30,3 @@ ts.createVariableStatement([ts.createModifier(ts.SyntaxKind.ConstKeyword)], [ts.createVariableDeclaration(errorIdentifier, undefined, ts.createCall(ts.createIdentifier(functionName), undefined, [VisitorUtils.objectIdentifier]))]), | ||
} | ||
function transformDecorator(node, parameterType, optional, visitorContext) { | ||
function transformDecorator(node, parameterType, parameterName, optional, visitorContext) { | ||
if (ts.isCallExpression(node.expression)) { | ||
@@ -38,3 +38,3 @@ const signature = visitorContext.checker.getResolvedSignature(node.expression); | ||
&& node.expression.arguments.length <= 1) { | ||
const arrowFunction = createArrowFunction(parameterType, optional, visitorContext); | ||
const arrowFunction = createArrowFunction(parameterType, parameterName, optional, visitorContext); | ||
const expression = ts.updateCall(node.expression, node.expression.expression, undefined, [arrowFunction].concat(node.expression.arguments)); | ||
@@ -46,2 +46,6 @@ return ts.updateDecorator(node, expression); | ||
} | ||
/** Figures out an appropriate human-readable name for the variable designated by `node`. */ | ||
function extractVariableName(node) { | ||
return node != null && ts.isIdentifier(node) ? node.escapedText.toString() : '$'; | ||
} | ||
function transformNode(node, visitorContext) { | ||
@@ -51,3 +55,3 @@ if (ts.isParameter(node) && node.type !== undefined && node.decorators !== undefined) { | ||
const required = !node.initializer && !node.questionToken; | ||
const mappedDecorators = node.decorators.map((decorator) => transformDecorator(decorator, type, !required, visitorContext)); | ||
const mappedDecorators = node.decorators.map((decorator) => transformDecorator(decorator, type, extractVariableName(node.name), !required, visitorContext)); | ||
return ts.updateParameter(node, mappedDecorators, node.modifiers, node.dotDotDotToken, node.name, node.questionToken, node.type, node.initializer); | ||
@@ -66,3 +70,3 @@ } | ||
const type = visitorContext.checker.getTypeFromTypeNode(typeArgument); | ||
const arrowFunction = createArrowFunction(type, false, Object.assign(Object.assign({}, visitorContext), { options: Object.assign(Object.assign({}, visitorContext.options), { disallowSuperfluousObjectProperties: isEquals || visitorContext.options.disallowSuperfluousObjectProperties }) })); | ||
const arrowFunction = createArrowFunction(type, extractVariableName(node.arguments[0]), false, Object.assign(Object.assign({}, visitorContext), { options: Object.assign(Object.assign({}, visitorContext.options), { disallowSuperfluousObjectProperties: isEquals || visitorContext.options.disallowSuperfluousObjectProperties }) })); | ||
return ts.updateCall(node, node.expression, node.typeArguments, [ | ||
@@ -69,0 +73,0 @@ ...node.arguments, |
{ | ||
"name": "typescript-is", | ||
"version": "0.14.3", | ||
"version": "0.15.0", | ||
"engines": { | ||
@@ -5,0 +5,0 @@ "node": ">=6.14.4" |
@@ -50,2 +50,8 @@ # typescript-is | ||
### Similar projects | ||
* [io-ts-transformer](https://github.com/awerlogus/io-ts-transformer) | ||
* [io-ts](https://github.com/gcanti/io-ts) | ||
* [ts-auto-guard](https://github.com/usabilityhub/ts-auto-guard) | ||
# 🎛️ Configuration | ||
@@ -90,2 +96,31 @@ | ||
## Using with `webpack + ts-loader` without `ttypescript` | ||
If you are using `ts-loader` in a `webpack` project, you can use [getCustomTransformers](https://github.com/TypeStrong/ts-loader#getcustomtransformers) as suggested in #54. | ||
This means you don't need to use `ttypescript` or write a custom compilation script. | ||
Example: | ||
```javascript | ||
const typescriptIsTransformer = require('typescript-is/lib/transform-inline/transformer').default | ||
module.exports = { | ||
// I am hiding the rest of the webpack config | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.ts$/, | ||
exclude: /node_modules/, | ||
loader: 'ts-loader', | ||
options: { | ||
getCustomTransformers: program => ({ | ||
before: [typescriptIsTransformer(program)] | ||
}) | ||
} | ||
} | ||
] | ||
} | ||
}; | ||
``` | ||
## Options | ||
@@ -92,0 +127,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
215765
2659
330