| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597 |
- /**
- * @fileoverview Rule to disallow deprecated API.
- * @author Toru Nagashima
- * @copyright 2016 Toru Nagashima. All rights reserved.
- * See LICENSE file in root directory for full license.
- */
- "use strict"
- //------------------------------------------------------------------------------
- // Requirements
- //------------------------------------------------------------------------------
- const deprecatedApis = require("../util/deprecated-apis")
- const getDocsUrl = require("../util/get-docs-url")
- const getValueIfString = require("../util/get-value-if-string")
- //------------------------------------------------------------------------------
- // Helpers
- //------------------------------------------------------------------------------
- const SENTINEL_TYPE = /^(?:.+?Statement|.+?Declaration|(?:Array|ArrowFunction|Assignment|Call|Class|Function|Member|New|Object)Expression|AssignmentPattern|Program|VariableDeclarator)$/
- const MODULE_ITEMS = getDeprecatedItems(deprecatedApis.modules, [], [])
- const GLOBAL_ITEMS = getDeprecatedItems(deprecatedApis.globals, [], [])
- /**
- * Gets the array of deprecated items.
- *
- * It's the paths which are separated by dots.
- * E.g. `buffer.Buffer`, `events.EventEmitter.listenerCount`
- *
- * @param {object} definition - The definition of deprecated APIs.
- * @param {string[]} result - The array of the result.
- * @param {string[]} stack - The array to manage the stack of paths.
- * @returns {string[]} `result`.
- */
- function getDeprecatedItems(definition, result, stack) {
- for (const key of Object.keys(definition)) {
- const item = definition[key]
- if (key === "$call") {
- result.push(`${stack.join(".")}()`)
- }
- else if (key === "$constructor") {
- result.push(`new ${stack.join(".")}()`)
- }
- else {
- stack.push(key)
- if (item.$deprecated) {
- result.push(stack.join("."))
- }
- else {
- getDeprecatedItems(item, result, stack)
- }
- stack.pop()
- }
- }
- return result
- }
- /**
- * Converts from a version number to a version text to display.
- *
- * @param {number} value - A version number to convert.
- * @returns {string} Covnerted text.
- */
- function toVersionText(value) {
- if (value <= 0.12) {
- return value.toFixed(2)
- }
- if (value < 1) {
- return value.toFixed(1)
- }
- return String(value)
- }
- /**
- * Makes a replacement message.
- *
- * @param {string|null} replacedBy - The text of substitute way.
- * @returns {string} Replacement message.
- */
- function toReplaceMessage(replacedBy) {
- return replacedBy ? ` Use ${replacedBy} instead.` : ""
- }
- /**
- * Gets the property name from a MemberExpression node or a Property node.
- *
- * @param {ASTNode} node - A node to get.
- * @returns {string|null} The property name of the node.
- */
- function getPropertyName(node) {
- switch (node.type) {
- case "MemberExpression":
- if (node.computed) {
- return getValueIfString(node.property)
- }
- return node.property.name
- case "Property":
- if (node.computed) {
- return getValueIfString(node.key)
- }
- if (node.key.type === "Literal") {
- return String(node.key.value)
- }
- return node.key.name
- // no default
- }
- /* istanbul ignore next: unreachable */
- return null
- }
- /**
- * Checks a given node is a ImportDeclaration node.
- *
- * @param {ASTNode} node - A node to check.
- * @returns {boolean} `true` if the node is a ImportDeclaration node.
- */
- function isImportDeclaration(node) {
- return node.type === "ImportDeclaration"
- }
- /**
- * Finds the variable object of a given Identifier node.
- *
- * @param {ASTNode} node - An Identifier node to find.
- * @param {escope.Scope} initialScope - A scope to start searching.
- * @returns {escope.Variable} Found variable object.
- */
- function findVariable(node, initialScope) {
- const location = node.range[0]
- let variable = null
- // Dive into the scope that the node exists.
- for (const childScope of initialScope.childScopes) {
- const range = childScope.block.range
- if (range[0] <= location && location < range[1]) {
- variable = findVariable(node, childScope)
- if (variable != null) {
- return variable
- }
- }
- }
- // Find the variable of that name in this scope or ancestor scopes.
- let scope = initialScope
- while (scope != null) {
- variable = scope.set.get(node.name)
- if (variable != null) {
- return variable
- }
- scope = scope.upper
- }
- return null
- }
- /**
- * Gets the top member expression node.
- *
- * @param {ASTNode} identifier - The node to get.
- * @returns {ASTNode} The top member expression node.
- */
- function getTopMemberExpression(identifier) {
- if (identifier.type !== "Identifier" && identifier.type !== "Literal") {
- return identifier
- }
- let node = identifier
- while (node.parent.type === "MemberExpression") {
- node = node.parent
- }
- return node
- }
- /**
- * The definition of this rule.
- *
- * @param {RuleContext} context - The rule context to check.
- * @returns {object} The definition of this rule.
- */
- function create(context) {
- const options = context.options[0] || {}
- const ignoredModuleItems = options.ignoreModuleItems || []
- const ignoredGlobalItems = options.ignoreGlobalItems || []
- let globalScope = null
- const varStack = []
- /**
- * Reports a use of a deprecated API.
- *
- * @param {ASTNode} node - A node to report.
- * @param {string} name - The name of a deprecated API.
- * @param {{since: number, replacedBy: string}} info - Information of the API.
- * @returns {void}
- */
- function report(node, name, info) {
- context.report({
- node,
- loc: getTopMemberExpression(node).loc,
- message: "{{name}} was deprecated since v{{version}}.{{replace}}",
- data: {
- name,
- version: toVersionText(info.since),
- replace: toReplaceMessage(info.replacedBy),
- },
- })
- }
- /**
- * Reports a use of a deprecated module.
- *
- * @param {ASTNode} node - A node to report.
- * @param {string} name - The name of a deprecated module.
- * @param {{since: number, replacedBy: string, global: boolean}} info - Information of the module.
- * @returns {void}
- */
- function reportModule(node, name, info) {
- if (ignoredModuleItems.indexOf(name) === -1) {
- report(node, `'${name}' module`, info)
- }
- }
- /**
- * Reports a use of a deprecated property.
- *
- * @param {ASTNode} node - A node to report.
- * @param {string[]} path - The path to a deprecated property.
- * @param {{since: number, replacedBy: string, global: boolean}} info - Information of the property.
- * @returns {void}
- */
- function reportCall(node, path, info) {
- const ignored = info.global ? ignoredGlobalItems : ignoredModuleItems
- const name = `${path.join(".")}()`
- if (ignored.indexOf(name) === -1) {
- report(node, `'${name}'`, info)
- }
- }
- /**
- * Reports a use of a deprecated property.
- *
- * @param {ASTNode} node - A node to report.
- * @param {string[]} path - The path to a deprecated property.
- * @param {{since: number, replacedBy: string, global: boolean}} info - Information of the property.
- * @returns {void}
- */
- function reportConstructor(node, path, info) {
- const ignored = info.global ? ignoredGlobalItems : ignoredModuleItems
- const name = `new ${path.join(".")}()`
- if (ignored.indexOf(name) === -1) {
- report(node, `'${name}'`, info)
- }
- }
- /**
- * Reports a use of a deprecated property.
- *
- * @param {ASTNode} node - A node to report.
- * @param {string[]} path - The path to a deprecated property.
- * @param {string} key - The name of the property.
- * @param {{since: number, replacedBy: string, global: boolean}} info - Information of the property.
- * @returns {void}
- */
- function reportProperty(node, path, key, info) {
- const ignored = info.global ? ignoredGlobalItems : ignoredModuleItems
- path.push(key)
- const name = path.join(".")
- path.pop()
- if (ignored.indexOf(name) === -1) {
- report(node, `'${name}'`, info)
- }
- }
- /**
- * Checks violations in destructuring assignments.
- *
- * @param {ASTNode} node - A pattern node to check.
- * @param {string[]} path - The path to a deprecated property.
- * @param {object} infoMap - A map of properties' information.
- * @returns {void}
- */
- function checkDestructuring(node, path, infoMap) {
- switch (node.type) {
- case "AssignmentPattern":
- checkDestructuring(node.left, path, infoMap)
- break
- case "Identifier": {
- const variable = findVariable(node, globalScope)
- if (variable != null) {
- checkVariable(variable, path, infoMap)
- }
- break
- }
- case "ObjectPattern":
- for (const property of node.properties) {
- const key = getPropertyName(property)
- if (key != null && hasOwnProperty.call(infoMap, key)) {
- const keyInfo = infoMap[key]
- if (keyInfo.$deprecated) {
- reportProperty(property.key, path, key, keyInfo)
- }
- else {
- path.push(key)
- checkDestructuring(property.value, path, keyInfo)
- path.pop()
- }
- }
- }
- break
- // no default
- }
- }
- /**
- * Checks violations in properties.
- *
- * @param {ASTNode} root - A node to check.
- * @param {string[]} path - The path to a deprecated property.
- * @param {object} infoMap - A map of properties' information.
- * @returns {void}
- */
- function checkProperties(root, path, infoMap) { //eslint-disable-line complexity
- let node = root
- while (!SENTINEL_TYPE.test(node.parent.type)) {
- node = node.parent
- }
- const parent = node.parent
- switch (parent.type) {
- case "CallExpression":
- if (parent.callee === node && infoMap.$call != null) {
- reportCall(parent, path, infoMap.$call)
- }
- break
- case "NewExpression":
- if (parent.callee === node && infoMap.$constructor != null) {
- reportConstructor(parent, path, infoMap.$constructor)
- }
- break
- case "MemberExpression":
- if (parent.object === node) {
- const key = getPropertyName(parent)
- if (key != null && hasOwnProperty.call(infoMap, key)) {
- const keyInfo = infoMap[key]
- if (keyInfo.$deprecated) {
- reportProperty(parent.property, path, key, keyInfo)
- }
- else {
- path.push(key)
- checkProperties(parent, path, keyInfo)
- path.pop()
- }
- }
- }
- break
- case "AssignmentExpression":
- if (parent.right === node) {
- checkDestructuring(parent.left, path, infoMap)
- checkProperties(parent, path, infoMap)
- }
- break
- case "AssignmentPattern":
- if (parent.right === node) {
- checkDestructuring(parent.left, path, infoMap)
- }
- break
- case "VariableDeclarator":
- if (parent.init === node) {
- checkDestructuring(parent.id, path, infoMap)
- }
- break
- // no default
- }
- }
- /**
- * Checks violations in the references of a given variable.
- *
- * @param {escope.Variable} variable - A variable to check.
- * @param {string[]} path - The path to a deprecated property.
- * @param {object} infoMap - A map of properties' information.
- * @returns {void}
- */
- function checkVariable(variable, path, infoMap) {
- if (varStack.indexOf(variable) !== -1) {
- return
- }
- varStack.push(variable)
- if (infoMap.$deprecated) {
- const key = path.pop()
- for (const reference of variable.references.filter(r => r.isRead())) {
- reportProperty(reference.identifier, path, key, infoMap)
- }
- }
- else {
- for (const reference of variable.references.filter(r => r.isRead())) {
- checkProperties(reference.identifier, path, infoMap)
- }
- }
- varStack.pop()
- }
- /**
- * Checks violations in a ModuleSpecifier node.
- *
- * @param {ASTNode} node - A ModuleSpecifier node to check.
- * @param {string[]} path - The path to a deprecated property.
- * @param {object} infoMap - A map of properties' information.
- * @returns {void}
- */
- function checkImportSpecifier(node, path, infoMap) {
- switch (node.type) {
- case "ImportSpecifier": {
- const key = node.imported.name
- if (hasOwnProperty.call(infoMap, key)) {
- const keyInfo = infoMap[key]
- if (keyInfo.$deprecated) {
- reportProperty(node.imported, path, key, keyInfo)
- }
- else {
- path.push(key)
- checkVariable(
- findVariable(node.local, globalScope),
- path,
- keyInfo
- )
- path.pop()
- }
- }
- break
- }
- case "ImportDefaultSpecifier":
- checkVariable(
- findVariable(node.local, globalScope),
- path,
- infoMap
- )
- break
- case "ImportNamespaceSpecifier":
- checkVariable(
- findVariable(node.local, globalScope),
- path,
- Object.assign({}, infoMap, { default: infoMap })
- )
- break
- // no default
- }
- }
- /**
- * Checks violations for CommonJS modules.
- * @returns {void}
- */
- function checkCommonJsModules() {
- const infoMap = deprecatedApis.modules
- const variable = globalScope.set.get("require")
- if (variable == null || variable.defs.length !== 0) {
- return
- }
- for (const reference of variable.references.filter(r => r.isRead())) {
- const id = reference.identifier
- const node = id.parent
- if (node.type === "CallExpression" && node.callee === id) {
- const key = getValueIfString(node.arguments[0])
- if (key != null && hasOwnProperty.call(infoMap, key)) {
- const moduleInfo = infoMap[key]
- if (moduleInfo.$deprecated) {
- reportModule(node, key, moduleInfo)
- }
- else {
- checkProperties(node, [key], moduleInfo)
- }
- }
- }
- }
- }
- /**
- * Checks violations for ES2015 modules.
- * @param {ASTNode} programNode - A program node to check.
- * @returns {void}
- */
- function checkES2015Modules(programNode) {
- const infoMap = deprecatedApis.modules
- for (const node of programNode.body.filter(isImportDeclaration)) {
- const key = node.source.value
- if (hasOwnProperty.call(infoMap, key)) {
- const moduleInfo = infoMap[key]
- if (moduleInfo.$deprecated) {
- reportModule(node, key, moduleInfo)
- }
- else {
- for (const specifier of node.specifiers) {
- checkImportSpecifier(specifier, [key], moduleInfo)
- }
- }
- }
- }
- }
- /**
- * Checks violations for global variables.
- * @returns {void}
- */
- function checkGlobals() {
- const infoMap = deprecatedApis.globals
- for (const key of Object.keys(infoMap)) {
- const keyInfo = infoMap[key]
- const variable = globalScope.set.get(key)
- if (variable != null && variable.defs.length === 0) {
- checkVariable(variable, [key], keyInfo)
- }
- }
- }
- return {
- "Program:exit"(node) {
- globalScope = context.getScope()
- checkCommonJsModules()
- checkES2015Modules(node)
- checkGlobals()
- },
- }
- }
- //------------------------------------------------------------------------------
- // Rule Definition
- //------------------------------------------------------------------------------
- module.exports = {
- create,
- meta: {
- docs: {
- description: "disallow deprecated APIs",
- category: "Best Practices",
- recommended: true,
- url: getDocsUrl("no-deprecated-api.md"),
- },
- fixable: false,
- schema: [
- {
- type: "object",
- properties: {
- ignoreModuleItems: {
- type: "array",
- items: { enum: MODULE_ITEMS },
- additionalItems: false,
- uniqueItems: true,
- },
- ignoreGlobalItems: {
- type: "array",
- items: { enum: GLOBAL_ITEMS },
- additionalItems: false,
- uniqueItems: true,
- },
- // Deprecated since v4.2.0
- ignoreIndirectDependencies: { type: "boolean" },
- },
- additionalProperties: false,
- },
- ],
- },
- }
|