Skip to content

Instantly share code, notes, and snippets.

@andrei-markeev
Created June 1, 2016 09:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrei-markeev/496a0e595075b478619ecc68d6844e79 to your computer and use it in GitHub Desktop.
Save andrei-markeev/496a0e595075b478619ecc68d6844e79 to your computer and use it in GitHub Desktop.
Simple lint based on TS compiler API
import * as ts from 'typescript'; // don't forget to "npm install typescript -g && npm link typescript"
var codeToAnalyse = `
function f1() {}
var x = 10;
function hello() { function hmm() {} }
`;
var source = ts.createSourceFile("test.ts", codeToAnalyse, ts.ScriptTarget.ES5);
analyse(source);
function analyse(node: ts.Node)
{
if (node.kind == ts.SyntaxKind.FunctionDeclaration)
{
var name = (<ts.FunctionDeclaration>node).name.text;
if (name.length < 5)
{
console.warn("Function name " + name + " is too short. Provide a better name please!");
}
}
ts.forEachChild(node, analyse);
}
// output:
// Function name f1 is too short. Provide a better name please!
// Function name hmm is too short. Provide a better name please!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment