All files / packages/design-system/scripts/gulp/lint validate-markup.js

0% Statements 0/32
0% Branches 0/2
0% Functions 0/11
0% Lines 0/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96                                                                                                                                                                                               
// Copyright (c) 2015-present, salesforce.com, inc. All rights reserved
// Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license
 
import { createValidator } from '@salesforce-ux/design-system-markup/server';
import createParser from '@salesforce-ux/design-system-parser';
 
import gulp from 'gulp';
import _ from 'lodash';
import path from 'path';
import through from 'through2';
import Vinyl from 'vinyl';
import PluginError from 'plugin-error';
 
import getComments from '../../ui/comments';
 
const renderMessage = result =>
  `${result.selector} not applied to ${result.restrict}`;
 
const shortReport = errors =>
  _(errors)
    .groupBy(x => x)
    .mapValues(v => v.length)
    .value();
 
const renderReport = (fullReport, fileCount) => ({
  uniqueErrors: Object.keys(fullReport).length,
  total: Object.keys(fullReport).reduce(
    (acc, k) => acc + fullReport[k].length,
    0
  ),
  fileCount,
  report: _.mapValues(fullReport, shortReport)
});
 
const create = filepath => (fullReport, r) => {
  const msg = renderMessage(r);
  return Object.assign(fullReport, {
    [msg]: (fullReport[msg] || []).concat(filepath)
  });
};
 
const printToConsole = (...xs) => console.log.apply(console, xs);
 
const report = validate => {
  const fullReport = {};
  let count = 0;
  const transform = (file, enc, next) => {
    const results = validate(file.contents);
    if (results.length) {
      _(results).reduce(create(file.path), fullReport);
    }
    count += 1;
    next(null, file, enc);
  };
 
  const flush = function(next) {
    const report = renderReport(fullReport, count);
    const json = JSON.stringify(report, null, 2);
    printToConsole(json, 'Full info in .reports/validate.json');
    if (report.total > 0) {
      throw new Error(
        new PluginError(
          'Style restrictions',
          `Encountered ${report.total} style restriction errors`,
          { showStack: false }
        )
      );
    }
    this.push(
      new Vinyl({
        path: 'validations.json',
        contents: Buffer.from(json)
      })
    );
    next();
  };
 
  return through.obj(transform, flush);
};
 
const runValidations = validate =>
  gulp
    .src(['.html/*'])
    .pipe(report(validate))
    .pipe(gulp.dest('.reports/'));
 
const validate = () =>
  getComments()
    .map(createParser)
    .map(parser =>
      createValidator(parser.comments.map(c => c.get('annotations')))
    )
    .map(runValidations);
 
export default validate;