Skip to content

Commit

Permalink
feat: use typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Mar 24, 2023
1 parent 0540eb3 commit f330b35
Show file tree
Hide file tree
Showing 39 changed files with 1,317 additions and 667 deletions.
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
24 changes: 20 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,34 @@
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
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.3.0"
},
"devDependencies": {
"@eslint-community/eslint-plugin-mysticatea": "^15.2.0",
"@types/eslint": "^8.21.0",
"@types/estree": "^1.0.0",
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@typescript-eslint/types": "^5.50.0",
"c8": "^7.12.0",
"dot-prop": "^6.0.1",
"esbuild-register": "^3.4.2",
"eslint": "^8.28.0",
"mocha": "^9.2.2",
"npm-run-all": "^4.1.5",
"opener": "^1.5.2",
"prettier": "2.8.4",
"rimraf": "^3.0.2",
"rollup": "^2.79.1",
"rollup-plugin-sourcemaps": "^0.6.3",
"semver": "^7.3.8",
"typescript": "^4.9.5",
"unbuild": "^1.1.1",
"vitepress": "^1.0.0-alpha.40",
"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 },
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { getPropertyName } from "./get-property-name.mjs"
import type { SourceCode } from "eslint"
import type * as ESTree from "estree"
import { getParent } from "./get-parent"
import { getPropertyName } from "./get-property-name"

/**
* Get the name and kind of the given function node.
* @param {ASTNode} node - The function node to get.
* @param {SourceCode} [sourceCode] The source code object to get the code of computed property keys.
* @returns {string} The name and kind of the function node.
* @param node - The function node to get.
* @param sourceCode The source code object to get the code of computed property keys.
* @returns The name and kind of the function node.
*/
// eslint-disable-next-line complexity
export function getFunctionNameWithKind(node, sourceCode) {
const parent = node.parent
export function getFunctionNameWithKind(
node: ESTree.Function,
sourceCode?: SourceCode,
): string {
const parent = getParent(node)!
const tokens = []
const isObjectMethod = parent.type === "Property" && parent.value === node
const isClassMethod =
Expand Down Expand Up @@ -68,7 +74,7 @@ export function getFunctionNameWithKind(node, sourceCode) {
}
}
}
} else if (node.id) {
} else if (node.type !== "ArrowFunctionExpression" && node.id) {
tokens.push(`'${node.id.name}'`)
} else if (
parent.type === "VariableDeclarator" &&
Expand Down
Loading

0 comments on commit f330b35

Please sign in to comment.