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

Port code to ember-rfc176-data new format #142

Merged
merged 12 commits into from
May 15, 2018
34 changes: 25 additions & 9 deletions lib/rules/new-module-imports.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
'use strict';

const EMBER_NAMESPACES = ['inject.controller', 'inject.service'];
const GLOBALS = require('ember-rfc176-data/globals.json');
const MAPPING = require('ember-rfc176-data');

const GLOBALS = MAPPING.reduce((memo, exportDefinition) => {
if (exportDefinition.deprecated) {
return memo;
}

if (exportDefinition.global in memo) {
return memo;
}

memo[exportDefinition.global] = exportDefinition; // eslint-disable-line no-param-reassign

return memo;
}, Object.create(null));

//------------------------------------------------------------------------------
// General rule - Use "New Module Imports" from Ember RFC #176
Expand Down Expand Up @@ -33,7 +47,7 @@ module.exports = {
reportNestedProperties(props, parent);
} else {
const key = item.key.name;
const match = GLOBALS[key];
const match = GLOBALS[`Ember.${key}`];
populateMessage({
node: item,
customKey: (key !== item.value.name) ? item.value.name : null,
Expand Down Expand Up @@ -67,7 +81,7 @@ module.exports = {
fullName = fullNames[i];

const key = fullName.replace(/^Ember\./, '');
const match = GLOBALS[key];
const match = GLOBALS[fullName];

const reportedError = populateMessage({ node, fullName, key, match });

Expand All @@ -81,7 +95,7 @@ module.exports = {

function reportNestedProperties(properties, parent) {
properties.forEach((item) => {
const match = GLOBALS[`${parent}.${item.key.name}`];
const match = GLOBALS[`Ember.${parent}.${item.key.name}`];

populateMessage({
node: item,
Expand All @@ -108,18 +122,20 @@ module.exports = {
// Ex: import { inject as service } from '@ember/service';
const isNamespace = EMBER_NAMESPACES.indexOf(`${obj.parent}.${obj.key}`) !== -1;

const isNamedExport = obj.match.export !== 'default';

let importSpecifier;
let message;

if (obj.match[1] && !isNamespace) {
importSpecifier = obj.match[2] ? `{ ${obj.match[1]} as ${obj.match[2]} }` : `{ ${obj.match[1]} }`;
} else if (obj.match[1] && isNamespace) {
if (isNamedExport && !isNamespace) {
importSpecifier = obj.match.localName ? `{ ${obj.match.export} as ${obj.match.localName} }` : `{ ${obj.match.export} }`;
} else if (isNamedExport && isNamespace) {
importSpecifier = `{ ${obj.parent} as ${obj.key} }`;
} else {
importSpecifier = obj.match[2] || obj.customKey || obj.key;
importSpecifier = obj.match.localName || obj.customKey || obj.key;
}

const replacement = `import ${importSpecifier} from '${obj.match[0]}';`;
const replacement = `import ${importSpecifier} from '${obj.match.module}';`;

if (obj.type === 'Property') {
message = `Use ${replacement} instead of using Ember destructuring`;
Expand Down
242 changes: 241 additions & 1 deletion lib/rules/no-old-shims.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,246 @@
'use strict';

const oldShimsData = require('ember-rfc176-data/old-shims.json');
// inlined from
// https://github.com/ember-cli/ember-rfc176-data/blob/v0.2.7/old-shims.json
// the newer format of ember-rfc176-data no longer supports the distinction
// between "old shim" and "generally deprecated"
const oldShimsData = {
'ember-application': {
default: ['@ember/application']
},
'ember-array': {
default: ['@ember/array']
},
'ember-array/mutable': {
default: ['@ember/array/mutable']
},
'ember-array/utils': {
A: ['@ember/array', 'A'],
isEmberArray: ['@ember/array', 'isArray'],
wrap: ['@ember/array', 'makeArray']
},
'ember-component': {
default: ['@ember/component']
},
'ember-components/checkbox': {
default: ['@ember/component/checkbox']
},
'ember-components/text-area': {
default: ['@ember/component/text-area']
},
'ember-components/text-field': {
default: ['@ember/component/text-field']
},
'ember-controller': {
default: ['@ember/controller']
},
'ember-controller/inject': {
default: ['@ember/controller', 'inject']
},
'ember-controller/proxy': {
default: ['@ember/array/proxy']
},
'ember-controllers/sortable': {
default: null
},
'ember-debug': {
log: ['@ember/debug', 'debug'],
inspect: ['@ember/debug', 'inspect'],
run: ['@ember/debug', 'runInDebug'],
warn: ['@ember/debug', 'warn']
},
'ember-debug/container-debug-adapter': {
default: ['@ember/debug/container-debug-adapter']
},
'ember-debug/data-adapter': {
default: ['@ember/debug/data-adapter']
},
'ember-deprecations': {
deprecate: ['@ember/application/deprecations', 'deprecate'],
deprecateFunc: ['@ember/application/deprecations', 'deprecateFunc']
},
'ember-enumerable': {
default: ['@ember/enumerable']
},
'ember-evented': {
default: ['@ember/object/evented']
},
'ember-evented/on': {
default: ['@ember/object/evented', 'on']
},
'ember-globals-resolver': {
default: ['@ember/application/globals-resolver', null, 'GlobalsResolver']
},
'ember-helper': {
default: ['@ember/component/helper'],
helper: ['@ember/component/helper', 'helper']
},
'ember-instrumentation': {
instrument: ['@ember/instrumentation', 'instrument'],
reset: ['@ember/instrumentation', 'reset'],
subscribe: ['@ember/instrumentation', 'subscribe'],
unsubscribe: ['@ember/instrumentation', 'unsubscribe']
},
'ember-locations/hash': {
default: ['@ember/routing/hash-location']
},
'ember-locations/history': {
default: ['@ember/routing/history-location']
},
'ember-locations/none': {
default: ['@ember/routing/none-location']
},
'ember-map': {
default: ['@ember/map'],
withDefault: ['@ember/map/with-default']
},
'ember-metal/destroy': {
default: null
},
'ember-metal/events': {
addListener: ['@ember/object/events', 'addListener'],
removeListener: ['@ember/object/events', 'removeListener'],
send: ['@ember/object/events', 'sendEvent']
},
'ember-metal/get': {
default: ['@ember/object', 'get'],
getProperties: ['@ember/object', 'getProperties']
},
'ember-metal/mixin': {
default: ['@ember/object/mixin']
},
'ember-metal/observer': {
default: ['@ember/object', 'observer'],
addObserver: ['@ember/object/observers', 'addObserver'],
removeObserver: ['@ember/object/observers', 'removeObserver']
},
'ember-metal/on-load': {
default: ['@ember/application', 'onLoad'],
run: ['@ember/application', 'runLoadHooks']
},
'ember-metal/set': {
default: ['@ember/object', 'set'],
setProperties: ['@ember/object', 'setProperties'],
trySet: ['@ember/object', 'trySet']
},
'ember-metal/utils': {
aliasMethod: ['@ember/object', 'aliasMethod'],
assert: ['@ember/debug', 'assert'],
cacheFor: ['@ember/object/internals', 'cacheFor'],
copy: ['@ember/object/internals', 'copy'],
guidFor: ['@ember/object/internals', 'guidFor']
},
'ember-object': {
default: ['@ember/object']
},
'ember-owner/get': {
default: ['@ember/application', 'getOwner']
},
'ember-owner/set': {
default: ['@ember/application', 'setOwner']
},
'ember-platform': {
assign: ['@ember/polyfills', 'assign'],
create: ['@ember/polyfills', 'create'],
hasAccessors: ['@ember/polyfills', 'hasPropertyAccessors'],
keys: ['@ember/polyfills', 'keys']
},
'ember-route': {
default: ['@ember/routing/route']
},
'ember-router': {
default: ['@ember/routing/router']
},
'ember-runloop': {
default: ['@ember/runloop', 'run'],
begin: ['@ember/runloop', 'begin'],
bind: ['@ember/runloop', 'bind'],
cancel: ['@ember/runloop', 'cancel'],
debounce: ['@ember/runloop', 'debounce'],
end: ['@ember/runloop', 'end'],
join: ['@ember/runloop', 'join'],
later: ['@ember/runloop', 'later'],
next: ['@ember/runloop', 'next'],
once: ['@ember/runloop', 'once'],
schedule: ['@ember/runloop', 'schedule'],
scheduleOnce: ['@ember/runloop', 'scheduleOnce'],
throttle: ['@ember/runloop', 'throttle']
},
'ember-service': {
default: ['@ember/service']
},
'ember-service/inject': {
default: ['@ember/service', 'inject']
},
'ember-set/ordered': {
default: null
},
'ember-string': {
camelize: ['@ember/string', 'camelize'],
capitalize: ['@ember/string', 'capitalize'],
classify: ['@ember/string', 'classify'],
dasherize: ['@ember/string', 'dasherize'],
decamelize: ['@ember/string', 'decamelize'],
fmt: ['@ember/string', 'fmt'],
htmlSafe: ['@ember/string', 'htmlSafe'],
loc: ['@ember/string', 'loc'],
underscore: ['@ember/string', 'underscore'],
w: ['@ember/string', 'w']
},
'ember-utils': {
isBlank: ['@ember/utils', 'isBlank'],
isEmpty: ['@ember/utils', 'isEmpty'],
isNone: ['@ember/utils', 'isNone'],
isPresent: ['@ember/utils', 'isPresent'],
tryInvoke: ['@ember/utils', 'tryInvoke'],
typeOf: ['@ember/utils', 'typeOf']
},
'ember-computed': {
default: ['@ember/object', 'computed'],
empty: ['@ember/object/computed', 'empty'],
notEmpty: ['@ember/object/computed', 'notEmpty'],
none: ['@ember/object/computed', 'none'],
not: ['@ember/object/computed', 'not'],
bool: ['@ember/object/computed', 'bool'],
match: ['@ember/object/computed', 'match'],
equal: ['@ember/object/computed', 'equal'],
gt: ['@ember/object/computed', 'gt'],
gte: ['@ember/object/computed', 'gte'],
lt: ['@ember/object/computed', 'lt'],
lte: ['@ember/object/computed', 'lte'],
alias: ['@ember/object/computed', 'alias'],
oneWay: ['@ember/object/computed', 'oneWay'],
reads: ['@ember/object/computed', 'reads'],
readOnly: ['@ember/object/computed', 'readOnly'],
deprecatingAlias: ['@ember/object/computed', 'deprecatingAlias'],
and: ['@ember/object/computed', 'and'],
or: ['@ember/object/computed', 'or'],
collect: ['@ember/object/computed', 'collect'],
sum: ['@ember/object/computed', 'sum'],
min: ['@ember/object/computed', 'min'],
max: ['@ember/object/computed', 'max'],
map: ['@ember/object/computed', 'map'],
sort: ['@ember/object/computed', 'sort'],
setDiff: ['@ember/object/computed', 'setDiff'],
mapBy: ['@ember/object/computed', 'mapBy'],
mapProperty: ['@ember/object/computed', 'mapProperty'],
filter: ['@ember/object/computed', 'filter'],
filterBy: ['@ember/object/computed', 'filterBy'],
filterProperty: ['@ember/object/computed', 'filterProperty'],
uniq: ['@ember/object/computed', 'uniq'],
union: ['@ember/object/computed', 'union'],
intersect: ['@ember/object/computed', 'intersect']
},
'ember-test': {
default: null
},
'ember-test/adapter': {
default: ['@ember/test/adapter']
},
'ember-test/qunit-adapter': {
default: null
}
};

//------------------------------------------------------------------------------
// General rule - Don't use import paths from ember-cli-shims
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"lerna-changelog": "^0.7.0"
},
"dependencies": {
"ember-rfc176-data": "^0.2.7",
"ember-rfc176-data": "^0.3.3",
"snake-case": "^2.1.0"
},
"changelog": {
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/rules/new-module-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ eslintTester.run('new-module-imports', rule, {
{ code: 'Ember.Handlebars.Utils.escapeExpression("foo");' },
{ code: 'Ember.onerror = function() {};' },
{ code: 'Ember.MODEL_FACTORY_INJECTIONS = true;' },
{ code: 'console.log(Ember.VERSION);' },
{ code: 'console.log(Ember.SOMETHING_NO_ONE_USES);' },
{ code: 'if (Ember.testing) {}' },
{
code: `import Component from '@ember/component';
Expand Down Expand Up @@ -80,7 +80,7 @@ eslintTester.run('new-module-imports', rule, {
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
errors: [
{ message: 'Use import Component from \'@ember/component\'; instead of using Ember destructuring', line: 3 },
{ message: 'Use import { htmlSafe } from \'@ember/string\'; instead of using Ember destructuring', line: 3 }
{ message: 'Use import { htmlSafe } from \'@ember/template\'; instead of using Ember destructuring', line: 3 }
]
},
{
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -896,9 +896,9 @@ ecc-jsbn@~0.1.1:
dependencies:
jsbn "~0.1.0"

ember-rfc176-data@^0.2.7:
version "0.2.7"
resolved "https://registry.yarnpkg.com/ember-rfc176-data/-/ember-rfc176-data-0.2.7.tgz#bd355bc9b473e08096b518784170a23388bc973b"
ember-rfc176-data@^0.3.3:
version "0.3.3"
resolved "https://registry.yarnpkg.com/ember-rfc176-data/-/ember-rfc176-data-0.3.3.tgz#27fba08d540a7463a4366c48eaa19c5a44971a39"

encoding@^0.1.11:
version "0.1.12"
Expand Down