Skip to content
This repository was archived by the owner on Jan 14, 2019. It is now read-only.

refactor: migrate node-utils from commonjs to es6 module #121

Merged
merged 3 commits into from
Jan 12, 2019

Conversation

armano2
Copy link
Contributor

@armano2 armano2 commented Jan 12, 2019

benefits: tree-shaking and faster access to imports

i'm going to wait with changing converter after #120 will be finished

import * as nodeUtils from './node-utils';

@codecov
Copy link

codecov bot commented Jan 12, 2019

Codecov Report

Merging #121 into master will increase coverage by 0.14%.
The diff coverage is 97.29%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #121      +/-   ##
==========================================
+ Coverage    93.2%   93.34%   +0.14%     
==========================================
  Files           8        8              
  Lines        1368     1398      +30     
  Branches      324      324              
==========================================
+ Hits         1275     1305      +30     
  Misses         51       51              
  Partials       42       42
Impacted Files Coverage Δ
src/parser.ts 78.5% <100%> (ø) ⬆️
src/node-utils.ts 96% <100%> (+0.7%) ⬆️
src/ast-converter.ts 100% <100%> (ø) ⬆️
src/convert-comments.ts 96.07% <100%> (ø) ⬆️
src/convert.ts 93.82% <95.77%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7176619...6e0e866. Read the comment docs.

@JamesHenry
Copy link
Owner

No rush at all, I'll just wait until you let me know this PR is ready. I was literally thinking about this change just this morning - another legacy of the codebase being originally a pure JS package

node.name,
ast,
(token: any) => {
if (!token || !token.kind) {
return false;
}
return nodeUtils.getTextForTokenKind(token.kind) === '(';
return getTextForTokenKind(token.kind) === '(';
},
ast
Copy link
Contributor Author

@armano2 armano2 Jan 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw. this function it's really expensive to be executed, i think it's not a best way to do this xd

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm interesting, I would have thought a function like that would benefit from some optimizations in the JS engine, but you might be right.

Would definitely be interesting to add some benchmarking now that things are settling down a bit.

I would not feel confident making large-scale changes without that in place (obviously not referring to something minor like this).

Copy link
Contributor Author

@armano2 armano2 Jan 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no i'm not talking about execution but what is done in findFirstMatchingToken

  while (previousToken) {
    if (predicate(previousToken)) {
      return previousToken;
    }
    previousToken = findNextToken(previousToken, parent, ast);
  }
  return undefined;

// ...

export function findNextToken(
  previousToken: ts.Node,
  parent: ts.Node,
  ast: ts.SourceFile
): ts.Node | undefined {
  return find(parent);

  function find(n: ts.Node): ts.Node | undefined {
    if (ts.isToken(n) && n.pos === previousToken.end) {
      // this is token that starts at the end of previous token - return it
      return n;
    }
    return firstDefined(n.getChildren(ast), (child: ts.Node) => {
      const shouldDiveInChildNode =
        // previous token is enclosed somewhere in the child
        (child.pos <= previousToken.pos && child.end > previousToken.end) ||
        // previous token ends exactly at the beginning of child
        child.pos === previousToken.end;
      return shouldDiveInChildNode && nodeHasTokens(child, ast)
        ? find(child)
        : undefined;
    });
  }
}

Copy link
Contributor Author

@armano2 armano2 Jan 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is loop in findFirstMatchingToken -> loop in findNextToken -> loop in firstDefined -> and recursion to findNextToken

this is not going to be an issue in most of scenarios, but it can be an issue in edge cases

@JamesHenry JamesHenry merged commit 4315f94 into JamesHenry:master Jan 12, 2019
@armano2 armano2 deleted the node-utils branch January 12, 2019 20:47
@JamesHenry
Copy link
Owner

🎉 This PR is included in version 18.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants