123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560 |
- 'use strict'
- const utils = require('../utils')
- const OPTIONS_SCHEMA = {
- type: 'object',
- properties: {
- code: {
- type: 'integer',
- minimum: 0
- },
- template: {
- type: 'integer',
- minimum: 0
- },
- comments: {
- type: 'integer',
- minimum: 0
- },
- tabWidth: {
- type: 'integer',
- minimum: 0
- },
- ignorePattern: {
- type: 'string'
- },
- ignoreComments: {
- type: 'boolean'
- },
- ignoreTrailingComments: {
- type: 'boolean'
- },
- ignoreUrls: {
- type: 'boolean'
- },
- ignoreStrings: {
- type: 'boolean'
- },
- ignoreTemplateLiterals: {
- type: 'boolean'
- },
- ignoreRegExpLiterals: {
- type: 'boolean'
- },
- ignoreHTMLAttributeValues: {
- type: 'boolean'
- },
- ignoreHTMLTextContents: {
- type: 'boolean'
- }
- },
- additionalProperties: false
- }
- const OPTIONS_OR_INTEGER_SCHEMA = {
- anyOf: [
- OPTIONS_SCHEMA,
- {
- type: 'integer',
- minimum: 0
- }
- ]
- }
- function computeLineLength(line, tabWidth) {
- let extraCharacterCount = 0
- const re = /\t/gu
- let ret
- while ((ret = re.exec(line))) {
- const offset = ret.index
- const totalOffset = offset + extraCharacterCount
- const previousTabStopOffset = tabWidth ? totalOffset % tabWidth : 0
- const spaceCount = tabWidth - previousTabStopOffset
- extraCharacterCount += spaceCount - 1
- }
- return Array.from(line).length + extraCharacterCount
- }
- function isTrailingComment(line, lineNumber, comment) {
- return Boolean(
- comment &&
- comment.loc.start.line === lineNumber &&
- lineNumber <= comment.loc.end.line &&
- (comment.loc.end.line > lineNumber ||
- comment.loc.end.column === line.length)
- )
- }
- function isFullLineComment(line, lineNumber, comment) {
- if (!comment) {
- return false
- }
- const start = comment.loc.start
- const end = comment.loc.end
- const isFirstTokenOnLine = !line.slice(0, comment.loc.start.column).trim()
- return (
- comment &&
- (start.line < lineNumber ||
- (start.line === lineNumber && isFirstTokenOnLine)) &&
- (end.line > lineNumber ||
- (end.line === lineNumber && end.column === line.length))
- )
- }
- function stripTrailingComment(line, comment) {
-
- return line.slice(0, comment.loc.start.column).replace(/\s+$/u, '')
- }
- function ensureArrayAndPush(object, key, value) {
- if (!Array.isArray(object[key])) {
- object[key] = []
- }
- object[key].push(value)
- }
- function groupByLineNumber(acc, node) {
- for (let i = node.loc.start.line; i <= node.loc.end.line; ++i) {
- ensureArrayAndPush(acc, i, node)
- }
- return acc
- }
- module.exports = {
- meta: {
- type: 'layout',
- docs: {
- description: 'enforce a maximum line length in `.vue` files',
- categories: undefined,
- url: 'https://eslint.vuejs.org/rules/max-len.html',
- extensionRule: true,
- coreRuleUrl: 'https://eslint.org/docs/rules/max-len'
- },
- schema: [
- OPTIONS_OR_INTEGER_SCHEMA,
- OPTIONS_OR_INTEGER_SCHEMA,
- OPTIONS_SCHEMA
- ],
- messages: {
- max: 'This line has a length of {{lineLength}}. Maximum allowed is {{maxLength}}.',
- maxComment:
- 'This line has a comment length of {{lineLength}}. Maximum allowed is {{maxCommentLength}}.'
- }
- },
-
- create(context) {
-
- const URL_REGEXP = /[^:/?#]:\/\/[^?#]/u
- const sourceCode = context.getSourceCode()
-
- const tokens = []
-
- const comments = []
-
- const htmlAttributeValues = []
-
- const options = Object.assign(
- {},
- context.options[context.options.length - 1]
- )
-
- if (typeof context.options[0] === 'number') {
- options.code = context.options[0]
- }
-
- if (typeof context.options[1] === 'number') {
- options.tabWidth = context.options[1]
- }
-
- const scriptMaxLength = typeof options.code === 'number' ? options.code : 80
-
- const tabWidth = typeof options.tabWidth === 'number' ? options.tabWidth : 2
-
- const templateMaxLength =
- typeof options.template === 'number' ? options.template : scriptMaxLength
- const ignoreComments = !!options.ignoreComments
- const ignoreStrings = !!options.ignoreStrings
- const ignoreTemplateLiterals = !!options.ignoreTemplateLiterals
- const ignoreRegExpLiterals = !!options.ignoreRegExpLiterals
- const ignoreTrailingComments =
- !!options.ignoreTrailingComments || !!options.ignoreComments
- const ignoreUrls = !!options.ignoreUrls
- const ignoreHTMLAttributeValues = !!options.ignoreHTMLAttributeValues
- const ignoreHTMLTextContents = !!options.ignoreHTMLTextContents
-
- const maxCommentLength = options.comments
-
- let ignorePattern = options.ignorePattern || null
- if (ignorePattern) {
- ignorePattern = new RegExp(ignorePattern, 'u')
- }
-
-
-
-
- function getAllStrings() {
- return tokens.filter(
- (token) =>
- token.type === 'String' ||
- (token.type === 'JSXText' &&
- sourceCode.getNodeByRangeIndex(token.range[0] - 1).type ===
- 'JSXAttribute')
- )
- }
-
- function getAllTemplateLiterals() {
- return tokens.filter((token) => token.type === 'Template')
- }
-
- function getAllRegExpLiterals() {
- return tokens.filter((token) => token.type === 'RegularExpression')
- }
-
- function getAllHTMLTextContents() {
- return tokens.filter((token) => token.type === 'HTMLText')
- }
-
- function checkProgramForMaxLength(node) {
- const programNode = node
- const templateBody = node.templateBody
-
- const scriptTokens = sourceCode.ast.tokens
- const scriptComments = sourceCode.getAllComments()
- if (context.parserServices.getTemplateBodyTokenStore && templateBody) {
- const tokenStore = context.parserServices.getTemplateBodyTokenStore()
- const templateTokens = tokenStore.getTokens(templateBody, {
- includeComments: true
- })
- if (templateBody.range[0] < programNode.range[0]) {
- tokens.push(...templateTokens, ...scriptTokens)
- } else {
- tokens.push(...scriptTokens, ...templateTokens)
- }
- } else {
- tokens.push(...scriptTokens)
- }
- if (ignoreComments || maxCommentLength || ignoreTrailingComments) {
-
- if (templateBody) {
- if (templateBody.range[0] < programNode.range[0]) {
- comments.push(...templateBody.comments, ...scriptComments)
- } else {
- comments.push(...scriptComments, ...templateBody.comments)
- }
- } else {
- comments.push(...scriptComments)
- }
- }
-
- let scriptLinesRange
- if (scriptTokens.length) {
- if (scriptComments.length) {
- scriptLinesRange = [
- Math.min(
- scriptTokens[0].loc.start.line,
- scriptComments[0].loc.start.line
- ),
- Math.max(
- scriptTokens[scriptTokens.length - 1].loc.end.line,
- scriptComments[scriptComments.length - 1].loc.end.line
- )
- ]
- } else {
- scriptLinesRange = [
- scriptTokens[0].loc.start.line,
- scriptTokens[scriptTokens.length - 1].loc.end.line
- ]
- }
- } else if (scriptComments.length) {
- scriptLinesRange = [
- scriptComments[0].loc.start.line,
- scriptComments[scriptComments.length - 1].loc.end.line
- ]
- }
- const templateLinesRange = templateBody && [
- templateBody.loc.start.line,
- templateBody.loc.end.line
- ]
-
- const lines = sourceCode.lines
- const strings = getAllStrings()
- const stringsByLine = strings.reduce(groupByLineNumber, {})
- const templateLiterals = getAllTemplateLiterals()
- const templateLiteralsByLine = templateLiterals.reduce(
- groupByLineNumber,
- {}
- )
- const regExpLiterals = getAllRegExpLiterals()
- const regExpLiteralsByLine = regExpLiterals.reduce(groupByLineNumber, {})
- const htmlAttributeValuesByLine = htmlAttributeValues.reduce(
- groupByLineNumber,
- {}
- )
- const htmlTextContents = getAllHTMLTextContents()
- const htmlTextContentsByLine = htmlTextContents.reduce(
- groupByLineNumber,
- {}
- )
- const commentsByLine = comments.reduce(groupByLineNumber, {})
- lines.forEach((line, i) => {
-
- const lineNumber = i + 1
- const inScript =
- scriptLinesRange &&
- scriptLinesRange[0] <= lineNumber &&
- lineNumber <= scriptLinesRange[1]
- const inTemplate =
- templateLinesRange &&
- templateLinesRange[0] <= lineNumber &&
- lineNumber <= templateLinesRange[1]
-
- if (!inScript && !inTemplate) {
-
- return
- }
- const maxLength =
- inScript && inTemplate
- ? Math.max(scriptMaxLength, templateMaxLength)
- : inScript
- ? scriptMaxLength
- : templateMaxLength
- if (
- (ignoreStrings && stringsByLine[lineNumber]) ||
- (ignoreTemplateLiterals && templateLiteralsByLine[lineNumber]) ||
- (ignoreRegExpLiterals && regExpLiteralsByLine[lineNumber]) ||
- (ignoreHTMLAttributeValues &&
- htmlAttributeValuesByLine[lineNumber]) ||
- (ignoreHTMLTextContents && htmlTextContentsByLine[lineNumber])
- ) {
-
- return
- }
-
- let lineIsComment = false
- let textToMeasure
-
- if (commentsByLine[lineNumber]) {
- const commentList = [...commentsByLine[lineNumber]]
- let comment = commentList.pop() || null
- if (isFullLineComment(line, lineNumber, comment)) {
- lineIsComment = true
- textToMeasure = line
- } else if (
- ignoreTrailingComments &&
- isTrailingComment(line, lineNumber, comment)
- ) {
- textToMeasure = stripTrailingComment(line, comment)
-
- comment = commentList.pop() || null
- while (isTrailingComment(textToMeasure, lineNumber, comment)) {
- textToMeasure = stripTrailingComment(textToMeasure, comment)
- }
- } else {
- textToMeasure = line
- }
- } else {
- textToMeasure = line
- }
- if (
- (ignorePattern && ignorePattern.test(textToMeasure)) ||
- (ignoreUrls && URL_REGEXP.test(textToMeasure))
- ) {
-
- return
- }
- const lineLength = computeLineLength(textToMeasure, tabWidth)
- const commentLengthApplies = lineIsComment && maxCommentLength
- if (lineIsComment && ignoreComments) {
- return
- }
- if (commentLengthApplies) {
- if (lineLength > maxCommentLength) {
- context.report({
- node,
- loc: { line: lineNumber, column: 0 },
- messageId: 'maxComment',
- data: {
- lineLength,
- maxCommentLength
- }
- })
- }
- } else if (lineLength > maxLength) {
- context.report({
- node,
- loc: { line: lineNumber, column: 0 },
- messageId: 'max',
- data: {
- lineLength,
- maxLength
- }
- })
- }
- })
- }
-
-
-
- return utils.compositingVisitors(
- utils.defineTemplateBodyVisitor(context, {
-
- 'VAttribute[directive=false] > VLiteral'(node) {
- htmlAttributeValues.push(node)
- }
- }),
- {
- 'Program:exit'(node) {
- checkProgramForMaxLength(node)
- }
- }
- )
- }
- }
|