Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Ban rule not picking up methods correctly #2868

Closed
evusik opened this issue Jun 2, 2017 · 5 comments
Closed

Ban rule not picking up methods correctly #2868

evusik opened this issue Jun 2, 2017 · 5 comments

Comments

@evusik
Copy link

evusik commented Jun 2, 2017

Bug Report

  • TSLint version: tslint@5.4.2
  • TypeScript version: typescript@2.3.4
  • Running TSLint via: CLI

TypeScript code being linted

'use strict';

module teamspace.components {
  'use strict';

  export type SearchResult = {
    titleHtml: string;
    descriptionHtml: string;
    originalObject: middletier.User
  };

  export class CallingRosterSearchResultsController {
    private minLength: number = 1;
    private titleField: string = 'displayName';
    private descriptionField: string = 'email';
    private searchFields: string[] = [this.titleField, this.descriptionField];
    private matchClass = 'search-highlight';
    private logger: services.Logger;

    public getParticipantsToSearch: () => middletier.User[];
    public searchString: string = '';
    public searchResults: SearchResult[] = [];
    public enablePstn: boolean = false;

    private searchDeferred: ng.IDeferred<SearchResult[]> = null;

    constructor(private $scope,
                private $sce: ng.ISCEService,
                private constants: Constants,
                private loggingService: services.LoggingService,
                private peopleService: services.PeopleService,
                private $q: ng.IQService,
                private pstnNumberService: services.PstnNumberService,
                private settingsService: services.SettingsService,
                private $translate: ng.translate.ITranslateService) {
      this.logger = loggingService.newLogger('roster-search');
      $scope.$watch('searchString', () => {
        this.processText();
      });
    }

    public addParticipant(searchResult: SearchResult) {
      this.$scope.$emit(this.constants.events.calling.participantSelected, searchResult.originalObject);
      this.searchString = '';
	  eval(searchString)
    }

    // Process the matched contacts to style it
    private findMatchString(target, searchString) {
      var result, matches, re;
      var escapedQuery = _.escapeRegExp(searchString);
      re = new RegExp('\\s' + escapedQuery + '|(^' + escapedQuery + ')', 'gi');
      matches = target.match(re);
      result = matches ? target.replace(re, '<span class="' + this.matchClass + '">' + matches[0] + '</span>') : target;
      return this.$sce.trustAsHtml(result);
    }
    }
  }

with tslint.json configuration:

{
  "rulesDirectory": [
	"node_modules/tslint/lib/rules"
  ],
  "rules": {
        "no-eval": true,
		"ban": [
				true,
				"eval",
				{"name": ["*", "trustAsHtml"], "message": "Dont use trustAsHtml"}
				]
    }
}

Actual behavior

Only one issue from no-eval rule found:

[{
		"endPosition": {
			"character": 7,
			"line": 44,
			"position": 1683
		},
		"failure": "forbidden eval",
		"name": "example2.ts",
		"ruleName": "no-eval",
		"startPosition": {
			"character": 3,
			"line": 44,
			"position": 1679
		}
	}
]

Expected behavior

Three issues should be reported. One eval from the no-eval rule, one eval from the ban rule and one trustAsHtml from ban rule.
I copied the json configuration from the guidance in https://palantir.github.io/tslint/rules/ban/, so I hope I got it right :)

@ajafff
Copy link
Contributor

ajafff commented Jun 2, 2017

"rulesDirectory": [
"node_modules/tslint/lib/rules"
],

I guess you are using a globally installed tslint for linting your project. Unfortunately you can't overwrite the rules directory for builtin rules.
I assume npm ls tslint -g does not print 4.5.2? So the global one is using an older version of the rule that doesn't support the new options format.

Either use node_modules/.bin/tslint or update your global installation (npm install tslint -g). You can remove the rulesDirectory from your tslint.json in any case.

@evusik
Copy link
Author

evusik commented Jun 6, 2017

Hi @ajafff,
thanks for the tip - you were right, I was running an older version of tslint globally. However, using the one from node_modules/.bin/tslint didn't help - I'm getting an empty result file now :(

mydir>node_modules\.bin\tslint --version
5.4.2

mydir>node_modules\.bin\tslint ./example.ts --config ./tslint-custom_ban.json --format json --out tslint_out.json
This results in an empty tslint_out.json.

If you run tslint with example.ts and tslint-custom_ban.json with the content listed in my original post and the following command, does it work for you?

tslint ./example.ts --config ./tslint-custom_ban.json --format json --out tslint_out.json

Thanks tons in advance for your help.

@ajafff
Copy link
Contributor

ajafff commented Jun 6, 2017

The bug with the empty outfile is fixed by #2867 which will be part of the next release

@adidahiya
Copy link
Contributor

fix for #2867 is now available in v5.4.3

@evusik
Copy link
Author

evusik commented Jun 8, 2017

Thanks tons guys!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants