Skip to content

Commit

Permalink
Added a rule that tries to check for property visibility annotations,…
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid authored and zepumph committed Oct 22, 2024
1 parent f799cf1 commit eb59871
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions eslint/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@
"require-statement-match": 2,
"phet-io-require-contains-ifphetio": 2,
"require-tandem-support": 0,
//
// Require @public/@private for this.something = result;
"property-visibility-annotation": 2,
"no-property-in-require-statement": 2,
// permit only one var declaration per line, see #390
"one-var": [
Expand Down
65 changes: 65 additions & 0 deletions eslint/rules/property-visibility-annotation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2016, University of Colorado Boulder
/**
* @fileoverview Rule to check that an assignment in a constructor provides @public or @private visibility annotation
* Developed with https://astexplorer.net/
* @author Sam Reid (PhET Interactive Simulations)
* @copyright 2016 University of Colorado Boulder
*/

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function( context ) {
'use strict';

return {

AssignmentExpression: function propertyVisibilityAnnotation( node ) {

if ( node.left && node.left && node.left.object && node.left.object.type === 'ThisExpression' ) {
var leadingComments = node.parent.leadingComments;
var i;
var a;
var isAnnotated = false;
if ( leadingComments ) {
for ( i = 0; i < leadingComments.length; i++ ) {
a = leadingComments[ i ];
if ( a.value.indexOf( '@public' ) >= 0 || a.value.indexOf( '@private' ) >= 0 ) {
isAnnotated = true;
}
}
}

var trailingComments = node.parent.trailingComments;
if ( trailingComments ) {
for ( i = 0; i < trailingComments.length; i++ ) {
a = trailingComments[ i ];
if ( a.value.indexOf( '@public' ) >= 0 || a.value.indexOf( '@private' ) >= 0 ) {
isAnnotated = true;
}
}
}
}

if ( node.parent && node.parent.parent && node.parent.parent.parent ) {
var parentFunction = node.parent.parent.parent;
if ( parentFunction.id && parentFunction.id.name ) {
if ( parentFunction.type === 'FunctionDeclaration' && parentFunction.id.name[ 0 ].toUpperCase() === parentFunction.id.name[ 0 ] ) {
if ( !isAnnotated ) {
context.report( {
node: node,
loc: node.loc.start,
message: 'missing visibility annotation'
} );
}
}
}
}
}
};
};

module.exports.schema = [
// JSON Schema for rule options goes here
];

0 comments on commit eb59871

Please sign in to comment.