Comparing version 1.0.4 to 1.0.5
import * as ts from 'typescript'; | ||
import { visitEachChild } from 'typescript'; | ||
function visitNode(node, ts) { | ||
function equalsWrappedText(wrappedValue, expected) { | ||
return wrappedValue === "'" + expected + "'" || wrappedValue === "\"" + expected + "\""; | ||
} | ||
var importBinding; | ||
function visitNode(node, pluginOptions) { | ||
var ts = pluginOptions.compilerModule; | ||
if (node == null) | ||
return node; | ||
if (ts.isCallExpression(node)) { | ||
if (node.expression.getText() === 'named') { | ||
if (node.expression.getText() === importBinding) { | ||
if (ts.isVariableDeclaration(node.parent) || // const v = named(id => ...) | ||
@@ -28,3 +34,10 @@ ts.isPropertyDeclaration(node.parent) || // class C { v = named(id => ...) } | ||
if (ts.isImportDeclaration(node)) { | ||
if (node.moduleSpecifier.getText() === "'ts-named'") { | ||
if (equalsWrappedText(node.moduleSpecifier.getText(), pluginOptions.tsNamedModule)) { | ||
var importClause = node.importClause; | ||
if (importClause) { | ||
var namedBindings = importClause.namedBindings; | ||
namedBindings.elements.forEach(function (value) { | ||
importBinding = value.name.text; | ||
}); | ||
} | ||
return undefined; | ||
@@ -35,8 +48,8 @@ } | ||
} | ||
function visitSourceFile(sourceFile, context, ts) { | ||
function visitSourceFile(sourceFile, context, pluginOptions) { | ||
function visitNodeAndChildren(node) { | ||
if (node == null) | ||
return node; | ||
node = ts.visitEachChild(node, function (childNode) { return visitNodeAndChildren(childNode); }, context); | ||
return visitNode(node, ts); | ||
node = visitEachChild(node, function (childNode) { return visitNodeAndChildren(childNode); }, context); | ||
return visitNode(node, pluginOptions); | ||
} | ||
@@ -46,3 +59,8 @@ return visitNodeAndChildren(sourceFile); | ||
function createTransformerFactory(cs) { | ||
return function (context) { return function (file) { return visitSourceFile(file, context, cs.compilerModule || ts); }; }; | ||
return function (context) { return function (file) { | ||
return visitSourceFile(file, context, { | ||
compilerModule: cs.compilerModule || ts, | ||
tsNamedModule: cs.tsNamedModule || 'ts-named', | ||
}); | ||
}; }; | ||
} | ||
@@ -52,3 +70,6 @@ function namedTransformer(pluginOptions) { | ||
return function (context) { return function (file) { | ||
return visitSourceFile(file, context, pluginOptions.compilerModule || ts); | ||
return visitSourceFile(file, context, { | ||
compilerModule: pluginOptions.compilerModule || ts, | ||
tsNamedModule: pluginOptions.tsNamedModule || 'ts-named', | ||
}); | ||
}; }; | ||
@@ -55,0 +76,0 @@ } |
@@ -5,7 +5,12 @@ 'use strict'; | ||
function visitNode(node, ts) { | ||
function equalsWrappedText(wrappedValue, expected) { | ||
return wrappedValue === "'" + expected + "'" || wrappedValue === "\"" + expected + "\""; | ||
} | ||
var importBinding; | ||
function visitNode(node, pluginOptions) { | ||
var ts = pluginOptions.compilerModule; | ||
if (node == null) | ||
return node; | ||
if (ts.isCallExpression(node)) { | ||
if (node.expression.getText() === 'named') { | ||
if (node.expression.getText() === importBinding) { | ||
if (ts.isVariableDeclaration(node.parent) || // const v = named(id => ...) | ||
@@ -31,3 +36,10 @@ ts.isPropertyDeclaration(node.parent) || // class C { v = named(id => ...) } | ||
if (ts.isImportDeclaration(node)) { | ||
if (node.moduleSpecifier.getText() === "'ts-named'") { | ||
if (equalsWrappedText(node.moduleSpecifier.getText(), pluginOptions.tsNamedModule)) { | ||
var importClause = node.importClause; | ||
if (importClause) { | ||
var namedBindings = importClause.namedBindings; | ||
namedBindings.elements.forEach(function (value) { | ||
importBinding = value.name.text; | ||
}); | ||
} | ||
return undefined; | ||
@@ -38,3 +50,3 @@ } | ||
} | ||
function visitSourceFile(sourceFile, context, ts) { | ||
function visitSourceFile(sourceFile, context, pluginOptions) { | ||
function visitNodeAndChildren(node) { | ||
@@ -44,3 +56,3 @@ if (node == null) | ||
node = ts.visitEachChild(node, function (childNode) { return visitNodeAndChildren(childNode); }, context); | ||
return visitNode(node, ts); | ||
return visitNode(node, pluginOptions); | ||
} | ||
@@ -50,3 +62,8 @@ return visitNodeAndChildren(sourceFile); | ||
function createTransformerFactory(cs) { | ||
return function (context) { return function (file) { return visitSourceFile(file, context, cs.compilerModule || ts); }; }; | ||
return function (context) { return function (file) { | ||
return visitSourceFile(file, context, { | ||
compilerModule: cs.compilerModule || ts, | ||
tsNamedModule: cs.tsNamedModule || 'ts-named', | ||
}); | ||
}; }; | ||
} | ||
@@ -56,3 +73,6 @@ function namedTransformer(pluginOptions) { | ||
return function (context) { return function (file) { | ||
return visitSourceFile(file, context, pluginOptions.compilerModule || ts); | ||
return visitSourceFile(file, context, { | ||
compilerModule: pluginOptions.compilerModule || ts, | ||
tsNamedModule: pluginOptions.tsNamedModule || 'ts-named', | ||
}); | ||
}; }; | ||
@@ -59,0 +79,0 @@ } |
import TS from 'typescript'; | ||
import { SourceFile, TransformationContext, TransformerFactory } from 'typescript'; | ||
export interface ConfigSet { | ||
compilerModule?: typeof TS; | ||
compilerModule: typeof TS; | ||
tsNamedModule: string; | ||
} | ||
export declare function visitSourceFile(sourceFile: SourceFile, context: TransformationContext, ts: typeof TS): TS.Node | undefined; | ||
export declare function createTransformerFactory(cs: ConfigSet): TransformerFactory<SourceFile>; | ||
export declare function namedTransformer(pluginOptions?: ConfigSet): TransformerFactory<SourceFile>; | ||
export declare function visitSourceFile(sourceFile: SourceFile, context: TransformationContext, pluginOptions: ConfigSet): TS.Node | undefined; | ||
export declare function createTransformerFactory(cs: Partial<ConfigSet>): TransformerFactory<SourceFile>; | ||
export declare function namedTransformer(pluginOptions?: Partial<ConfigSet>): TransformerFactory<SourceFile>; |
{ | ||
"name": "ts-named", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Ts-Named : TypeScript Transformer for extracting variable name", | ||
@@ -5,0 +5,0 @@ "author": "Ozan Gunalp <ozangunalp@gmail.com>", |
@@ -47,8 +47,6 @@ # Ts-Named : TypeScript Transformer for extracting variable name | ||
One catch is that | ||
### Webpack | ||
```js | ||
const named = require('ts-named'); | ||
const tsNamed = require('ts-named'); | ||
@@ -65,3 +63,3 @@ module.exports = { | ||
options: { | ||
getCustomTransformers: () => ({ before: [named] }), | ||
getCustomTransformers: () => ({ before: [tsNamed()] }), | ||
}, | ||
@@ -81,3 +79,3 @@ }, | ||
const ts = require('gulp-typescript'); | ||
const named = require('ts-named'); | ||
const tsNamed = require('ts-named'); | ||
@@ -89,3 +87,3 @@ gulp.task('typescript', function() { | ||
ts({ | ||
getCustomTransformers: () => ({ before: [named] }), | ||
getCustomTransformers: () => ({ before: [tsNamed()] }), | ||
}) | ||
@@ -92,0 +90,0 @@ ) |
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
12839
8
176
118