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

investigate ts-morth integration #35

Open
lifeart opened this issue Nov 27, 2020 · 0 comments
Open

investigate ts-morth integration #35

lifeart opened this issue Nov 27, 2020 · 0 comments

Comments

@lifeart
Copy link
Owner

lifeart commented Nov 27, 2020

const ts = require('ts-morph');

const Project = ts.Project;
const Node = ts.Node;

function getHelper(filePath) {

  const project = new Project({ compilerOptions: { allowJs: true } });

  const helperFile = project.addSourceFileAtPath(filePath);

  const defaultExportSymbol = helperFile.getDefaultExportSymbol();

  if (defaultExportSymbol) {
    const declaration = defaultExportSymbol.getDeclarations()[0];
    if (declaration && Node.isExportAssignment(declaration)) {
      let helperWrapper = declaration.getExpression();
      let helper;
      let helperWrapperDefinition;
      if (Node.isIdentifier(helperWrapper)) {
        helperWrapperDefinition = helperWrapper.getDefinitionNodes()[0];
        helperWrapper = helperWrapperDefinition.getChildren().slice(-1)[0];
      }

      helper = helperWrapper.getArguments()[0];
      if (Node.isIdentifier(helper)) {
        helper = helper.getDefinitionNodes()[0];
      }

      if (helper.isExported && helper.isExported()) {
        helper.setIsExported(false);
      }

      const txt = helper.getFullText();
      helper.remove();
      helperWrapperDefinition?.remove();
      declaration.remove();

      return `${helperFile.getFullText()} export default ${txt}`;
    }
  }
  return null;
}

const h = getHelper('./app/helpers/custom.js');
console.log(h);
const ts = require('ts-morph');

const Project = ts.Project;
const Node = ts.Node;

function getModifier(filePath) {

  const project = new Project({ compilerOptions: { allowJs: true } });

  const modifierFile = project.addSourceFileAtPath(filePath);

  const defaultExportSymbol = modifierFile.getDefaultExportSymbol();

  if (defaultExportSymbol) {
    const declaration = defaultExportSymbol.getDeclarations()[0];
    if (declaration && Node.isExportAssignment(declaration)) {
      let modifierWrapper = declaration.getExpression();
      let modifierWrapperDefinition;
      if (Node.isIdentifier(modifierWrapper)) {
        modifierWrapperDefinition = modifierWrapper.getDefinitionNodes()[0];
        modifierWrapper = modifierWrapperDefinition.getChildren().slice(-1)[0];
      }

      const functions = modifierWrapper.getDescendantsOfKind(ts.SyntaxKind.MethodDeclaration);
      const installer = functions.find(f => f.getChildren()[0].getText() === 'installModifier');
      const updater = functions.find(f => f.getChildren()[0].getText() === 'updateModifier');
      const destroyer = functions.find(f => f.getChildren()[0].getText() === 'destroyModifier');

      let validFn;
      validFn = validFn || (installer.getParameters().length === 3 && installer);
      validFn = validFn || (updater.getParameters().length === 2 && updater);
      validFn = validFn || (destroyer.getParameters().length === 2 && destroyer);

      const txt = validFn.getFullText();

      try {
        modifierWrapper.remove();
      } catch (e) {}

      declaration.remove();

      return `${modifierFile.getFullText()} export default ${txt}`;
    }
  }
  return null;
}

const h = getModifier('./node_modules/@ember/render-modifiers/addon/modifiers/did-insert.js');
console.log(h);
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

1 participant