Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🏗 Report unused private fields #14761

Merged
merged 5 commits into from
Apr 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"amphtml-internal/no-style-property-setting": 2,
"amphtml-internal/query-selector": 2,
"amphtml-internal/todo-format": 0,
"amphtml-internal/unused-private-field": 1,
"amphtml-internal/vsync": 0,
"array-bracket-spacing": [2, "never"],
"arrow-parens": [2, "as-needed"],
Expand Down
101 changes: 101 additions & 0 deletions build-system/eslint-rules/unused-private-field.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* Copyright 2018 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';

module.exports = function(context) {
function stripComments(text) {
// Multi-line comments
text = text.replace(/\/\*(?!.*\*\/)(.|\n)*?\*\//g, function(match) {
// Preserve the newlines
const newlines = [];
for (let i = 0; i < match.length; i++) {
if (match[i] === '\n') {
newlines.push('\n');
}
}
return newlines.join('');
});
// Single line comments either on its own line or following a space,
// semi-colon, or closing brace
return text.replace(/( |}|;|^) *\/\/.*/g, '$1');
}

function checkClassUse(node, name) {
if (!name.endsWith('_')) {
return;
}

const comments = context.getCommentsBefore(node);
const testing = comments.some(comment => {
return /@(visibleForTesting|protected|override)\b/.test(comment.value);
});
if (testing) {
return;
}

const ancestors = context.getAncestors(node);
const body = ancestors.reverse().find(node => node.type === 'ClassBody');

if (!body) {
return;
}

// Yah, I know, we're not using the AST anymore.
// But there's no good way to do inner traversals, so this is all we got.
const source = context.getSourceCode();
const bodyText = stripComments(source.getText(body));

// Requires two uses of the name to qualify;
const index = bodyText.indexOf(name);
if (!bodyText.includes(name, index + 1)) {
context.report(node, `Unused private "${name}".\n` +
'\tIf this is used for testing, annotate with "@visibleForTesting"\n' +
'\tIf this is a protected definition in a base class,' +
' annotate with "@protected"\n' +
'\tIf this is an override of a protected, annotate with "@override"\n');
}
}

return {
MemberExpression(node) {
if (/\btest\b/.test(context.getFilename())) {
return;
}

const {property} = node;
if (property.type !== 'Identifier') {
return;
}

const {name} = property;
checkClassUse(node, name);
},

MethodDefinition(node) {
if (/\btest\b/.test(context.getFilename())) {
return;
}

const {computed, key} = node;
if (computed) {
return;
}

const {name} = key;
checkClassUse(node, name);
},
};
};
4 changes: 2 additions & 2 deletions src/animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ class AnimationPlayer {
this.startTime_ = 0;

/** @private {./time.normtimeDef} */
this.normLinearTime_ = 0;
// this.normLinearTime_ = 0;

/** @private {./time.normtimeDef} */
this.normTime_ = 0;
// this.normTime_ = 0;

/** @private {boolean} */
this.running_ = false;
Expand Down