eslint-plugin-svelte
Advanced tools
Comparing version 3.0.0-next.17 to 3.0.0-next.18
@@ -17,3 +17,3 @@ import './rule-types.js'; | ||
name: "eslint-plugin-svelte"; | ||
version: "3.0.0-next.17"; | ||
version: "3.0.0-next.18"; | ||
}; | ||
@@ -20,0 +20,0 @@ export declare const processors: { |
export declare const name = "eslint-plugin-svelte"; | ||
export declare const version = "3.0.0-next.17"; | ||
export declare const version = "3.0.0-next.18"; |
@@ -5,2 +5,2 @@ // IMPORTANT! | ||
export const name = 'eslint-plugin-svelte'; | ||
export const version = '3.0.0-next.17'; | ||
export const version = '3.0.0-next.18'; |
import { createRule } from '../utils/index.js'; | ||
import { getSvelteVersion } from '../utils/svelte-context.js'; | ||
import { getFilename } from '../utils/compat.js'; | ||
const EXPECTED_PROP_NAMES = ['data', 'errors', 'form', 'snapshot']; | ||
function checkProp(node, context) { | ||
const EXPECTED_PROP_NAMES_SVELTE5 = [...EXPECTED_PROP_NAMES, 'children']; | ||
function checkProp(node, context, expectedPropNames) { | ||
if (node.id.type !== 'ObjectPattern') | ||
@@ -9,3 +12,3 @@ return; | ||
p.value.type === 'Identifier' && | ||
!EXPECTED_PROP_NAMES.includes(p.value.name)) { | ||
!expectedPropNames.includes(p.value.name)) { | ||
context.report({ | ||
@@ -39,2 +42,4 @@ node: p.value, | ||
let isScript = false; | ||
const isSvelte5 = getSvelteVersion(getFilename(context)) === '5'; | ||
const expectedPropNames = isSvelte5 ? EXPECTED_PROP_NAMES_SVELTE5 : EXPECTED_PROP_NAMES; | ||
return { | ||
@@ -58,3 +63,3 @@ // <script> | ||
if (node.id.type === 'Identifier') { | ||
if (!EXPECTED_PROP_NAMES.includes(node.id.name)) { | ||
if (!expectedPropNames.includes(node.id.name)) { | ||
context.report({ | ||
@@ -69,3 +74,3 @@ node, | ||
// export let { xxx, yyy } = zzz | ||
checkProp(node, context); | ||
checkProp(node, context, expectedPropNames); | ||
}, | ||
@@ -82,3 +87,3 @@ // Svelte5 | ||
} | ||
checkProp(node, context); | ||
checkProp(node, context, expectedPropNames); | ||
} | ||
@@ -85,0 +90,0 @@ }; |
@@ -25,2 +25,3 @@ import type { RuleContext } from '../types.js'; | ||
}; | ||
export declare function getSvelteVersion(filePath: string): SvelteContext['svelteVersion']; | ||
export declare function getSvelteContext(context: RuleContext): SvelteContext | null; |
@@ -5,2 +5,3 @@ import fs from 'fs'; | ||
import { getFilename, getSourceCode } from './compat.js'; | ||
import { createCache } from './cache.js'; | ||
const isRunInBrowser = !fs.readFileSync; | ||
@@ -53,13 +54,32 @@ function getSvelteFileType(filePath) { | ||
} | ||
function extractMajorVersion(version, recognizePrereleaseVersion) { | ||
if (recognizePrereleaseVersion) { | ||
const match = /^(?:\^|~)?(\d+\.0\.0-next)/.exec(version); | ||
if (match && match[1]) { | ||
return match[1]; | ||
} | ||
} | ||
const match = /^(?:\^|~)?(\d+)\./.exec(version); | ||
if (match && match[1]) { | ||
return match[1]; | ||
} | ||
return null; | ||
} | ||
const svelteKitContextCache = createCache(); | ||
function getSvelteKitContext(context) { | ||
const filePath = getFilename(context); | ||
const cached = svelteKitContextCache.get(filePath); | ||
if (cached) | ||
return cached; | ||
const svelteKitVersion = getSvelteKitVersion(filePath); | ||
if (svelteKitVersion == null) { | ||
return { | ||
const result = { | ||
svelteKitFileType: null, | ||
svelteKitVersion: null | ||
}; | ||
svelteKitContextCache.set(filePath, result); | ||
return result; | ||
} | ||
if (isRunInBrowser) { | ||
return { | ||
const result = { | ||
svelteKitVersion, | ||
@@ -69,2 +89,4 @@ // Judge by only file path if it runs in browser. | ||
}; | ||
svelteKitContextCache.set(filePath, result); | ||
return result; | ||
} | ||
@@ -75,16 +97,26 @@ const routes = (context.settings?.svelte?.kit?.files?.routes ?? | ||
if (!filePath.startsWith(path.join(projectRootDir, routes))) { | ||
return { | ||
const result = { | ||
svelteKitVersion, | ||
svelteKitFileType: null | ||
}; | ||
svelteKitContextCache.set(filePath, result); | ||
return result; | ||
} | ||
return { | ||
const result = { | ||
svelteKitVersion, | ||
svelteKitFileType: getSvelteKitFileTypeFromFilePath(filePath) | ||
}; | ||
svelteKitContextCache.set(filePath, result); | ||
return result; | ||
} | ||
function getSvelteVersion(filePath) { | ||
const svelteVersionCache = createCache(); | ||
export function getSvelteVersion(filePath) { | ||
const cached = svelteVersionCache.get(filePath); | ||
if (cached) | ||
return cached; | ||
// Hack: if it runs in browser, it regards as Svelte project. | ||
if (isRunInBrowser) | ||
if (isRunInBrowser) { | ||
svelteVersionCache.set(filePath, '5'); | ||
return '5'; | ||
} | ||
try { | ||
@@ -99,4 +131,6 @@ const packageJsons = getPackageJsons(filePath); | ||
if (major === '3' || major === '4') { | ||
svelteVersionCache.set(filePath, '3/4'); | ||
return '3/4'; | ||
} | ||
svelteVersionCache.set(filePath, major); | ||
return major; | ||
@@ -108,4 +142,6 @@ } | ||
} | ||
svelteVersionCache.set(filePath, null); | ||
return null; | ||
} | ||
const svelteKitVersionCache = createCache(); | ||
/** | ||
@@ -120,5 +156,10 @@ * Check givin file is under SvelteKit project. | ||
function getSvelteKitVersion(filePath) { | ||
const cached = svelteKitVersionCache.get(filePath); | ||
if (cached) | ||
return cached; | ||
// Hack: if it runs in browser, it regards as SvelteKit project. | ||
if (isRunInBrowser) | ||
if (isRunInBrowser) { | ||
svelteKitVersionCache.set(filePath, '2'); | ||
return '2'; | ||
} | ||
try { | ||
@@ -131,2 +172,3 @@ const packageJsons = getPackageJsons(filePath); | ||
// So always it returns 2 if it runs on the package. | ||
svelteKitVersionCache.set(filePath, '2'); | ||
return '2'; | ||
@@ -138,5 +180,8 @@ } | ||
if (typeof version !== 'string') { | ||
svelteKitVersionCache.set(filePath, null); | ||
return null; | ||
} | ||
return extractMajorVersion(version, true); | ||
const major = extractMajorVersion(version, true); | ||
svelteKitVersionCache.set(filePath, major); | ||
return major; | ||
} | ||
@@ -147,17 +192,6 @@ } | ||
} | ||
svelteKitVersionCache.set(filePath, null); | ||
return null; | ||
} | ||
function extractMajorVersion(version, recognizePrereleaseVersion) { | ||
if (recognizePrereleaseVersion) { | ||
const match = /^(?:\^|~)?(\d+\.0\.0-next)/.exec(version); | ||
if (match && match[1]) { | ||
return match[1]; | ||
} | ||
} | ||
const match = /^(?:\^|~)?(\d+)\./.exec(version); | ||
if (match && match[1]) { | ||
return match[1]; | ||
} | ||
return null; | ||
} | ||
const projectRootDirCache = createCache(); | ||
/** | ||
@@ -171,11 +205,20 @@ * Gets a project root folder path. | ||
return null; | ||
const cached = projectRootDirCache.get(filePath); | ||
if (cached) | ||
return cached; | ||
const packageJsons = getPackageJsons(filePath); | ||
if (packageJsons.length === 0) { | ||
projectRootDirCache.set(filePath, null); | ||
return null; | ||
} | ||
const packageJsonFilePath = packageJsons[0].filePath; | ||
if (!packageJsonFilePath) | ||
if (!packageJsonFilePath) { | ||
projectRootDirCache.set(filePath, null); | ||
return null; | ||
return path.dirname(path.resolve(packageJsonFilePath)); | ||
} | ||
const projectRootDir = path.dirname(path.resolve(packageJsonFilePath)); | ||
projectRootDirCache.set(filePath, projectRootDir); | ||
return projectRootDir; | ||
} | ||
const svelteContextCache = createCache(); | ||
export function getSvelteContext(context) { | ||
@@ -185,2 +228,5 @@ const { parserServices } = getSourceCode(context); | ||
const filePath = getFilename(context); | ||
const cached = svelteContextCache.get(filePath); | ||
if (cached) | ||
return cached; | ||
const svelteKitContext = getSvelteKitContext(context); | ||
@@ -190,3 +236,3 @@ const svelteVersion = getSvelteVersion(filePath); | ||
if (svelteVersion == null) { | ||
return { | ||
const result = { | ||
svelteVersion: null, | ||
@@ -198,5 +244,7 @@ svelteFileType: null, | ||
}; | ||
svelteContextCache.set(filePath, result); | ||
return result; | ||
} | ||
if (svelteVersion === '3/4') { | ||
return { | ||
const result = { | ||
svelteVersion, | ||
@@ -208,5 +256,7 @@ svelteFileType: svelteFileType === '.svelte' ? '.svelte' : null, | ||
}; | ||
svelteContextCache.set(filePath, result); | ||
return result; | ||
} | ||
if (svelteFileType == null) { | ||
return { | ||
const result = { | ||
svelteVersion, | ||
@@ -218,4 +268,6 @@ svelteFileType: null, | ||
}; | ||
svelteContextCache.set(filePath, result); | ||
return result; | ||
} | ||
return { | ||
const result = { | ||
svelteVersion, | ||
@@ -227,2 +279,4 @@ runes: svelteParseContext?.runes ?? 'undetermined', | ||
}; | ||
svelteContextCache.set(filePath, result); | ||
return result; | ||
} |
{ | ||
"name": "eslint-plugin-svelte", | ||
"version": "3.0.0-next.17", | ||
"version": "3.0.0-next.18", | ||
"description": "ESLint plugin for Svelte using AST", | ||
@@ -5,0 +5,0 @@ "repository": "git+https://github.com/sveltejs/eslint-plugin-svelte.git", |
734124
17029