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

Align APIs of transforms, fileHeader, formats etc. registers #1049

Open
jorenbroekema opened this issue Nov 27, 2023 · 0 comments
Open

Align APIs of transforms, fileHeader, formats etc. registers #1049

jorenbroekema opened this issue Nov 27, 2023 · 0 comments
Assignees
Labels
4.0 Planned for v4 major version enhancement

Comments

@jorenbroekema
Copy link
Collaborator

jorenbroekema commented Nov 27, 2023

Our APIs for registering custom things should be more consistent, it's currently quite confusing and easy to make mistakes due to lack of consistency.

Before:

registerParser({ pattern: /\.json$/g, parse: () => {} });

registerPreprocessor({ name: 'foo', preprocessor: () => {} });

registerTransform({ name: 'foo', type: 'name', transformer: () => {} });

registerTransformGroup({ name: 'foo', transforms: [] });

registerFormat({ name: 'foo', formatter: () => {} });

registerFilter({ name: 'foo', filter: () => {} });

registerFileHeader({ name: 'foo', fileHeader: () => {} });

registerAction({ name: 'foo', do: () => {}, undo: () => {} });

parsers = [];
preprocessors = {};
transform = {};
transformGroup = {};
format = {};
filter = {};
fileHeader = {};
action = {};

After:

registerParser({ name: 'foo', pattern: /\.json$/g, parser: () => {} }); // add name prop, parse -> parser

registerPreprocessor({ name: 'foo', preprocessor: () => {} }); // no change

registerTransform({ name: 'foo', type: 'name', transform: () => {} }); // transformer -> transform

registerTransformGroup({ name: 'foo', transforms: [] }); // no change

registerFormat({ name: 'foo', format: () => {} }); // formatter -> format

registerFilter({ name: 'foo', filter: () => {} }); // no change

registerFileHeader({ name: 'foo', fileHeader: () => {} }); // no change

registerAction({ name: 'foo', do: () => {}, undo: () => {} }); // no change

hooks.parsers = {}; // scoped to hooks object & changed to keyed object
hooks.preprocessors = {}; // scoped to hooks object
hooks.transforms = {}; // scoped to hooks object & changed to plural
hooks.transformGroups = {}; // scoped to hooks object & changed to plural 
hooks.formats = {}; // scoped to hooks object & changed to plural
hooks.filters = {}; // scoped to hooks object & changed to plural
hooks.fileHeaders = {}; // scoped to hooks object & changed to plural
hooks.actions = {}; // scoped to hooks object & changed to plural

Also, registered parsers don't just run by default because they're registered, for consistency sake you should apply parsers on the global config level by name, the same way all other hooks are applied:

export default {
  hooks: {
    // inline definition of php parser as alternative to `registerParser` method
    parsers: {
      "php-parser": {
        pattern: /\.php$/,
        parse: ({ filePath, contents }) => {
          return parse(contents);
        },
      },
    },
  },
  source: [
    "**/*.tokens.json"
  ],
  // applying the parser to this dictionary config
  parsers: ["php-parser"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.0 Planned for v4 major version enhancement
Projects
Development

No branches or pull requests

1 participant