Question
· 23 hr ago

There's any way to check if some part of the code is an abbreviated command?

Hello every one, here in our company we are trying to develop a tool to help us in our "Code Review" that today is completely done for another developer.

So I need to develope a tool that reads a class/routine (Already done) and identify if in the current line there is some abbreviated command, that is against our policy of codification, for example:

s variable = "test"
d ..SomeMethod()

So in this cases I want to warn the original developer that the code has parts that need to change the "s" to "Set" and de "d" to "Do".

I tried to found some library in the documentation but with no success, if there's any way to do It will help us a lot.


Thank all of you so much. 

Product version: Ensemble 2018.1
Discussion (5)2
Log in or sign up to continue

@Igor Barboza 
You can use %Library.SyntaxColor to parse ObjectScript. Here's some code to get you started:

ClassMethod WriteAllCommands()
{
    Set syn = ##class(%SyntaxColor).%New(), in = ##class(%Stream.TmpCharacter).%New(), out = ##class(%Stream.TmpCharacter).%New()
    #; TODO Put your document's contents into "in"
    Do syn.Color(in,out,"COS" /* or "INT" or "CLS" */,"K" /* means JSON output */)
    #; Format of the JSON output:
    #; [
    #;     #; One array for each source line
    #;     [
    #;         {
    #;             #; Language of the token. See Languages() in %Library.SyntaxColor.
    #;             "l": %Integer,
    #;             #; Attribute of the token. See Attributes() in %Library.SyntaxColor.
    #;             "s": %Integer,
    #;             #; Zero-indexed start position of the token on the line
    #;             "p": %Integer,
    #;             #; Length of the token in characters
    #;             "c": %Integer
    #;         }
    #;     ]
    #; ]
    Set json = ##class(%DynamicArray).%FromJSON(out), lineIter = json.%GetIterator()
    While lineIter.%GetNext(.lineNum,.lineTokens) {
        Set tokensIter = lineTokens.%GetIterator()
        While tokensIter.%GetNext(,.token) {
            If (
                #; COS
                token.l = 1 && (
                    #; Command
                    (token.s = 32) ||
                    #; User-defined Z command 
                    (token.s = 52)
                )
            ) {
                Write "Command starting in column ",token.p + 1," of line ",lineNum + 1,!
            }
        }
    }
}