Skip to content

Commit

Permalink
Merge pull request #12558 from rwjblue/enforce-const-usage
Browse files Browse the repository at this point in the history
Enforce const usage in module scope only.
  • Loading branch information
mixonic committed Nov 5, 2015
2 parents 7584a77 + 41e4e57 commit 341a325
Show file tree
Hide file tree
Showing 39 changed files with 132 additions and 92 deletions.
1 change: 1 addition & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"esnext": true,
"excludeFiles": ["ember-runtime/ext/rsvp.js"],
"additionalRules": [ "lib/jscs-rules/*.js" ],
"disallowConstOutsideModuleScope": true,
"disallowSpacesInsideArrayBrackets": "all",
"disallowMultipleVarDeclWithAssignment": true,
"disallowPaddingNewlinesInBlocks": true,
Expand Down
38 changes: 38 additions & 0 deletions lib/jscs-rules/disallow-const-outside-module-scope.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var assert = require('assert');

module.exports = function() { };

module.exports.prototype = {
configure: function(option) {
assert(option === true, this.getOptionName() + ' requires a true value');
},

getOptionName: function() {
return 'disallowConstOutsideModuleScope';
},

check: function(file, errors) {
file.iterateNodesByType('VariableDeclaration', function(node) {
if (node.parentNode.type === 'Program') {
// declaration is in root of module
return;
}

if (node.parentNode.type === 'ExportNamedDeclaration' && node.parentNode.parentNode.type === 'Program') {
// declaration is a `export const foo = 'asdf'` in root of the module
return;
}

for (var i = 0; i < node.declarations.length; i++) {
var thisDeclaration = node.declarations[i];

if (thisDeclaration.parentNode.kind === 'const') {
errors.add(
'`const` should only be used in module scope (not inside functions/blocks).',
node.loc.start
);
}
}
});
}
};
8 changes: 4 additions & 4 deletions packages/container/tests/container_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,11 @@ if (isEnabled('ember-container-inject-owner')) {
});
} else {
QUnit.test('A `container` property is appended to every instantiated object', function() {
const registry = new Registry();
const container = registry.container();
const PostController = factory();
let registry = new Registry();
let container = registry.container();
let PostController = factory();
registry.register('controller:post', PostController);
const postController = container.lookup('controller:post');
let postController = container.lookup('controller:post');

strictEqual(postController.container, container, '');
});
Expand Down
3 changes: 2 additions & 1 deletion packages/container/tests/test-helpers/build-owner.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ export default function buildOwner(props) {
let Owner = EmberObject.extend(RegistryProxy, ContainerProxy, {
init() {
this._super(...arguments);
const registry = this.__registry__ = new Registry();
let registry = this.__registry__ = new Registry();
this.__container__ = registry.container({ owner: this });
}
});

return Owner.create(props);
}
2 changes: 1 addition & 1 deletion packages/ember-htmlbars/lib/hooks/has-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default function hasHelperHook(env, scope, helperName) {
return true;
}

const owner = env.owner;
let owner = env.owner;
if (validateLazyHelperName(helperName, owner, env.hooks.keywords)) {
var registrationName = 'helper:' + helperName;
if (owner.hasRegistration(registrationName)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-htmlbars/lib/keywords/closure-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function createClosureComponentCell(env, originalComponentPath, params, hash, la
}

function isValidComponentPath(env, path) {
const result = lookupComponent(env.owner, path);
let result = lookupComponent(env.owner, path);

return !!(result.component || result.layout);
}
Expand Down
10 changes: 5 additions & 5 deletions packages/ember-htmlbars/lib/keywords/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
} from 'ember-metal/observer';

function labelFor(source, key) {
const sourceLabel = source.label ? source.label : '';
const keyLabel = key.label ? key.label : '';
let sourceLabel = source.label ? source.label : '';
let keyLabel = key.label ? key.label : '';
return `(get ${sourceLabel} ${keyLabel})`;
}

Expand All @@ -34,7 +34,7 @@ let DynamicKeyStream = BasicStream.extend({
},

key() {
const key = this.keyDep.getValue();
let key = this.keyDep.getValue();
if (typeof key === 'string') {
return key;
}
Expand Down Expand Up @@ -84,12 +84,12 @@ let DynamicKeyStream = BasicStream.extend({
});

const buildStream = function buildStream(params) {
const [objRef, pathRef] = params;
let [objRef, pathRef] = params;

assert('The first argument to {{get}} must be a stream', isStream(objRef));
assert('{{get}} requires at least two arguments', params.length > 1);

const stream = buildDynamicKeyStream(objRef, pathRef);
let stream = buildDynamicKeyStream(objRef, pathRef);

return stream;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export function createComponent(_component, isAngleBracket, props, renderNode, e
props.renderer = props.parentView ? props.parentView.renderer : env.owner.lookup('renderer:-dom');
props._viewRegistry = props.parentView ? props.parentView._viewRegistry : env.owner.lookup('-view-registry:main');

const component = _component.create(props);
let component = _component.create(props);

if (props.parentView) {
props.parentView.appendChild(component);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export function createOrUpdateComponent(component, options, createOptions, rende

mergeBindings(props, snapshot);

const owner = options.parentView ? getOwner(options.parentView) : env.owner;
let owner = options.parentView ? getOwner(options.parentView) : env.owner;

setOwner(props, owner);
props.renderer = options.parentView ? options.parentView.renderer : owner && owner.lookup('renderer:-dom');
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-htmlbars/lib/system/lookup-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function findHelper(name, view, env) {
var helper = env.helpers[name];

if (!helper) {
const owner = env.owner;
let owner = env.owner;
if (validateLazyHelperName(name, owner, env.hooks.keywords)) {
var helperName = 'helper:' + name;
if (owner.hasRegistration(helperName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function extractPositionalParams(renderNode, component, params, a
}

export function processPositionalParams(renderNode, positionalParams, params, attrs) {
const isNamed = typeof positionalParams === 'string';
let isNamed = typeof positionalParams === 'string';

if (isNamed) {
processRestPositionalParameters(renderNode, positionalParams, params, attrs);
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-htmlbars/lib/utils/is-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { isStream } from 'ember-metal/streams/utils';
name was found in the container.
*/
export default function isComponent(env, scope, path) {
const owner = env.owner;
let owner = env.owner;
if (!owner) { return false; }
if (typeof path === 'string') {
if (CONTAINS_DOT_CACHE.get(path)) {
Expand Down
6 changes: 3 additions & 3 deletions packages/ember-htmlbars/tests/compat/view_helper_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ QUnit.module('ember-htmlbars: compat - view helper', {
});

QUnit.test('using the view helper fails assertion', function(assert) {
const ViewClass = EmberView.extend({
let ViewClass = EmberView.extend({
template: compile('fooView')
});
owner.register('view:foo', ViewClass);
Expand Down Expand Up @@ -67,7 +67,7 @@ QUnit.module('ember-htmlbars: compat - view helper [LEGACY]', {
});

QUnit.test('using the view helper with a string (inline form) fails assertion [LEGACY]', function(assert) {
const ViewClass = EmberView.extend({
let ViewClass = EmberView.extend({
template: compile('fooView')
});
owner.register('view:foo', ViewClass);
Expand All @@ -85,7 +85,7 @@ QUnit.test('using the view helper with a string (inline form) fails assertion [L
});

QUnit.test('using the view helper with a string (block form) fails assertion [LEGACY]', function(assert) {
const ViewClass = EmberView.extend({
let ViewClass = EmberView.extend({
template: compile('Foo says: {{yield}}')
});
owner.register('view:foo', ViewClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function renderComponent(tag, component) {
.map(key => `${key}=${hash[key]}`)
.join(' ');

const owner = buildOwner();
let owner = buildOwner();
owner.register('component-lookup:main', ComponentLookup);
owner.register(`component:${tag}`, implementation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function commonTeardown() {
}

function appendViewFor(template, hash={}) {
const view = EmberView.extend({
let view = EmberView.extend({
[OWNER]: owner,
template: compile(template)
}).create(hash);
Expand Down
10 changes: 5 additions & 5 deletions packages/ember-htmlbars/tests/system/lookup-helper_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function generateEnv(helpers, owner) {
}

function generateOwner() {
const owner = buildOwner();
let owner = buildOwner();

owner.register('component-lookup:main', ComponentLookup);

Expand Down Expand Up @@ -58,7 +58,7 @@ QUnit.test('does not lookup in the container if the name does not contain a dash
});

QUnit.test('does a lookup in the container if the name contains a dash (and helper is not found in env)', function() {
const owner = generateOwner();
let owner = generateOwner();
var env = generateEnv(null, owner);
var view = {
[OWNER]: owner
Expand All @@ -73,7 +73,7 @@ QUnit.test('does a lookup in the container if the name contains a dash (and help
});

QUnit.test('does a lookup in the container if the name is found in knownHelpers', function() {
const owner = generateOwner();
let owner = generateOwner();
var env = generateEnv(null, owner);
var view = {
[OWNER]: owner
Expand All @@ -90,7 +90,7 @@ QUnit.test('does a lookup in the container if the name is found in knownHelpers'

QUnit.test('looks up a shorthand helper in the container', function() {
expect(2);
const owner = generateOwner();
let owner = generateOwner();
var env = generateEnv(null, owner);
var view = {
[OWNER]: owner
Expand All @@ -113,7 +113,7 @@ QUnit.test('looks up a shorthand helper in the container', function() {

QUnit.test('fails with a useful error when resolving a function', function() {
expect(1);
const owner = generateOwner();
let owner = generateOwner();
var env = generateEnv(null, owner);
var view = {
[OWNER]: owner
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-htmlbars/tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function registerAstPlugin(plugin) {
}

function removeAstPlugin(plugin) {
const index = plugins['ast'].indexOf(plugin);
let index = plugins['ast'].indexOf(plugin);
plugins['ast'].splice(index, 1);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/ember-metal/lib/injected_property.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function InjectedProperty(type, name) {

function injectedPropertyGet(keyName) {
var desc = this[keyName];
const owner = getOwner(this);
let owner = getOwner(this);

assert(`InjectedProperties should be defined with the Ember.inject computed property macros.`, desc && desc.isDescriptor && desc.type);
assert(`Attempting to lookup an injected property on an object without a container, ensure that the object was instantiated via a container.`, owner);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ QUnit.test('action should be called on the correct scope', function(assert) {
QUnit.test('arguments to action are passed, curry', function(assert) {
assert.expect(4);

const first = 'mitch';
const second = 'martin';
const third = 'matt';
const fourth = 'wacky wycats';
let first = 'mitch';
let second = 'martin';
let third = 'matt';
let fourth = 'wacky wycats';

innerComponent = EmberComponent.extend({
fireAction() {
Expand Down Expand Up @@ -155,7 +155,7 @@ QUnit.test('arguments to action are passed, curry', function(assert) {
QUnit.test('arguments to action are bound', function(assert) {
assert.expect(1);

const value = 'lazy leah';
let value = 'lazy leah';

innerComponent = EmberComponent.extend({
fireAction() {
Expand Down Expand Up @@ -186,9 +186,9 @@ QUnit.test('arguments to action are bound', function(assert) {
QUnit.test('array arguments are passed correctly to action', function(assert) {
assert.expect(3);

const first = 'foo';
const second = [3, 5];
const third = [4, 9];
let first = 'foo';
let second = [3, 5];
let third = [4, 9];

innerComponent = EmberComponent.extend({
fireAction() {
Expand Down Expand Up @@ -384,7 +384,7 @@ QUnit.test('action can create closures over actions with target', function(asser
QUnit.test('value can be used with action over actions', function(assert) {
assert.expect(1);

const newValue = 'yelping yehuda';
let newValue = 'yelping yehuda';

innerComponent = EmberComponent.extend({
fireAction() {
Expand Down Expand Up @@ -419,7 +419,7 @@ QUnit.test('value can be used with action over actions', function(assert) {
QUnit.test('action will read the value of a first property', function(assert) {
assert.expect(1);

const newValue = 'irate igor';
let newValue = 'irate igor';

innerComponent = EmberComponent.extend({
fireAction() {
Expand Down Expand Up @@ -449,7 +449,7 @@ QUnit.test('action will read the value of a first property', function(assert) {
QUnit.test('action will read the value of a curried first argument property', function(assert) {
assert.expect(1);

const newValue = 'kissing kris';
let newValue = 'kissing kris';

innerComponent = EmberComponent.extend({
fireAction() {
Expand Down
Loading

0 comments on commit 341a325

Please sign in to comment.