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

@nrwl/devkit names function incorrectly converts PascalName to CONSTANT_NAME #12681

Closed
ApplY3D opened this issue Oct 18, 2022 · 5 comments · Fixed by #17046
Closed

@nrwl/devkit names function incorrectly converts PascalName to CONSTANT_NAME #12681

ApplY3D opened this issue Oct 18, 2022 · 5 comments · Fixed by #17046
Assignees
Labels
community This is a good first issue for contributing outdated scope: devkit type: bug

Comments

@ApplY3D
Copy link
Contributor

ApplY3D commented Oct 18, 2022

Current Behavior

export const <%= constantName %> = {
  FEATURE_NAME: '<%= fileName %>',
} as const;

if InventoryGroupingTypes passed, it became

export const INVENTORYGROUPINGTYPES = {
  FEATURE_NAME: 'inventory-grouping-types',
} as const;

Expected Behavior

name function should correctly convert InventoryGroupingTypes to INVENTORY_GROUPING_TYPES

Steps to Reproduce

  1. Create generator (index.ts):
import {
  Tree,
  formatFiles,
  generateFiles,
  joinPathFragments,
  names,
  readJson,
} from '@nrwl/devkit';
import { SchemaServiceWithFacade } from './schema';

const useProjectConfig = (tree: Tree, options: any) => {
  const workspace = readJson(tree, 'angular.json');

  if (options.project) {
    const project = workspace.projects[options.project];
    if (options.path.includes(<string>project.sourceRoot)) {
    } else {
      options.path = `${project.sourceRoot}/${options.path}`;
    }
  }

  return { tree, options };
};

export default async function (tree: Tree, options: SchemaServiceWithFacade) {
  useProjectConfig(tree, options);

  const namesObj = names(options.name);

  const createPath = options.flat
    ? joinPathFragments(options.path)
    : joinPathFragments(options.path, namesObj.fileName);

  generateFiles(tree, joinPathFragments(__dirname, 'files'), createPath, {
    tmpl: '',
    ...namesObj,
  });
  await formatFiles(tree);
}
  1. Create files folder with any file contains lines in Current Behavior named ...__tmpl__

Environment

 >  NX   Report complete - copy this into the issue template

   Node : 16.17.0
   OS   : darwin arm64
   pnpm : 7.13.4
   
   nx : 14.6.1
   @nrwl/angular : 14.6.1
   @nrwl/cypress : 14.6.1
   @nrwl/detox : Not Found
   @nrwl/devkit : 14.6.1
   @nrwl/eslint-plugin-nx : 14.6.1
   @nrwl/express : Not Found
   @nrwl/jest : 14.6.1
   @nrwl/js : 14.6.1
   @nrwl/linter : 14.6.1
   @nrwl/nest : 14.6.1
   @nrwl/next : Not Found
   @nrwl/node : 14.6.1
   @nrwl/nx-cloud : 14.6.0
   @nrwl/nx-plugin : Not Found
   @nrwl/react : Not Found
   @nrwl/react-native : Not Found
   @nrwl/schematics : Not Found
   @nrwl/storybook : 14.6.1
   @nrwl/web : 14.6.1
   @nrwl/workspace : 14.6.1
   typescript : 4.7.4
   ---------------------------------------
   Local workspace plugins:
   ---------------------------------------
   Community plugins:
         @ngrx/effects: 14.0.2
         @ngrx/entity: 14.0.2
         @ngrx/eslint-plugin: 14.3.0
         @ngrx/router-store: 14.0.2
         @ngrx/store: 14.0.2
         @ngrx/store-devtools: 14.0.2
         @ngrx/schematics: 14.0.2
         ngx-build-plus: 14.0.0
@ApplY3D
Copy link
Contributor Author

ApplY3D commented Oct 18, 2022

also this files should be at the same level as index.ts

schema.d.ts:

export interface SchemaServiceWithFacade {
  name: string;
  path: string;
  project?: string;
  flat?: boolean;
}

schema.json

{
  "$schema": "http://json-schema.org/schema",
  "cli": "nx",
  "$id": "service-with-facade",
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "description": "Library name",
      "$default": {
        "$source": "argv",
        "index": 0
      }
    },
    "path": {
      "type": "string",
      "format": "path",
      "description": "The path at which to create the class, relative to the workspace root.",
      "visible": false
    },
    "project": {
      "type": "string",
      "description": "The name of the project."
    },
    "flat": {
      "type": "boolean"
    }
  },
  "required": ["name", "path"]
}

@ApplY3D ApplY3D changed the title @nrwl/devkit name function incorrectly converts PascalName to CONSTANT_NAME @nrwl/devkit names function incorrectly converts PascalName to CONSTANT_NAME Oct 18, 2022
@FrozenPandaz FrozenPandaz added community This is a good first issue for contributing scope: devkit labels Oct 18, 2022
@gperdomor
Copy link
Contributor

In Nx 12, 13 and 14

names("INPUT_ENGINE").constantName returns INPUT_ENGINE
names("input-engine").constantName returns INPUT_ENGINE

But in Nx 15.0.4 the behavior changes to:

names("INPUT_ENGINE").constantName returns I_NPUTENGINE
names("input-engine").constantName returns INPUT_ENGINE

This is a bug?

@ApplY3D
Copy link
Contributor Author

ApplY3D commented Oct 29, 2022

@gperdomor

Looks like yes, but CONSTANT_NAME not even tested to be input in any cases.
https://github.com/nrwl/nx/blob/master/packages/devkit/src/utils/names.spec.ts

@jensbodal
Copy link
Contributor

One solution here is to just check if the provided name is all uppercase then treat it as lowercase for the transformations

  // names function expects a non constant name for most cases, if one is provided then transform it as lowercase for consistency
  const normalizedName = name.toUpperCase() === name ? name.toLowerCase() : name;

Though it seems like this just wasn't intended to be used for constant names as the base for transformation, so might be better to just type check the input value.

type NotUpperCase<T extends string> = T extends Uppercase<T> ? never : T;

export function names<T extends string>(name: NotUpperCase<T>): {

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
community This is a good first issue for contributing outdated scope: devkit type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants