Skip to content

Change parser to allow parsing two script tags on <script setup> #108

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

Merged
merged 10 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor
  • Loading branch information
ota-meshi committed Jun 18, 2021
commit 70a28a16e811ab8ca8da21e6daeb9cffc31cfaeb
8 changes: 4 additions & 4 deletions src/common/fix-locations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
ParseError,
} from "../ast"
import { traverseNodes } from "../ast"
import type { LocationFixCalculator } from "./location-fix-calculator"
import type { LocationCalculator } from "./location-calculator"

/**
* Do post-process of parsing an expression.
Expand All @@ -19,7 +19,7 @@ import type { LocationFixCalculator } from "./location-fix-calculator"
*/
export function fixLocations(
result: ESLintExtendedProgram,
locationCalculator: LocationFixCalculator,
locationCalculator: LocationCalculator,
): void {
// There are cases which the same node instance appears twice in the tree.
// E.g. `let {a} = {}` // This `a` appears twice at `Property#key` and `Property#value`.
Expand Down Expand Up @@ -74,7 +74,7 @@ export function fixLocations(
*/
export function fixLocation<T extends HasLocation>(
node: T,
locationCalculator: LocationFixCalculator,
locationCalculator: LocationCalculator,
): T {
const range = node.range
const loc = node.loc
Expand Down Expand Up @@ -105,7 +105,7 @@ export function fixLocation<T extends HasLocation>(
*/
export function fixErrorLocation(
error: ParseError,
locationCalculator: LocationFixCalculator,
locationCalculator: LocationCalculator,
) {
const diff = locationCalculator.getFixOffset(error.index, "start")

Expand Down
31 changes: 24 additions & 7 deletions src/common/location-calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,24 @@ import sortedLastIndex from "lodash/sortedLastIndex"
import type { HasLocation, Location, ParseError } from "../ast"
import { fixErrorLocation, fixLocation } from "./fix-locations"
import { LinesAndColumns } from "./lines-and-columns"
import type { LocationFixCalculator } from "./location-fix-calculator"

/**
* Location calculators.
*/
export interface LocationCalculator {
/**
* Gets the fix location offset of the given offset with using the base offset of this calculator.
* @param offset The offset to modify.
*/
getFixOffset(offset: number, kind: "start" | "end"): number

/**
* Calculate the location of the given index.
* @param index The index to calculate their location.
* @returns The location of the index.
*/
getLocFromIndex(index: number): Location
}

/**
* Location calculators.
Expand All @@ -21,9 +38,9 @@ import type { LocationFixCalculator } from "./location-fix-calculator"
* - Adjusts the locations of script ASTs.
* - Creates expression containers in postprocess.
*/
export class LocationCalculator
export class LocationCalculatorForHtml
extends LinesAndColumns
implements LocationFixCalculator
implements LocationCalculator
{
private gapOffsets: number[]
private baseOffset: number
Expand Down Expand Up @@ -59,8 +76,8 @@ export class LocationCalculator
* @param offset The base offset of new sub calculator.
* @returns Sub calculator.
*/
public getSubCalculatorAfter(offset: number): LocationCalculator {
return new LocationCalculator(
public getSubCalculatorAfter(offset: number): LocationCalculatorForHtml {
return new LocationCalculatorForHtml(
this.gapOffsets,
this.ltOffsets,
this.baseOffset + offset,
Expand All @@ -73,8 +90,8 @@ export class LocationCalculator
* @param offset The shift of new sub calculator.
* @returns Sub calculator.
*/
public getSubCalculatorShift(offset: number): LocationCalculator {
return new LocationCalculator(
public getSubCalculatorShift(offset: number): LocationCalculatorForHtml {
return new LocationCalculatorForHtml(
this.gapOffsets,
this.ltOffsets,
this.baseOffset,
Expand Down
28 changes: 0 additions & 28 deletions src/common/location-fix-calculator.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/html/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type {
} from "../ast"
import { NS, ParseError } from "../ast"
import { debug } from "../common/debug"
import { LocationCalculator } from "../common/location-calculator"
import { LocationCalculatorForHtml } from "../common/location-calculator"
import {
convertToDirective,
processMustache,
Expand Down Expand Up @@ -161,7 +161,7 @@ function propagateEndLocation(node: VDocumentFragment | VElement): void {
*/
export class Parser {
private tokenizer: IntermediateTokenizer
private locationCalculator: LocationCalculator
private locationCalculator: LocationCalculatorForHtml
private parserOptions: ParserOptions
private isSFC: boolean
private document: VDocumentFragment
Expand Down Expand Up @@ -239,7 +239,7 @@ export class Parser {
*/
public constructor(tokenizer: Tokenizer, parserOptions: ParserOptions) {
this.tokenizer = new IntermediateTokenizer(tokenizer)
this.locationCalculator = new LocationCalculator(
this.locationCalculator = new LocationCalculatorForHtml(
tokenizer.gaps,
tokenizer.lineTerminators,
)
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
import * as path from "path"
import * as AST from "./ast"
import { LocationCalculator } from "./common/location-calculator"
import { LocationCalculatorForHtml } from "./common/location-calculator"
import { HTMLParser, HTMLTokenizer } from "./html"
import { parseScript, parseScriptElement } from "./script"
import * as services from "./parser-services"
Expand Down Expand Up @@ -104,7 +104,7 @@ export function parseForESLint(

let result: AST.ESLintExtendedProgram
let document: AST.VDocumentFragment | null
let locationCalculator: LocationCalculator | null
let locationCalculator: LocationCalculatorForHtml | null
if (!isVueFile(code, options)) {
result = parseScript(code, options)
document = null
Expand All @@ -114,7 +114,7 @@ export function parseForESLint(
const tokenizer = new HTMLTokenizer(code, options)
const rootAST = new HTMLParser(tokenizer, options).parse()

locationCalculator = new LocationCalculator(
locationCalculator = new LocationCalculatorForHtml(
tokenizer.gaps,
tokenizer.lineTerminators,
)
Expand Down
4 changes: 2 additions & 2 deletions src/parser-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
VAttribute,
} from "./ast"
import { getFallbackKeys, KEYS, traverseNodes } from "./ast/traverse"
import type { LocationCalculator } from "./common/location-calculator"
import type { LocationCalculatorForHtml } from "./common/location-calculator"
import type {
CustomBlockContext,
ESLintCustomBlockParser,
Expand Down Expand Up @@ -98,7 +98,7 @@ export function define(
sourceText: string,
rootAST: ESLintProgram,
document: VDocumentFragment | null,
globalLocationCalculator: LocationCalculator | null,
globalLocationCalculator: LocationCalculatorForHtml | null,
{ parserOptions }: { parserOptions: ParserOptions },
): ParserServices {
const customBlocksEmitters = new Map<
Expand Down
31 changes: 21 additions & 10 deletions src/script-setup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import type { ESLintExtendedProgram, ESLintStatement, VElement } from "../ast"
import { ParseError, traverseNodes } from "../ast"
import { fixErrorLocation, fixLocations } from "../common/fix-locations"
import type { LinesAndColumns } from "../common/lines-and-columns"
import type { LocationFixCalculator } from "../common/location-fix-calculator"
import { simpleOffsetLocationFixCalculator } from "../common/location-fix-calculator"
import type { LocationCalculator } from "../common/location-calculator"
import type { ParserOptions } from "../common/parser-options"
import { parseScript, parseScriptFragment } from "../script"

Expand Down Expand Up @@ -101,15 +100,15 @@ export function parseScriptSetupElements(
if (!scriptSetupCodeBlocks) {
return parseScriptFragment(
"",
simpleOffsetLocationFixCalculator(
simpleOffsetLocationCalculator(
scriptSetupElement.startTag.range[1],
linesAndColumns,
),
parserOptions,
)
}

const locationFixCalculator: LocationFixCalculator = {
const locationCalculator: LocationCalculator = {
getFixOffset(offset, kind) {
const test: (block: RemapBlock) => boolean =
kind === "start"
Expand All @@ -135,7 +134,7 @@ export function parseScriptSetupElements(
} catch (err) {
const perr = ParseError.normalize(err)
if (perr) {
fixErrorLocation(perr, locationFixCalculator)
fixErrorLocation(perr, locationCalculator)
throw perr
}
throw err
Expand All @@ -145,7 +144,7 @@ export function parseScriptSetupElements(
remapAST(result, scriptSetupCodeBlocks)

/* Remap locations */
remapLocationAndTokens(result, scriptSetupCodeBlocks, locationFixCalculator)
remapLocationAndTokens(result, scriptSetupCodeBlocks, locationCalculator)

// Adjust AST and tokens
if (result.ast.tokens != null) {
Expand Down Expand Up @@ -177,7 +176,7 @@ export function parseScriptSetupElements(
0,
)
result.ast.range[1] = programEndOffset
result.ast.loc.end = locationFixCalculator.getLocFromIndex(programEndOffset)
result.ast.loc.end = locationCalculator.getLocFromIndex(programEndOffset)
if (result.ast.end != null) {
result.ast.end = [scriptSetupElement, scriptElement].reduce(
(end, node) => {
Expand Down Expand Up @@ -328,7 +327,7 @@ function getScriptSetupCodeBlocks(
if (perr) {
fixErrorLocation(
perr,
simpleOffsetLocationFixCalculator(
simpleOffsetLocationCalculator(
scriptStartOffset,
linesAndColumns,
),
Expand Down Expand Up @@ -446,7 +445,7 @@ function remapAST(
function remapLocationAndTokens(
result: ESLintExtendedProgram,
{ codeBlocks }: ScriptSetupCodeBlocks,
locationFixCalculator: LocationFixCalculator,
locationCalculator: LocationCalculator,
) {
traverseNodes(result.ast, {
visitorKeys: result.visitorKeys,
Expand Down Expand Up @@ -480,5 +479,17 @@ function remapLocationAndTokens(
}
}

fixLocations(result, locationFixCalculator)
fixLocations(result, locationCalculator)
}

function simpleOffsetLocationCalculator(
offset: number,
linesAndColumns: LinesAndColumns,
): LocationCalculator {
return {
getFixOffset() {
return offset
},
getLocFromIndex: linesAndColumns.getLocFromIndex.bind(linesAndColumns),
}
}
Loading