Example #1
0
def jsCheckSyntax(file, codestring):
    """
    Function to check a Javascript source file for syntax errors.
    
    @param file source filename (string)
    @param codestring string containing the code to check (string)
    @return dictionary with the keys 'error' and 'warnings' which
            hold a list containing details about the error/ warnings
            (file name, line number, column, codestring (only at syntax
            errors), the message, a list with arguments for the message)
    """
    import jasy.js.parse.Parser as jsParser
    import jasy.js.tokenize.Tokenizer as jsTokenizer
    
    codestring = normalizeCode(codestring)
    
    try:
        jsParser.parse(codestring, file)
    except (jsParser.SyntaxError, jsTokenizer.ParseError) as exc:
        details = exc.args[0]
        error, details = details.splitlines()
        fn, line = details.strip().rsplit(":", 1)
        error = error.split(":", 1)[1].strip()
        
        cline = min(len(codestring.splitlines()), int(line)) - 1
        code = codestring.splitlines()[cline]
        return [{'error': (fn, int(line), 0, code, error)}]
    
    return [{}]
Example #2
0
File: api.py Project: zynga/jasy
    def process(self, code):
        node = Parser.parse(code)
        ScopeScanner.scan(node)
        data = Data.ApiData("test")
        data.scanTree(node)

        return data
Example #3
0
    def getTree(self, permutation=None, cleanup=True):
        """
        Returns the tree (of nodes from the parser) of the class. This parses the class,
        creates the tree, applies and optional permutation, scans for variables usage 
        and puts the tree into the cache before returning it. The cache works with the
        permutation, so every permutated tree is cached separately.
        """
        
        permutation = self.filterPermutation(permutation)
        
        field = "tree[%s]-%s-%s" % (self.__id, permutation, cleanup)
        tree = self.__cache.read(field, self.__mtime)
        if tree is not None:
            return tree
            
        # Parse tree
        tree = Parser.parse(self.getText(), self.__name)

        # Apply permutation
        if permutation:
            jasy.js.clean.Permutate.patch(tree, permutation)

        # Remove dead code
        if cleanup:
            jasy.js.clean.DeadCode.cleanup(tree)

        # Scan for variable usage
        ScopeScanner.scan(tree)
        
        # Remove unused variables/functions
        if cleanup:
            jasy.js.clean.Unused.cleanup(tree)
        
        self.__cache.store(field, tree, self.__mtime, True)
        return tree
Example #4
0
File: inject.py Project: Val9/jasy
 def process(self, code, contextId=""):
     node = Parser.parse(code)
     permutation = Permutation.Permutation(
         {"debug": False, "legacy": True, "engine": "webkit", "version": 3, "fullversion": 3.11}
     )
     Permutate.patch(node, permutation)
     return Compressor.Compressor().compress(node)
Example #5
0
File: api.py Project: Andais/jasy
 def process(self, code):
     node = Parser.parse(code)
     ScopeScanner.scan(node)
     data = Data.ApiData("test")
     data.scanTree(node)
     
     return data
Example #6
0
    def getTree(self, permutation=None, cleanup=True):
        """
        Returns the tree (of nodes from the parser) of the class. This parses the class,
        creates the tree, applies and optional permutation, scans for variables usage 
        and puts the tree into the cache before returning it. The cache works with the
        permutation, so every permutated tree is cached separately.
        """

        permutation = self.filterPermutation(permutation)

        field = "tree[%s]-%s-%s" % (self.__id, permutation, cleanup)
        tree = self.__cache.read(field, self.__mtime)
        if tree is not None:
            return tree

        # Parse tree
        tree = Parser.parse(self.getText(), self.__name)

        # Apply permutation
        if permutation:
            jasy.js.clean.Permutate.patch(tree, permutation)

        # Remove dead code
        if cleanup:
            jasy.js.clean.DeadCode.cleanup(tree)

        # Scan for variable usage
        ScopeScanner.scan(tree)

        # Remove unused variables/functions
        if cleanup:
            jasy.js.clean.Unused.cleanup(tree)

        self.__cache.store(field, tree, self.__mtime, True)
        return tree
