Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
[Issue #211,#212,#213,#214,#215,#216] grunt package now copies JSON f…
Browse files Browse the repository at this point in the history
…or release build
  • Loading branch information
t-ligu authored and HamletDRC committed Sep 2, 2016
1 parent df0d95f commit b781330
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 46 deletions.
94 changes: 50 additions & 44 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var _ = require('underscore');

module.exports = function(grunt) {
module.exports = function (grunt) {

let additionalMetadata;
let allCweDescriptions;
Expand Down Expand Up @@ -36,7 +36,7 @@ module.exports = function(grunt) {
}

function createCweDescription(metadata) {
allCweDescriptions = allCweDescriptions || grunt.file.readJSON('./cwe_descriptions.json', {encoding: 'UTF-8'});
allCweDescriptions = allCweDescriptions || grunt.file.readJSON('./cwe_descriptions.json', { encoding: 'UTF-8' });

const cwe = getMetadataValue(metadata, 'commonWeaknessEnumeration', true, true);
if (cwe === '') {
Expand All @@ -62,7 +62,7 @@ module.exports = function(grunt) {
}

function getMetadataValue(metadata, name, allowEmpty, doNotEscape) {
additionalMetadata = additionalMetadata || grunt.file.readJSON('./additional_rule_metadata.json', {encoding: 'UTF-8'});
additionalMetadata = additionalMetadata || grunt.file.readJSON('./additional_rule_metadata.json', { encoding: 'UTF-8' });

let value = metadata[name];
if (value == null) {
Expand Down Expand Up @@ -94,7 +94,7 @@ module.exports = function(grunt) {
}

function camelize(input) {
return _(input).reduce(function(memo, element) {
return _(input).reduce(function (memo, element) {
if (element.toLowerCase() === element) {
memo = memo + element;
} else {
Expand All @@ -107,7 +107,7 @@ module.exports = function(grunt) {
function getAllRuleNames(options) {
options = options || { skipTsLintRules: false }

var convertToRuleNames = function(filename) {
var convertToRuleNames = function (filename) {
filename = filename
.replace(/Rule\..*/, '') // file extension plus Rule name
.replace(/.*\//, ''); // leading path
Expand All @@ -126,7 +126,7 @@ module.exports = function(grunt) {

function getAllFormatterNames() {

var convertToRuleNames = function(filename) {
var convertToRuleNames = function (filename) {
filename = filename
.replace(/Formatter\..*/, '') // file extension plus Rule name
.replace(/.*\//, ''); // leading path
Expand All @@ -139,7 +139,7 @@ module.exports = function(grunt) {
}

function camelCase(input) {
return input.toLowerCase().replace(/-(.)/g, function(match, group1) {
return input.toLowerCase().replace(/-(.)/g, function (match, group1) {
return group1.toUpperCase();
});
}
Expand All @@ -166,6 +166,12 @@ module.exports = function(grunt) {
src: ['**/*.js', '!references.js'],
dest: 'dist/build'
},
{
expand: true,
cwd: 'dist/src',
src: ['**/*.json'],
dest: 'dist/build'
},
{
expand: true,
cwd: 'dist/tests',
Expand Down Expand Up @@ -246,7 +252,7 @@ module.exports = function(grunt) {
},
tests: {
options: {
configuration: (function() {
configuration: (function () {
let tslintJson = grunt.file.readJSON("tslint.json", { encoding: 'UTF-8' });
tslintJson.rules['no-multiline-string'] = false;
tslintJson.rules['quotemark'] = false;
Expand Down Expand Up @@ -299,26 +305,26 @@ module.exports = function(grunt) {
grunt.registerTask('validate-documentation', 'A task that validates that all rules defined in src are documented in README.md\n' +
'and validates that the package.json version is the same version defined in README.md', function () {

var readmeText = grunt.file.read('README.md', { encoding: 'UTF-8' });
var packageJson = grunt.file.readJSON('package.json', { encoding: 'UTF-8' });
getAllRuleNames({ skipTsLintRules: true }).forEach(function(ruleName) {
if (readmeText.indexOf(ruleName) === -1) {
grunt.fail.warn('A rule was found that is not documented in README.md: ' + ruleName);
}
});
getAllFormatterNames().forEach(function(formatterName) {
if (readmeText.indexOf(formatterName) === -1) {
grunt.fail.warn('A formatter was found that is not documented in README.md: ' + formatterName);
var readmeText = grunt.file.read('README.md', { encoding: 'UTF-8' });
var packageJson = grunt.file.readJSON('package.json', { encoding: 'UTF-8' });
getAllRuleNames({ skipTsLintRules: true }).forEach(function (ruleName) {
if (readmeText.indexOf(ruleName) === -1) {
grunt.fail.warn('A rule was found that is not documented in README.md: ' + ruleName);
}
});
getAllFormatterNames().forEach(function (formatterName) {
if (readmeText.indexOf(formatterName) === -1) {
grunt.fail.warn('A formatter was found that is not documented in README.md: ' + formatterName);
}
});

if (readmeText.indexOf('\nVersion ' + packageJson.version + ' ') === -1) {
grunt.fail.warn('Version not documented in README.md correctly.\n' +
'package.json declares: ' + packageJson.version + '\n' +
'README.md declares something different.');
}
});

if (readmeText.indexOf('\nVersion ' + packageJson.version + ' ') === -1) {
grunt.fail.warn('Version not documented in README.md correctly.\n' +
'package.json declares: ' + packageJson.version + '\n' +
'README.md declares something different.');
}
});

grunt.registerTask('validate-config', 'A task that makes sure all the rules in the project are defined to run during the build.', function () {

var tslintConfig = grunt.file.readJSON('tslint.json', { encoding: 'UTF-8' });
Expand All @@ -329,7 +335,7 @@ module.exports = function(grunt) {
'no-empty-line-after-opening-brace': true
};
var errors = [];
getAllRuleNames().forEach(function(ruleName) {
getAllRuleNames().forEach(function (ruleName) {
if (rulesToSkip[ruleName]) {
return;
}
Expand All @@ -352,7 +358,7 @@ module.exports = function(grunt) {
const procedure = 'TSLint Procedure';
const header = 'Title,Description,ErrorID,Tool,IssueClass,IssueType,SDL Bug Bar Severity,' +
'SDL Level,Resolution,SDL Procedure,CWE,CWE Description';
getAllRules().forEach(function(ruleFile) {
getAllRules().forEach(function (ruleFile) {
const metadata = getMetadataFromFile(ruleFile);

const issueClass = getMetadataValue(metadata, 'issueClass');
Expand All @@ -373,15 +379,15 @@ module.exports = function(grunt) {
});
rows.sort();
rows.unshift(header);
grunt.file.write('tslint-warnings.csv', rows.join('\n'), {encoding: 'UTF-8'});
grunt.file.write('tslint-warnings.csv', rows.join('\n'), { encoding: 'UTF-8' });

});

grunt.registerTask('generate-recommendations', 'A task that generates the recommended_ruleset.js file', function () {

const groupedRows = {};

getAllRules().forEach(function(ruleFile) {
getAllRules().forEach(function (ruleFile) {
const metadata = getMetadataFromFile(ruleFile);

const groupName = getMetadataValue(metadata, 'group');
Expand All @@ -402,20 +408,20 @@ module.exports = function(grunt) {

_.values(groupedRows).forEach(function (element) { element.sort(); });

let data = grunt.file.read('./templates/recommended_ruleset.js.snippet', {encoding: 'UTF-8'});
data = data.replace('%security_rules%', groupedRows['Security'].join('\n'));
data = data.replace('%correctness_rules%', groupedRows['Correctness'].join('\n'));
data = data.replace('%clarity_rules%', groupedRows['Clarity'].join('\n'));
data = data.replace('%whitespace_rules%', groupedRows['Whitespace'].join('\n'));
data = data.replace('%configurable_rules%', groupedRows['Configurable'].join('\n'));
data = data.replace('%deprecated_rules%', groupedRows['Deprecated'].join('\n'));
let data = grunt.file.read('./templates/recommended_ruleset.js.snippet', { encoding: 'UTF-8' });
data = data.replace('%security_rules%', groupedRows['Security'].join('\n'));
data = data.replace('%correctness_rules%', groupedRows['Correctness'].join('\n'));
data = data.replace('%clarity_rules%', groupedRows['Clarity'].join('\n'));
data = data.replace('%whitespace_rules%', groupedRows['Whitespace'].join('\n'));
data = data.replace('%configurable_rules%', groupedRows['Configurable'].join('\n'));
data = data.replace('%deprecated_rules%', groupedRows['Deprecated'].join('\n'));
data = data.replace('%accessibilityy_rules%', groupedRows['Accessibility'].join('\n'));
grunt.file.write('recommended_ruleset.js', data, {encoding: 'UTF-8'});
grunt.file.write('recommended_ruleset.js', data, { encoding: 'UTF-8' });
});

grunt.registerTask('generate-default-tslint-json', 'A task that converts recommended_ruleset.js to ./dist/build/tslint.json', function () {
const data = require('./recommended_ruleset.js');
grunt.file.write('./dist/build/tslint.json', JSON.stringify(data, null, 2), {encoding: 'UTF-8'});
grunt.file.write('./dist/build/tslint.json', JSON.stringify(data, null, 2), { encoding: 'UTF-8' });
});

grunt.registerTask('create-rule', 'A task that creates a new rule from the rule templates. --rule-name parameter required', function () {
Expand All @@ -436,15 +442,15 @@ module.exports = function(grunt) {
var testFileName = './tests/' + ruleFile.charAt(0).toUpperCase() + ruleFile.substr(1) + 'Tests.ts';
var walkerName = ruleFile.charAt(0).toUpperCase() + ruleFile.substr(1) + 'Walker';

var ruleTemplateText = grunt.file.read('./templates/rule.snippet', {encoding: 'UTF-8'});
var testTemplateText = grunt.file.read('./templates/rule-tests.snippet', {encoding: 'UTF-8'});
var ruleTemplateText = grunt.file.read('./templates/rule.snippet', { encoding: 'UTF-8' });
var testTemplateText = grunt.file.read('./templates/rule-tests.snippet', { encoding: 'UTF-8' });

grunt.file.write(sourceFileName, applyTemplates(ruleTemplateText), {encoding: 'UTF-8'});
grunt.file.write(testFileName, applyTemplates(testTemplateText), {encoding: 'UTF-8'});
grunt.file.write(sourceFileName, applyTemplates(ruleTemplateText), { encoding: 'UTF-8' });
grunt.file.write(testFileName, applyTemplates(testTemplateText), { encoding: 'UTF-8' });

var currentRuleset = grunt.file.readJSON('./tslint.json', {encoding: 'UTF-8'});
var currentRuleset = grunt.file.readJSON('./tslint.json', { encoding: 'UTF-8' });
currentRuleset.rules[ruleName] = true;
grunt.file.write('./tslint.json', JSON.stringify(currentRuleset, null, 2), {encoding: 'UTF-8'});
grunt.file.write('./tslint.json', JSON.stringify(currentRuleset, null, 2), { encoding: 'UTF-8' });
}
});

Expand Down
16 changes: 15 additions & 1 deletion src/a11yImgHasAltRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import * as ts from 'typescript';
import * as Lint from 'tslint/lib/lint';

import { ExtendedMetadata } from './utils/ExtendedMetadata';
import {
getAllAttributesFromJsxElement,
getJsxAttributesFromJsxElement,
Expand All @@ -29,6 +29,20 @@ A reference for the presentation role can be found at https://www.w3.org/TR/wai-
}

export class Rule extends Lint.Rules.AbstractRule {
public static metadata: ExtendedMetadata = {
ruleName: 'a11y-img-has-alt',
type: 'maintainability',
description: 'Enforce that an `img` element contains the `alt` attribute or `role=\'presentation\'` for decorative image.',
options: 'string[]',
optionExamples: ['[true, [\'Image\']]'],
issueClass: 'Non-SDL',
issueType: 'Warning',
severity: 'Important',
level: 'Opportunity for Excellence',
group: 'Clarity',
commonWeaknessEnumeration: '398, 710'
};

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return sourceFile.languageVariant === ts.LanguageVariant.JSX
? this.applyWithWalker(new ImgHasAltWalker(sourceFile, this.getOptions()))
Expand Down
2 changes: 1 addition & 1 deletion src/utils/attributes/IAria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export interface IAria {
type: string;
values: string[];
allowUndefined: boolean;
}
}

0 comments on commit b781330

Please sign in to comment.