Skip to content

Commit

Permalink
feat: export meta object (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed May 9, 2023
1 parent 5197597 commit 170f816
Show file tree
Hide file tree
Showing 19 changed files with 132 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-ways-leave.md
@@ -0,0 +1,5 @@
---
"eslint-plugin-svelte": minor
---

feat: export meta object
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -26,7 +26,8 @@
"sveltejs"
],
"scripts": {
"build": "yarn build:ts",
"build": "yarn build:meta && yarn build:ts",
"build:meta": "yarn ts ./tools/update-meta.ts",
"build:ts": "tsc --project ./tsconfig.build.json",
"clean": "rimraf .nyc_output lib coverage build .svelte-kit svelte.config-dist.js",
"cover": "nyc --reporter=lcov yarn test",
Expand Down
3 changes: 3 additions & 0 deletions src/configs/base.ts
@@ -1,3 +1,6 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "yarn update"
export = {
plugins: ["svelte"],
overrides: [
Expand Down
3 changes: 3 additions & 0 deletions src/configs/prettier.ts
@@ -1,3 +1,6 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "yarn update"
import path from "path"
const base = require.resolve("./base")
const baseExtend =
Expand Down
3 changes: 3 additions & 0 deletions src/configs/recommended.ts
@@ -1,3 +1,6 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "yarn update"
import path from "path"
const base = require.resolve("./base")
const baseExtend =
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Expand Up @@ -5,6 +5,7 @@ import recommended from "./configs/recommended"
import prettier from "./configs/prettier"
import all from "./configs/all"
import * as processor from "./processor"
import * as meta from "./meta"

const configs = {
base,
Expand All @@ -19,6 +20,7 @@ const rules = ruleList.reduce((obj, r) => {
}, {} as { [key: string]: RuleModule })

export = {
meta,
configs,
rules,
processors: {
Expand Down
5 changes: 5 additions & 0 deletions src/meta.ts
@@ -0,0 +1,5 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "yarn update"
export const name = "eslint-plugin-svelte" as const
export const version = "2.27.4" as const
1 change: 1 addition & 0 deletions src/processor/index.ts
@@ -1,6 +1,7 @@
import type { Linter } from "eslint"
import type { Shared } from "../shared"
import { beginShared, terminateShared } from "../shared"
export * as meta from "../meta"

/** preprocess */
export function preprocess(code: string, filename: string): string[] {
Expand Down
3 changes: 3 additions & 0 deletions src/types-for-node.ts
@@ -1,3 +1,6 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "yarn update"
//
// The information here can be calculated by calculating the type,
// but is pre-defined to avoid the computational cost.
Expand Down
3 changes: 3 additions & 0 deletions src/utils/rules.ts
@@ -1,3 +1,6 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "yarn update"
import type { RuleModule } from "../types"
import typescriptEslintNoUnnecessaryCondition from "../rules/@typescript-eslint/no-unnecessary-condition"
import blockLang from "../rules/block-lang"
Expand Down
21 changes: 21 additions & 0 deletions tests/src/meta.ts
@@ -0,0 +1,21 @@
import assert from "assert"
import plugin from "../../src"
import { version } from "../../package.json"
const expectedMeta = {
name: "eslint-plugin-svelte",
version,
}

describe("Test for meta object", () => {
it("A plugin should have a meta object.", () => {
assert.strictEqual(plugin.meta.name, expectedMeta.name)
assert.strictEqual(typeof plugin.meta.version, "string")
})

for (const [name, processor] of Object.entries(plugin.processors)) {
it(`"${name}" processor should have a meta object.`, () => {
assert.strictEqual(processor.meta.name, expectedMeta.name)
assert.strictEqual(typeof processor.meta.version, "string")
})
}
})
20 changes: 2 additions & 18 deletions tools/lib/changesets-util.ts
@@ -1,25 +1,9 @@
import assembleReleasePlan from "@changesets/assemble-release-plan"
import readChangesets from "@changesets/read"
import { read } from "@changesets/config"
import { getPackages } from "@manypkg/get-packages"
import { readPreState } from "@changesets/pre"
import getReleasePlan from "@changesets/get-release-plan"
import path from "path"

const root = path.resolve(__dirname, "../..")

/** Get new version string from changesets */
export async function getNewVersion(): Promise<string> {
const packages = await getPackages(root)
const preState = await readPreState(root)
const config = await read(root, packages)
const changesets = await readChangesets(root)

const releasePlan = assembleReleasePlan(
changesets,
packages,
config,
preState,
)
const releasePlan = await getReleasePlan(path.resolve(__dirname, "../.."))

return releasePlan.releases.find(
({ name }) => name === "eslint-plugin-svelte",
Expand Down
38 changes: 38 additions & 0 deletions tools/update-meta.ts
@@ -0,0 +1,38 @@
import fs from "fs"
import path from "path"
import { ESLint } from "eslint"
import { name, version } from "../package.json"
import { getNewVersion } from "./lib/changesets-util"

const META_PATH = path.join(__dirname, "../src/meta.ts")

void main()

/** main */
async function main() {
if (!fs.existsSync(META_PATH)) {
fs.writeFileSync(META_PATH, "", "utf8")
}
const eslint = new ESLint({ fix: true })
const [result] = await eslint.lintText(
`/*
* IMPORTANT!
* This file has been automatically generated,
* in order to update its content execute "yarn update"
*/
export const name = ${JSON.stringify(name)} as const;
export const version = ${JSON.stringify(await getVersion())} as const;
`,
{ filePath: META_PATH },
)
fs.writeFileSync(META_PATH, result.output!, "utf8")
}

/** Get version */
function getVersion() {
// eslint-disable-next-line no-process-env -- ignore
if (process.env.IN_VERSION_CI_SCRIPT) {
return getNewVersion()
}
return version
}
6 changes: 5 additions & 1 deletion tools/update-rules.ts
Expand Up @@ -21,7 +21,11 @@ function toIdentifier(str: string) {
return camelCase(clean)
}

const content = `
const content = `/*
* IMPORTANT!
* This file has been automatically generated,
* in order to update its content execute "yarn update"
*/
import type { RuleModule } from "../types"
${rules
.map(
Expand Down
21 changes: 18 additions & 3 deletions tools/update-rulesets.ts
Expand Up @@ -2,7 +2,12 @@ import path from "path"
import fs from "fs"
import { rules } from "./lib/load-rules"

const baseContent = `export = {
const baseContent = `/*
* IMPORTANT!
* This file has been automatically generated,
* in order to update its content execute "yarn update"
*/
export = {
plugins: ["svelte"],
overrides: [
{
Expand Down Expand Up @@ -37,7 +42,12 @@ const baseFilePath = path.resolve(__dirname, "../src/configs/base.ts")
// Update file.
fs.writeFileSync(baseFilePath, baseContent)

const recommendedContent = `import path from "path"
const recommendedContent = `/*
* IMPORTANT!
* This file has been automatically generated,
* in order to update its content execute "yarn update"
*/
import path from "path"
const base = require.resolve("./base")
const baseExtend =
path.extname(\`\${base}\`) === ".ts" ? "plugin:svelte/base" : base
Expand All @@ -64,7 +74,12 @@ const recommendedFilePath = path.resolve(
// Update file.
fs.writeFileSync(recommendedFilePath, recommendedContent)

const prettierContent = `import path from "path"
const prettierContent = `/*
* IMPORTANT!
* This file has been automatically generated,
* in order to update its content execute "yarn update"
*/
import path from "path"
const base = require.resolve("./base")
const baseExtend =
path.extname(\`\${base}\`) === ".ts" ? "plugin:svelte/base" : base
Expand Down
14 changes: 12 additions & 2 deletions tools/update-types-for-node.ts
Expand Up @@ -25,7 +25,12 @@ const svelteNodeNames = Object.keys(visitorKeys).filter(
)

const estreeCode = [
`//
`/*
* IMPORTANT!
* This file has been automatically generated,
* in order to update its content execute "yarn update"
*/
//
// Replace type information to use "@typescript-eslint/types" instead of "estree".
//
Expand All @@ -38,7 +43,12 @@ export type Statement = TSESTree.Statement
export type Pattern = TSESTree.Pattern`,
]
const typesForNodeCode = [
`//
`/*
* IMPORTANT!
* This file has been automatically generated,
* in order to update its content execute "yarn update"
*/
//
// The information here can be calculated by calculating the type,
// but is pre-defined to avoid the computational cost.
//
Expand Down
1 change: 1 addition & 0 deletions tools/update.ts
Expand Up @@ -4,3 +4,4 @@ import "./update-docs"
import "./update-readme"
import "./update-docs-rules-index"
import "./update-types-for-node"
import "./update-meta"
3 changes: 2 additions & 1 deletion tsconfig.json
Expand Up @@ -19,7 +19,8 @@
"paths": {
"*": ["typings/*"]
},
"skipLibCheck": true
"skipLibCheck": true,
"resolveJsonModule": true
},
"include": [
"src/**/*",
Expand Down
3 changes: 3 additions & 0 deletions typings/estree/index.d.ts
@@ -1,3 +1,6 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "yarn update"
//
// Replace type information to use "@typescript-eslint/types" instead of "estree".
//
Expand Down

0 comments on commit 170f816

Please sign in to comment.