Skip to content

Commit

Permalink
feat: add body and comments into ast, fix #12
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Jul 31, 2019
1 parent fb19d5b commit 5b28bd5
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ module.exports = {
parserOptions: {
parser: 'babel-eslint',
},
rules: {
'no-unused-expressions': 0,
},
},
{
files: ['*.ts', '*.tsx'],
Expand Down
116 changes: 83 additions & 33 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ import { isComment } from './utils'

import { Position, Parent } from 'unist'
import { AST, Linter } from 'eslint'
import {
Comment,
ModuleDeclaration,
Statement,
// SourceLocation` is not exported from estree, but it is actually working
// eslint-disable-next-line import/named
SourceLocation,
} from 'estree'

const normalizePosition = (position: Position) => {
const start = position.start.offset
Expand All @@ -26,8 +34,62 @@ const normalizePosition = (position: Position) => {
}
}

const getRawText = (code: string, position: Position) =>
code.slice(position.start.offset, position.end.offset)
interface BaseNode {
type: string
loc?: SourceLocation
range?: [number, number]
}

function normalizeLoc<T extends BaseNode>(
node: T,
startLine: number,
offset = 0,
): T {
if (!node || !node.loc || !node.range) {
return node
}

Object.entries(node).forEach(([key, value]) => {
if (!value) {
return
}

if (Array.isArray(value)) {
node[key as keyof T] = value.map(child =>
normalizeLoc(child, startLine, offset),
) as any
} else {
node[key as keyof T] = normalizeLoc(
value,
startLine,
offset,
) as T[keyof T]
}
})

const {
loc: { start: startLoc, end: endLoc },
range,
} = node
const start = range[0] + offset
const end = range[1] + offset
return {
...node,
start,
end,
range: [start, end],
loc: {
start: {
line: startLine + startLoc.line,
column: startLoc.column,
},
end: {
line: startLine + endLoc.line,
column: endLoc.column,
},
},
}
}

export const parseForESLint = (
code: string,
Expand Down Expand Up @@ -58,6 +120,8 @@ export const parseForESLint = (
.use(remarkMdx)
.parse(code) as Parent

const body: Array<Statement | ModuleDeclaration> = []
const comments: Comment[] = []
const tokens: AST.Token[] = []

traverse(root, {
Expand All @@ -66,7 +130,7 @@ export const parseForESLint = (
return
}

const rawText = getRawText(code, position)
const rawText = code.slice(position.start.offset, position.end.offset)

// fix #4
if (isComment(rawText)) {
Expand All @@ -82,54 +146,40 @@ export const parseForESLint = (
} catch (e) {
if (e instanceof SyntaxError) {
e.index += node.start
e.column += node.loc.start.column - 1
e.lineNumber += node.loc.start.line - 1
e.column += node.loc.start.column
e.lineNumber += node.loc.start.line - 1 // lineNumber in 0-indexed, but line is 1-indexed
}

throw e
}

const { tokens: esTokens, range } = program
const {
body: esBody,
comments: esComments,
tokens: esTokens,
range,
} = program

const startLine = node.loc.start.line - 1 //! line is 1-indexed, change to 0-indexed to simplify usage
const offset = node.start - range[0]

body.push(...esBody.map(item => normalizeLoc(item, startLine, offset)))
comments.push(
...esComments.map(comment => normalizeLoc(comment, startLine, offset)),
)
tokens.push(
...esTokens.map(token => {
const {
loc: { start: tokenStart, end: tokenEnd },
} = token
const start = token.range[0] + offset
const end = token.range[1] + offset
const startLine = node.loc.start.line + tokenStart.line - 1
const startColumn = node.loc.start.column + tokenStart.column - 1
return {
...token,
start,
end,
range: [start, end],
loc: {
start: {
line: startLine,
column: startColumn,
},
end: {
line: startLine + tokenEnd.line - 1,
column: startLine + tokenEnd.column - 1,
},
},
} as AST.Token
}),
...esTokens.map(token => normalizeLoc(token, startLine, offset)),
)
},
})

return {
ast: {
...normalizePosition(root.position),
comments: [],
body: [],
type: 'Program',
sourceType: 'module',
body,
comments,
tokens,
},
}
Expand Down
Empty file added test/FollowReveal.tsx
Empty file.
Empty file added test/Playground.tsx
Empty file.
1 change: 1 addition & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Fixtures
Empty file added test/component.tsx
Empty file.
5 changes: 1 addition & 4 deletions test/fixture4.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ route: /follow-reveal
A `<FollowReveal>` is a like a `<TurnReveal>` that follows the mouse's movements in and out of the component.
Just like the `<TurnReveal>`, place it in an element that has `position: relative` and make sure it is a direct child of the element on which you define the `perspective` property.

import { Props, Playground } from 'docz'
import FollowReveal from './FollowReveal'
import { Front, PlaceHolderImage, Grid } from 'docs/PlayGroundUtils'

<Props of={FollowReveal} />
import { Grid, Front, PlaceHolderImage, Playground } from './Playground'

## Basic Usage

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"baseUrl": ".",
"esModuleInterop": true,
"jsx": "preserve",
"lib": ["esnext", "dom"],
"lib": ["esnext"],
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1938,7 +1938,7 @@ eslint-import-resolver-node@^0.3.2:
debug "^2.6.9"
resolve "^1.5.0"

"eslint-import-resolver-typescript@github:JounQin/eslint-import-resolver-typescript#feat/resolve_dts":
eslint-import-resolver-typescript@JounQin/eslint-import-resolver-typescript#feat/resolve_dts:
version "1.1.1"
resolved "https://codeload.github.com/JounQin/eslint-import-resolver-typescript/tar.gz/23e2e8cf71ee6c19da9f55e85b2ab34543d2a12e"
dependencies:
Expand Down

0 comments on commit 5b28bd5

Please sign in to comment.