View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.ecmascript;
5   
6   import net.sourceforge.pmd.Rule;
7   import net.sourceforge.pmd.lang.ParserOptions;
8   import net.sourceforge.pmd.lang.rule.properties.BooleanProperty;
9   import net.sourceforge.pmd.lang.rule.properties.EnumeratedProperty;
10  import net.sourceforge.pmd.util.StringUtil;
11  
12  import org.mozilla.javascript.Context;
13  
14  public class EcmascriptParserOptions extends ParserOptions {
15  
16      public enum Version {
17  	VERSION_DEFAULT("default", Context.VERSION_DEFAULT),
18  	VERSION_1_0("1.0", Context.VERSION_1_0),
19  	VERSION_1_1("1.1", Context.VERSION_1_1),
20  	VERSION_1_2("1.2", Context.VERSION_1_2),
21  	VERSION_1_3("1.3", Context.VERSION_1_3),
22  	VERSION_1_4("1.4", Context.VERSION_1_4),
23  	VERSION_1_5("1.5", Context.VERSION_1_5),
24  	VERSION_1_6("1.6", Context.VERSION_1_6),
25  	VERSION_1_7("1.7", Context.VERSION_1_7),
26  	VERSION_1_8("1.8", Context.VERSION_1_8);
27  
28  	private final String name;
29  	private final int version;
30  
31  	private Version(String name, int version) {
32  	    this.name = name;
33  	    this.version = version;
34  	}
35  
36  	public String getLabel() {
37  	    return name;
38  	}
39  
40  	public int getVersion() {
41  	    return version;
42  	}
43      }
44  
45      private static final String[] VERSION_LABELS = new String[] { Version.VERSION_DEFAULT.getLabel(),
46  	    Version.VERSION_1_0.getLabel(), Version.VERSION_1_1.getLabel(), Version.VERSION_1_2.getLabel(),
47  	    Version.VERSION_1_3.getLabel(), Version.VERSION_1_4.getLabel(), Version.VERSION_1_5.getLabel(),
48  	    Version.VERSION_1_6.getLabel(), Version.VERSION_1_7.getLabel(), Version.VERSION_1_8.getLabel(), };
49  
50      // Note: The UI order values are chosen to be larger than those built into XPathRule.
51      public static final BooleanProperty RECORDING_COMMENTS_DESCRIPTOR = new BooleanProperty("recordingComments",
52  	    "Specifies that comments are produced in the AST.", Boolean.TRUE, 3.0f);
53      public static final BooleanProperty RECORDING_LOCAL_JSDOC_COMMENTS_DESCRIPTOR = new BooleanProperty(
54  	    "recordingLocalJsDocComments", "Specifies that JsDoc comments are produced in the AST.", Boolean.TRUE, 4.0f);
55      public static final EnumeratedProperty<Version> RHINO_LANGUAGE_VERSION = new EnumeratedProperty<>(
56  	    "rhinoLanguageVersion",
57  	    "Specifies the Rhino Language Version to use for parsing.  Defaults to Rhino default.", VERSION_LABELS,
58  	    Version.values(), 0, 5.0f);
59  
60      private boolean recordingComments;
61      private boolean recordingLocalJsDocComments;
62      private Version rhinoLanguageVersion;
63  
64      public EcmascriptParserOptions() {
65  	this.recordingComments = RECORDING_COMMENTS_DESCRIPTOR.defaultValue().booleanValue();
66  	this.recordingLocalJsDocComments = RECORDING_LOCAL_JSDOC_COMMENTS_DESCRIPTOR.defaultValue().booleanValue();
67  	this.rhinoLanguageVersion = (Version) RHINO_LANGUAGE_VERSION.valueFrom((String) RHINO_LANGUAGE_VERSION
68  		.defaultValue());
69      }
70  
71      public EcmascriptParserOptions(Rule rule) {
72  	this.recordingComments = rule.getProperty(RECORDING_COMMENTS_DESCRIPTOR);
73  	this.recordingLocalJsDocComments = rule.getProperty(RECORDING_LOCAL_JSDOC_COMMENTS_DESCRIPTOR);
74  	this.rhinoLanguageVersion = (Version) RHINO_LANGUAGE_VERSION.valueFrom((String) rule
75  		.getProperty(RHINO_LANGUAGE_VERSION));
76      }
77  
78      public boolean isRecordingComments() {
79  	return this.recordingComments;
80      }
81  
82      public void setRecordingComments(boolean recordingComments) {
83  	this.recordingComments = recordingComments;
84      }
85  
86      public boolean isRecordingLocalJsDocComments() {
87  	return this.recordingLocalJsDocComments;
88      }
89  
90      public void setRecordingLocalJsDocComments(boolean recordingLocalJsDocComments) {
91  	this.recordingLocalJsDocComments = recordingLocalJsDocComments;
92      }
93  
94      public Version getRhinoLanguageVersion() {
95  	return this.rhinoLanguageVersion;
96      }
97  
98      public void setRhinoLanguageVersion(Version rhinoLanguageVersion) {
99  	this.rhinoLanguageVersion = rhinoLanguageVersion;
100     }
101 
102     @Override
103     public int hashCode() {
104 	final int prime = 31;
105 	int result = super.hashCode();
106 	result = prime * result + (recordingComments ? 1231 : 1237);
107 	result = prime * result + (recordingLocalJsDocComments ? 1231 : 1237);
108 	result = prime * result + ((rhinoLanguageVersion == null) ? 0 : rhinoLanguageVersion.hashCode());
109 	return result;
110     }
111 
112     @Override
113     public boolean equals(Object obj) {
114 	if (this == obj) {
115 	    return true;
116 	}
117 	if (obj == null || getClass() != obj.getClass()) {
118 	    return false;
119 	}
120 	final EcmascriptParserOptions that = (EcmascriptParserOptions) obj;
121 	return StringUtil.isSame(this.suppressMarker, that.suppressMarker, false, false, false)
122 		&& this.recordingComments == that.recordingComments
123 		&& this.recordingLocalJsDocComments == that.recordingLocalJsDocComments
124 		&& this.rhinoLanguageVersion == that.rhinoLanguageVersion;
125     }
126 }