How to use the jscodeshift.withParser function in jscodeshift

To help you get started, we’ve selected a few jscodeshift examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sibelius / graphql2ts / test / testUtils.ts View on Github external
function runInlineTest(module, options, input, expectedOutput) {
  // Handle ES6 modules using default export for the transform
  const transform = module.default ? module.default : module;

  // Jest resets the module registry after each test, so we need to always get
  // a fresh copy of jscodeshift on every test run.
  let jscodeshift = require('jscodeshift');
  if (module.parser) {
    jscodeshift = jscodeshift.withParser(module.parser);
  }

  const output = transform(
    input,
    {
      jscodeshift,
      stats: () => {},
    },
    options || {},
  );
  expect((output || '').trim()).toEqual(expectedOutput.trim());
}
exports.runInlineTest = runInlineTest;
github xsburg / vscode-javascript-booster / client / src / services / astService.ts View on Github external
import { File } from 'ast-types';
import * as jscodeshift from 'jscodeshift';
import * as os from 'os';
import { PrinterOptions } from 'recast';
import * as vscode from 'vscode';
import { registerCollectionExtensions } from '../utils';
import logService from './logService';

// Hack to adjust default recast options
// making it as close to Prettier as possible.

// tslint:disable-next-line:variable-name
const CollectionPrototype = jscodeshift.withParser('babylon')('').constructor.prototype;
const toSource = CollectionPrototype.toSource;
CollectionPrototype.toSource = function(options: PrinterOptions) {
    return toSource.call(this, {
        quote: 'single',
        ...options
    });
};

registerCollectionExtensions(jscodeshift as jscodeshift.JsCodeShift);

// Zero-based offset
export interface Selection {
    anchor: number;
    active: number;
}
github xsburg / vscode-javascript-booster / client / src / services / astService.ts View on Github external
}

export type LanguageId = 'javascript' | 'javascriptreact' | 'typescript' | 'typescriptreact';

const supportedLanguages: LanguageId[] = [
    'javascript',
    'javascriptreact',
    'typescript',
    'typescriptreact'
];

const codeshifts: { [languageId in LanguageId]: jscodeshift.JsCodeShift } = {
    javascript: jscodeshift.withParser('babylon'),
    javascriptreact: jscodeshift.withParser('babylon'),
    typescript: jscodeshift.withParser('typescript'),
    typescriptreact: jscodeshift.withParser('tsx')
};

export type AstRoot = jscodeshift.Collection;

class AstService {
    public readonly supportedlanguages = supportedLanguages;

    private _astCache: Map<
        string, // cached by fileName
        {
            source: string;
            ast: AstRoot | null;
        }
    > = new Map();

