Skip to content

Commit 05c3935

Browse files
Ben Mosherljharb
Ben Mosher
authored andcommitted
repair no-deprecated for ESLint* 5
* technically espree v5 - also technically it doesn't support comma-first anymore but that seems reasonable
1 parent 10c9811 commit 05c3935

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

src/ExportMap.js

+36-12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import doctrine from 'doctrine'
44

55
import debug from 'debug'
66

7+
import SourceCode from 'eslint/lib/util/source-code'
8+
79
import parse from 'eslint-module-utils/parse'
810
import resolve from 'eslint-module-utils/resolve'
911
import isIgnored, { hasValidExtension } from 'eslint-module-utils/ignore'
@@ -193,22 +195,28 @@ export default class ExportMap {
193195
* @param {...[type]} nodes [description]
194196
* @return {{doc: object}}
195197
*/
196-
function captureDoc(docStyleParsers) {
198+
function captureDoc(source, docStyleParsers) {
197199
const metadata = {}
198200
, nodes = Array.prototype.slice.call(arguments, 1)
199201

200202
// 'some' short-circuits on first 'true'
201203
nodes.some(n => {
202-
if (!n.leadingComments) return false
203-
204-
for (let name in docStyleParsers) {
205-
const doc = docStyleParsers[name](n.leadingComments)
206-
if (doc) {
207-
metadata.doc = doc
204+
try {
205+
// n.leadingComments is legacy `attachComments` behavior
206+
let leadingComments = n.leadingComments || source.getCommentsBefore(n)
207+
if (leadingComments.length === 0) return false
208+
209+
for (let name in docStyleParsers) {
210+
const doc = docStyleParsers[name](leadingComments)
211+
if (doc) {
212+
metadata.doc = doc
213+
}
208214
}
209-
}
210215

211-
return true
216+
return true
217+
} catch (err) {
218+
return false
219+
}
212220
})
213221

214222
return metadata
@@ -338,6 +346,8 @@ ExportMap.parse = function (path, content, context) {
338346
docStyleParsers[style] = availableDocStyleParsers[style]
339347
})
340348

349+
const source = makeSourceCode(content, ast)
350+
341351
// attempt to collect module doc
342352
if (ast.comments) {
343353
ast.comments.some(c => {
@@ -405,7 +415,7 @@ ExportMap.parse = function (path, content, context) {
405415
ast.body.forEach(function (n) {
406416

407417
if (n.type === 'ExportDefaultDeclaration') {
408-
const exportMeta = captureDoc(docStyleParsers, n)
418+
const exportMeta = captureDoc(source, docStyleParsers, n)
409419
if (n.declaration.type === 'Identifier') {
410420
addNamespace(exportMeta, n.declaration)
411421
}
@@ -441,12 +451,12 @@ ExportMap.parse = function (path, content, context) {
441451
case 'TSInterfaceDeclaration':
442452
case 'TSAbstractClassDeclaration':
443453
case 'TSModuleDeclaration':
444-
m.namespace.set(n.declaration.id.name, captureDoc(docStyleParsers, n))
454+
m.namespace.set(n.declaration.id.name, captureDoc(source, docStyleParsers, n))
445455
break
446456
case 'VariableDeclaration':
447457
n.declaration.declarations.forEach((d) =>
448458
recursivePatternCapture(d.id,
449-
id => m.namespace.set(id.name, captureDoc(docStyleParsers, d, n))))
459+
id => m.namespace.set(id.name, captureDoc(source, docStyleParsers, d, n))))
450460
break
451461
}
452462
}
@@ -531,3 +541,17 @@ function childContext(path, context) {
531541
path,
532542
}
533543
}
544+
545+
546+
/**
547+
* sometimes legacy support isn't _that_ hard... right?
548+
*/
549+
function makeSourceCode(text, ast) {
550+
if (SourceCode.length > 1) {
551+
// ESLint 3
552+
return new SourceCode(text, ast)
553+
} else {
554+
// ESLint 4, 5
555+
return new SourceCode({ text, ast })
556+
}
557+
}

tests/files/deprecated.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ export const MY_TERRIBLE_ACTION = "ugh"
2727
* @deprecated this chain is awful
2828
* @type {String}
2929
*/
30-
export const CHAIN_A = "a"
30+
export const CHAIN_A = "a",
3131
/**
3232
* @deprecated so awful
3333
* @type {String}
3434
*/
35-
, CHAIN_B = "b"
35+
CHAIN_B = "b",
3636

3737
/**
3838
* @deprecated still terrible
3939
* @type {String}
4040
*/
41-
, CHAIN_C = "C"
41+
CHAIN_C = "C"
4242

4343
/**
4444
* this one is fine

tests/src/core/parse.js

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ describe('parse(content, { settings, ecmaFeatures })', function () {
3838
.that.is.eql(parserOptions.ecmaFeatures)
3939
.and.is.not.equal(parserOptions.ecmaFeatures)
4040
expect(parseSpy.args[0][1], 'custom parser to get parserOptions.attachComment equal to true').to.have.property('attachComment', true)
41+
expect(parseSpy.args[0][1], 'custom parser to get parserOptions.tokens equal to true').to.have.property('tokens', true)
42+
expect(parseSpy.args[0][1], 'custom parser to get parserOptions.range equal to true').to.have.property('range', true)
4143
expect(parseSpy.args[0][1], 'custom parser to get parserOptions.filePath equal to the full path of the source file').to.have.property('filePath', path)
4244
})
4345

utils/parse.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ exports.default = function parse(path, content, context) {
1919
parserOptions = Object.assign({}, parserOptions)
2020
parserOptions.ecmaFeatures = Object.assign({}, parserOptions.ecmaFeatures)
2121

22-
// always include and attach comments
22+
// always include comments and tokens (for doc parsing)
2323
parserOptions.comment = true
24-
parserOptions.attachComment = true
24+
parserOptions.attachComment = true // keeping this for backward-compat with older parsers
25+
parserOptions.tokens = true
2526

2627
// attach node locations
2728
parserOptions.loc = true
29+
parserOptions.range = true
2830

2931
// provide the `filePath` like eslint itself does, in `parserOptions`
3032
// https://github.com/eslint/eslint/blob/3ec436ee/lib/linter.js#L637

0 commit comments

Comments
 (0)