Skip to content

Commit

Permalink
Fix import-js#1266, make SourceCode only for no-deprecated rule
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-startsev committed Jan 28, 2019
1 parent 1ec80fa commit fc26273
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
20 changes: 11 additions & 9 deletions src/ExportMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ export default class ExportMap {

/**
* parse docs from the first node that has leading comments
* @param {...[type]} nodes [description]
* @param {SourceCode} source parsed source code
* @param {object} docStyleParsers a set of available docs parses
* @return {{doc: object}}
*/
function captureDoc(source, docStyleParsers) {
Expand All @@ -202,9 +203,10 @@ function captureDoc(source, docStyleParsers) {
// 'some' short-circuits on first 'true'
nodes.some(n => {
try {
let commentsBefore = source && source.getCommentsBefore(n)
// n.leadingComments is legacy `attachComments` behavior
let leadingComments = n.leadingComments || source.getCommentsBefore(n)
if (leadingComments.length === 0) return false
let leadingComments = n.leadingComments || commentsBefore
if (!leadingComments || leadingComments.length === 0) return false

for (let name in docStyleParsers) {
const doc = docStyleParsers[name](leadingComments)
Expand Down Expand Up @@ -274,14 +276,14 @@ function captureTomDoc(comments) {
}
}

ExportMap.get = function (source, context) {
ExportMap.get = function (source, context, options) {
const path = resolve(source, context)
if (path == null) return null

return ExportMap.for(childContext(path, context))
return ExportMap.for(childContext(path, context), options)
}

ExportMap.for = function (context) {
ExportMap.for = function (context, options) {
const { path } = context

const cacheKey = hashObject(context).digest('hex')
Expand Down Expand Up @@ -315,7 +317,7 @@ ExportMap.for = function (context) {
}

log('cache miss', cacheKey, 'for path', path)
exportMap = ExportMap.parse(path, content, context)
exportMap = ExportMap.parse(path, content, context, options)

// ambiguous modules return null
if (exportMap == null) return null
Expand All @@ -327,7 +329,7 @@ ExportMap.for = function (context) {
}


ExportMap.parse = function (path, content, context) {
ExportMap.parse = function (path, content, context, options = {}) {
var m = new ExportMap(path)

try {
Expand All @@ -346,7 +348,7 @@ ExportMap.parse = function (path, content, context) {
docStyleParsers[style] = availableDocStyleParsers[style]
})

const source = makeSourceCode(content, ast)
const source = options.parseComments && makeSourceCode(content, ast)

// attempt to collect module doc
if (ast.comments) {
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ module.exports = {
if (node.type !== 'ImportDeclaration') return
if (node.source == null) return // local export, ignore

const imports = Exports.get(node.source.value, context)
const options = {parseComments: true}
const imports = Exports.get(node.source.value, context, options)
if (imports == null) return

let moduleDeprecation
Expand Down
9 changes: 5 additions & 4 deletions tests/src/core/getExports.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ describe('ExportMap', function () {
context('deprecated imports', function () {
let imports
before('parse file', function () {
const path = getFilename('deprecated.js')
, contents = fs.readFileSync(path, { encoding: 'utf8' })
imports = ExportMap.parse(path, contents, parseContext)
const path = getFilename('deprecated.js'),
contents = fs.readFileSync(path, {encoding: 'utf8'}),
options = {parseComments: true}
imports = ExportMap.parse(path, contents, parseContext, options)

// sanity checks
expect(imports.errors).to.be.empty
Expand All @@ -112,7 +113,7 @@ describe('ExportMap', function () {
expect(imports.get('fn'))
.to.have.deep.property('doc.tags[0].title', 'deprecated')
expect(imports.get('fn'))
.to.have.deep.property('doc.tags[0].description', "please use 'x' instead.")
.to.have.deep.property('doc.tags[0].description', 'please use \'x\' instead.')
})

it('works with default imports.', function () {
Expand Down

0 comments on commit fc26273

Please sign in to comment.