UNPKG

eslint-plugin-sonarjs

Version:
150 lines (149 loc) • 6.38 kB
"use strict"; /* * SonarQube JavaScript Plugin * Copyright (C) SonarSource Sàrl * mailto:info AT sonarsource DOT com * * You can redistribute and/or modify this program under the terms of * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the Sonar Source-Available License for more details. * * You should have received a copy of the Sonar Source-Available License * along with this program; if not, see https://sonarsource.com/license/ssal/ */ // https://sonarsource.github.io/rspec/#/rspec/S9145/javascript var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.rule = void 0; const ast_js_1 = require("../helpers/ast.js"); const generate_meta_js_1 = require("../helpers/generate-meta.js"); const module_js_1 = require("../helpers/module.js"); const vue_js_1 = require("../helpers/vue.js"); const meta = __importStar(require("./generated-meta.js")); const messages = { deprecatedClassComponent: 'Replace this deprecated Vue class-based component pattern with the Composition API.', }; const VUE_CLASS_COMPONENT_MODULE = 'vue-class-component'; const VUE_FQN = 'vue-class-component.Vue'; const DECORATOR_FQNS = new Set([ 'vue-class-component.Component', 'vue-class-component.Options', // v7 exports `Component` as a default export only, which resolves to the bare module FQN; `Vue` is never re-exported by any version, so this bare FQN only ever means the decorator VUE_CLASS_COMPONENT_MODULE, ]); const PROPERTY_DECORATOR_FQNS = new Set([ 'Prop', 'PropSync', 'Model', 'ModelSync', 'Watch', 'Provide', 'Inject', 'ProvideReactive', 'InjectReactive', 'Emit', 'Ref', 'VModel', ].map(name => `vue-property-decorator.${name}`)); exports.rule = { meta: (0, generate_meta_js_1.generateMeta)(meta, { messages }), create(context) { if ((0, vue_js_1.isVue2OrEarlier)(context)) { return {}; } function checkClass(node) { const classNode = node; if (extendsVueClassComponent(context, classNode) || hasVueClassComponentDecorator(context, classNode) || hasVuePropertyDecoratorUsage(context, classNode)) { context.report({ node: getReportNode(classNode), messageId: 'deprecatedClassComponent', }); } } return { ClassDeclaration: checkClass, ClassExpression: checkClass, }; }, }; /** * Whether the class extends the `Vue` base class imported from `vue-class-component`. * Resolved by import origin, not by the local (possibly aliased) identifier name. */ function extendsVueClassComponent(context, classNode) { const { superClass } = classNode; return (superClass?.type === 'Identifier' && (0, module_js_1.getFullyQualifiedName)(context, superClass) === VUE_FQN); } /** * Whether the class carries a `@Component` or `@Options` decorator imported from * `vue-class-component` (named or, for v7's `Component`, default import), whether used bare * (`@Component`) or called (`@Options({...})`). Resolved by import origin so lookalikes from * other libraries (e.g. `vue-facing-decorator`) or locally-defined decorators of the same name * are not flagged. */ function hasVueClassComponentDecorator(context, classNode) { return (classNode.decorators ?? []).some(decorator => isDecoratorFromModule(context, decorator, DECORATOR_FQNS)); } /** * Whether the class or one of its members carries a `vue-property-decorator` decorator * (`@Prop`, `@Watch`, `@Emit`, ...). `vue-property-decorator` is deprecated and archived just * like `vue-class-component` (which it depends on), so its decorators are an equally strong, * import-resolved signal of a class-based Vue component, even without an explicit `extends Vue` * or `@Component`/`@Options` on the class itself. */ function hasVuePropertyDecoratorUsage(context, classNode) { const classDecorators = classNode.decorators ?? []; const memberDecorators = classNode.body.body.flatMap(member => member.decorators ?? []); return [...classDecorators, ...memberDecorators].some(decorator => isDecoratorFromModule(context, decorator, PROPERTY_DECORATOR_FQNS)); } /** * Whether `decorator`'s expression, bare (`@Foo`) or called (`@Foo({...})`), resolves by import * origin to one of `fqns`. */ function isDecoratorFromModule(context, decorator, fqns) { const expression = decorator.expression; const target = (0, ast_js_1.isFunctionCall)(expression) ? expression.callee : expression; return (0, ast_js_1.isIdentifier)(target) && fqns.has((0, module_js_1.getFullyQualifiedName)(context, target) ?? ''); } function getReportNode(classNode) { return (classNode.id ?? classNode); }