amzn / style-dictionary

A build system for creating cross-platform styles.
https://styledictionary.com
Apache License 2.0
3.87k stars 543 forks source link

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

Open jorenbroekema opened 10 months ago

jorenbroekema commented 10 months ago

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"]
}