facebook

How to add a rule that prohibits indefinite variables

  1. CodeMix & Angular IDE
  2.  > 
  3. Webclipse 1.x Help
Viewing 15 posts - 1 through 15 (of 63 total)
  • Author
    Posts
  • #517375 Reply

    gorand
    Participant

    Hellow!
    I need to specify a rule TSLint that in the code variables in which type is not specified are shown – but thus I do not need to show variables at which type it is calculated by assignment of value.

    
    var i = 0; <-- need NOT warning (autodetect number)
    var s = "test"; <-- need NOT warning (autodetect string)
    var t = this; <-- need NOT warning (autodetect class this)
    var o = location.hash; <-- need NOT warning (autodetect string)
    
    var a; <-- need WARNING (not autodetect)
    function(p1) {} <-- need WARNING (not autodetect p1)
    

    I do not like the TSLint “typedef” rule because it does not auto-determine the type of variables (warning var i = 1; ).
    What rule should I specify for defining variables without a type, but skipping variables with type auto-detection?
    How can I add my rules to your system?

    • This topic was modified 7 years, 7 months ago by gorand.
    Attachments:
    You must be logged in to view attached files.
    #517383 Reply

    support-piotr
    Participant

    Hi Gorand!

    You can use your own custom rules in TSLint. Here is short walkthrough: https://palantir.github.io/tslint/develop/custom-rules/ . The source of typedef rule is here: https://github.com/palantir/tslint/blob/master/src/rules/typedefRule.ts . If you happen to start developing your custom rule, I would suggest testing it by executing tslint on command line (through Terminal+ view), because Webclipse doesn’t reload rules and you need to restart it for the changes to be picked up.

    I think the reason why TSLint is not working as you would expect is because there is TypeScript compiler flag “noImplicitAny” (Raise error on expressions and declarations with an implied any type.). It will show error for all the cases, where type checks would be suppressed due to missing type declaration. Note that the local “let a” does not need type spec as the type is calculated on-the-fly and e.g.:

    let a;
    a = 15;
    fun(a)
    a = "ff";
    fun(a);
    
    function fun(a:number) {}

    will report error on the second call of fun. So basically, there is no way you can pass an incorrect type to a method, or return an incorrect type.

    Best regards,
    Piotr Tomiak

    • This reply was modified 7 years, 7 months ago by support-piotr.
    #517431 Reply

    gorand
    Participant

    Many thanks to you for the help. TypeScript compiler flag “noImplicitAny” does not work as I need. However, at your prompt I was able to find and modify the file typedefRule.js
    I spread this file here (see attached file andrTypedefRule.js)
    Original file: \eclipse\plugins\com.genuitec.eclipse.typescript_2.1.5.201703072245\bin\node_modules\tslint\lib\rules\typedefRule.js
    Modified file need copy to path: \eclipse\plugins\com.genuitec.eclipse.typescript_2.1.5.201703072245\bin\node_modules\tslint\lib\rules\andrTypedefRule.js
    if you not find this path – search file typedefRule.js into you hard disk for find path tslint\rules
    To enabled modified file need write any flags into tslint.json:

    
    {
     "rules": {
      "andrtypedef": [true, "parameter", "variable-declaration", "arrow-call-signature", "call-signature", "arrow-parameter", "property-declaration", "member-variable-declaration"]
     }
    }
    

    and restart Eclipse

    • This reply was modified 7 years, 7 months ago by gorand. Reason: bold text
    • This reply was modified 7 years, 7 months ago by gorand. Reason: add images
    • This reply was modified 7 years, 7 months ago by gorand. Reason: add images
    • This reply was modified 7 years, 7 months ago by gorand. Reason: add images
    Attachments:
    You must be logged in to view attached files.
    #517445 Reply

    gorand
    Participant

    Try number two. My previous message somewhere was missing.

    Many thanks to you for the help. TypeScript compiler flag “noImplicitAny” does not work as I need. However, at your prompt I was able to find and modify the file typedefRule.js
    I spread this file here (see attached file andrTypedefRule.js)

    Original file: \eclipse\plugins\com.genuitec.eclipse.typescript_2.1.5.201703072245\bin\node_modules\tslint\lib\rules\typedefRule.js
    Modified file need copy to path: \eclipse\plugins\com.genuitec.eclipse.typescript_2.1.5.201703072245\bin\node_modules\tslint\lib\rules\andrTypedefRule.js
    if you not find this path – search file typedefRule.js into you hard disk for find path tslint\rules
    To enabled modified file need write any flags into tslint.json:

    
    {
     "rules": {
      "andrtypedef": [true, "parameter", "variable-declaration", "arrow-call-signature", "call-signature", "arrow-parameter", "property-declaration", "member-variable-declaration"]
     }
    }
    

    and restart Eclipse

    Attachments:
    You must be logged in to view attached files.
    #517447 Reply

    gorand
    Participant

    Strangely behaving this forum – the message disappears – it appears with a bunch of pictures. How to remove the excess?

    #517448 Reply

    support-piotr
    Participant

    Gorand,

    I am glad you were able to adjust the rule to your needs! You don’t need to copy the rule to node_modules folder to use it. TSLint supports “rulesDirectory” entry in tslint.json, which allows you to specify additional locations to search for rules. For instance your tslint.json could look as follows (assuming your custom rule is in rules folder of your project):

    
    {
     "rulesDirectory": [
       "rules"
     ],
     "rules": {
      "andrtypedef": [true, "parameter", "variable-declaration", "arrow-call-signature", "call-signature", "arrow-parameter", "property-declaration", "member-variable-declaration"]
     }
    }
    

    Relative and absolute paths are also supported and changes to tslint.json are picked up immediately after save. Let me know if you need any further assistance!

    #517450 Reply

    support-piotr
    Participant

    gorand,

    wrt to post disappearing – our anti-spam tooling is misbehaving and marking posts as spams and I had to manually unmark it. Sorry for the problems – our portal team is already working on it.

    Best regards,
    Piotr Tomiak

    #517535 Reply

    gorand
    Participant

    Hellow!
    “rulesDirectory” – not working (see attached images)
    How used “rulesDirectory” ?

    • This reply was modified 7 years, 7 months ago by gorand.
    Attachments:
    You must be logged in to view attached files.
    #517540 Reply

    support-piotr
    Participant

    Gorand,

    The directory is properly loaded – if it wouldn’t be loaded, you would get information about tslint.json being malformed. Your rule name in tslint.json should be andr-typedef, as it is expected to be in ‘kebab-case’ style, where custom rule file name is expected to be in ‘camelCase’ style with ‘Rule’ suffix. Let me know if it works for you!

    #517552 Reply

    gorand
    Participant

    Everything worked out! Thank you.

    #517575 Reply

    gorand
    Participant

    Hellow!
    Does not work as it should. I changed the andrTypedefRule.js file and it only works if there is a file with andrTypedefRule.ts (with compile error) – but over time the JS file automatically deleted from the folder I specified.
    If you completely remove the TS file, then in general the system does not find the rules in the specified folder.
    I can not use the TS file. For him I do not have modules:
    Import * as utils from “tsutils”;
    Import * as ts from “typescript”;
    Import * as Lint from “../index”;

    What to do? Where download “tsutils”, “typescript”, “../index” ?

    • This reply was modified 7 years, 7 months ago by gorand.
    #517597 Reply

    support-piotr
    Participant

    Gorand,

    This is very bizarre. TSLint needs only JS file to load the rule. Is the rule stored in external folder, or your project folder? Do you have compileOnSave enabled?

    #517773 Reply

    gorand
    Participant

    Hellow!
    No, all disabled!
    tsconfig.json:

    
    {
      "compilerOptions": {
        "target": "ES3",
        "module": "CommonJS",
        "moduleResolution": "classic"
      },
      "compileOnSave": false
    }
    

    I created a JavaScript project in eclipse -> then converted it into a TypeScript project and there I keep the files … Rule.js and … Rule.ts.
    The folder for this project was specified in the “rulesDirectory”.
    (see attached images)
    Yesterday 3 times the JS file was spontaneously deleted from the folder of this project.
    If you copy this file to a folder \node_modules\tslint\lib\rules\ then everything works, and there is no “rulesDirectory”.
    I rebooted eclipse – it did not help.
    I have tried both the absolute path and the relative – the result is the same and the same.

    Attachments:
    You must be logged in to view attached files.
    #517777 Reply

    support-piotr
    Participant

    Gorand,

    Looks like a problem is that for some reason TSLint is not able to load typescript module with such a setup. Typescript should be served from Webclipse node_modules, so this looks to be a bug I would have to work on. I am sorry for the issue and I will try to fix it ASAP.

    #518096 Reply

    gorand
    Participant

    Hellow!
    Today, WebClipse has been updated and now there was an error when connecting standard files for its TSLint rule.
    see attached images

    Attachments:
    You must be logged in to view attached files.
Viewing 15 posts - 1 through 15 (of 63 total)
Reply To: How to add a rule that prohibits indefinite variables

You must be logged in to post in the forum log in