Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kasbah opened this issue Feb 11, 2018 · 8 comments
Closed

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

kasbah opened this issue Feb 11, 2018 · 8 comments

Comments

@kasbah
Copy link

kasbah commented Feb 11, 2018

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
Copy link
Owner

cevek commented Feb 12, 2018

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
Copy link
Author

kasbah commented Feb 13, 2018

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
Copy link
Owner

cevek commented Feb 13, 2018

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
Copy link
Author

kasbah commented Feb 13, 2018

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
Copy link
Owner

cevek commented Feb 13, 2018

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

@kasbah
Copy link
Author

kasbah commented Feb 13, 2018

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
Copy link
Owner

cevek commented Feb 13, 2018

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

@kasbah
Copy link
Author

kasbah commented Feb 13, 2018

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

@kasbah kasbah closed this as completed Feb 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants