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

586 walk in new rule template #702

Merged
merged 4 commits into from
Dec 28, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ejs = require('ejs');
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');
reduckted marked this conversation as resolved.
Show resolved Hide resolved
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.ejs'), 'utf8');
const ruleSource = ejs.render(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.ejs
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.

6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"chalk": "^2.4.1",
"chokidar": "^2.0.4",
"cpy-cli": "^2.0.0",
"ejs": "^2.6.1",
"glob": "^7.1.3",
"husky": "^1.1.3",
"inquirer": "^6.2.1",
Expand Down