Coder Social home page Coder Social logo

mattlewis92 / codelyzer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mgechev/codelyzer

0.0 3.0 0.0 16.53 MB

Static analysis for Angular projects.

Home Page: http://codelyzer.com/

License: MIT License

TypeScript 99.97% JavaScript 0.03%

codelyzer's Introduction

npm version Downloads Build Status Build status code style: prettier Conventional Commits Gitter Chat

codelyzer

A set of tslint rules for static code analysis of Angular TypeScript projects.

You can run the static code analyzer over web apps, NativeScript, Ionic, etc.

Vote for your favorite feature here. For more details about the feature request process see this document

How to use?

Angular CLI

Angular CLI has support for codelyzer. In order to validate your code with CLI and the custom Angular specific rules just use:

ng new codelyzer
ng lint

Note that by default all components are aligned with the style guide so you won't see any errors in the console.

Angular Seed

Another project which has out of the box integration with codelyzer is angular-seed. In order to run the linter you should:

# Skip if you've already cloned Angular Seed
git clone https://github.com/mgechev/angular-seed

# Skip if you've already installed all the dependencies of Angular Seed
cd angular-seed && npm i

# Run all the tslint and codelyzer rules
npm run lint

Note that by default all components are aligned with the style guide so you won't see any errors in the console.

Custom Setup

Preset

You can use the tslint-angular preset. All you need is:

npm i tslint-angular

After that create a tslint.json file with the following configuration:

{
  "extends": ["tslint-angular"]
}

Run the linter with:

./node_modules/.bin/tslint -c tslint.json

TSLint will now complain that there are rules which require type checking. In order to fix this, use thw -p config option:

./node_modules/.bin/tslint -p tsconfig.json -c tslint.json

Custom Installation

You can easily use codelyzer with your custom setup:

npm i codelyzer tslint @angular/compiler @angular/core

A. Using codelyzer package in PATH

Create the following tslint.json file like:

{
  "extends": ["codelyzer"],
  "rules": {
    "banana-in-box": true,
    "templates-no-negated-async": true,
    "directive-selector": [true, "attribute", "sg", "camelCase"],
    "component-selector": [true, "element", "sg", "kebab-case"],
    "max-inline-declarations": true,
    "no-life-cycle-call": true,
    "prefer-output-readonly": true,
    "no-conflicting-life-cycle-hooks": true,
    "enforce-component-selector": true,
    "no-queries-parameter": true,
    "prefer-inline-decorator": true,
    "use-input-property-decorator": true,
    "use-output-property-decorator": true,
    "use-host-property-decorator": true,
    "use-view-encapsulation": true,
    "no-attribute-parameter-decorator": true,
    "no-output-named-after-standard-event": true,
    "no-input-rename": true,
    "no-output-rename": true,
    "no-output-on-prefix": true,
    "no-forward-ref": true,
    "no-unused-css": true,
    "use-life-cycle-interface": true,
    "contextual-life-cycle": true,
    "trackBy-function": true,
    "relative-url-prefix": true,
    "use-pipe-transform-interface": true,
    "component-class-suffix": true,
    "directive-class-suffix": true,
    "pipe-impure": true,
    "i18n": [true, "check-id", "check-text"],
    "template-cyclomatic-complexity": [true, 5],
    "template-conditional-complexity": [true, 4]
  }
}

To run TSLint with this setup you can use one of the following alternatives:

  1. Install codelyzer globally npm install -g codelyzer

  2. Run TSLint from a package.json script by adding a script like tslint . to your package.json, similar to:

"scripts": {
  ...
  "lint": "tslint .",
  ...
},

Then run npm run lint

B. Using codelyzer from node_modules directory

Now create the following tslint.json file where your node_modules directory is:

