String prototype extension not detected by code validator



7 years ago
3 years ago


(Reporter: jorgev, Assigned: basta)


Q1 2012


(Whiteboard: [ReviewTeam], URL)



7 years ago
I'm reviewing version 0.51 of the DiggiDig extension:, and found that it has String prototype extension in file options.js, line 219. This should have been picked up by the validator. These are the current results for that file:

Comment 1

7 years ago
I just had a second look and found this warning:

Global variable overwrite
Warning: An attempt was made to overwrite a global variable in some JavaScript code.

It points to code following the line that should have been flagged. However, the warning says nothing of this being a case of prototype extension, and the code that really matters (String.prototype.diggiTrim = function () ...)doesn't appear anywhere.
Assignee: nobody → mattbasta

Comment 2

7 years ago
I can't see the validation results on prod. Can you pastebin the output for me?

Comment 3

7 years ago
Here's the JSON output:
Target Milestone: 6.3.0 → 6.3.2
Target Milestone: 6.3.2 → 6.3.3

Comment 4

7 years ago
I'm going to say that this is the intended behavior. String prototypes (or any prototypes, for that matter, are treated the same way. There's no way right now to "traverse back" through the global scope to see where the JSPrototype object is referenced from, so any modification to it is a global variable overwrite error.

As for the code not appearing in the snippet, that's because the assignment spans multiple lines. Since assignments are statements and not declarations, the "start" line (which we use to position the window for the snippet) is at the end of the assignment, rather than the beginning (where the String.prototype.diggiTrim bit is). There's really no workaround for that unless Spidermonkey changes the way it generates its parse tree, unfortunately.

Unless there's something particular about this specific case that you think would be appropriate, I can't do a whole lot to make the warning easier to interpret.

Comment 5

7 years ago
It'd be more effective then to change the validation to a regular expression like
(String|Number|Object).prototype.something =
And then you can distinguish it from the other generic errors.

Comment 6

7 years ago
The problem there is that it would yield a second error since that kind of assignment is considered an overwrite of a critical global object. It could be marked as non-critical, but then we wouldn't get warnings/errors for things like this:

    var x = String;
    x.prototype.something = foo;


    var x = String.prototype;
    x.something = foo;

The regex would also have to consider the alternative means of assignment:

    String.prototype["something"] = foo;

where "something" can be any expression.

Comment 7

7 years ago
Almost every single prototype extension case I've seen uses a pattern similar to comment #5. Since the current warnings are not being helpful, I think that adding the RE is an improvement. Extending it to cover String.prototype["something"] sounds good.
Target Milestone: 6.3.3 → 6.3.4
Target Milestone: 6.3.4 → Q1 2012


7 years ago
Last Resolved: 7 years ago
Resolution: --- → FIXED

Comment 9

7 years ago
Reclassifying editor bugs and changing to a new whiteboard flag. Spam, spam, spam, spam...
Whiteboard: [required amo-editors] → [ReviewTeam]
Product: → Graveyard
You need to log in before you can comment on or make changes to this bug.