123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871 |
- 'use strict'
- const HTML_ELEMENT_NAMES = new Set(require('./html-elements.json'))
- const SVG_ELEMENT_NAMES = new Set(require('./svg-elements.json'))
- const VOID_ELEMENT_NAMES = new Set(require('./void-elements.json'))
- const assert = require('assert')
- const path = require('path')
- const vueEslintParser = require('vue-eslint-parser')
- function wrapContextToOverrideTokenMethods (context, tokenStore) {
- const sourceCode = new Proxy(context.getSourceCode(), {
- get (object, key) {
- return key in tokenStore ? tokenStore[key] : object[key]
- }
- })
- return {
- __proto__: context,
- getSourceCode () {
- return sourceCode
- }
- }
- }
- module.exports = {
-
- defineTemplateBodyVisitor (context, templateBodyVisitor, scriptVisitor) {
- if (context.parserServices.defineTemplateBodyVisitor == null) {
- context.report({
- loc: { line: 1, column: 0 },
- message: 'Use the latest vue-eslint-parser. See also https://eslint.vuejs.org/user-guide/#what-is-the-use-the-latest-vue-eslint-parser-error'
- })
- return {}
- }
- return context.parserServices.defineTemplateBodyVisitor(templateBodyVisitor, scriptVisitor)
- },
-
- wrapCoreRule (coreRule, options) {
- const { category, skipDynamicArguments } = options || {}
- return {
- create (context) {
- const tokenStore =
- context.parserServices.getTemplateBodyTokenStore &&
- context.parserServices.getTemplateBodyTokenStore()
-
-
- if (tokenStore) {
- context = wrapContextToOverrideTokenMethods(context, tokenStore)
- }
-
- const handlers = coreRule.create(context)
- if (handlers.Program) {
- handlers["VElement[parent.type!='VElement']"] = handlers.Program
- delete handlers.Program
- }
- if (handlers['Program:exit']) {
- handlers["VElement[parent.type!='VElement']:exit"] = handlers['Program:exit']
- delete handlers['Program:exit']
- }
- if (skipDynamicArguments) {
- let withinDynamicArguments = false
- for (const name of Object.keys(handlers)) {
- const original = handlers[name]
- handlers[name] = (...args) => {
- if (withinDynamicArguments) return
- original(...args)
- }
- }
- handlers['VDirectiveKey > VExpressionContainer'] = () => { withinDynamicArguments = true }
- handlers['VDirectiveKey > VExpressionContainer:exit'] = () => { withinDynamicArguments = false }
- }
-
- return module.exports.defineTemplateBodyVisitor(context, handlers)
- },
- meta: Object.assign({}, coreRule.meta, {
- docs: Object.assign({}, coreRule.meta.docs, {
- category,
- url: `https://eslint.vuejs.org/rules/${path.basename(coreRule.meta.docs.url || '')}.html`
- })
- })
- }
- },
-
- isRootElement (node) {
- assert(node && node.type === 'VElement')
- return (
- node.parent.type === 'VDocumentFragment' ||
- node.parent.parent.type === 'VDocumentFragment'
- )
- },
-
- prevSibling (node) {
- assert(node && node.type === 'VElement')
- let prevElement = null
- for (const siblingNode of (node.parent && node.parent.children) || []) {
- if (siblingNode === node) {
- return prevElement
- }
- if (siblingNode.type === 'VElement') {
- prevElement = siblingNode
- }
- }
- return null
- },
-
- hasAttribute (node, name, value) {
- assert(node && node.type === 'VElement')
- return Boolean(this.getAttribute(node, name, value))
- },
-
- hasDirective (node, name, argument) {
- assert(node && node.type === 'VElement')
- return Boolean(this.getDirective(node, name, argument))
- },
-
- hasAttributeValue (node) {
- assert(node && node.type === 'VAttribute')
- return (
- node.value != null &&
- (node.value.expression != null || node.value.syntaxError != null)
- )
- },
-
- getAttribute (node, name, value) {
- assert(node && node.type === 'VElement')
- return node.startTag.attributes.find(a =>
- !a.directive &&
- a.key.name === name &&
- (
- value === undefined ||
- (a.value != null && a.value.value === value)
- )
- )
- },
-
- getDirective (node, name, argument) {
- assert(node && node.type === 'VElement')
- return node.startTag.attributes.find(a =>
- a.directive &&
- a.key.name.name === name &&
- (argument === undefined || (a.key.argument && a.key.argument.name) === argument)
- )
- },
-
- getRegisteredComponents (componentObject) {
- const componentsNode = componentObject.properties
- .find(p =>
- p.type === 'Property' &&
- p.key.type === 'Identifier' &&
- p.key.name === 'components' &&
- p.value.type === 'ObjectExpression'
- )
- if (!componentsNode) { return [] }
- return componentsNode.value.properties
- .filter(p => p.type === 'Property')
- .map(node => {
- const name = this.getStaticPropertyName(node)
- return name ? { node, name } : null
- })
- .filter(comp => comp != null)
- },
-
- prevElementHasIf (node) {
- assert(node && node.type === 'VElement')
- const prev = this.prevSibling(node)
- return (
- prev != null &&
- prev.startTag.attributes.some(a =>
- a.directive &&
- (a.key.name.name === 'if' || a.key.name.name === 'else-if')
- )
- )
- },
-
- isCustomComponent (node) {
- assert(node && node.type === 'VElement')
- return (
- (this.isHtmlElementNode(node) && !this.isHtmlWellKnownElementName(node.rawName)) ||
- this.hasAttribute(node, 'is') ||
- this.hasDirective(node, 'bind', 'is')
- )
- },
-
- isHtmlElementNode (node) {
- assert(node && node.type === 'VElement')
- return node.namespace === vueEslintParser.AST.NS.HTML
- },
-
- isSvgElementNode (node) {
- assert(node && node.type === 'VElement')
- return node.namespace === vueEslintParser.AST.NS.SVG
- },
-
- isMathMLElementNode (node) {
- assert(node && node.type === 'VElement')
- return node.namespace === vueEslintParser.AST.NS.MathML
- },
-
- isHtmlWellKnownElementName (name) {
- assert(typeof name === 'string')
- return HTML_ELEMENT_NAMES.has(name)
- },
-
- isSvgWellKnownElementName (name) {
- assert(typeof name === 'string')
- return SVG_ELEMENT_NAMES.has(name)
- },
-
- isHtmlVoidElementName (name) {
- assert(typeof name === 'string')
- return VOID_ELEMENT_NAMES.has(name)
- },
-
- parseMemberExpression (node) {
- const members = []
- let memberExpression
- if (node.type === 'MemberExpression') {
- memberExpression = node
- while (memberExpression.type === 'MemberExpression') {
- if (memberExpression.property.type === 'Identifier') {
- members.push(memberExpression.property.name)
- }
- memberExpression = memberExpression.object
- }
- if (memberExpression.type === 'ThisExpression') {
- members.push('this')
- } else if (memberExpression.type === 'Identifier') {
- members.push(memberExpression.name)
- }
- }
- return members.reverse()
- },
-
- getStaticPropertyName (node) {
- let prop
- switch (node && node.type) {
- case 'Property':
- case 'MethodDefinition':
- prop = node.key
- break
- case 'MemberExpression':
- prop = node.property
- break
- case 'Literal':
- case 'TemplateLiteral':
- case 'Identifier':
- prop = node
- break
-
- }
- switch (prop && prop.type) {
- case 'Literal':
- return String(prop.value)
- case 'TemplateLiteral':
- if (prop.expressions.length === 0 && prop.quasis.length === 1) {
- return prop.quasis[0].value.cooked
- }
- break
- case 'Identifier':
- if (!node.computed) {
- return prop.name
- }
- break
-
- }
- return null
- },
-
- getComponentProps (componentObject) {
- const propsNode = componentObject.properties
- .find(p =>
- p.type === 'Property' &&
- p.key.type === 'Identifier' &&
- p.key.name === 'props' &&
- (p.value.type === 'ObjectExpression' || p.value.type === 'ArrayExpression')
- )
- if (!propsNode) {
- return []
- }
- let props
- if (propsNode.value.type === 'ObjectExpression') {
- props = propsNode.value.properties
- .filter(prop => prop.type === 'Property')
- .map(prop => {
- return {
- key: prop.key, value: this.unwrapTypes(prop.value), node: prop,
- propName: this.getStaticPropertyName(prop)
- }
- })
- } else {
- props = propsNode.value.elements
- .filter(prop => prop)
- .map(prop => {
- const key = prop.type === 'Literal' && typeof prop.value === 'string' ? prop : null
- return { key, value: null, node: prop, propName: key != null ? prop.value : null }
- })
- }
- return props
- },
-
- getComputedProperties (componentObject) {
- const computedPropertiesNode = componentObject.properties
- .find(p =>
- p.type === 'Property' &&
- p.key.type === 'Identifier' &&
- p.key.name === 'computed' &&
- p.value.type === 'ObjectExpression'
- )
- if (!computedPropertiesNode) { return [] }
- return computedPropertiesNode.value.properties
- .filter(cp => cp.type === 'Property')
- .map(cp => {
- const key = cp.key.name
- let value
- if (cp.value.type === 'FunctionExpression') {
- value = cp.value.body
- } else if (cp.value.type === 'ObjectExpression') {
- value = cp.value.properties
- .filter(p =>
- p.type === 'Property' &&
- p.key.type === 'Identifier' &&
- p.key.name === 'get' &&
- p.value.type === 'FunctionExpression'
- )
- .map(p => p.value.body)[0]
- }
- return { key, value }
- })
- },
- isVueFile (path) {
- return path.endsWith('.vue') || path.endsWith('.jsx')
- },
-
- isVueComponentFile (node, path) {
- return this.isVueFile(path) &&
- node.type === 'ExportDefaultDeclaration' &&
- node.declaration.type === 'ObjectExpression'
- },
-
- isVueComponent (node) {
- if (node.type === 'CallExpression') {
- const callee = node.callee
- if (callee.type === 'MemberExpression') {
- const calleeObject = this.unwrapTypes(callee.object)
- const isFullVueComponent = calleeObject.type === 'Identifier' &&
- calleeObject.name === 'Vue' &&
- callee.property.type === 'Identifier' &&
- ['component', 'mixin', 'extend'].indexOf(callee.property.name) > -1 &&
- node.arguments.length >= 1 &&
- node.arguments.slice(-1)[0].type === 'ObjectExpression'
- return isFullVueComponent
- }
- if (callee.type === 'Identifier') {
- const isDestructedVueComponent = callee.name === 'component' &&
- node.arguments.length >= 1 &&
- node.arguments.slice(-1)[0].type === 'ObjectExpression'
- return isDestructedVueComponent
- }
- }
- return false
- },
-
- isVueInstance (node) {
- const callee = node.callee
- return node.type === 'NewExpression' &&
- callee.type === 'Identifier' &&
- callee.name === 'Vue' &&
- node.arguments.length &&
- node.arguments[0].type === 'ObjectExpression'
- },
-
- executeOnVue (context, cb) {
- return Object.assign(
- this.executeOnVueComponent(context, cb),
- this.executeOnVueInstance(context, cb)
- )
- },
-
- executeOnVueInstance (context, cb) {
- const _this = this
- return {
- 'NewExpression:exit' (node) {
-
- if (!_this.isVueInstance(node)) return
- cb(node.arguments[0])
- }
- }
- },
-
- executeOnVueComponent (context, cb) {
- const filePath = context.getFilename()
- const sourceCode = context.getSourceCode()
- const _this = this
- const componentComments = sourceCode.getAllComments().filter(comment => /@vue\/component/g.test(comment.value))
- const foundNodes = []
- const isDuplicateNode = (node) => {
- if (foundNodes.some(el => el.loc.start.line === node.loc.start.line)) return true
- foundNodes.push(node)
- return false
- }
- return {
- 'ObjectExpression:exit' (node) {
- if (!componentComments.some(el => el.loc.end.line === node.loc.start.line - 1) || isDuplicateNode(node)) return
- cb(node)
- },
- 'ExportDefaultDeclaration:exit' (node) {
-
- if (!_this.isVueComponentFile(node, filePath) || isDuplicateNode(node.declaration)) return
- cb(node.declaration)
- },
- 'CallExpression:exit' (node) {
-
- if (!_this.isVueComponent(node) || isDuplicateNode(node.arguments.slice(-1)[0])) return
- cb(node.arguments.slice(-1)[0])
- }
- }
- },
-
- executeOnCallVueComponent (_context, cb) {
- return {
- "CallExpression > MemberExpression > Identifier[name='component']": (node) => {
- const callExpr = node.parent.parent
- const callee = callExpr.callee
- if (callee.type === 'MemberExpression') {
- const calleeObject = this.unwrapTypes(callee.object)
- if (calleeObject.type === 'Identifier' &&
- calleeObject.name === 'Vue' &&
- callee.property === node &&
- callExpr.arguments.length >= 1) {
- cb(callExpr)
- }
- }
- }
- }
- },
-
- * iterateProperties (node, groups) {
- const nodes = node.properties.filter(p => p.type === 'Property' && groups.has(this.getStaticPropertyName(p.key)))
- for (const item of nodes) {
- const name = this.getStaticPropertyName(item.key)
- if (!name) continue
- if (item.value.type === 'ArrayExpression') {
- yield * this.iterateArrayExpression(item.value, name)
- } else if (item.value.type === 'ObjectExpression') {
- yield * this.iterateObjectExpression(item.value, name)
- } else if (item.value.type === 'FunctionExpression') {
- yield * this.iterateFunctionExpression(item.value, name)
- }
- }
- },
-
- * iterateArrayExpression (node, groupName) {
- assert(node.type === 'ArrayExpression')
- for (const item of node.elements) {
- const name = this.getStaticPropertyName(item)
- if (name) {
- const obj = { name, groupName, node: item }
- yield obj
- }
- }
- },
-
- * iterateObjectExpression (node, groupName) {
- assert(node.type === 'ObjectExpression')
- for (const item of node.properties) {
- const name = this.getStaticPropertyName(item)
- if (name) {
- const obj = { name, groupName, node: item.key }
- yield obj
- }
- }
- },
-
- * iterateFunctionExpression (node, groupName) {
- assert(node.type === 'FunctionExpression')
- if (node.body.type === 'BlockStatement') {
- for (const item of node.body.body) {
- if (item.type === 'ReturnStatement' && item.argument && item.argument.type === 'ObjectExpression') {
- yield * this.iterateObjectExpression(item.argument, groupName)
- }
- }
- }
- },
-
- executeOnFunctionsWithoutReturn (treatUndefinedAsUnspecified, cb) {
- let funcInfo = {
- funcInfo: null,
- codePath: null,
- hasReturn: false,
- hasReturnValue: false,
- node: null
- }
- function isReachable (segment) {
- return segment.reachable
- }
- function isValidReturn () {
- if (funcInfo.codePath.currentSegments.some(isReachable)) {
- return false
- }
- return !treatUndefinedAsUnspecified || funcInfo.hasReturnValue
- }
- return {
- onCodePathStart (codePath, node) {
- funcInfo = {
- codePath,
- funcInfo: funcInfo,
- hasReturn: false,
- hasReturnValue: false,
- node
- }
- },
- onCodePathEnd () {
- funcInfo = funcInfo.funcInfo
- },
- ReturnStatement (node) {
- funcInfo.hasReturn = true
- funcInfo.hasReturnValue = Boolean(node.argument)
- },
- 'ArrowFunctionExpression:exit' (node) {
- if (!isValidReturn() && !node.expression) {
- cb(funcInfo.node)
- }
- },
- 'FunctionExpression:exit' (node) {
- if (!isValidReturn()) {
- cb(funcInfo.node)
- }
- }
- }
- },
-
- isSingleLine (node) {
- return node.loc.start.line === node.loc.end.line
- },
-
- hasInvalidEOF (node) {
- const body = node.templateBody
- if (body == null || body.errors == null) {
- return
- }
- return body.errors.some(error => typeof error.code === 'string' && error.code.startsWith('eof-'))
- },
-
- parseMemberOrCallExpression (node) {
- const parsedCallee = []
- let n = node
- let isFunc
- while (n.type === 'MemberExpression' || n.type === 'CallExpression') {
- if (n.type === 'CallExpression') {
- n = n.callee
- isFunc = true
- } else {
- if (n.computed) {
- parsedCallee.push('[]' + (isFunc ? '()' : ''))
- } else if (n.property.type === 'Identifier') {
- parsedCallee.push(n.property.name + (isFunc ? '()' : ''))
- }
- isFunc = false
- n = n.object
- }
- }
- if (n.type === 'Identifier') {
- parsedCallee.push(n.name)
- }
- if (n.type === 'ThisExpression') {
- parsedCallee.push('this')
- }
- return parsedCallee.reverse().join('.').replace(/\.\[/g, '[')
- },
-
- unwrapTypes (node) {
- return node.type === 'TSAsExpression' ? node.expression : node
- }
- }
|