{
  "rulesDirectory": ["node_modules/codelyzer"],
  "rules": {
    "banana-in-box": true,
    "templates-no-negated-async": true,
    "directive-selector": [true, "attribute", "sg", "camelCase"],
    "component-selector": [true, "element", "sg", "kebab-case"],
    "max-inline-declarations": true,
    "no-life-cycle-call": true,
    "prefer-output-readonly": true,
    "no-conflicting-life-cycle-hooks": true,
    "enforce-component-selector": true,
    "no-queries-parameter": true,
    "prefer-inline-decorator": true,
    "use-input-property-decorator": true,
    "use-output-property-decorator": true,
    "use-host-property-decorator": true,
    "use-view-encapsulation": true,
    "no-attribute-parameter-decorator": true,
    "no-output-named-after-standard-event": true,
    "no-input-rename": true,
    "no-output-rename": true,
    "no-output-on-prefix": true,
    "no-forward-ref": true,
    "no-unused-css": true,
    "use-life-cycle-interface": true,
    "contextual-life-cycle": true,
    "trackBy-function": true,
    "relative-url-prefix": true,
    "use-pipe-transform-interface": true,
    "component-class-suffix": true,
    "directive-class-suffix": true,
    "pipe-impure": true,
    "i18n": [true, "check-id", "check-text"],
    "template-cyclomatic-complexity": [true, 5],
    "template-conditional-complexity": [true, 4]
  }
}

Next you can create a component file in the same directory with name component.ts and the following content:

import { Component } from '@angular/core';

@Component({
  selector: 'codelyzer',
  template: `
    <h1>Hello {{ name }}!</h1>
  `
})
class Codelyzer {
  name: string = 'World';

  ngOnInit() {
    console.log('Initialized');
  }
}

As last step you can execute all the rules against your code with tslint:

./node_modules/.bin/tslint -c tslint.json component.ts

You should see the following output:

component.ts[4, 13]: The selector of the component "Codelyzer" should have prefix "sg" (https://goo.gl/cix8BY)
component.ts[12, 3]: Implement lifecycle hook interface OnInit for method ngOnInit in class Codelyzer (https://goo.gl/w1Nwk3)
component.ts[9, 7]: The name of the class Codelyzer should end with the suffix Component (https://goo.gl/5X1TE7)

Editor Configuration

Note that you need to have tslint plugin install on your editor.

Codelyzer should work out of the box with Atom but for VSCode you will have to open Code > Preferences > User Settings, and enter the following config:

{
  "tslint.rulesDirectory": "./node_modules/codelyzer",
  "typescript.tsdk": "node_modules/typescript/lib"
}

Now you should have the following result:

VSCode Codelyzer

Enjoy!

Changelog

You can find it here.

Recommended configuration

Below you can find a recommended configuration which is based on the Angular Style Guide.

{
  // The rule have the following arguments:
  // [ENABLED, "attribute" | "element", "selectorPrefix" | ["listOfPrefixes"], "camelCase" | "kebab-case"]
  "directive-selector": [true, "attribute", ["dir-prefix1", "dir-prefix2"], "camelCase"],
  "component-selector": [true, "element", ["cmp-prefix1", "cmp-prefix2"], "kebab-case"],

  "use-input-property-decorator": true,
  "use-output-property-decorator": true,
  "use-host-property-decorator": true,
  "no-attribute-parameter-decorator": true,
  "no-input-rename": true,
  "no-output-on-prefix": true,
  "no-output-rename": true,
  "no-forward-ref": true,
  "use-life-cycle-interface": true,
  "use-pipe-transform-interface": true,
  "no-output-named-after-standard-event": true,
  "max-inline-declarations": true,
  "no-life-cycle-call": true,
  "prefer-output-readonly": true,
  "no-conflicting-life-cycle-hooks": true,
  "enforce-component-selector": true,
  "no-queries-parameter": true,
  "prefer-inline-decorator": true,
  "relative-url-prefix": true,
  // [ENABLED, "SUFFIX"]
  // Where "SUFFIX" is your custom suffix, for instance "Page" for Ionic 2 components.
  "component-class-suffix": [true, "Component"],
  "directive-class-suffix": [true, "Directive"]
}

Rules Status

Rule Status
banana-in-box Stable
contextual-life-cycle Stable
decorator-not-allowed Stable
pipe-impure Stable
templates-no-negated-async Stable
no-attribute-parameter-decorator Stable
no-forward-ref Stable
no-input-prefix Stable
no-input-rename Stable
no-output-on-prefix Stable
no-output-rename Stable
use-life-cycle-interface Stable
use-pipe-decorator Stable
use-pipe-transform-interface Stable
use-view-encapsulation Stable
component-class-suffix Stable
component-selector Stable
directive-class-suffix Stable
directive-selector Stable
use-host-property-decorator Stable
use-input-property-decorator Stable
use-output-property-decorator Stable
trackBy-function Stable
import-destructuring-spacing Stable
no-output-named-after-standard-event Stable
max-inline-declarations Stable
prefer-output-readonly Stable
enforce-component-selector Stable
no-life-cycle-call Stable
no-template-call-expression Stable
no-queries-parameter Stable
prefer-inline-decorator Stable
template-cyclomatic-complexity Stable
relative-url-prefix Experimental
no-conflicting-life-cycle-hooks Experimental
i18n Experimental
no-unused-css Experimental
template-conditional-complexity Experimental
angular-whitespace Deprecated
pipe-naming Deprecated

Disable a rule that validates Template or Styles

Lint rules can be disabled by adding a marker in TypeScript files. More information here.

To disable rules that validate templates or styles you'd need to add a marker in the TypeScript file referencing them.

import { Component } from '@angular/core';

/* tslint:disable:trackBy-function */
@Component({
  selector: 'codelyzer',
  templateUrl: './codelyzer.component.html'
})
class Codelyzer {}

Advanced configuration

Codelyzer supports any template and style language by custom hooks. If you're using Sass for instance, you can allow codelyzer to analyze your styles by creating a file .codelyzer.js in the root of your project (where the node_modules directory is). In the configuration file can implement custom pre-processing and template resolution logic:

// Demo of transforming Sass styles
var sass = require('node-sass');

module.exports = {
  // Definition of custom interpolation strings
  interpolation: ['{{', '}}'],

  // You can transform the urls of your external styles and templates
  resolveUrl(url, decorator) {
    return url;
  },

  // Transformation of the templates. This hooks is quite useful
  // if you're using any other templating language, for instance
  // jade, markdown, haml, etc.
  //
  // NOTE that this method WILL NOT throw an error in case of invalid template.
  //
  transformTemplate(code, url, decorator) {
    return { code: code, url: url };
  },

  // Transformation of styles. This hook is useful is you're using
  // any other style language, for instance Sass, Less, etc.
  //
  // NOTE that this method WILL NOT throw an error in case of invalid style.
  //
  transformStyle(code, url, decorator) {
    var result = { code: code, url: url };
    if (url && /\.scss$/.test(url)) {
      var transformed = sass.renderSync({ data: code, sourceMap: true, outFile: '/dev/null' });
      result.source = code;
      result.code = transformed.css.toString();
      result.map = transformed.map.toString();
    }
    return result;
  },

  // Custom predefined directives in case you get error for
  // missing property and you are using a template reference
  predefinedDirectives: [{ selector: 'form', exportAs: 'ngForm' }],

  // None = 0b000, Error = 0b001, Info = 0b011, Debug = 0b111
  logLevel: 0b111
};

Contributors

mgechev wKoza preslavsh rafaelss95 rokerkony GregOnNet
mgechev wKoza preslavsh rafaelss95 rokerkony GregOnNet
alan-agius4 kevinphelps eppsilon csvn ghsyeung Kobzol
alan-agius4 kevinphelps eppsilon csvn ghsyeung Kobzol
lazarljubenovic sagittarius-rev connor4312 Foxandxss gbilodeau NagRock
lazarljubenovic sagittarius-rev connor4312 Foxandxss gbilodeau NagRock
Hotell Martin-Wegner comfroels plantain-00 nexus-uw alexkpek
Hotell Martin-Wegner comfroels plantain-00 nexus-uw alexkpek
loktionov129 alisd23 Moeriki sneas Gillespie59 eromano
loktionov129 alisd23 Moeriki sneas Gillespie59 eromano
Manduro karol-depka leosvelperez muhammadghazali PapsOu piyukore06
Manduro karol-depka leosvelperez muhammadghazali PapsOu piyukore06
rwlogel robzenn92 rtfpessoa scttcper lacolaco tmair
rwlogel robzenn92 rtfpessoa scttcper lacolaco tmair
cexbrayat clydin reduckted someblue
cexbrayat clydin reduckted someblue

License

MIT

codelyzer's People

Contributors

mgechev avatar wkoza avatar rafaelss95 avatar rokerkony avatar alan-agius4 avatar kevinphelps avatar eppsilon avatar csvn avatar ghsyeung avatar kobzol avatar lazarljubenovic avatar sagittarius-rev avatar nexus-uw avatar plantain-00 avatar comfroels avatar hotell avatar nagrock avatar gbilodeau avatar foxandxss avatar connor4312 avatar someblue avatar reduckted avatar clydin avatar cexbrayat avatar tmair avatar stschake avatar scttcper avatar rtfpessoa avatar robzenn92 avatar rwlogel avatar

Watchers

James Cloos avatar Matt Lewis avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.