Skip to content

Commit

Permalink
test: add more cases for new options
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinjiang committed Feb 1, 2024
1 parent 03cf058 commit 300d9ea
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 115 deletions.
4 changes: 2 additions & 2 deletions src/parser/messages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const BRACKET_NOT_CLOSED = '括号未闭合'
export const BRACKET_NOT_OPEN = '括号未匹配'

export const QUOTE_NOT_CLOSED = '引号未闭合'
export const QUOTE_NOT_OPEN = '引号未匹配'
export const QUOTATION_NOT_CLOSED = '引号未闭合'
export const QUOTATION_NOT_OPEN = '引号未匹配'
10 changes: 5 additions & 5 deletions src/parser/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { checkCharType } from './char'
import {
BRACKET_NOT_CLOSED,
BRACKET_NOT_OPEN,
QUOTE_NOT_CLOSED,
QUOTE_NOT_OPEN
QUOTATION_NOT_CLOSED,
QUOTATION_NOT_OPEN
} from './messages'
import {
CharType,
Expand Down Expand Up @@ -80,7 +80,7 @@ export const handlePunctuation = (
} else if (QUOTATION_CHAR_SET.right.indexOf(char) >= 0) {
if (!status.lastGroup || !status.lastGroup.startValue) {
addUnmatchedToken(status, i, char)
addError(status, i, QUOTE_NOT_OPEN)
addError(status, i, QUOTATION_NOT_OPEN)
} else {
finalizeCurrentGroup(status, i, char)
}
Expand Down Expand Up @@ -492,14 +492,14 @@ export const handleErrors = (status: ParseStatus): void => {
// record an error if the last group not fully resolved
const lastGroup = status.lastGroup
if (lastGroup && lastGroup.startValue && !lastGroup.endValue) {
addError(status, lastGroup.startIndex, QUOTE_NOT_CLOSED)
addError(status, lastGroup.startIndex, QUOTATION_NOT_CLOSED)
}

// record an error if `groupStack` not fully resolved
if (status.groupStack.length > 0) {
status.groupStack.forEach((group) => {
if (group !== lastGroup && group.startValue && !group.endValue) {
addError(status, group.startIndex, QUOTE_NOT_CLOSED)
addError(status, group.startIndex, QUOTATION_NOT_CLOSED)
}
})
}
Expand Down
6 changes: 3 additions & 3 deletions src/rules/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export const CONTENT_NOSPACE_FULL_WIDTH = '此处全角内容之间不需要空
export const CONTENT_SPACE_MIXED_WIDTH = '此处中英文内容之间需要一个空格'
export const CONTENT_NOSPACE_MIXED_WIDTH = '此处中英文内容之间需要一个空格'

export const QUOTE_NOSPACE_INSIDE = '此处引号的内部不需要空格'
export const QUOTE_NOSPACE_OUTSIDE = '此处引号的外部不需要空格'
export const QUOTE_SPACE_OUTSIDE = '此处引号的外部需要一个空格'
export const QUOTATION_NOSPACE_INSIDE = '此处引号的内部不需要空格'
export const QUOTATION_NOSPACE_OUTSIDE = '此处引号的外部不需要空格'
export const QUOTATION_SPACE_OUTSIDE = '此处引号的外部需要一个空格'

export const TRIM_SPACE = '此处需要去除外部空格'
20 changes: 10 additions & 10 deletions src/rules/space-bracket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* - non-left-bracket x right-bracket
* - spaceOutsideHalfBracket:
* - right-half-bracket x left-half-bracket
* - right-half-bracket x content/left-quote/code
* - content/right-quote/code x left-half-bracket
* - right-half-bracket x content/left-quotation/code
* - content/right-quotation/code x left-half-bracket
* - noSpaceOutsideFullBracket:
* - right-full-bracket x left-full-bracket
* - right-full-bracket x content/left-quote/code
* - content/right-quote/code x left-full-bracket
* - right-full-bracket x content/left-quotation/code
* - content/right-quotation/code x left-full-bracket
*/

import {
Expand Down Expand Up @@ -187,8 +187,8 @@ const generateHandler = (options: Options): Handler => {
}
}

// 2.2 content/right-quote/code x left-bracket
// 2.3 right-racket x content/left-quote/code
// 2.2 content/right-quotation/code x left-bracket
// 2.3 right-racket x content/left-quotation/code
if (token.markSide === MarkSideType.LEFT) {
if (
contentTokenBefore &&
Expand All @@ -197,8 +197,8 @@ const generateHandler = (options: Options): Handler => {
contentTokenBefore.type === HyperTokenType.CODE_CONTENT)
) {
if (beforeSpaceHost) {
// 2.2.1 content/right-quote/code x left-full-bracket
// 2.2.2 content/right-quote/code x left-half-bracket
// 2.2.1 content/right-quotation/code x left-full-bracket
// 2.2.2 content/right-quotation/code x left-half-bracket
if (
fullWidth ||
(contentTokenBefore.type === GroupTokenType.GROUP &&
Expand Down Expand Up @@ -229,8 +229,8 @@ const generateHandler = (options: Options): Handler => {
contentTokenAfter.type === HyperTokenType.CODE_CONTENT)
) {
if (afterSpaceHost) {
// 2.3.1 right-full-bracket x content/left-quote/code
// 2.4.2 right-half-bracket x content/left-quote/code
// 2.3.1 right-full-bracket x content/left-quotation/code
// 2.4.2 right-half-bracket x content/left-quotation/code
if (
fullWidth ||
(contentTokenAfter.type === GroupTokenType.GROUP &&
Expand Down
6 changes: 3 additions & 3 deletions src/rules/space-punctuation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
*
* Details:
* - noSpaceBeforePunctuation:
* content/right-quote/right-bracket/code x punctuation
* content/right-quotation/right-bracket/code x punctuation
* - spaceAfterHalfWidthPunctuation:
* half x content/left-quote/left-bracket/code
* half x content/left-quotation/left-bracket/code
* - noSpaceAfterFullWidthPunctuation:
* full x content/left-quote/left-bracket/code
* full x content/left-quotation/left-bracket/code
*
* - skip half-width punctuations between half-width content without space
* - skip successive multiple half-width punctuations
Expand Down
126 changes: 63 additions & 63 deletions src/rules/space-quotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
* This rule is checking spaces besides quotations.
*
* Options
* - noSpaceInsideQuote: boolean | undefined
* - spaceOutsideHalfQuote: boolean | undefined
* - noSpaceOutsideFullQuote: boolean | undefined
* - noSpaceInsideQuotation: boolean | undefined
* - spaceOutsideHalfwidthQuotation: boolean | undefined
* - noSpaceOutsideFullwidthQuotation: boolean | undefined
*
* Details:
* - noSpaceInsideQuote:
* - left-quote x right-quote
* - content/punctuation/right-quote/right-bracket/code/unknown/container x right-quote
* - left-quote x content/punctuation/left-quote/left-bracket/code/unknown/container
* - spaceOutsideHalfQuote:
* - right-half-quote x left-half-quote
* - content/code x left-half-quote
* - right-half-quote x content/code
* - noSpaceOutsideFullQuote:
* - right-full-quote x left-full-quote
* - content/code x left-full-quote
* - right-full-quote x content/code
* - noSpaceInsideQuotation:
* - left-quotation x right-quotation
* - content/punctuation/right-quotation/right-bracket/code/unknown/container x right-quotation
* - left-quotation x content/punctuation/left-quotation/left-bracket/code/unknown/container
* - spaceOutsideHalfwidthQuotation:
* - right-half-quotation x left-half-quotation
* - content/code x left-half-quotation
* - right-half-quotation x content/code
* - noSpaceOutsideFullwidthQuotation:
* - right-full-quotation x left-full-quotation
* - content/code x left-full-quotation
* - right-full-quotation x content/code
*/

import {
Expand All @@ -42,19 +42,19 @@ import {
Options
} from './util'
import {
QUOTE_NOSPACE_INSIDE,
QUOTE_NOSPACE_OUTSIDE,
QUOTE_SPACE_OUTSIDE
QUOTATION_NOSPACE_INSIDE,
QUOTATION_NOSPACE_OUTSIDE,
QUOTATION_SPACE_OUTSIDE
} from './messages'

const isFullWidth = (char: string, adjusted: string): boolean => {
return isFullwidthPair(char) && adjusted.indexOf(char) === -1
}

const generateHandler = (options: Options): Handler => {
const noSpaceInsideQuoteOption = options.noSpaceInsideQuotation
const spaceOutsideHalfQuoteOption = options.spaceOutsideHalfwidthQuotation
const noSpaceOutsideFullQuoteOption = options.noSpaceOutsideFullwidthQuotation
const noSpaceInsideQuotationOption = options.noSpaceInsideQuotation
const spaceOutsideHalfQuotationOption = options.spaceOutsideHalfwidthQuotation
const noSpaceOutsideFullQuotationOption = options.noSpaceOutsideFullwidthQuotation
const adjustedFullWidthOption = options.adjustedFullwidthPunctuation || ''

return (token: MutableToken, _: number, group: MutableGroupToken) => {
Expand All @@ -64,34 +64,34 @@ const generateHandler = (options: Options): Handler => {
}

// 1. no space inside quotation
if (noSpaceInsideQuoteOption) {
// 1.1 left-quote x content/punctuation/left-quote/left-bracket/code/unknown/container
if (noSpaceInsideQuotationOption) {
// 1.1 left-quotation x content/punctuation/left-quotation/left-bracket/code/unknown/container
const firstInsdieToken = token[0]
if (
firstInsdieToken &&
firstInsdieToken.markSide !== MarkSideType.RIGHT
) {
checkInnerSpaceBefore(token, '', QUOTE_NOSPACE_INSIDE)
checkInnerSpaceBefore(token, '', QUOTATION_NOSPACE_INSIDE)
}

// 1.2 content/punctuation/right-quote/right-bracket/code/unknown/container x right-quote
// 1.2 content/punctuation/right-quotation/right-bracket/code/unknown/container x right-quotation
const lastInsideToken = token[token.length - 1]
if (lastInsideToken && lastInsideToken.markSide !== MarkSideType.LEFT) {
checkSpaceAfter(lastInsideToken, '', QUOTE_NOSPACE_INSIDE)
checkSpaceAfter(lastInsideToken, '', QUOTATION_NOSPACE_INSIDE)
}

// 1.3 left-quote x right-quote
// 1.3 left-quotation x right-quotation
if (!firstInsdieToken) {
checkInnerSpaceBefore(token, '', QUOTE_NOSPACE_INSIDE)
checkInnerSpaceBefore(token, '', QUOTATION_NOSPACE_INSIDE)
}
}

// 2. space outside half/full quotation
if (
typeof spaceOutsideHalfQuoteOption !== 'undefined' ||
noSpaceOutsideFullQuoteOption
typeof spaceOutsideHalfQuotationOption !== 'undefined' ||
noSpaceOutsideFullQuotationOption
) {
// 2.1 right-quote x left-quote
// 2.1 right-quotation x left-quotation
const contentTokenAfter = findNonCodeVisibleTokenAfter(group, token)
if (
contentTokenAfter &&
Expand All @@ -109,25 +109,25 @@ const generateHandler = (options: Options): Handler => {
contentTokenAfter.modifiedStartValue,
adjustedFullWidthOption
)
// 2.1.1 right-full-quote x left-full-quote
// 2.1.2 right-half-quote x left-half-quote
// 2.1.1 right-full-quotation x left-full-quotation
// 2.1.2 right-half-quotation x left-half-quotation
if (fullWidth) {
if (noSpaceOutsideFullQuoteOption) {
checkSpaceAfter(spaceHost, '', QUOTE_SPACE_OUTSIDE)
if (noSpaceOutsideFullQuotationOption) {
checkSpaceAfter(spaceHost, '', QUOTATION_SPACE_OUTSIDE)
}
} else {
if (typeof spaceOutsideHalfQuoteOption !== 'undefined') {
const spaceAfter = spaceOutsideHalfQuoteOption ? ' ' : ''
const message = spaceOutsideHalfQuoteOption
? QUOTE_SPACE_OUTSIDE
: QUOTE_NOSPACE_OUTSIDE
if (typeof spaceOutsideHalfQuotationOption !== 'undefined') {
const spaceAfter = spaceOutsideHalfQuotationOption ? ' ' : ''
const message = spaceOutsideHalfQuotationOption
? QUOTATION_SPACE_OUTSIDE
: QUOTATION_NOSPACE_OUTSIDE
checkSpaceAfter(spaceHost, spaceAfter, message)
}
}
}
}

// 2.2 content/code x left-quote
// 2.2 content/code x left-quotation
const contentTokenBefore = findNonCodeVisibleTokenBefore(group, token)
if (
contentTokenBefore &&
Expand All @@ -145,25 +145,25 @@ const generateHandler = (options: Options): Handler => {
adjustedFullWidthOption
)

// 2.2.1 content/code x left-full-quote
// 2.2.2 content/code x left-half-quote
// 2.2.1 content/code x left-full-quotation
// 2.2.2 content/code x left-half-quotation
if (fullWidth) {
if (noSpaceOutsideFullQuoteOption) {
checkSpaceAfter(spaceHost, '', QUOTE_NOSPACE_OUTSIDE)
if (noSpaceOutsideFullQuotationOption) {
checkSpaceAfter(spaceHost, '', QUOTATION_NOSPACE_OUTSIDE)
}
} else {
if (typeof spaceOutsideHalfQuoteOption !== 'undefined') {
const spaceAfter = spaceOutsideHalfQuoteOption ? ' ' : ''
const message = spaceOutsideHalfQuoteOption
? QUOTE_SPACE_OUTSIDE
: QUOTE_NOSPACE_OUTSIDE
if (typeof spaceOutsideHalfQuotationOption !== 'undefined') {
const spaceAfter = spaceOutsideHalfQuotationOption ? ' ' : ''
const message = spaceOutsideHalfQuotationOption
? QUOTATION_SPACE_OUTSIDE
: QUOTATION_NOSPACE_OUTSIDE
checkSpaceAfter(spaceHost, spaceAfter, message)
}
}
}
}

// 2.3 right-quote x content/code
// 2.3 right-quotation x content/code
if (
contentTokenAfter &&
(isLetterType(contentTokenAfter.type) ||
Expand All @@ -180,18 +180,18 @@ const generateHandler = (options: Options): Handler => {
adjustedFullWidthOption
)

// 2.3.1 right-full-quote x content/code
// 2.3.2 right-half-quote x content/code
// 2.3.1 right-full-quotation x content/code
// 2.3.2 right-half-quotation x content/code
if (fullWidth) {
if (noSpaceOutsideFullQuoteOption) {
checkSpaceAfter(spaceHost, '', QUOTE_NOSPACE_OUTSIDE)
if (noSpaceOutsideFullQuotationOption) {
checkSpaceAfter(spaceHost, '', QUOTATION_NOSPACE_OUTSIDE)
}
} else {
if (typeof spaceOutsideHalfQuoteOption !== 'undefined') {
const spaceAfter = spaceOutsideHalfQuoteOption ? ' ' : ''
const message = spaceOutsideHalfQuoteOption
? QUOTE_SPACE_OUTSIDE
: QUOTE_NOSPACE_OUTSIDE
if (typeof spaceOutsideHalfQuotationOption !== 'undefined') {
const spaceAfter = spaceOutsideHalfQuotationOption ? ' ' : ''
const message = spaceOutsideHalfQuotationOption
? QUOTATION_SPACE_OUTSIDE
: QUOTATION_NOSPACE_OUTSIDE
checkSpaceAfter(spaceHost, spaceAfter, message)
}
}
Expand All @@ -202,9 +202,9 @@ const generateHandler = (options: Options): Handler => {
}

export const defaultConfig: Options = {
spaceOutsideHalfQuote: true,
noSpaceInsideQuote: true,
noSpaceOutsideFullQuote: true
spaceOutsideHalfwidthQuotation: true,
noSpaceInsideQuotation: true,
noSpaceOutsideFullwidthQuotation: true
}

export default generateHandler
20 changes: 10 additions & 10 deletions test/example-units-fixed.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
mark-raw:a `b` c `d` e `f` g `h` i
mark-raw:a `b` c `d` e `f` g `h` i 中文

mark-type:a__[b](x)__c __[d](y)__ e
mark-type:a__[b](x)__c __[d](y)__ e 中文

unify-punctuation:中文,中文 (中文) 中文 ‘中文’ 中文 “中文” 中文 (中文)(中文) 中文 (中文)。

case-abbr:Pure JavaScript (a.k.a. Vanilla)
case-abbr:Pure JavaScript (a.k.a. Vanilla) 中文

case-html-entity:中文< & >中文

space-punctuation:中文。中文 (中文) 中文。中文。中文 (中文) 中文。

case-math-exp:1+1=2 1 + 1 = 2 1 + 2=3 2020/01/01 2020-01-01 vue-custom-element 100% a/b Chrome 53+
case-math-exp:1+1=2 1 + 1 = 2 1 + 2=3 2020/01/01 2020-01-01 vue-custom-element 100% a/b Chrome 53+ 中文

case-backslash:a \# b 中文\# __中文__ \# 中文 __\#__ __中文__\#中文__\#__

space-brackets:(x)a(b)c (d) e (f) g (h) i (j) k (l) m __(a)__ b (__c__) d(e)
space-brackets:(x)a(b)c (d) e (f) g (h) i (j) k (l) m __(a)__ b (__c__) d(e) 中文

space-quotes:a “hello world” b
space-quotations:a “hello world” b 中文

case-traditional:a “b ‘c’ d” e
case-traditional:a “b ‘c’ d” e 中文

case-datetime:2020/01/02 01:20:30 中文 2020 年1月1日0天0号0时0分00秒

case-ellipsis:中文...中文...a...b... 中文 ... 中文 ... a ... b ...

case-raw:`AC`/`DC`
case-raw:`AC`/`DC` 中文

case-linebreak:
this is
a
multiline
text
text 中文

3 minite(s) left
3 minite(s) left 中文

😉  中文
Loading

0 comments on commit 300d9ea

Please sign in to comment.