Merge pull request #5707 from atom/bf-grim-6to5

Report deprecation warning when 'use 6to5' is used instead of 'use babel'.
This commit is contained in:
Kevin Sawicki
2015-02-25 10:07:39 -08:00
2 changed files with 40 additions and 6 deletions

View File

@@ -1,7 +1,14 @@
babel = require '../src/babel'
crypto = require 'crypto'
grim = require 'grim'
describe "Babel transpiler support", ->
beforeEach ->
jasmine.snapshotDeprecations()
afterEach ->
jasmine.restoreDeprecationsSnapshot()
describe "::createBabelVersionAndOptionsDigest", ->
it "returns a digest for the library version and specified options", ->
defaultOptions =
@@ -30,21 +37,27 @@ describe "Babel transpiler support", ->
it "transpiles it using babel", ->
transpiled = require('./fixtures/babel/babel-single-quotes.js')
expect(transpiled(3)).toBe 4
expect(grim.getDeprecationsLength()).toBe 0
describe "when a .js file starts with 'use 6to5';", ->
it "transpiles it using 6to5", ->
it "transpiles it using babel and adds a pragma deprecation", ->
expect(grim.getDeprecationsLength()).toBe 0
transpiled = require('./fixtures/babel/6to5-single-quotes.js')
expect(transpiled(3)).toBe 4
expect(grim.getDeprecationsLength()).toBe 1
describe 'when a .js file starts with "use babel";', ->
it "transpiles it using babel", ->
transpiled = require('./fixtures/babel/babel-double-quotes.js')
expect(transpiled(3)).toBe 4
expect(grim.getDeprecationsLength()).toBe 0
describe 'when a .js file starts with "use 6to5";', ->
it "transpiles it using babel", ->
it "transpiles it using babel and adds a pragma deprecation", ->
expect(grim.getDeprecationsLength()).toBe 0
transpiled = require('./fixtures/babel/6to5-double-quotes.js')
expect(transpiled(3)).toBe 4
expect(grim.getDeprecationsLength()).toBe 1
describe "when a .js file does not start with 'use 6to6';", ->
it "does not transpile it using babel", ->

View File

@@ -8,6 +8,7 @@ crypto = require 'crypto'
fs = require 'fs-plus'
path = require 'path'
babel = null # Defer until used
Grim = null # Defer until used
stats =
hits: 0
@@ -132,10 +133,30 @@ transpile = (sourceCode, filePath, cachePath) ->
# either generated on the fly or pulled from cache.
loadFile = (module, filePath) ->
sourceCode = fs.readFileSync(filePath, 'utf8')
return module._compile(sourceCode, filePath) unless sourceCode.startsWith('"use 6to5"') or
sourceCode.startsWith("'use 6to5'") or
sourceCode.startsWith('"use babel"') or
sourceCode.startsWith("'use babel'")
if sourceCode.startsWith('"use babel"') or sourceCode.startsWith("'use babel'")
# Continue.
else if sourceCode.startsWith('"use 6to5"') or sourceCode.startsWith("'use 6to5'")
# Create a manual deprecation since the stack is too deep to use Grim
# which limits the depth to 3
Grim ?= require 'grim'
stack = [
{
fileName: __filename
functionName: 'loadFile'
location: "#{__filename}:161:5"
}
{
fileName: filePath
functionName: '<unknown>'
location: "#{filePath}:1:1"
}
]
deprecation =
message: "Use the 'use babel' pragma instead of 'use 6to5'"
stacks: [stack]
Grim.addSerializedDeprecation(deprecation)
else
return module._compile(sourceCode, filePath)
cachePath = getCachePath(sourceCode)
js = getCachedJavaScript(cachePath) ? transpile(sourceCode, filePath, cachePath)