Example #7
0
    def process(self, code):
        node = Parser.parse(code)

        translation = Translation.TranslationItem(
            None,
            id="de_DE",
            table={
                "Hello World": "Hallo Welt",
                "Short": "Kurz",
                "Thank you for the flowers": "Danke fĆ¼r die Blumen",
                "Hello %1!": "Hallo: %1!",
                "Hello %1! %1!": "Hallo: %1! %1!",
                "Chat[C:Chat (noum)]": "Unterhaltung",
                "Chat %1[C:Chat (noum) %1]": "Unterhaltung %1",
                "You have got a new mail[N:You have got new mails]": {
                    0: "Du hast eine neue E-Mail",
                    1: "Du hast neue E-Mails"
                },
                "You have got a new mail[N:You have got %1 new mails]": {
                    0: "Du hast eine neue E-Mail",
                    1: "Du hast %1 neue E-Mail erhalten"
                }
            })

        TranslationOptimizer.optimize(node, translation)

        return Compressor.Compressor().compress(node)
Example #8
0
 def process(self, code, contextId=""):
     node = Parser.parse(code)
     permutation = Permutation.Permutation({
         'debug': False,
         'legacy': True,
         'engine': 'webkit',
         'version': 3,
         'fullversion': 3.11
     })
     Permutate.patch(node, permutation)
     return Compressor.Compressor().compress(node)
Example #9
0
 def process(self, code, contextId=""):
     node = Parser.parse(code)
     permutation = Permutation.Permutation({
         'debug': False,
         'legacy': True,
         'engine': 'webkit',
         'version': 3,
         'fullversion': 3.11
     })
     Permutate.patch(node, permutation)
     return Compressor.Compressor().compress(node)    
Example #10
0
 def __getTree(self, context=None):
     
     field = "tree[%s]" % self.id
     tree = self.project.getCache().read(field, self.mtime)
     if not tree:
         info("Processing class %s %s...", colorize(self.id, "bold"), colorize("[%s]" % context, "cyan"))
         
         indent()
         tree = Parser.parse(self.getText(), self.id)
         ScopeScanner.scan(tree)
         outdent()
         
         self.project.getCache().store(field, tree, self.mtime, True)
     
     return tree
Example #11
0
 def parse(self):
     """
     Public method to parse the source.
     
     @return dictionary containing the parsed information
     """
     try:
         self.__root = jsParser.parse(self.__source, self.__file)
         self.__visit(self.__root)
     except jsParser.SyntaxError:
         # ignore syntax errors of the parser
         pass
     except jsTokenizer.ParseError:
         # ignore syntax errors of the tokenizer
         pass
     
     return self.__dict
Example #12
0
File: Class.py Project: zynga/jasy
    def __getTree(self, context=None):

        field = "tree[%s]" % self.id
        tree = self.project.getCache().read(field, self.mtime)
        if not tree:
            Console.info("Processing class %s %s...",
                         Console.colorize(self.id, "bold"),
                         Console.colorize("[%s]" % context, "cyan"))

            Console.indent()
            tree = Parser.parse(self.getText(), self.id)
            ScopeScanner.scan(tree)
            Console.outdent()

            self.project.getCache().store(field, tree, self.mtime, True)

        return tree
Example #13
0
    def parse(self):
        """
        Public method to parse the source.
        
        @return dictionary containing the parsed information
        """
        try:
            self.__root = jsParser.parse(self.__source, self.__file)
            self.__visit(self.__root)
        except jsParser.SyntaxError:
            # ignore syntax errors of the parser
            pass
        except jsTokenizer.ParseError:
            # ignore syntax errors of the tokenizer
            pass

        return self.__dict
