From 49be811bee356130eaa2772c87513e60b4f6fa1b Mon Sep 17 00:00:00 2001 From: Michael Bolin Date: Mon, 23 Feb 2015 22:34:30 -0800 Subject: [PATCH 1/5] Report a deprecation warning when 'use 6to5' is used instead of 'use babel'. --- src/babel.coffee | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/babel.coffee b/src/babel.coffee index 766324ce0..d4b53c204 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 @@ -127,15 +128,33 @@ transpile = (sourceCode, filePath, cachePath) -> js +isRoot = (filePath) -> + parts = path.parse(filePath) + parts.root == filePath + # Function that obeys the contract of an entry in the require.extensions map. # Returns the transpiled version of the JavaScript code at filePath, which is # 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'") + packageName + directory = filePath + until packageName + directory = path.dirname(directory) + manifest = path.join(directory, 'package.json') + if fs.existsSync(manifest) + json = JSON.parse(fs.readFileSync(manifest)) + packageName = json.name + else if isRoot(directory) + break + + Grim ?= require 'grim' + Grim.deprecate("Use the 'use babel' pragma instead of 'use 6to5' in #{filePath}", {packageName}) + else + return module._compile(sourceCode, filePath) cachePath = getCachePath(sourceCode) js = getCachedJavaScript(cachePath) ? transpile(sourceCode, filePath, cachePath) From 1c9dbbf3b3dabd07a585585ac9e060ffa6ea3ead Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 25 Feb 2015 09:24:18 -0800 Subject: [PATCH 2/5] Craft manual deprecation to get around stack depth limit This allows the deprecation to be properly associated with the package it originates from. --- src/babel.coffee | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/babel.coffee b/src/babel.coffee index d4b53c204..01214fa1a 100644 --- a/src/babel.coffee +++ b/src/babel.coffee @@ -140,19 +140,25 @@ loadFile = (module, filePath) -> if sourceCode.startsWith('"use babel"') or sourceCode.startsWith("'use babel'") # Continue. else if sourceCode.startsWith('"use 6to5"') or sourceCode.startsWith("'use 6to5'") - packageName - directory = filePath - until packageName - directory = path.dirname(directory) - manifest = path.join(directory, 'package.json') - if fs.existsSync(manifest) - json = JSON.parse(fs.readFileSync(manifest)) - packageName = json.name - else if isRoot(directory) - break - + # Create a manual deprecation since the stack is too deep to use Grim + # which limits the depth to 3 Grim ?= require 'grim' - Grim.deprecate("Use the 'use babel' pragma instead of 'use 6to5' in #{filePath}", {packageName}) + 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) From 7a719d585db96ff7d2977db9067e1d9d4d0adf1a Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 25 Feb 2015 09:25:22 -0800 Subject: [PATCH 3/5] Remove unused method --- src/babel.coffee | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/babel.coffee b/src/babel.coffee index 01214fa1a..c93112e78 100644 --- a/src/babel.coffee +++ b/src/babel.coffee @@ -128,10 +128,6 @@ transpile = (sourceCode, filePath, cachePath) -> js -isRoot = (filePath) -> - parts = path.parse(filePath) - parts.root == filePath - # Function that obeys the contract of an entry in the require.extensions map. # Returns the transpiled version of the JavaScript code at filePath, which is # either generated on the fly or pulled from cache. From e51f8b298e32201e77f50eb5bd38fa54ad0842ec Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 25 Feb 2015 09:37:42 -0800 Subject: [PATCH 4/5] Add specs for pragma deprecations --- spec/babel-spec.coffee | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/spec/babel-spec.coffee b/spec/babel-spec.coffee index f9abac930..58f67fd03 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 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", -> + 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", -> From feb37c5df6874b78976c3016b5813cc8f55896a6 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 25 Feb 2015 09:42:49 -0800 Subject: [PATCH 5/5] :memo: Mention deprecations --- spec/babel-spec.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/babel-spec.coffee b/spec/babel-spec.coffee index 58f67fd03..4aec0ef8d 100644 --- a/spec/babel-spec.coffee +++ b/spec/babel-spec.coffee @@ -40,7 +40,7 @@ describe "Babel transpiler support", -> expect(grim.getDeprecationsLength()).toBe 0 describe "when a .js file starts with 'use 6to5';", -> - it "transpiles it using babel and adds a deprecation", -> + 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 @@ -53,7 +53,7 @@ describe "Babel transpiler support", -> 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