diff --git a/packages/metro-bundler/package.json b/packages/metro-bundler/package.json index 205f03a5..5eb1278e 100644 --- a/packages/metro-bundler/package.json +++ b/packages/metro-bundler/package.json @@ -30,6 +30,7 @@ "fbjs": "0.8.12", "graceful-fs": "^4.1.3", "image-size": "^0.3.5", + "jest-docblock": "^20.0.3", "jest-haste-map": "^20.0.4", "json-stable-stringify": "^1.0.1", "json5": "^0.4.0", diff --git a/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js b/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js index d361ae46..ccf06956 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js @@ -16,7 +16,7 @@ const JsFileWrapping = require('./JsFileWrapping'); const asyncify = require('async/asyncify'); const collectDependencies = require('./collect-dependencies'); const defaults = require('../../defaults'); -const docblock = require('../../node-haste/DependencyGraph/docblock'); +const docblock = require('jest-docblock'); const generate = require('./generate'); const path = require('path'); const series = require('async/series'); @@ -99,7 +99,7 @@ function transformModule( ); }); - const annotations = docblock.parseAsObject(docblock.extract(code)); + const annotations = docblock.parse(docblock.extract(code)); callback(null, { type: 'code', diff --git a/packages/metro-bundler/src/node-haste/DependencyGraph/docblock.js b/packages/metro-bundler/src/node-haste/DependencyGraph/docblock.js deleted file mode 100644 index 62550ff6..00000000 --- a/packages/metro-bundler/src/node-haste/DependencyGraph/docblock.js +++ /dev/null @@ -1,83 +0,0 @@ -/** - * Copyright (c) 2015-present, Facebook, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - * @flow - * @format - */ - -'use strict'; - -var docblockRe = /^\s*(\/\*\*(.|\r?\n)*?\*\/)/; - -var ltrimRe = /^\s*/; -/** - * @param {String} contents - * @return {String} - */ -function extract(contents: string): string { - var match = contents.match(docblockRe); - if (match) { - return match[0].replace(ltrimRe, '') || ''; - } - return ''; -} - -var commentStartRe = /^\/\*\*/; -var commentEndRe = /\*\/$/; -var wsRe = /[\t ]+/g; -var stringStartRe = /(\r?\n|^) *\*/g; -var multilineRe = /(?:^|\r?\n) *(@[^\r\n]*?) *\r?\n *([^@\r\n\s][^@\r\n]+?) *\r?\n/g; -var propertyRe = /(?:^|\r?\n) *@(\S+) *([^\r\n]*)/g; - -/** - * @param {String} contents - * @return {Array} - */ -function parse(docblock: string): Array<[string, string]> { - docblock = docblock - .replace(commentStartRe, '') - .replace(commentEndRe, '') - .replace(wsRe, ' ') - .replace(stringStartRe, '$1'); - - // Normalize multi-line directives - var prev = ''; - while (prev !== docblock) { - prev = docblock; - docblock = docblock.replace(multilineRe, '\n$1 $2\n'); - } - docblock = docblock.trim(); - - var result = []; - var match; - while ((match = propertyRe.exec(docblock))) { - result.push([match[1], match[2].trim()]); - } - - return result; -} - -/** - * Same as parse but returns an object of prop: value instead of array of paris - * If a property appers more than once the last one will be returned - * - * @param {String} contents - * @return {Object} - */ -function parseAsObject(docblock: string): {[string]: string} { - var pairs = parse(docblock); - var result = {}; - for (var i = 0; i < pairs.length; i++) { - result[pairs[i][0]] = pairs[i][1]; - } - return result; -} - -exports.extract = extract; -exports.parse = parse; -exports.parseAsObject = parseAsObject; diff --git a/packages/metro-bundler/src/node-haste/Module.js b/packages/metro-bundler/src/node-haste/Module.js index fa11ef95..1103ac75 100644 --- a/packages/metro-bundler/src/node-haste/Module.js +++ b/packages/metro-bundler/src/node-haste/Module.js @@ -13,7 +13,7 @@ 'use strict'; const crypto = require('crypto'); -const docblock = require('./DependencyGraph/docblock'); +const docblock = require('jest-docblock'); const fs = require('fs'); const invariant = require('fbjs/lib/invariant'); const isAbsolutePath = require('absolute-path'); @@ -209,7 +209,7 @@ class Module { _readDocBlock(): DocBlock { if (this._docBlock == null) { - this._docBlock = docblock.parseAsObject(this._readSourceCode()); + this._docBlock = docblock.parse(docblock.extract(this._readSourceCode())); } return this._docBlock; }