Example #14
0
    def process(self, code):
        node = Parser.parse(code)

        translation = Translation.Translation(
            "de_DE",
            table={
                "Hello World": "Hallo Welt",
                "Short": "Kurz",
                "Thank you for the flowers": "Danke fĆ¼r die Blumen",
                "Hello %1!": "Hallo: %1!",
                "Hello %1! %1!": "Hallo: %1! %1!",
                "Chat (noum)": "Unterhaltung",
                "Chat (noum) %1": "Unterhaltung %1",
                "You have got a new mail": "Du hast eine neue E-Mail",
                "You have got new mails": "Du hast neue E-Mails",
                "You have got %1 new mails": "Du hast %1 neue E-Mail erhalten"
            })

        translation.patch(node)

        return Compressor.Compressor().compress(node)
Example #15
0
    def process(self, code):
        node = Parser.parse(code)

        translation = Translation.TranslationItem(None, id="de_DE", table={
            
            "Hello World": "Hallo Welt",
            "Short": "Kurz",
            "Thank you for the flowers": "Danke fĆ¼r die Blumen",
            
            "Hello %1!": "Hallo: %1!",
            "Hello %1! %1!": "Hallo: %1! %1!",
            
            "Chat[C:Chat (noum)]": "Unterhaltung",
            "Chat %1[C:Chat (noum) %1]": "Unterhaltung %1",
            
            "You have got a new mail[N:You have got new mails]": {0:"Du hast eine neue E-Mail", 1:"Du hast neue E-Mails"},
            "You have got a new mail[N:You have got %1 new mails]": {0:"Du hast eine neue E-Mail", 1:"Du hast %1 neue E-Mail erhalten"}
            
        })
        
        TranslationOptimizer.optimize(node, translation)
        
        return Compressor.Compressor().compress(node)        
Example #16
0
    def process(self, code):
        node = Parser.parse(code)

        translation = Translation.Translation("de_DE", table={
            
            "Hello World": "Hallo Welt",
            "Short": "Kurz",
            "Thank you for the flowers": "Danke fĆ¼r die Blumen",
            
            "Hello %1!": "Hallo: %1!",
            "Hello %1! %1!": "Hallo: %1! %1!",
            
            "Chat (noum)": "Unterhaltung",
            "Chat (noum) %1": "Unterhaltung %1",
            
            "You have got a new mail": "Du hast eine neue E-Mail",
            "You have got new mails": "Du hast neue E-Mails",
            "You have got %1 new mails": "Du hast %1 neue E-Mail erhalten"
            
        })
        
        translation.patch(node)
        
        return Compressor.Compressor().compress(node)        
Example #17
0
 def process(self, code):
     node = Parser.parse(code)
     ScopeScanner.scan(node)
     CombineDeclarations.optimize(node)
     return Compressor.Compressor().compress(node)
