From eb59871b9b718c559bbfc27ccf37a2236003c0d2 Mon Sep 17 00:00:00 2001 From: samreid Date: Sun, 5 Nov 2017 07:37:05 -0700 Subject: [PATCH] Added a rule that tries to check for property visibility annotations, see https://github.com/phetsims/chipper/issues/618 --- eslint/.eslintrc | 3 + .../rules/property-visibility-annotation.js | 65 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 eslint/rules/property-visibility-annotation.js diff --git a/eslint/.eslintrc b/eslint/.eslintrc index 113079a8..73659369 100644 --- a/eslint/.eslintrc +++ b/eslint/.eslintrc @@ -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": [ diff --git a/eslint/rules/property-visibility-annotation.js b/eslint/rules/property-visibility-annotation.js new file mode 100644 index 00000000..b436f7ba --- /dev/null +++ b/eslint/rules/property-visibility-annotation.js @@ -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 +]; \ No newline at end of file