Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Logic error with quotes order #12

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default function retextQuotes(options) {
expected = preferred === 'smart' ? '’' : "'"
} else {
const markers = preferred === 'smart' ? smart : straight
expected = markers[(stack.length + 1) % markers.length]
expected = markers[(stack.length - 1) % markers.length]

if (expected.length > 1) {
expected = expected.charAt(style.type === 'open' ? 0 : 1)
Expand Down
39 changes: 39 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ const nesting = [
'\'One "sentence". Two sentences.\''
].join('\n\n')

const multiNesting = [
'A sentence “with ‘multi «mutli “nested ‘quotes’”»’”.', // correct case when {smart: ['“”', '‘’', '«»']} (TODO: REMOVE THESE COMMENTS!!!)
'A sentence ‘with “multi «mutli ‘nested “quotes”’»”’.', // '“”' and '‘’' are swapped -> wrong
'A sentence «with “multi ‘mutli «nested “quotes”»’”».' // that's how it is currently evalutes to be right -> but it should be wrong
].join('\n\n')

const moreApostrophes = 'Isn’t it funny? It was acceptable in the ’80s'

const soManyOpenings = '“Open this, ‘Open that, “open here, ‘open there'
Expand Down Expand Up @@ -195,6 +201,39 @@ test('retextQuotes', async function (t) {
}
)

await t.test(
'should detect nesting with more than 2 defined quotes correctly',
async function () {
const file = await retext()
.use(retextQuotes, {smart: ['“”', '‘’', '«»']})
.process(multiNesting)

console.log(file.messages.map(String))

assert.deepEqual(file.messages.map(String), [
'3:12-3:13: Unexpected `‘` at this level of nesting, expected `“`',
'3:18-3:19: Unexpected `“` at this level of nesting, expected `‘`',
'3:32-3:33: Unexpected `‘` at this level of nesting, expected `“`',
'3:40-3:41: Unexpected `“` at this level of nesting, expected `‘`',
'3:47-3:48: Unexpected `”` at this level of nesting, expected `’`',
'3:48-3:49: Unexpected `’` at this level of nesting, expected `”`',
'3:50-3:51: Unexpected `”` at this level of nesting, expected `’`',
'3:51-3:52: Unexpected `’` at this level of nesting, expected `”`',

'5:12-5:13: Unexpected `«` at this level of nesting, expected `“`',
'5:18-5:19: Unexpected `“` at this level of nesting, expected `‘`',
'5:25-5:26: Unexpected `‘` at this level of nesting, expected `«`',
'5:32-5:33: Unexpected `«` at this level of nesting, expected `“`',
'5:40-5:41: Unexpected `“` at this level of nesting, expected `‘`',
'5:47-5:48: Unexpected `”` at this level of nesting, expected `’`',
'5:48-5:49: Unexpected `»` at this level of nesting, expected `”`',
'5:49-5:50: Unexpected `’` at this level of nesting, expected `»`',
'5:50-5:51: Unexpected `”` at this level of nesting, expected `’`',
'5:51-5:52: Unexpected `»` at this level of nesting, expected `”`'
])
}
)

await t.test('should deal with funky nesting', async function () {
const file = await retext().use(retextQuotes).process(soManyOpenings)

Expand Down
Loading