cevek / ttypescript

Over TypeScript tool to use custom transformers in the tsconfig.json
1.53k stars 56 forks source link

Can't get this to work with a simple example #3

Closed kasbah closed 6 years ago

kasbah commented 6 years ago

I took this simple example from this blog post but I couldn't get it to run:

// transform.ts
import * as ts from 'typescript';

export default function(/*opts?: Opts*/) {
  const visitor: ts.Visitor = (node: ts.Node): ts.VisitResult => {
    if (ts.isCallExpression(node) && node.expression.getText(sf) == 'safely') {
      // we have found an expression of the form safely(...)
      // we can now replace or update it

      // get the argument to safely
      const target = node.arguments[0];
      // check to make sure it is a property access, like "a.b"
      if (ts.isPropertyAccessExpression(target)) {
        // return a binary expression with a && a.b
        return ts.createBinary(
          target.expression, // the left hand operand is the object
          ts.SyntaxKind.AmpersandAmpersandToken, // the && operator
          target,
        ); // the right hand operand is the full expression
      }
    }
    // otherwise continue visiting all the nodes
    return ts.visitEachChild(node, visitor, ctx);
  };
  return (ctx: ts.TransformationContext): ts.Transformer => {
    return (sf: ts.SourceFile) => ts.visitNode(sf, visitor(ctx, sf));
  };
}
// example.ts
const a = {b: 1}
const c = safely(a.b)

tsconfig.json

{
  "compilerOptions": {
   "customTransformers": {
      "before": [
        "./transform.ts"
      ]
    }
  }
}

I ran:

ttsc example.ts

Result:

var a = { b: 1 };
var c = safely(a.b);

Expected:

var a = { b: 1 };
var c = a && a.b;
cevek commented 6 years ago

Currently transformers work only with js files in commonjs style. But soon I'll implement supporting ts files Second problem that you give a wrapper function, but transformers must be without this one

export default function(/*opts?: Opts*/)

Work transformer version

//transform.js
'use strict';
var ts = require('typescript');
function visitor(ctx, sf, opts) {
    var visitor = function(node) {
        if (ts.isCallExpression(node) && ts.getTextOfNodeFromSourceText(sf.text, node.expression)) {
            var target = node.arguments[0];
            if (ts.isPropertyAccessExpression(target)) {
                return ts.createBinary(
                    target.expression, // the left hand operand is the object
                    ts.SyntaxKind.AmpersandAmpersandToken, // the && operator
                    target
                );
            }
        }
        return ts.visitEachChild(node, visitor, ctx);
    };
    return visitor;
}
module.exports = function(ctx) {
    return function(sf) {
        return ts.visitNode(sf, visitor(ctx, sf, {}));
    };
};
kasbah commented 6 years ago

Thanks for your help. I copied your transform.js and changed tsconfig.json to:

{
  "compilerOptions": {
   "customTransformers": {
      "before": [
        "./transform.js"
      ]
    }
  }
}

But the ttsc transformation is still as above. In fact, I noticed even if I change ./transform.js to a non-existent file it runs the same.

cevek commented 6 years ago

I've publish new version(1.0.5) that can works with transformers written in ts Also I've attach an example project Read readme.md

kasbah commented 6 years ago

Nice, I tried the example and it does work for me when I run ttsc on its own. However, if I run ttsc test.ts it doesn't work as expected (seems to be like running tsc -- the transform is not applied).

(Also, I noticed you committed an out of date example/test.js)

cevek commented 6 years ago

When you run tsc test.ts tsc doesn't use your tsconfig.json

kasbah commented 6 years ago

Ah, thanks. That's kind of confusing behaviour but I haven't used tsc very much. It might make sense for ttsc file.ts to still use the transform section of the config because right now it's just like calling tsc file.ts?

cevek commented 6 years ago

Yeah, you right, maybe in the next version I'll add this feature

kasbah commented 6 years ago

Thanks again for this project and your help. I feel like with the addition of an example this is resolved.