    public isSupportedLanguage(languageId: string): boolean {
github xsburg / vscode-javascript-booster / server / src / services / astService.ts View on Github external
export interface Selection {
    anchor: number;
    active: number;
}

export type LanguageId = 'javascript' | 'javascriptreact' | 'typescript' | 'typescriptreact';

const supportedLanguages: LanguageId[] = [
    'javascript',
    'javascriptreact',
    'typescript',
    'typescriptreact'
];

const codeshifts: { [languageId in LanguageId]: jscodeshift.JsCodeShift } = {
    javascript: jscodeshift.withParser('babylon'),
    javascriptreact: jscodeshift.withParser('babylon'),
    typescript: jscodeshift.withParser('typescript'),
    typescriptreact: jscodeshift.withParser('tsx')
};

export type AstRoot = jscodeshift.Collection;

class AstService {
    public readonly supportedlanguages = supportedLanguages;

    private _astCache: Map<
        string, // cached by fileName
        {
            source: string;
            ast: AstRoot | null;
        }
github OfficeDev / office-ui-fabric-react / packages / codepen-loader / src / codepenTransform.ts View on Github external
while (contents.startsWith('//')) {
    contents = contents.replace(/^.*?\r?\n/, '');
  }
  // Hack to quickly get rid of exports
  contents = contents.replace(/^export /gm, '');
  fileInfo.contents = contents;
}

// PeopleExampleData imports TestImages and uses it in a constant.
// Replace the import with the TestImages file contents to avoid a runtime error.
exampleDataFiles.peopleExampleData.contents = exampleDataFiles.peopleExampleData.contents.replace(
  /import .*?\bTestImages\b.*?\r?\n/,
  exampleDataFiles.testImages.contents
);

const j = jscodeshift.withParser('babylon');

const parse = (source: string) =>
  babylon.parse(source, {
    sourceType: 'module',
    plugins: ['jsx', 'typescript', 'classProperties', 'objectRestSpread']
  });

/**
 * This Transform modifies Fabric website code examples into a format that will allow them to be
 * rendered on Codepen, as part of the "Export to Codepen" feature.
 * There are two types of supported example templates:
 *
 *     [imports]
 *     [named variable export with example code inside]
 *
 * and
github xsburg / vscode-javascript-booster / client / src / services / astService.ts View on Github external
export interface Selection {
    anchor: number;
    active: number;
}

export type LanguageId = 'javascript' | 'javascriptreact' | 'typescript' | 'typescriptreact';

const supportedLanguages: LanguageId[] = [
    'javascript',
    'javascriptreact',
    'typescript',
    'typescriptreact'
];

const codeshifts: { [languageId in LanguageId]: jscodeshift.JsCodeShift } = {
    javascript: jscodeshift.withParser('babylon'),
    javascriptreact: jscodeshift.withParser('babylon'),
    typescript: jscodeshift.withParser('typescript'),
    typescriptreact: jscodeshift.withParser('tsx')
};

export type AstRoot = jscodeshift.Collection;

class AstService {
    public readonly supportedlanguages = supportedLanguages;

    private _astCache: Map<
        string, // cached by fileName
        {
            source: string;
            ast: AstRoot | null;
        }
github xsburg / vscode-javascript-booster / server / src / services / astService.ts View on Github external
import { File } from 'ast-types';
import * as jscodeshift from 'jscodeshift';
import * as _ from 'lodash';
import * as os from 'os';
import { PrinterOptions } from 'recast';
import * as vscode from 'vscode-languageserver-types';
import { registerCollectionExtensions } from '../utils/collectionExtensions';
import logService from './logService';

// Hack to adjust default recast options
// making it as close to Prettier as possible.

// tslint:disable-next-line:variable-name
const CollectionPrototype = jscodeshift.withParser('babylon')('').constructor.prototype;
const toSource = CollectionPrototype.toSource;
CollectionPrototype.toSource = function(options: PrinterOptions) {
    return toSource.call(this, {
        quote: 'single',
        ...options
    });
};

registerCollectionExtensions(jscodeshift as jscodeshift.JsCodeShift);

// Zero-based offset
export interface Selection {
    anchor: number;
    active: number;
}
github xsburg / vscode-javascript-booster / server / src / services / astService.ts View on Github external
}

export type LanguageId = 'javascript' | 'javascriptreact' | 'typescript' | 'typescriptreact';

const supportedLanguages: LanguageId[] = [
    'javascript',
    'javascriptreact',
    'typescript',
    'typescriptreact'
];

const codeshifts: { [languageId in LanguageId]: jscodeshift.JsCodeShift } = {
    javascript: jscodeshift.withParser('babylon'),
    javascriptreact: jscodeshift.withParser('babylon'),
    typescript: jscodeshift.withParser('typescript'),
    typescriptreact: jscodeshift.withParser('tsx')
};

export type AstRoot = jscodeshift.Collection;

class AstService {
    public readonly supportedlanguages = supportedLanguages;

    private _astCache: Map<
        string, // cached by fileName
        {
            source: string;
            ast: AstRoot | null;
        }
    > = new Map();

    public constructor() {
github slonoed / jsref / src / fixers / __tests__ / function-to-arrow.spec.ts View on Github external
it('no action when "this" is used inside', () => {
    const source = 'const a = function () { this.call() }'
    const api = jscodeshift.withParser('babylon')
    const params = {
      j: api,
      ast: api(source),
      selection: range.create(1, 11, 1, 11),
      logger: console,
    }
    const action = fixer.suggestCodeAction(params)

    expect(action).toEqual(null)
  })
})
github slonoed / jsref / src / ast-service.ts View on Github external
getCodeShift(uri: string): jscodeshift.JSCodeshift | null {
    const document = this.documents.get(uri)

    if (!document) {
      this.logger.error(`No document found for uri: ${uri}`)
      return null
    }
    const langId = document.languageId

    if (langId === 'javascript' && isFlowCode(document.getText())) {
      return jscodeshift.withParser('flow')
    }

    if (langId === 'typescript') {
      return jscodeshift.withParser('ts')
    }

    if (langId === 'javascript') {
      return jscodeshift.withParser('babylon')
    }

    const errorMsg = `${langId} is not supported`
    this.logger.error(errorMsg)

    return null
  }
}