question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Transformation error (Using the export keyword between a decorator and a class is not allowed. Please use `export @dec class` instead.

See original GitHub issue

While doing the codemod via jscodeshift API, I get the error as below.

Screenshot 2022-04-21 at 2 51 05 PM

Transformation error (Using the export keyword between a decorator and a class is not allowed. Please use export @dec class instead.

My React Code

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import _ from 'lodash';
import {Button} from 'common/components';
import Link from 'common/components/ui/Link';
import showDialog from 'app/lib/showDialog';
import PersonaEditDialogContainer from 'app/components/personas/PersonaEditDialogContainer';

function mapStateToProps(state, ownProps) {
  return {
    personas: _.values(state.entities.personas).filter(
      (p) => ownProps.personaIds.indexOf(p.id) != -1,
    ),
  };
}
@connect(
  mapStateToProps,
  {},
)
export default class PersonaLinks extends Component {
  static propTypes = {
    personaIds: PropTypes.array.isRequired,
    handlePersonaUpdate: PropTypes.func,
  };

  handlePersonaClick = (persona) => {
    showDialog(this.context, PersonaEditDialogContainer, {
      mode: 'edit',
      persona,
      updatePersonaCallback: this.props.handlePersonaUpdate,
    });
  };

  render() {
    const { personas } = this.props;
    if (personas.length == 0) {
      return null;
    } else {
      return (
        <span>
          {personas.map((p, i) => (
            <span>
              <Button iconName="minus"/>
              <Link onClick={this.handlePersonaClick.bind(this, p)}>{p.name}</Link>
              {i < personas.length - 2 && ', '}
              {i == personas.length - 2 && ' and '}
            </span>
          ))}
        </span>
      );
    }
  }
}

My TransformFile

/* eslint-disable no-param-reassign */
const addImports = require('jscodeshift-add-imports');

module.exports.parser = 'babel';

module.exports = function transformer(file, api) {
  const j = api.jscodeshift;
  const { statement } = j.template;

  const rootSource = j(file.source);

  /**
   * Remove import statements if Button is the only specifier in the import statements
   */
  rootSource.find(j.ImportDeclaration).forEach((path) => {
    if (path.value.specifiers.length === 1 && path.value.specifiers[0].local.name === 'Button') {
      j(path).remove();
    }
  });

  /**
   * Remove old button references from import statements
   */
  rootSource.find(j.ImportDeclaration).forEach((path) => {
    const updatedSpecifiers = [];
    const specifiers = path.value.specifiers;
    specifiers.forEach((specifier) => {
      if (specifier.local.name !== 'Button') {
        updatedSpecifiers.push(specifier);
      }
      path.value.specifiers = updatedSpecifiers;
    });
  });

  /**
   * Adds the NewButton import statement.
   * * Uses `jscodeshift-add-imports` library for the same.
   */
  addImports(rootSource, [
    statement`import NewButton from 'common/components/design-system/Button';`,
  ]);

  // Modifiy the component name in the usage
  rootSource.find(j.JSXIdentifier, { name: 'Button' }).forEach((path) => {
    path.value.name = 'NewButton';
  });

  //Modify the props for the Button component
  rootSource.find(j.JSXIdentifier, { name: 'NewButton' }).forEach((path) => {
    const attributes = path.parentPath.value.attributes;
    attributes &&
      attributes.forEach((apath) => {
        if (apath.name && apath.name.name === 'iconName') {
          apath.name.name = 'icon';
        }
      });
  });

  return rootSource.toSource();
};

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Daniel15commented, May 4, 2022

Maybe we should change it in a major version bump just in case it breaks anything. We can bundle other potentially-risky changes in the same version.

0reactions
klippxcommented, Dec 8, 2022

I do not personally like decorators, but here I am in a codebase where I am trying to write a codemod to migrate out of typegraphql to remove all decorator annotations. But I hit a snag that the parser does not comprehend it. How do I get it to parse a file such as this one?

import { Directive, Field, ObjectType } from 'type-graphql';
import { SchemaBase } from './schemaBase';
import { Visualization } from './visualization';

@ObjectType({
  description: 'Description goes here',
})
@Directive('@cacheControl(maxAge: 200)')
export class VisualizationList {
  @Field(() => [Visualization], {
    nullable: true,
  })
  visualizations?: Visualization[];
}

EDIT: Got it working with this config.

const options = {
  dry: true,
  print: true,
  verbose: 1,
  parser: 'babylon',
  parserConfig: {
    sourceType: 'module',
    allowImportExportEverywhere: true,
    allowReturnOutsideFunction: true,
    startLine: 1,
    tokens: true,
    plugins: [
      'typescript',
      ['decorators', { decoratorsBeforeExport: true }],
    ],
  },
};

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using the export keyword between a decorator and a class is ...
I am getting this error using 8.2.2 , I've tried the solution given in #662 but it's not ... Please use export @dec...
Read more >
babel 7 Using the export keyword between a decorator and a ...
Parsing error: Using the export keyword between a decorator and a class is not allowed. Please use export @dec class instead. my code...
Read more >
decorator after export keyword produces syntax error.
WebStorm shows syntax error after export keyword, but this is valid code. ... a decorator and a class is not allowed. Please use...
Read more >
babel 7 Using the export keyword between a decorator and a ...
Parsing error: Using the export keyword between a decorator and a class is not allowed. Please use export @dec class instead.
Read more >
babel 7 Using the export keyword between a decorator and a ...
[Solved]-babel 7 Using the export keyword between a decorator and a class is not allowed. Please use `export @dec class` instead-babel.js · score:9....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found