forked from oppia/oppia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disallow $parent and $broadcast angularJs property (oppia#13291)
- Loading branch information
1 parent
35302c4
commit 90d0152
Showing
8 changed files
with
137 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
scripts/linters/custom_eslint_checks/rules/disallow-angularjs-properties.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2021 The Oppia 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. | ||
|
||
/** | ||
* @fileoverview Lint check to disallow $parent and $broadcast | ||
* properties of angularJs. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: ( | ||
'Lint check to disallow $parent and $broadcast properties of angularJs' | ||
), | ||
category: 'Best Practices', | ||
recommended: true, | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
disallowBrodcast: ( | ||
'Please do not use $broadcast/$on for propagating' + | ||
' events. Use @Input/@Output instead'), | ||
disallowParent: ( | ||
'Please do not access parent properties using $parent.' + | ||
' Use the scope object for this purpose.') | ||
}, | ||
}, | ||
|
||
create: function(context) { | ||
var checkAndReportAngularJsProperties = function(node) { | ||
if (node.property.name === '$parent') { | ||
context.report({ | ||
node: node, | ||
messageId: 'disallowParent' | ||
}); | ||
} | ||
if (node.property.name === '$broadcast') { | ||
context.report({ | ||
node: node, | ||
messageId: 'disallowBrodcast' | ||
}); | ||
} | ||
}; | ||
|
||
return { | ||
MemberExpression: function(node) { | ||
checkAndReportAngularJsProperties(node); | ||
} | ||
}; | ||
} | ||
}; |
69 changes: 69 additions & 0 deletions
69
scripts/linters/custom_eslint_checks/rules/disallow-angularjs-properties.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2021 The Oppia 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. | ||
|
||
/** | ||
* @fileoverview Tests for the disallow-angularjs-properties.js file. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var rule = require('./disallow-angularjs-properties'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
var ruleTester = new RuleTester(); | ||
ruleTester.run('disallow-angularjs-properties', rule, { | ||
valid: [ | ||
` | ||
var ParentCtrl = function($scope) { | ||
$scope.cities = ['NY', 'Amsterdam', 'Barcelona']; | ||
}; | ||
` | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: | ||
` | ||
var ChildCtrl = function($scope) { | ||
$scope.parentcities = $scope.$parent.cities; | ||
}; | ||
`, | ||
errors: [{ | ||
message: ( | ||
'Please do not access parent properties using $parent.' + | ||
' Use the scope object for this purpose.') | ||
}] | ||
}, | ||
{ | ||
code: | ||
` | ||
angular.module('oppia').directive('sampleDirective', [ | ||
function() { | ||
return { | ||
controller: ['$rootScope', | ||
function($rootScope) { | ||
$rootScope.$broadcast('sampleEvent'); | ||
} | ||
] | ||
}; | ||
}]); | ||
`, | ||
errors: [{ | ||
message: ( | ||
'Please do not use $broadcast/$on for propagating events.' + | ||
' Use @Input/@Output instead') | ||
}] | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.