Example #18
0
def patch(node, permutation):
    """ Replaces all occourences with incoming values """

    modified = False

    if node.type == "dot" and node.parent.type == "call":
        assembled = assembleDot(node)

        # jasy.Env.getValue(key)
        if assembled == "jasy.Env.getValue" and node.parent.type == "call":
            callNode = node.parent
            params = callNode[1]
            name = params[0].value

            Console.debug("Found jasy.Env.getValue(%s) in line %s", name,
                          node.line)

            replacement = __translateToJS(permutation.get(name))
            if replacement:
                replacementNode = Parser.parseExpression(replacement)
                callNode.parent.replace(callNode, replacementNode)
                modified = True

                Console.debug("Replaced with %s", replacement)

        # jasy.Env.isSet(key, expected)
        # also supports boolean like: jasy.Env.isSet(key)
        elif assembled == "jasy.Env.isSet" and node.parent.type == "call":

            callNode = node.parent
            params = callNode[1]
            name = params[0].value

            Console.debug("Found jasy.Env.isSet(%s) in line %s", name,
                          node.line)

            replacement = __translateToJS(permutation.get(name))

            if replacement != None:
                # Auto-fill second parameter with boolean "true"
                expected = params[1] if len(
                    params) > 1 else Parser.parseExpression("true")

                if expected.type in ("string", "number", "true", "false"):
                    parsedReplacement = Parser.parseExpression(replacement)
                    expectedValue = getattr(expected, "value", None)

                    if expectedValue is not None:
                        if getattr(parsedReplacement, "value",
                                   None) is not None:
                            replacementResult = parsedReplacement.value in str(
                                expected.value).split("|")
                        else:
                            replacementResult = parsedReplacement.type in str(
                                expected.value).split("|")
                    else:
                        replacementResult = parsedReplacement.type == expected.type

                    # Do actual replacement
                    replacementNode = Parser.parseExpression(
                        "true" if replacementResult else "false")
                    callNode.parent.replace(callNode, replacementNode)
                    modified = True

                    Console.debug("Replaced with %s",
                                  "true" if replacementResult else "false")

        # jasy.Env.select(key, map)
        elif assembled == "jasy.Env.select" and node.parent.type == "call":
            Console.debug("Found jasy.Env.select() in line %s", node.line)

            callNode = node.parent
            params = callNode[1]
            replacement = __translateToJS(permutation.get(params[0].value))
            if replacement:
                parsedReplacement = Parser.parseExpression(replacement)
                if parsedReplacement.type != "string":
                    raise Exception(
                        "jasy.Env.select requires that the given replacement is of type string."
                    )

                # Directly try to find matching identifier in second param (map)
                objectInit = params[1]
                if objectInit.type == "object_init":
                    fallbackNode = None
                    for propertyInit in objectInit:
                        if propertyInit[0].value == "default":
                            fallbackNode = propertyInit[1]

                        elif parsedReplacement.value in str(
                                propertyInit[0].value).split("|"):
                            callNode.parent.replace(callNode, propertyInit[1])
                            modified = True
                            break

                    if not modified and fallbackNode is not None:
                        callNode.parent.replace(callNode, fallbackNode)
                        modified = True

                        Console.debug("Updated with %s", replacement)

    # Process children
    for child in reversed(node):
        if child != None:
            if patch(child, permutation):
                modified = True

    return modified
Example #19
0
 def process(self, code):
     node = Parser.parse(code)
     ScopeScanner.scan(node)
     CombineDeclarations.optimize(node)
     return Compressor.Compressor().compress(node)
Example #20
0
 def process(self, code):
     node = Parser.parse(code)
     BlockReducer.optimize(node)
     return Compressor.Compressor().compress(node)
Example #21
0
 def process(self, code, contextId=""):
     node = Parser.parse(code)
     CryptPrivates.optimize(node, contextId)
     return Compressor.Compressor().compress(node)        
Example #22
0
 def process(self, code):
     node = Parser.parse(code)
     ScopeScanner.scan(node)
     LocalVariables.optimize(node)
     return Compressor.Compressor().compress(node)
Example #23
0
 def process(self, code):
     node = Parser.parse(code)
     return node
Example #24
0
File: meta.py Project: Andais/jasy
 def process(self, code):
     tree = Parser.parse(code)
     meta = MetaData(tree)
     return meta
