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

feat: use typescript #60

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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 .c8rc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"include": ["src/**/*.mjs"],
"include": ["src/**/*.ts"],
"reporter": ["lcov", "text-summary"]
}
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
!.vitepress
/docs/.vitepress/dist
/docs/.vitepress/cache
/dist
22 changes: 18 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,32 @@
module.exports = {
root: true,
extends: ["plugin:@eslint-community/mysticatea/es2020"],
parserOptions: {
project: "./tsconfig.json",
},
rules: {
"@eslint-community/mysticatea/prettier": "off",
"@eslint-community/mysticatea/ts/naming-convention": "off",
"@eslint-community/mysticatea/ts/prefer-readonly-parameter-types":
"off",
"@eslint-community/mysticatea/node/no-missing-import": [
"error",
{ allowModules: ["estree", "unbuild"] },
],
},
settings: {
node: {
tryExtensions: [".js", ".json", ".mjs", ".node", ".ts", ".tsx"],
},
},
overrides: [
{
files: ["src/**/*.mjs", "test/**/*.mjs"],
files: ["src/**/*.ts", "test/**/*.ts"],
extends: ["plugin:@eslint-community/mysticatea/+modules"],
rules: {
"init-declarations": "off",

"@eslint-community/mysticatea/node/no-unsupported-features/es-syntax":
["error", { ignores: ["modules"] }],
"no-duplicate-imports": "off",
"no-shadow": "off",
},
},
],
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ jobs:
- name: ▶️ Run lint script
run: npm run lint

- name: 🏗 Build
run: npm run build

test:
name:
🧪 Test (Node@${{ matrix.node }} - ESLint@${{ matrix.eslint }} - ${{
Expand Down Expand Up @@ -83,9 +86,6 @@ jobs:
- name: 📥 Install ESLint v${{ matrix.eslint }}
run: npm install --save-dev eslint@${{ matrix.eslint }}

- name: 🏗 Build
run: npm run build

- name: ▶️ Run test script
run: npm run test

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/node_modules
/index.*
/test.*
/dist
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
{
"eslint.validate": ["javascript", "typescript"],
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": ["source.organizeImports", "source.fixAll"]
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": ["source.organizeImports", "source.fixAll"]
},
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
Expand Down
14 changes: 14 additions & 0 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineBuildConfig } from "unbuild"

export default defineBuildConfig({
externals: ["estree"],
hooks: {
"rollup:options"(_ctx, options) {
for (const output of [options.output].flat()) {
if (output!.format === "cjs") {
output!.exports = "named"
}
}
},
},
})
29 changes: 18 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,56 @@
"sideEffects": false,
"exports": {
".": {
"import": "./index.mjs",
"require": "./index.js"
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"./package.json": "./package.json"
},
"main": "index",
"module": "index.mjs",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"index.*"
"dist"
],
"scripts": {
"prebuild": "npm run -s clean",
"build": "rollup -c",
"build": "unbuild",
"clean": "rimraf .nyc_output coverage index.*",
"coverage": "opener ./coverage/lcov-report/index.html",
"docs:build": "vitepress build docs",
"docs:watch": "vitepress dev docs",
"format": "npm run -s format:prettier -- --write",
"format:prettier": "prettier .",
"format:check": "npm run -s format:prettier -- --check",
"lint": "eslint .",
"test": "c8 mocha --reporter dot \"test/*.mjs\"",
"lint": "tsc && eslint .",
"test": "c8 mocha --require=esbuild-register --reporter dot \"test/*.ts\"",
"preversion": "npm test && npm run -s build",
"postversion": "git push && git push --tags",
"prewatch": "npm run -s clean",
"watch": "warun \"{src,test}/**/*.mjs\" -- npm run -s test:mocha"
"watch": "warun \"{src,test}/**/*.ts\" -- npm run -s test:mocha"
},
"dependencies": {
"eslint-visitor-keys": "^3.4.2"
},
"devDependencies": {
"@eslint-community/eslint-plugin-mysticatea": "^15.5.0",
"@types/eslint": "^8.21.0",
"@types/estree": "^1.0.0",
"@types/mocha": "^10.0.1",
"@types/node": "^12.20.55",
"@typescript-eslint/types": "^5.50.0",
"c8": "^8.0.1",
"dot-prop": "^7.2.0",
"esbuild-register": "^3.4.2",
"eslint": "^8.46.0",
"mocha": "^9.2.2",
"npm-run-all": "^4.1.5",
"opener": "^1.5.2",
"prettier": "2.8.8",
"rimraf": "^3.0.2",
"rollup": "^2.79.1",
"rollup-plugin-sourcemaps": "^0.6.3",
"semver": "^7.5.4",
"typescript": "^4.9.5",
"unbuild": "^1.1.1",
"vitepress": "^1.0.0-beta.7",
"warun": "^1.0.0"
},
Expand Down
27 changes: 0 additions & 27 deletions rollup.config.js

This file was deleted.

29 changes: 0 additions & 29 deletions src/find-variable.mjs

This file was deleted.

34 changes: 34 additions & 0 deletions src/find-variable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { Scope } from "eslint"
import type * as ESTree from "estree"
import { getInnermostScope } from "./get-innermost-scope"

/**
* Find the variable of a given name.
* @param initialScope The scope to start finding.
* @param nameOrNode The variable name to find. If this is a Node object then it should be an Identifier node.
* @returns The found variable or null.
*/
export function findVariable(
initialScope: Scope.Scope,
nameOrNode: ESTree.Identifier | string,
): Scope.Variable | null {
let name = ""
let scope: Scope.Scope | null = initialScope

if (typeof nameOrNode === "string") {
name = nameOrNode
} else {
name = nameOrNode.name
scope = getInnermostScope(scope, nameOrNode)
}

while (scope != null) {
const variable = scope.set.get(name)
if (variable != null) {
return variable
}
scope = scope.upper
}

return null
}
47 changes: 0 additions & 47 deletions src/get-function-head-location.mjs

This file was deleted.

56 changes: 56 additions & 0 deletions src/get-function-head-location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import type { SourceCode } from "eslint"
import type * as ESTree from "estree"
import { getParent } from "./get-parent"
import { isArrowToken, isOpeningParenToken } from "./token-predicate"

/**
* Get the `(` token of the given function node.
* @param node - The function node to get.
* @param sourceCode - The source code object to get tokens.
* @returns `(` token.
*/
function getOpeningParenOfParams(
node: ESTree.Function,
sourceCode: SourceCode,
) {
return node.type !== "ArrowFunctionExpression" && node.id
? sourceCode.getTokenAfter(node.id, isOpeningParenToken)!
: sourceCode.getFirstToken(node, isOpeningParenToken)!
}

/**
* Get the location of the given function node for reporting.
* @param node - The function node to get.
* @param sourceCode - The source code object to get tokens.
* @returns The location of the function node for reporting.
*/
export function getFunctionHeadLocation(
node: ESTree.Function,
sourceCode: SourceCode,
): ESTree.SourceLocation {
const parent = getParent(node)!
let start: ESTree.Position | null = null
let end: ESTree.Position | null = null

if (node.type === "ArrowFunctionExpression") {
const arrowToken = sourceCode.getTokenBefore(node.body, isArrowToken)!

start = arrowToken.loc.start
end = arrowToken.loc.end
} else if (
parent.type === "Property" ||
parent.type === "MethodDefinition" ||
parent.type === "PropertyDefinition"
) {
start = parent.loc!.start
end = getOpeningParenOfParams(node, sourceCode).loc.start
} else {
start = node.loc!.start
end = getOpeningParenOfParams(node, sourceCode).loc.start
}

return {
start: { ...start },
end: { ...end },
}
}
Loading