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

Commit

Permalink
586 walk in new rule template (#702)
Browse files Browse the repository at this point in the history
* Used a walk function instead of extending RuleWalker in the new rule template.

* Used EJS for the new rule template.

* Added a prompt asking whether the new rule has options.

* Replaced EJS templates with Underscore templates.
  • Loading branch information
reduckted authored and Josh Goldberg committed Dec 28, 2018
1 parent f47d93e commit a065198
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 59 deletions.
32 changes: 12 additions & 20 deletions build-tasks/create-rule.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { execSync } = require('child_process');
const { template } = require('underscore');
const fs = require('fs');
const inquirer = require('inquirer');
const { execSync } = require('child_process');
const path = require('path');
const { writeFile } = require('./common/files');

const questions = [
Expand All @@ -27,6 +29,12 @@ const questions = [
return 'Please enter a description for the rule.';
}
},
{
name: 'hasOptions',
message: 'Has options:',
type: 'confirm',
default: false
},
{
name: 'typescriptOnly',
message: 'TypeScript only:',
Expand Down Expand Up @@ -92,21 +100,9 @@ inquirer.prompt(questions).then(answers => {
function createImplementationFile(answers) {
const ruleFile = camelCase(answers.name) + 'Rule';
const sourceFileName = 'src/' + ruleFile + '.ts';
const walkerName = pascalCase(ruleFile) + 'Walker';

const ruleTemplate = require('./templates/rule.template');
const ruleSource = ruleTemplate({
ruleName: answers.name,
walkerName,
type: answers.type,
description: answers.description,
typescriptOnly: answers.typescriptOnly,
issueClass: answers.issueClass,
issueType: answers.issueType,
severity: answers.severity,
level: answers.level,
group: answers.group
});

const ruleTemplate = fs.readFileSync(path.resolve(__dirname, 'templates/rule.template'), 'utf8');
const ruleSource = template(ruleTemplate)(answers);

writeFile(sourceFileName, ruleSource);

Expand Down Expand Up @@ -147,10 +143,6 @@ function camelCase(input) {
return input.toLowerCase().replace(/-(.)/g, (match, group1) => group1.toUpperCase());
}

function pascalCase(input) {
return input.charAt(0).toUpperCase() + input.substr(1);
}

function tryOpenFiles(files) {
// Check if we're running in the VS Code terminal. If we
// are, then we can try to open the new files in VS Code.
Expand Down
48 changes: 48 additions & 0 deletions build-tasks/templates/rule.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as ts from 'typescript';
import * as Lint from 'tslint';

import { ExtendedMetadata } from './utils/ExtendedMetadata';
import { AstUtils } from './utils/AstUtils';
import { Utils } from './utils/Utils';

const FAILURE_STRING: string = 'Some error message: '; // TODO: Define an error message
<% if (hasOptions) { %>
interface Options {
// TODO: Add option properties.
}
<% } %>
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: ExtendedMetadata = {
ruleName: '<%= name %>',
type: '<%= type %>',
description: '<%= description %>',<% if (hasOptions) { %>
options: {
// TODO: Fill in the options.
},
optionsDescription: '', // TODO: Fill in the options description.
optionExamples: [], // TODO: Add option examples.
<% } else { %>
options: null, // tslint:disable-line:no-null-keyword
optionsDescription: '',
<% } %>typescriptOnly: <%= typescriptOnly %>,
issueClass: '<%= issueClass %>',
issueType: '<%= issueType %>',
severity: '<%= severity %>',
level: '<%= level %>',
group: '<%= group %>',
commonWeaknessEnumeration: '...' // if possible, please map your rule to a CWE (see cwe_descriptions.json and https://cwe.mitre.org)
};

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk<% if (hasOptions) { %>, this.getOptions()<% } %>);
}
}

function walk(ctx: Lint.WalkContext<<% if (hasOptions) { %>Options<% } else { %>void<% } %>>) {
function cb(node: ts.Node): void {
// TODO: Implement the rule here.
return ts.forEachChild(node, cb);
}

return ts.forEachChild(ctx.sourceFile, cb);
}
39 changes: 0 additions & 39 deletions build-tasks/templates/rule.template.js

This file was deleted.

0 comments on commit a065198

Please sign in to comment.