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

Add a simple warning system for common issues #146

Merged
merged 1 commit into from
May 30, 2016
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
10 changes: 10 additions & 0 deletions packages/esify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
npm install -g esify
```

## Limitations

The tools on which `esify` is build have certain limitations that prevent us from providing the ideal conversion in some cases. We strongly recommend you have our linting configuration, [`eslint-plugin-shopify`](../eslint-plugin-shopify), set up for your project before beginning to translate in order to easily identify small translation errors (unused or missing references, indentation, etc). Below is a list of limitations that you should check for in the code you are converting:

- All comments will be removed in the transformed output (including Sprockets directives)
- CoffeeScript soak calls with embedded methods (e.g., `foo.bar()?.baz`) will compile to JavaScript that is hard to read
- Assignment to a global outside of the file creating that global will result in incorrect exports (e.g., `Shopify.UIPopover.foo = 'bar'` outside the file declaring `Shopify.UIPopover.foo`)
- Strings and regular expressions with complex escapes might be converted improperly
- Multiline CoffeeScript strings become a single-line string with newlines inserted as needed

## Usage

From the root of the Shopify directory, run this script with a single, relative CoffeeScript file, or a glob pattern. Wait for it to finish, and marvel at the clean ESNext code that is spit out beside the original file! Note this script does not delete the original CoffeeScript file — you should review the output before pushing any changes.
Expand Down
32 changes: 32 additions & 0 deletions packages/esify/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var path = require('path');
var decaf = require('shopify-decaf');
var chalk = require('chalk');
var jscodeshift = require('jscodeshift');

require('babel-register')({ignore: false});
Expand Down Expand Up @@ -114,8 +115,39 @@ function runTransform(code, name) {
return newCode == null ? code : newCode;
}

function warn(message) {
console.log(chalk.yellow('[warning]') + ' ' + message);
}

var WARNING_CHECKS = [
function checkForComments(source) {
if (/#[^=]/.test(source)) {
warn('Your file contains comments. Unfortunately, the CoffeeScript compiler does not expose these comments. Make sure to copy over any important comments to the appropriate place in your new JavaScript file');
}
},
function checkForSprocketsDirective(source) {
if (/#=/.test(source)) {
warn('Your file contains Sprockets directives. If these directives are requiring additional files, you must translate them into `import` statements if this command failed to do so automatically. Other directives will need to be copied over to the new JavaScript file.');
}
},
function checkForGlobalClass(source) {
var match = source.match(/class\s((?:@|this\.)\w+)/);
if (match) {
warn('Your file contains a class exposed to `window` using the form `' + match[1] + '`. These are not supported by `esify` and will not do what you expect when bundled with Sprockets commoner. Expose this to some other global namespace before trying again.');
}
},
function checkForMultilineStrings(source) {
if (/"""/.test(source)) {
warn('Your file contains multiline strings. These get compiled down to a single-line string with spaces added; you may want to update the formatting to use an ES6 multiline string instead.');
}
},
];

module.exports = function transform(source, options) {
var testTransforms = options.testTransforms == null ? false : options.testTransforms;

WARNING_CHECKS.forEach(function(warningCheck) { warningCheck(source); });

var code = decaf.compile(source, OPTIONS.printOptions);

TRANSFORMS.forEach(function(transformer) {
Expand Down
1 change: 1 addition & 0 deletions packages/esify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"bin": "./bin/esify",
"dependencies": {
"babel-register": "^6.9.0",
"chalk": "^1.1.3",
"glob": "7.0.3",
"js-codemod": "cpojer/js-codemod",
"jscodeshift": "^0.3.20",
Expand Down