svelte-eslint-parser
Advanced tools
Comparing version 0.23.0 to 0.24.0
@@ -25,2 +25,3 @@ import type { ScopeManager, Scope } from "eslint-scope"; | ||
}; | ||
addPostProcess: (callback: () => void) => void; | ||
}; | ||
@@ -48,2 +49,3 @@ type TypeGenHelper = { | ||
private readonly restoreCallbacks; | ||
private readonly programRestoreCallbacks; | ||
private readonly closeScopeCallbacks; | ||
@@ -65,2 +67,3 @@ private readonly unique; | ||
closeScope(): void; | ||
addProgramRestore(callback: ScriptLetRestoreCallback): void; | ||
private appendScript; | ||
@@ -73,2 +76,10 @@ private appendScriptWithoutOffset; | ||
restore(result: ESLintExtendedProgram): void; | ||
/** | ||
* Restore AST nodes | ||
*/ | ||
private restoreNodes; | ||
/** | ||
* Restore program node | ||
*/ | ||
private restoreProgram; | ||
private remapNodes; | ||
@@ -75,0 +86,0 @@ /** Fix locations */ |
@@ -35,2 +35,3 @@ "use strict"; | ||
this.restoreCallbacks = []; | ||
this.programRestoreCallbacks = []; | ||
this.closeScopeCallbacks = []; | ||
@@ -349,2 +350,5 @@ this.unique = new unique_1.UniqueIdGenerator(); | ||
} | ||
addProgramRestore(callback) { | ||
this.programRestoreCallbacks.push(callback); | ||
} | ||
appendScript(text, offset, callback) { | ||
@@ -377,2 +381,44 @@ const resultCallback = this.appendScriptWithoutOffset(text, (node, tokens, comments, result) => { | ||
restore(result) { | ||
const nodeToScope = getNodeToScope(result.scopeManager); | ||
const postprocessList = []; | ||
const callbackOption = { | ||
getScope, | ||
getInnermostScope, | ||
registerNodeToScope, | ||
scopeManager: result.scopeManager, | ||
visitorKeys: result.visitorKeys, | ||
addPostProcess: (cb) => postprocessList.push(cb), | ||
}; | ||
this.restoreNodes(result, callbackOption); | ||
this.restoreProgram(result, callbackOption); | ||
postprocessList.forEach((p) => p()); | ||
// Helpers | ||
/** Get scope */ | ||
function getScope(node) { | ||
return (0, scope_1.getScopeFromNode)(result.scopeManager, node); | ||
} | ||
/** Get innermost scope */ | ||
function getInnermostScope(node) { | ||
return (0, scope_1.getInnermostScopeFromNode)(result.scopeManager, node); | ||
} | ||
/** Register node to scope */ | ||
function registerNodeToScope(node, scope) { | ||
// If we replace the `scope.block` at this time, | ||
// the scope restore calculation will not work, so we will replace the `scope.block` later. | ||
postprocessList.push(() => { | ||
scope.block = node; | ||
}); | ||
const scopes = nodeToScope.get(node); | ||
if (scopes) { | ||
scopes.push(scope); | ||
} | ||
else { | ||
nodeToScope.set(node, [scope]); | ||
} | ||
} | ||
} | ||
/** | ||
* Restore AST nodes | ||
*/ | ||
restoreNodes(result, callbackOption) { | ||
let orderedRestoreCallback = this.restoreCallbacks.shift(); | ||
@@ -387,4 +433,2 @@ if (!orderedRestoreCallback) { | ||
const processedComments = []; | ||
const nodeToScope = getNodeToScope(result.scopeManager); | ||
const postprocessList = []; | ||
let tok; | ||
@@ -458,9 +502,3 @@ while ((tok = tokens.shift())) { | ||
const targetComments = comments.splice(startIndex.comment, endIndex.comment - startIndex.comment); | ||
restoreCallback.callback(node, targetTokens, targetComments, { | ||
getScope, | ||
getInnermostScope, | ||
registerNodeToScope, | ||
scopeManager: result.scopeManager, | ||
visitorKeys: result.visitorKeys, | ||
}); | ||
restoreCallback.callback(node, targetTokens, targetComments, callbackOption); | ||
processedTokens.push(...targetTokens); | ||
@@ -476,27 +514,10 @@ processedComments.push(...targetComments); | ||
result.ast.comments = processedComments; | ||
postprocessList.forEach((p) => p()); | ||
// Helpers | ||
/** Get scope */ | ||
function getScope(node) { | ||
return (0, scope_1.getScopeFromNode)(result.scopeManager, node); | ||
} | ||
/** | ||
* Restore program node | ||
*/ | ||
restoreProgram(result, callbackOption) { | ||
for (const callback of this.programRestoreCallbacks) { | ||
callback(result.ast, result.ast.tokens, result.ast.comments, callbackOption); | ||
} | ||
/** Get innermost scope */ | ||
function getInnermostScope(node) { | ||
return (0, scope_1.getInnermostScopeFromNode)(result.scopeManager, node); | ||
} | ||
/** Register node to scope */ | ||
function registerNodeToScope(node, scope) { | ||
// If we replace the `scope.block` at this time, | ||
// the scope restore calculation will not work, so we will replace the `scope.block` later. | ||
postprocessList.push(() => { | ||
scope.block = node; | ||
}); | ||
const scopes = nodeToScope.get(node); | ||
if (scopes) { | ||
scopes.push(scope); | ||
} | ||
else { | ||
nodeToScope.set(node, [scope]); | ||
} | ||
} | ||
} | ||
@@ -503,0 +524,0 @@ remapNodes(maps, tokens, comments, visitorKeys) { |
@@ -6,4 +6,5 @@ import { parseForESLint } from "./parser"; | ||
export { AST, ParseError }; | ||
export declare const name = "svelte-eslint-parser"; | ||
export { parseForESLint }; | ||
export declare const VisitorKeys: import("eslint").SourceCode.VisitorKeys; | ||
export { traverseNodes }; |
@@ -26,3 +26,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.traverseNodes = exports.VisitorKeys = exports.parseForESLint = exports.ParseError = exports.AST = void 0; | ||
exports.traverseNodes = exports.VisitorKeys = exports.parseForESLint = exports.name = exports.ParseError = exports.AST = void 0; | ||
const parser_1 = require("./parser"); | ||
@@ -37,4 +37,5 @@ Object.defineProperty(exports, "parseForESLint", { enumerable: true, get: function () { return parser_1.parseForESLint; } }); | ||
Object.defineProperty(exports, "ParseError", { enumerable: true, get: function () { return errors_1.ParseError; } }); | ||
exports.name = "svelte-eslint-parser"; | ||
// Keys | ||
// eslint-disable-next-line @typescript-eslint/naming-convention -- ignore | ||
exports.VisitorKeys = visitor_keys_1.KEYS; |
@@ -64,2 +64,18 @@ "use strict"; | ||
} | ||
// Set the scope of the Program node. | ||
ctx.scriptLet.addProgramRestore((node, _tokens, _comments, { scopeManager, registerNodeToScope, addPostProcess }) => { | ||
const scopes = []; | ||
for (const scope of scopeManager.scopes) { | ||
if (scope.block === node) { | ||
registerNodeToScope(ast, scope); | ||
scopes.push(scope); | ||
} | ||
} | ||
addPostProcess(() => { | ||
// Reverts the node indicated by `block` to the original Program node. | ||
// This state is incorrect, but `eslint-utils`'s `referenceTracker.iterateEsmReferences()` tracks import statements | ||
// from Program nodes set to `block` in global scope. This can only be handled by the original Program node. | ||
scopeManager.globalScope.block = node; | ||
}); | ||
}); | ||
return ast; | ||
@@ -66,0 +82,0 @@ } |
{ | ||
"name": "svelte-eslint-parser", | ||
"version": "0.23.0", | ||
"version": "0.24.0", | ||
"description": "Svelte parser for ESLint", | ||
@@ -48,2 +48,7 @@ "repository": "git+https://github.com/ota-meshi/svelte-eslint-parser.git", | ||
}, | ||
"peerDependenciesMeta": { | ||
"svelte": { | ||
"optional": true | ||
} | ||
}, | ||
"dependencies": { | ||
@@ -67,6 +72,5 @@ "eslint-scope": "^7.0.0", | ||
"@typescript-eslint/eslint-plugin": "^5.4.0", | ||
"@typescript-eslint/parser": "~5.50.0", | ||
"@typescript-eslint/parser": "~5.54.0", | ||
"benchmark": "^2.1.4", | ||
"chai": "^4.3.4", | ||
"code-red": "^0.2.3", | ||
"esbuild": "^0.17.0", | ||
@@ -89,3 +93,3 @@ "esbuild-register": "^3.3.3", | ||
"locate-character": "^2.0.5", | ||
"magic-string": "^0.27.0", | ||
"magic-string": "^0.30.0", | ||
"mocha": "^10.0.0", | ||
@@ -92,0 +96,0 @@ "mocha-chai-jest-snapshot": "^1.1.3", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
234621
44
5736