Example #25
0
def patch(node, permutation):
    """ Replaces all occourences with incoming values """

    modified = False
    
    if node.type == "dot" and node.parent.type == "call":
        assembled = assembleDot(node)
        
        # jasy.Env.getValue(key)
        if assembled == "jasy.Env.getValue" and node.parent.type == "call":
            callNode = node.parent
            params = callNode[1]
            name = params[0].value

            Console.debug("Found jasy.Env.getValue(%s) in line %s", name, node.line)

            replacement = __translateToJS(permutation.get(name))
            if replacement:
                replacementNode = Parser.parseExpression(replacement)
                callNode.parent.replace(callNode, replacementNode)
                modified = True

                Console.debug("Replaced with %s", replacement)
         
        
        # jasy.Env.isSet(key, expected)
        # also supports boolean like: jasy.Env.isSet(key)
        elif assembled == "jasy.Env.isSet" and node.parent.type == "call":

            callNode = node.parent
            params = callNode[1]
            name = params[0].value

            Console.debug("Found jasy.Env.isSet(%s) in line %s", name, node.line)

            replacement = __translateToJS(permutation.get(name))

            if replacement != None:
                # Auto-fill second parameter with boolean "true"
                expected = params[1] if len(params) > 1 else Parser.parseExpression("true")

                if expected.type in ("string", "number", "true", "false"):
                    parsedReplacement = Parser.parseExpression(replacement)
                    expectedValue = getattr(expected, "value", None)
                    
                    if expectedValue is not None:
                        if getattr(parsedReplacement, "value", None) is not None:
                            replacementResult = parsedReplacement.value in str(expected.value).split("|")
                        else:
                            replacementResult = parsedReplacement.type in str(expected.value).split("|")
                    else:
                        replacementResult = parsedReplacement.type == expected.type

                    # Do actual replacement
                    replacementNode = Parser.parseExpression("true" if replacementResult else "false")
                    callNode.parent.replace(callNode, replacementNode)
                    modified = True

                    Console.debug("Replaced with %s", "true" if replacementResult else "false")

        
        # jasy.Env.select(key, map)
        elif assembled == "jasy.Env.select" and node.parent.type == "call":
            Console.debug("Found jasy.Env.select() in line %s", node.line)

            callNode = node.parent
            params = callNode[1]
            replacement = __translateToJS(permutation.get(params[0].value))
            if replacement:
                parsedReplacement = Parser.parseExpression(replacement)
                if parsedReplacement.type != "string":
                    raise Exception("jasy.Env.select requires that the given replacement is of type string.")

                # Directly try to find matching identifier in second param (map)
                objectInit = params[1]
                if objectInit.type == "object_init":
                    fallbackNode = None
                    for propertyInit in objectInit:
                        if propertyInit[0].value == "default":
                            fallbackNode = propertyInit[1]

                        elif parsedReplacement.value in str(propertyInit[0].value).split("|"):
                            callNode.parent.replace(callNode, propertyInit[1])
                            modified = True
                            break

                    if not modified and fallbackNode is not None:
                        callNode.parent.replace(callNode, fallbackNode)
                        modified = True

                        Console.debug("Updated with %s", replacement)


    # Process children
    for child in reversed(node):
        if child != None:
            if patch(child, permutation):
                modified = True

    return modified


    
Example #26
0
 def process(self, code):
     tree = Parser.parse(code)
     meta = MetaData(tree)
     return meta
Example #27
0
 def process(self, code):
     return Compressor.Compressor().compress(Parser.parse(code))
Example #28
0
 def process(self, code):
     node = Parser.parse(code)
     DeadCode.cleanup(node)
     return Compressor.Compressor().compress(node)
Example #29
0
 def process(self, code):
     return Compressor.Compressor().compress(Parser.parse(code))
Example #30
0
File: unused.py Project: Val9/jasy
 def process(self, code):
     node = Parser.parse(code)
     Unused.cleanup(node)
     return Compressor.Compressor().compress(node)
Example #31
0
 def process(self, code):
     node = Parser.parse(code)
     BlockReducer.optimize(node)
     return Compressor.Compressor().compress(node)
Example #32
0
 def process(self, code):
     node = Parser.parse(code)
     ScopeScanner.scan(node)
     LocalVariables.optimize(node)
     return Compressor.Compressor().compress(node)
Example #33
0
 def process(self, code):
     node = Parser.parse(code)
     return node
Example #34
0
 def process(self, code, contextId=""):
     node = Parser.parse(code)
     CryptPrivates.optimize(node, contextId)
     return Compressor.Compressor().compress(node)