diff --git a/spec/babel-spec.coffee b/spec/babel-spec.coffee index f9abac930..4aec0ef8d 100644 --- a/spec/babel-spec.coffee +++ b/spec/babel-spec.coffee @@ -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", -> diff --git a/src/babel.coffee b/src/babel.coffee index 766324ce0..c93112e78 100644 --- a/src/babel.coffee +++ b/src/babel.coffee @@ -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: '' + 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)