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

Document mappings.json format. #53

Merged
merged 8 commits into from
Apr 24, 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
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,53 @@ node scripts/generate-markdown-table.js

## Contributing


### mappings.json format

The `mappings.json` file contains an array of entries with the following format:

```ts
interface Mapping {
/**
The globals based API that this module and export replace.
*/
global: string;

/**
The module to import.
*/
module: string;

/**
The export name from the module.
*/
export: string;

/**
`true` if this module / export combination has been deprecated.
*/
deprecated: boolean;

/**
The recommended `localName` to use for a given module/export. Only present
when a name other than the value for `export` should be used.

This is useful for things like ember-modules-codemod or eslint-plugin-ember
so that they can provide a nice suggested import for a given global path usage.
*/
localName?: string;

/**
When this mapping is deprecated it may include a replacement module/export which
should be used instead.
*/
replacement?: {
module: string;
export: string;
}
}
```

### Module Changes

If you want to change how globals are mapped into modules, you will find
Expand Down
10 changes: 5 additions & 5 deletions mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"global": "Ember.Array",
"module": "@ember/array",
"export": "default",
"localName": "Array",
"localName": "EmberArray",
"deprecated": false
},
{
Expand Down Expand Up @@ -263,7 +263,7 @@
"global": "Ember.Error",
"module": "@ember/error",
"export": "default",
"localName": "Error",
"localName": "EmberError",
"deprecated": false
},
{
Expand Down Expand Up @@ -294,7 +294,7 @@
"global": "Ember.Map",
"module": "@ember/map",
"export": "default",
"localName": "Map",
"localName": "EmberMap",
"deprecated": false
},
{
Expand All @@ -308,7 +308,7 @@
"global": "Ember.Object",
"module": "@ember/object",
"export": "default",
"localName": "Object",
"localName": "EmberObject",
"deprecated": false
},
{
Expand Down Expand Up @@ -2484,4 +2484,4 @@
"export": "resolve",
"deprecated": false
}
]
]
1 change: 0 additions & 1 deletion reserved.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"Atomics",
"DataView",
"JSON",
"Promise",
"Generator",
"GeneratorFunction",
"Reflect",
Expand Down
46 changes: 46 additions & 0 deletions scripts/reformat-mappings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const fs = require('fs');
const mappings = require('../mappings');
const reserved = require('../reserved');
const { compare } = require('./shared');

function sortByModuleAndExport(mappingA, mappingB) {
if (mappingA.module === mappingB.module) {
// ensure default exports sort higher within a package
let aExport = mappingA.export === 'default' ? '' : mappingA.export;
let bExport = mappingB.export === 'default' ? '' : mappingB.export;
return compare(aExport || '', bExport || '');
}

return compare(mappingA.module, mappingB.module);
}

let updated = mappings.sort(sortByModuleAndExport).map(mapping => {
let localName = mapping.localName || mapping.export;

// ensure localName is set when a reserved word collides
if (reserved.includes(localName)) {
localName = 'Ember' + localName;
}

// remove localName when it matches export
if (localName === mapping.export) {
localName = undefined;
}

// return a new object so that all of the properties in the JSON
// are in the same order
let ret = {
global: mapping.global,
module: mapping.module,
export: mapping.export,
localName,
deprecated: !!mapping.deprecated,
replacement: mapping.replacement,
};

return ret;
});

fs.writeFileSync('mappings.json', JSON.stringify(updated, null, 2) + '\n', {
encoding: 'utf-8',
});
19 changes: 0 additions & 19 deletions scripts/sort-mappings-by-module-and-export.js

This file was deleted.

22 changes: 22 additions & 0 deletions tests/validate-mapping-format.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const mappings = require('../mappings.json');

for (let mapping of mappings) {
describe('Validate mapping format', () => {
test(`${mapping.global} - ${mapping.module} - ${mapping.export}`, () => {
expect(mapping.global).toMatch(/.+/);
expect(mapping.module).toMatch(/.+/);
expect(mapping.export).toMatch(/.+/);
expect([true, false]).toContain(mapping.deprecated);

if ('localName' in mapping) {
// when present ensure it is a valid string
expect(mapping.localName).toMatch(/.+/);
}

if ('replacement' in mapping) {
expect(mapping.replacement.module).toMatch(/.+/);
expect(mapping.replacement.export).toMatch(/.+/);
}
});
});
}