Skip to content

Commit df4fffd

Browse files
authored
Refactor test cases (#222)
* Refactor test cases * sort messages * combine the messages that are output twice. * refactor for no-html-messages test * update * refactor test * remove unused source code
1 parent 8d094f9 commit df4fffd

13 files changed

+1180
-1538
lines changed

lib/rules/no-duplicate-keys-in-locale.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
SourceCode
1515
} from '../types'
1616
import { joinPath } from '../utils/key-path'
17+
import { getCwd } from '../utils/get-cwd'
1718
const debug = debugBuilder('eslint-plugin-vue-i18n:no-duplicate-keys-in-locale')
1819

1920
interface DictData {
@@ -29,9 +30,10 @@ interface PathStack {
2930
upper?: PathStack
3031
}
3132

32-
function getMessageFilepath(fullPath: string) {
33-
if (fullPath.startsWith(process.cwd())) {
34-
return fullPath.replace(process.cwd() + '/', './')
33+
function getMessageFilepath(fullPath: string, context: RuleContext) {
34+
const cwd = getCwd(context)
35+
if (fullPath.startsWith(cwd)) {
36+
return fullPath.replace(cwd + '/', './')
3537
}
3638
return fullPath
3739
}
@@ -121,23 +123,31 @@ function create(context: RuleContext): RuleListener {
121123
const keyPath = [...pathStack.keyPath, key]
122124
const keyPathStr = joinPath(...keyPath)
123125
const nextOtherDictionaries: DictData[] = []
126+
const reportFiles = []
124127
for (const value of keyOtherValues) {
125128
if (typeof value.value === 'string') {
126-
context.report({
127-
message: `duplicate key '${keyPathStr}' in '${
128-
pathStack.locale
129-
}'. "${getMessageFilepath(
130-
value.source.fullpath
131-
)}" has the same key`,
132-
loc: reportNode.loc
133-
})
129+
reportFiles.push(
130+
'"' + getMessageFilepath(value.source.fullpath, context) + '"'
131+
)
134132
} else {
135133
nextOtherDictionaries.push({
136134
dict: value.value,
137135
source: value.source
138136
})
139137
}
140138
}
139+
if (reportFiles.length) {
140+
reportFiles.sort()
141+
const last = reportFiles.pop()
142+
context.report({
143+
message: `duplicate key '${keyPathStr}' in '${pathStack.locale}'. ${
144+
reportFiles.length === 0
145+
? last
146+
: reportFiles.join(', ') + ', and ' + last
147+
} has the same key`,
148+
loc: reportNode.loc
149+
})
150+
}
141151

142152
pushKey(
143153
existsKeyNodes[pathStack.locale] ||

lib/rules/no-unused-keys.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type {
2121
CustomBlockVisitorFactory
2222
} from '../types'
2323
import { joinPath, parsePath } from '../utils/key-path'
24+
import { getCwd } from '../utils/get-cwd'
2425
const debug = debugBuilder('eslint-plugin-vue-i18n:no-unused-keys')
2526

2627
type UsedKeys = {
@@ -524,12 +525,13 @@ function create(context: RuleContext): RuleListener {
524525
debug(`ignore ${filename} in no-unused-keys`)
525526
return {}
526527
}
527-
const src = options.src || process.cwd()
528+
const src = options.src || getCwd(context)
528529
const extensions = options.extensions || ['.js', '.vue']
529530

530531
const usedLocaleMessageKeys = usedKeysCache.collectKeysFromFiles(
531532
[src],
532-
extensions
533+
extensions,
534+
context
533535
)
534536
const sourceCode = context.getSourceCode()
535537

lib/types/eslint.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export interface RuleContext {
3333
'vue-i18n'?: {
3434
localeDir?: SettingsVueI18nLocaleDir
3535
messageSyntaxVersion?: string
36+
cwd?: string // for test
3637
}
3738
}
3839
parserPath: string
@@ -44,6 +45,7 @@ export interface RuleContext {
4445
getSourceCode(): SourceCode
4546
getScope(): Scope
4647
report(descriptor: ReportDescriptor): void
48+
getCwd?: () => string
4749
}
4850

4951
interface ReportDescriptorOptionsBase {

lib/utils/collect-keys.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import { ResourceLoader } from './resource-loader'
1111
import { CacheLoader } from './cache-loader'
1212
import { defineCacheFunction } from './cache-function'
1313
import debugBuilder from 'debug'
14-
import type { VisitorKeys } from '../types'
14+
import type { RuleContext, VisitorKeys } from '../types'
1515
// @ts-expect-error -- ignore
1616
import { Legacy } from '@eslint/eslintrc'
17+
import { getCwd } from './get-cwd'
1718
const debug = debugBuilder('eslint-plugin-vue-i18n:collect-keys')
1819
const { CascadingConfigArrayFactory } = Legacy
1920

@@ -246,16 +247,16 @@ export function collectKeysFromAST(
246247

247248
class UsedKeysCache {
248249
private _targetFilesLoader: CacheLoader<
249-
[string[], string[], string],
250+
[string, string[], string[], string],
250251
string[]
251252
>
252253
private _collectKeyResourcesFromFiles: (
253254
fileNames: string[],
254255
cwd: string
255256
) => ResourceLoader<string[]>[]
256257
constructor() {
257-
this._targetFilesLoader = new CacheLoader((files, extensions) => {
258-
return listFilesToProcess(files, { extensions })
258+
this._targetFilesLoader = new CacheLoader((cwd, files, extensions) => {
259+
return listFilesToProcess(files, { cwd, extensions })
259260
.filter(f => !f.ignored && extensions.includes(extname(f.filename)))
260261
.map(f => f.filename)
261262
})
@@ -271,9 +272,13 @@ class UsedKeysCache {
271272
* @param {string[]} extensions
272273
* @returns {string[]}
273274
*/
274-
collectKeysFromFiles(files: string[], extensions: string[]) {
275+
collectKeysFromFiles(
276+
files: string[],
277+
extensions: string[],
278+
context: RuleContext
279+
) {
275280
const result = new Set<string>()
276-
for (const resource of this._getKeyResources(files, extensions)) {
281+
for (const resource of this._getKeyResources(context, files, extensions)) {
277282
for (const key of resource.getResource()) {
278283
result.add(key)
279284
}
@@ -284,9 +289,13 @@ class UsedKeysCache {
284289
/**
285290
* @returns {ResourceLoader[]}
286291
*/
287-
_getKeyResources(files: string[], extensions: string[]) {
288-
const cwd = process.cwd()
289-
const fileNames = this._targetFilesLoader.get(files, extensions, cwd)
292+
_getKeyResources(
293+
context: RuleContext,
294+
files: string[],
295+
extensions: string[]
296+
) {
297+
const cwd = getCwd(context)
298+
const fileNames = this._targetFilesLoader.get(cwd, files, extensions, cwd)
290299
return this._collectKeyResourcesFromFiles(fileNames, cwd)
291300
}
292301
}

lib/utils/get-cwd.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { RuleContext } from '../types'
2+
3+
export function getCwd(context: RuleContext): string {
4+
return (
5+
context.settings?.['vue-i18n']?.cwd ?? context.getCwd?.() ?? process.cwd()
6+
)
7+
}

lib/utils/index.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type {
2424
} from '../types'
2525
import * as jsoncESLintParser from 'jsonc-eslint-parser'
2626
import * as yamlESLintParser from 'yaml-eslint-parser'
27+
import { getCwd } from './get-cwd'
2728

2829
interface LocaleFiles {
2930
files: string[]
@@ -162,7 +163,10 @@ export function getLocaleMessages(context: RuleContext): LocaleMessages {
162163
return new LocaleMessages([
163164
...(getLocaleMessagesFromI18nBlocks(context, i18nBlocks) || []),
164165
...((localeDir &&
165-
localeDirLocaleMessagesCache.getLocaleMessagesFromLocaleDir(localeDir)) ||
166+
localeDirLocaleMessagesCache.getLocaleMessagesFromLocaleDir(
167+
context,
168+
localeDir
169+
)) ||
166170
[])
167171
])
168172
}
@@ -174,7 +178,9 @@ class LocaleDirLocaleMessagesCache {
174178
cwd: string
175179
) => FileLocaleMessage[]
176180
constructor() {
177-
this._targetFilesLoader = new CacheLoader(pattern => glob.sync(pattern))
181+
this._targetFilesLoader = new CacheLoader((pattern, cwd) =>
182+
glob.sync(pattern, { cwd })
183+
)
178184

179185
this._loadLocaleMessages = defineCacheFunction(
180186
(localeFilesList: LocaleFiles[], cwd) => {
@@ -186,8 +192,11 @@ class LocaleDirLocaleMessagesCache {
186192
* @param {SettingsVueI18nLocaleDir} localeDir
187193
* @returns {LocaleMessage[]}
188194
*/
189-
getLocaleMessagesFromLocaleDir(localeDir: SettingsVueI18nLocaleDir) {
190-
const cwd = process.cwd()
195+
getLocaleMessagesFromLocaleDir(
196+
context: RuleContext,
197+
localeDir: SettingsVueI18nLocaleDir
198+
) {
199+
const cwd = getCwd(context)
191200
let localeFilesList: LocaleFiles[]
192201
if (Array.isArray(localeDir)) {
193202
localeFilesList = localeDir.map(dir => this._toLocaleFiles(dir, cwd))

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
"release:prepare": "shipjs prepare",
114114
"release:trigger": "shipjs trigger",
115115
"test": "mocha --require ts-node/register \"./tests/**/*.ts\"",
116-
"test:debug": "mocha --require ts-node/register/transpile-only --inspect \"./tests/**/*.ts\"",
116+
"test:debug": "mocha --require ts-node/register/transpile-only \"./tests/**/*.ts\"",
117117
"test:coverage": "nyc mocha --require ts-node/register \"./tests/**/*.ts\" --timeout 60000",
118118
"test:integrations": "mocha ./tests-integrations/*.js --timeout 60000"
119119
}

0 commit comments

Comments
 (0)