Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: vuejs/vue-eslint-parser
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v9.4.0
Choose a base ref
...
head repository: vuejs/vue-eslint-parser
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v9.4.2
Choose a head ref
  • 7 commits
  • 40 files changed
  • 2 contributors

Commits on Jan 19, 2024

  1. Update bug_report.yml

    ota-meshi authored Jan 19, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    7b1f6a1 View commit details

Commits on Jan 22, 2024

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    18e8e17 View commit details
  2. fix: shorthand camelize should be used in rawName (#218)

    * fix: shorthand camelize should be used in `rawName`
    
    * test: add camelcase test
    
    * chore: update scope
    waynzh authored Jan 22, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    d4eef64 View commit details
  3. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    f3ac447 View commit details
  4. 9.4.1

    ota-meshi committed Jan 22, 2024
    Copy the full SHA
    7c9c24b View commit details
  5. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    2b3a762 View commit details
  6. 9.4.2

    ota-meshi committed Jan 22, 2024
    Copy the full SHA
    d79bcad View commit details
Showing with 18,246 additions and 193 deletions.
  1. +1 −1 .github/ISSUE_TEMPLATE/bug_report.yml
  2. +1 −1 package.json
  3. +6 −138 scripts/update-fixtures-ast.js
  4. +34 −5 src/script/generic.ts
  5. +52 −13 src/script/index.ts
  6. +1 −1 src/template/index.ts
  7. +19 −34 test/ast.js
  8. +968 −0 test/fixtures/ast/v-bind-same-name-shorthand07-camelcase/ast.json
  9. +3 −0 test/fixtures/ast/v-bind-same-name-shorthand07-camelcase/parser-options.json
  10. +3 −0 test/fixtures/ast/v-bind-same-name-shorthand07-camelcase/requirements.json
  11. +154 −0 test/fixtures/ast/v-bind-same-name-shorthand07-camelcase/scope.json
  12. +7 −0 test/fixtures/ast/v-bind-same-name-shorthand07-camelcase/source.vue
  13. +34 −0 test/fixtures/ast/v-bind-same-name-shorthand07-camelcase/token-ranges.json
  14. +73 −0 test/fixtures/ast/v-bind-same-name-shorthand07-camelcase/tree.json
  15. +2,633 −0 test/fixtures/ast/vue3.3-generic-4-with-spaces/ast.json
  16. +6 −0 test/fixtures/ast/vue3.3-generic-4-with-spaces/parser-options.json
  17. +15 −0 test/fixtures/ast/vue3.3-generic-4-with-spaces/source.vue
  18. +104 −0 test/fixtures/ast/vue3.3-generic-4-with-spaces/token-ranges.json
  19. +39 −0 test/fixtures/ast/vue3.3-generic-4-with-spaces/tree.json
  20. +2,759 −0 test/fixtures/ast/vue3.3-generic-5-with-spaces/ast.json
  21. +6 −0 test/fixtures/ast/vue3.3-generic-5-with-spaces/parser-options.json
  22. +16 −0 test/fixtures/ast/vue3.3-generic-5-with-spaces/source.vue
  23. +111 −0 test/fixtures/ast/vue3.3-generic-5-with-spaces/token-ranges.json
  24. +39 −0 test/fixtures/ast/vue3.3-generic-5-with-spaces/tree.json
  25. +2,975 −0 test/fixtures/ast/vue3.3-generic-6-with-default/ast.json
  26. +6 −0 test/fixtures/ast/vue3.3-generic-6-with-default/parser-options.json
  27. +21 −0 test/fixtures/ast/vue3.3-generic-6-with-default/source.vue
  28. +123 −0 test/fixtures/ast/vue3.3-generic-6-with-default/token-ranges.json
  29. +39 −0 test/fixtures/ast/vue3.3-generic-6-with-default/tree.json
  30. +3,346 −0 test/fixtures/document-fragment/vue3.3-generic-5-with-spaces/document-fragment.json
  31. +6 −0 test/fixtures/document-fragment/vue3.3-generic-5-with-spaces/parser-options.json
  32. +16 −0 test/fixtures/document-fragment/vue3.3-generic-5-with-spaces/source.vue
  33. +74 −0 test/fixtures/document-fragment/vue3.3-generic-5-with-spaces/token-ranges.json
  34. +194 −0 test/fixtures/document-fragment/vue3.3-generic-5-with-spaces/tree.json
  35. +3,883 −0 test/fixtures/document-fragment/vue3.3-generic-6-with-default/document-fragment.json
  36. +6 −0 test/fixtures/document-fragment/vue3.3-generic-6-with-default/parser-options.json
  37. +21 −0 test/fixtures/document-fragment/vue3.3-generic-6-with-default/source.vue
  38. +86 −0 test/fixtures/document-fragment/vue3.3-generic-6-with-default/token-ranges.json
  39. +226 −0 test/fixtures/document-fragment/vue3.3-generic-6-with-default/tree.json
  40. +140 −0 test/test-utils.js
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ body:
- type: textarea
id: eslint-plugin-vue-version
attributes:
label: What version of `eslint-plugin-vue` and ` vue-eslint-parser` are you using?
label: What version of `eslint-plugin-vue` and `vue-eslint-parser` are you using?
value: |
- vue-eslint-parser@0.0.0
- eslint-plugin-vue@0.0.0
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-eslint-parser",
"version": "9.4.0",
"version": "9.4.2",
"description": "The ESLint custom parser for `.vue` files.",
"engines": {
"node": "^14.17.0 || >=16.0.0"
144 changes: 6 additions & 138 deletions scripts/update-fixtures-ast.js
Original file line number Diff line number Diff line change
@@ -12,8 +12,13 @@
const fs = require("fs")
const path = require("path")
const parser = require("../src")
const escope = require("eslint-scope")
const semver = require("semver")
const {
scopeToJSON,
analyze,
replacer,
getAllTokens,
} = require("../test/test-utils")

//------------------------------------------------------------------------------
// Helpers
@@ -30,40 +35,6 @@ const PARSER_OPTIONS = {
eslintScopeManager: true,
}

/**
* Remove `parent` proeprties from the given AST.
* @param {string} key The key.
* @param {any} value The value of the key.
* @returns {any} The value of the key to output.
*/
function replacer(key, value) {
if (key === "parent") {
return undefined
}
if (key === "errors" && Array.isArray(value)) {
return value.map((e) => ({
message: e.message,
index: e.index,
lineNumber: e.lineNumber,
column: e.column,
}))
}
return value
}

/**
* Get all tokens of the given AST.
* @param {ASTNode} ast The root node of AST.
* @returns {Token[]} Tokens.
*/
function getAllTokens(ast) {
const tokenArrays = [ast.tokens, ast.comments]
if (ast.templateBody != null) {
tokenArrays.push(ast.templateBody.tokens, ast.templateBody.comments)
}
return Array.prototype.concat.apply([], tokenArrays)
}

/**
* Create simple tree.
* @param {string} source The source code.
@@ -98,109 +69,6 @@ function getTree(source, ast) {
return root.children
}

function scopeToJSON(scopeManager) {
return JSON.stringify(normalizeScope(scopeManager.globalScope), replacer, 4)

function normalizeScope(scope) {
return {
type: scope.type,
variables: scope.variables.map(normalizeVar),
references: scope.references.map(normalizeReference),
childScopes: scope.childScopes.map(normalizeScope),
through: scope.through.map(normalizeReference),
}
}

function normalizeVar(v) {
return {
name: v.name,
identifiers: v.identifiers.map(normalizeId),
defs: v.defs.map(normalizeDef),
references: v.references.map(normalizeReference),
}
}

function normalizeReference(reference) {
return {
identifier: normalizeId(reference.identifier),
from: reference.from.type,
resolved: normalizeId(
reference.resolved &&
reference.resolved.defs &&
reference.resolved.defs[0] &&
reference.resolved.defs[0].name,
),
init: reference.init || null,
vueUsedInTemplate: reference.vueUsedInTemplate
? reference.vueUsedInTemplate
: undefined,
}
}

function normalizeDef(def) {
return {
type: def.type,
node: normalizeDefNode(def.node),
name: def.name.name,
}
}

function normalizeId(identifier) {
return (
identifier && {
type: identifier.type,
name: identifier.name,
loc: identifier.loc,
}
)
}

function normalizeDefNode(node) {
return {
type: node.type,
loc: node.loc,
}
}
}

/**
* Analyze scope
*/
function analyze(ast, parserOptions) {
const ecmaVersion = parserOptions.ecmaVersion || 2017
const ecmaFeatures = parserOptions.ecmaFeatures || {}
const sourceType = parserOptions.sourceType || "script"
const result = escope.analyze(ast, {
ignoreEval: true,
nodejsScope: false,
impliedStrict: ecmaFeatures.impliedStrict,
ecmaVersion,
sourceType,
fallback: getFallbackKeys,
})

return result

function getFallbackKeys(node) {
return Object.keys(node).filter(fallbackKeysFilter, node)
}

function fallbackKeysFilter(key) {
const value = null
return (
key !== "comments" &&
key !== "leadingComments" &&
key !== "loc" &&
key !== "parent" &&
key !== "range" &&
key !== "tokens" &&
key !== "trailingComments" &&
typeof value === "object" &&
(typeof value.type === "string" || Array.isArray(value))
)
}
}

//------------------------------------------------------------------------------
// Main
//------------------------------------------------------------------------------
39 changes: 34 additions & 5 deletions src/script/generic.ts
Original file line number Diff line number Diff line change
@@ -124,11 +124,40 @@ function getConstraint(node: TSESTree.TSTypeParameter, rawParam: string) {
if (!node.constraint) {
return "unknown"
}
const start = node.range[0]
return rawParam.slice(
node.constraint.range[0] - start,
node.constraint.range[1] - start,
)
let index = rawParam.indexOf(node.name.name) + node.name.name.length
let startIndex: number | null = null
while (index < rawParam.length) {
if (startIndex == null) {
if (rawParam.startsWith("extends", index)) {
startIndex = index = index + 7
continue
}
} else if (rawParam[index] === "=") {
return rawParam.slice(startIndex, index)
}
if (rawParam.startsWith("//", index)) {
const lfIndex = rawParam.indexOf("\n", index)
if (lfIndex >= 0) {
index = lfIndex + 1
continue
}
return "unknown"
}
if (rawParam.startsWith("/*", index)) {
const endIndex = rawParam.indexOf("*/", index)
if (endIndex >= 0) {
index = endIndex + 2
continue
}
return "unknown"
}
index++
}
if (startIndex == null) {
return "unknown"
}

return rawParam.slice(startIndex)
}

/** Remove variable def */
65 changes: 52 additions & 13 deletions src/script/index.ts
Original file line number Diff line number Diff line change
@@ -212,9 +212,34 @@ export function parseScriptFragment(
code: string,
locationCalculator: LocationCalculator,
parserOptions: ParserOptions,
): ESLintExtendedProgram {
return parseScriptFragmentWithOption(
code,
locationCalculator,
parserOptions,
)
}

/**
* Parse the given source code.
*
* @param code The source code to parse.
* @param locationCalculator The location calculator for fixLocations.
* @param parserOptions The parser options.
* @param processOptions The process options.
* @returns The result of parsing.
*/
function parseScriptFragmentWithOption(
code: string,
locationCalculator: LocationCalculator,
parserOptions: ParserOptions,
processOptions?: {
preFixLocationProcess?: (result: ESLintExtendedProgram) => void
},
): ESLintExtendedProgram {
try {
const result = parseScript(code, parserOptions)
processOptions?.preFixLocationProcess?.(result)
fixLocations(result, locationCalculator)
return result
} catch (err) {
@@ -1259,19 +1284,38 @@ export function parseGenericExpression(
throwEmptyError(locationCalculator, "a type parameter")
}

try {
const result = parseScriptFragment(
`void function<${code}>(){}`,
locationCalculator.getSubCalculatorShift(-14),
{ ...parserOptions, project: undefined },
)
function getParams(result: ESLintExtendedProgram) {
const { ast } = result
const statement = ast.body[0] as ESLintExpressionStatement
const rawExpression = statement.expression as ESLintUnaryExpression
const classDecl = rawExpression.argument as ESLintClassExpression
const typeParameters = (classDecl as TSESTree.ClassExpression)
.typeParameters
const params = typeParameters?.params
return typeParameters?.params
}

try {
const rawParams: string[] = []
const scriptLet = `void function<${code}>(){}`
const result = parseScriptFragmentWithOption(
scriptLet,
locationCalculator.getSubCalculatorShift(-14),
{ ...parserOptions, project: undefined },
{
preFixLocationProcess(preResult) {
const params = getParams(preResult)
if (params) {
for (const param of params) {
rawParams.push(
scriptLet.slice(param.range[0], param.range[1]),
)
}
}
},
},
)
const { ast } = result
const params = getParams(result)

if (!params || params.length === 0) {
return {
@@ -1300,12 +1344,7 @@ export function parseGenericExpression(
loc: { start: firstParam.loc.start, end: lastParam.loc.end },
parent: DUMMY_PARENT,
params,
rawParams: params.map((param) =>
code.slice(
param.range[0] - typeParameters.range[0] - 1,
param.range[1] - typeParameters.range[0] - 1,
),
),
rawParams,
}

// Modify parent.
2 changes: 1 addition & 1 deletion src/template/index.ts
Original file line number Diff line number Diff line change
@@ -703,7 +703,7 @@ function convertForVBindSameNameShorthandValue(
}
// v-bind same-name shorthand (Vue 3.4+)
const vId = directive.key.argument
const camelName = camelize(vId.name)
const camelName = camelize(vId.rawName)
let result: ESLintExtendedProgram | null = null
try {
result = parseScriptFragment(
Loading