@babel/types#Node TypeScript Examples

The following examples show how to use @babel/types#Node. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: js.ts    From css-to-js with MIT License 6 votes vote down vote up
/**
 * Gets the string representation of an AST node, given the input code.
 * @param node AST node
 * @param code input code that AST was generated from
 */
export function nodeToString(node: Node, code: string): string {
  const { start, end } = node;
  if (start === null || end === null) {
    // I'm not sure when this would happen, but it probably means
    // we can't convert this node to string
    throw new Error(
      `Can't convert node to string: ${JSON.stringify(node, null, 2)}`
    );
  }

  // Copy the string value directly from the input code
  return code.slice(start, end).trim();
}
Example #2
Source File: babel-polyfill.ts    From nota with MIT License 6 votes vote down vote up
traverse = (node: Node, visit: (node: Node) => void) => {
  visit(node);
  Object.keys(node).forEach(k => {
    let v = (node as any)[k];
    if (is_node(v)) {
      traverse(v, visit);
    } else if (v instanceof Array && v.length > 0 && is_node(v[0])) {
      v.forEach(child => traverse(child, visit));
    }
  });
}
Example #3
Source File: getMemberExpressionParams.ts    From engine with MIT License 5 votes vote down vote up
getMemberExpressionParams = (node: Node): any[] => {
  if (node.type === "MemberExpression") {
    if (node.property.type === "MemberExpression") {
      let result = getMemberExpressionParams(node.property.object);
      if (node.property.property) {
        const params = getMemberExpressionParams(node.property.property);
        result = result.concat(params);
      }
      let pathArg;
      if (result[0] === PathProps.EXTERNAL) {
        result.shift();
        pathArg = "@" + result.join(".");
      } else if (result[0] === PathProps.INTERNAL) {
        result.shift();
        pathArg = "$" + result.join(".");
      } else if (result[0] === PathProps.PARAM) {
        result.shift();
        pathArg = ":" + result.join(".");
      } else {
        pathArg = {
          __node__: node.property,
        };
      }
      return [...getMemberExpressionParams(node.object), pathArg];
    } else {
      let value;
      if (isIdentifier(node.property) && node.computed) {
        value = { __node__: node.property };
      } else if (isTemplateLiteral(node.property)) {
        value = { __node__: node.property };
      } else {
        value = (node.property as any).name || (node.property as any).value;
      }

      return [...getMemberExpressionParams(node.object), value];
    }
  } else if (node.hasOwnProperty("value")) {
    return [(node as ValueNodes).value];
  } else if (node.hasOwnProperty("name")) {
    return [(node as Identifier).name];
  } else {
    return [];
  }
}
Example #4
Source File: babel-polyfill.ts    From nota with MIT License 5 votes vote down vote up
is_node = (x: any): x is Node => typeof x == "object" && x && x.type
Example #5
Source File: cloneDeepWithHook.ts    From plasmic with MIT License 5 votes vote down vote up
cloneDeepWithHook = <T extends Node>(
  n: T,
  cloneNodeHook: (n: Node) => Node | undefined
): T => {
  return cloneNode(n, true, false, cloneNodeHook) as T;
}
Example #6
Source File: index.ts    From plasmic with MIT License 5 votes vote down vote up
cloneNode: <T extends Node>(
  n: T,
  deep?: boolean,
  withoutLoc?: boolean,
  cloneNodeHook?: (n: T) => T | undefined
) => T = cloneNodeImpl as any
Example #7
Source File: inspector-loader.ts    From react-dev-inspector with MIT License 5 votes vote down vote up
/**
 * [webpack compile time]
 *
 * inject line, column, relative-path to JSX html data attribute in source code
 *
 * @type webpack.loader.Loader
 * ref: https://astexplorer.net  +  @babel/parser
 */
export default function inspectorLoader(this: webpack.loader.LoaderContext, source: string) {
  const {
    rootContext: rootPath,
    resourcePath: filePath,
  } = this

  /**
   * example:
   * rootPath: /home/xxx/project
   * filePath: /home/xxx/project/src/ooo/xxx.js
   * relativePath: src/ooo/xxx.js
   */
  const relativePath = path.relative(rootPath, filePath)

  const options: InspectorConfig = getOptions(this)

  const isSkip = pathMatch(filePath, options.excludes)
  if (isSkip) {
    return source
  }

  const ast: Node = parse(source, {
    sourceType: 'module',
    allowUndeclaredExports: true,
    allowImportExportEverywhere: true,
    plugins: [
      'typescript',
      'jsx',
      'decorators-legacy',
      'classProperties',
      ...options?.babelPlugins ?? [],
    ],
    ...options?.babelOptions,
  })


  /**
   * astexplorer + @babel/parser
   * https://astexplorer.net
   */
  traverse(ast, {
    JSXOpeningElement: {
      enter(path) {
        doJSXOpeningElement(
          path.node as JSXOpeningElement,
          { relativePath },
        )
      },
    },
  })

  const {
    code,
  } = generate(ast, {
    decoratorsBeforeExport: true,
  })

  return code
}