feat(babel6): Fix destructuring default from exports and update linter

This commit is contained in:
Evan Morikawa 2016-05-04 12:15:30 -07:00
parent aa6020c871
commit 47c064617d
6 changed files with 65 additions and 27 deletions

View file

@ -1,6 +1,12 @@
path = require 'path'
fs = require 'fs-plus'
normalizeRequirePath = (requirePath, fPath) ->
if requirePath[0] is "."
return path.normalize(path.join(path.dirname(fPath), requirePath))
return requirePath
module.exports = (grunt) ->
grunt.registerMultiTask 'nylaslint', 'Check requires for file extensions compiled away', ->
done = @async()
@ -25,6 +31,7 @@ module.exports = (grunt) ->
esNoExport = {}
esExportDefault = {}
# Build the list of ES6 files that export things and categorize
for f in fileset.src
continue if not esExtensions[path.extname(f)]
lookupPath = "#{path.dirname(f)}/#{path.basename(f, path.extname(f))}"
@ -41,18 +48,29 @@ module.exports = (grunt) ->
else
esNoExport[lookupPath] = true
# blacklist = ["events", "main", "package", "task"]
# for item in blacklist
# delete esExportDefault[item]
# delete esExport[item]
# delete esNoExport[item]
# Now look again through all ES6 files, this time to check imports
# instead of exports.
for f in fileset.src
continue if not esExtensions[path.extname(f)]
content = fs.readFileSync(f, encoding:'utf8')
importRe = /import \{.*\} from ['"](.*?)['"]/gmi
while result = importRe.exec(content)
i = 1
while i < result.length
requirePath = result[i]
i += 1
lookupPath = normalizeRequirePath(requirePath, f)
if esExportDefault[lookupPath] or esNoExport[lookupPath]
errors.push("#{f}: Don't destructure default export #{requirePath}")
# file.src is the list of all matching file names.
# Now look through all coffeescript files
# If they require things from ES6 files, ensure they're using the
# proper syntax.
for f in fileset.src
continue if esExtensions[path.extname(f)]
content = fs.readFileSync(f, encoding:'utf8')
if extensionRegex.test(content)
errors.push("#{f}: Remove require extension!")
errors.push("#{f}: Remove extensions when requiring files")
requireRe = /require[ (]['"]([\w_./-]*?)['"]/gmi
while result = requireRe.exec(content)
@ -61,10 +79,7 @@ module.exports = (grunt) ->
requirePath = result[i]
i += 1
if requirePath[0] is "."
lookupPath = path.normalize(path.join(path.dirname(f), requirePath))
else
lookupPath = requirePath
lookupPath = normalizeRequirePath(requirePath, f)
baseRequirePath = path.basename(requirePath)
@ -73,22 +88,44 @@ module.exports = (grunt) ->
if esExport[lookupPath]
if not plainRequireRe.test(content)
errors.push("#{f}: ES6 no `default` exported #{requirePath}")
errors.push("#{f}: No `default` exported #{requirePath}")
else if esNoExport[lookupPath]
errors.push("#{f}: nothing exported from #{requirePath}")
errors.push("#{f}: Nothing exported from #{requirePath}")
else if esExportDefault[lookupPath]
if not defaultRequireRe.test(content)
errors.push("#{f}: ES6 add `default` to require #{requirePath}")
errors.push("#{f}: Add `default` to require #{requirePath}")
else
# must be a coffeescript or core file
if defaultRequireRe.test(content)
errors.push("#{f}: don't ask for `default` from #{requirePath}")
errors.push("#{f}: Don't ask for `default` from #{requirePath}")
if errors.length > 0
grunt.log.error(err) for err in errors
done(new Error("Please fix the #{errors.length} linter errors! Since we compile files in production to plain `.js` files it's very important you do NOT include the file extension when `require`ing a file. Also, as of Babel 6, `require` no longer returns whatever the `default` value is. If you are `require`ing an es6 file from a coffeescript file, you must explicitly request the `default` property. For example: do `require('./my-es6-file').default`"))
error = """
Please fix the #{errors.length} linter errors above. These are the issues we're looking for:
ISSUES WITH COFFEESCRIPT FILES:
1. Remove extensions when requiring files:
Since we compile files in production to plain `.js` files it's very important you do NOT include the file extension when `require`ing a file.
2. Add `default` to require:
As of Babel 6, `require` no longer returns whatever the `default` value is. If you are `require`ing an es6 file from a coffeescript file, you must explicitly request the `default` property. For example: do `require('./my-es6-file').default`
3. Don't ask for `default`:
If you're requiring a coffeescript file from a coffeescript file, you will almost never need to load a `default` object. This is likely an indication you incorrectly thought you were importing an ES6 file.
ISSUES WITH ES6 FILES:
4. Don't use module.exports in ES6:
You sholudn't manually assign module.exports anymore. Use proper ES6 module syntax like `export default` or `export const FOO`.
5. Don't destructure default export:
If you're using `import {FOO} from './bar'` in ES6 files, it's important that `./bar` does NOT export a `default`. Instead, in './bar', do `export const FOO = 'foo'`
"""
done(new Error(error))
done(null)

View file

@ -1,5 +1,6 @@
import {AccountStore, CategoryStore} from 'nylas-exports'
import {Gmail} from '../lib/category-removal-target-rulesets'
import CategoryRemovalTargetRulesets from '../lib/category-removal-target-rulesets'
const {Gmail} = CategoryRemovalTargetRulesets;
describe('CategoryRemovalTargetRulesets', ()=> {
describe('Gmail', ()=> {

View file

@ -3,7 +3,7 @@ import React, {Component, PropTypes} from 'react';
import {FocusedPerspectiveStore} from 'nylas-exports';
import {RetinaImg, MailLabel} from 'nylas-component-kit';
import {SNOOZE_CATEGORY_NAME, PLUGIN_ID} from './snooze-constants';
import {snoozedUntilMessage} from './snooze-utils';
import SnoozeUtils from './snooze-utils';
class SnoozeMailLabel extends Component {
@ -32,7 +32,7 @@ class SnoozeMailLabel extends Component {
if (metadata) {
// TODO this is such a hack
const {snoozeDate} = metadata;
const message = snoozedUntilMessage(snoozeDate).replace('Snoozed', '')
const message = SnoozeUtils.snoozedUntilMessage(snoozeDate).replace('Snoozed', '')
const content = (
<span className="snooze-mail-label">
<RetinaImg

View file

@ -19,7 +19,7 @@ import {
import WeekView from '../../../src/components/nylas-calendar/week-view'
describe("Nylas Calendar Week View", () => {
describe("Nylas Calendar Week View", function weekViewSpec() {
beforeEach(() => {
spyOn(WeekView.prototype, "_now").andReturn(now());

View file

@ -5,7 +5,9 @@ import {
Category,
CategoryStore,
} from 'nylas-exports'
import {Default} from '../internal_packages/thread-list/lib/category-removal-target-rulesets'
import CategoryRemovalTargetRulesets from '../internal_packages/thread-list/lib/category-removal-target-rulesets'
const {Default} = CategoryRemovalTargetRulesets;
describe('MailboxPerspective', ()=> {

View file

@ -1,6 +1,4 @@
export default {
DAY_VIEW: "day",
WEEK_VIEW: "week",
MONTH_VIEW: "month",
YEAR_VIEW: "year",
}
export const DAY_VIEW = "day";
export const WEEK_VIEW = "week";
export const MONTH_VIEW = "month";
export const YEAR_VIEW = "year";