All files / src/helpers cssSnapshots.ts

78.95% Statements 15/19
50% Branches 1/2
80% Functions 4/5
75% Lines 12/16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 361x 1x 1x   1x   1x   1x 2x 11x 9x 9x   2x                     1x                    
import { extractICSS, IICSSExports } from 'icss-utils';
import * as postcss from 'postcss';
import * as postcssIcssSelectors from 'postcss-icss-selectors';
import * as ts_module from 'typescript/lib/tsserverlibrary';
import { transformClasses } from './classTransforms';
 
const processor = postcss(postcssIcssSelectors({ mode: 'local' }));
 
export const getClasses = (css: string) =>
  extractICSS(processor.process(css).root).icssExports;
const classNameToProperty = (className: string) => `'${className}': string;`;
const flattenClassNames = (IpreviousValue: string[] = [], currentValue: string[]) =>
  previousValue.concat(currentValue);
 
export const createExports = (classes: IICSSExports, options: IOptions) => `\
declare const classes: {
  ${Object.keys(classes)
    .map(transformClasses(options.camelCase))
    .reduce(flattenClassNames)
    .map(classNameToProperty)
    .join('\n  ')}
};
export default classes;
`;
 
export const getDtsSnapshot = (
  ts: typeof ts_module,
  scriptSnapshot: ts.IScriptSnapshot,
  options: IOptions,
) => {
  const css = scriptSnapshot.getText(0, scriptSnapshot.getLength());
  const classes = getClasses(css);
  const dts = createExports(classes, options);
  return ts.ScriptSnapshot.fromString(dts);
};